• Post Reply Bookmark Topic Watch Topic
  • New Topic

Handling No-reflexion constructed objets with reflexion-api  RSS feed

 
Fabricio Zelada
Greenhorn
Posts: 7
Java Netscape Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an object that has been already constructed using non-reflexive API, but I now I new to access to some of it methods and fields using reflexive API.
But for doing that I must have a object Class referencing such Original object.
Let say I have and Object obj, wich I created before using Object obj = new Object; but now I need to access it using reflixive API, so how can I have a object Class that refers to such object obj.
Any help please!!
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fabricio Zelada wrote:I have an object that has been already constructed using non-reflexive API, but I now I new to access to some of it methods and fields using reflexive API.
But for doing that I must have a object Class referencing such Original object.
Let say I have and Object obj, wich I created before using Object obj = new Object; but now I need to access it using reflixive API, so how can I have a object Class that refers to such object obj.
Any help please!!


Well, you can start with....



Henry
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I presume you have seen the reflection part of the Java Tutorials?
 
Fabricio Zelada
Greenhorn
Posts: 7
Java Netscape Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the welcome and for the answer, but the problem is that I have the object obj just as string, this is because I have so many object like this been created dinamicaly. Any thought?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fabricio Zelada wrote:Thanks for the welcome and for the answer, but the problem is that I have the object obj just as string, this is because I have so many object like this been created dinamicaly. Any thought?



What do you mean by "obj just as string"? A string object is just like any other object, and hence, you can get the Class class for the string object. Or do you mean that the string object is holding the name of the class that you want? In that case, the forName() method of the Class class, can get you the Class instance that you want.

Regardless, please take a look at the tutorial that Campbell posted again. The forName() method was one of the early examples provided.

Henry

 
Fabricio Zelada
Greenhorn
Posts: 7
Java Netscape Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah right, I think I was not clear, in the begging I create object obj by using by common method (by calling the constructor).
But later I need to use its methods. The only reference of such object, its name, is contained into a String.

So I cannot use forName(String) method, because I will have to create other object. I would like to create a Class object that reference object obj. But only thing I have is its name inside the String. So is there any way?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fabricio Zelada wrote:Yeah right, I think I was not clear, in the begging I create object obj by using by common method (by calling the constructor).
But later I need to use its methods. The only reference of such object, its name, is contained into a String.

So I cannot use forName(String) method, because I will have to create other object. I would like to create a Class object that reference object obj. But only thing I have is its name inside the String. So is there any way?



The forName(String) method, only creates a Class object if it doesn't exist. And it doesn't create an instance of the object -- you have to use the Class object for that.


And BTW, you are completely not clear. Either you have a reference to the object or you don't. If you do, then use getClass(). If you don't, and have the name, then use "forName()". Or get a reference.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!