• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reflection  RSS feed

 
duhit Choudhary
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am trying to read properties file through which i can print the class and its methods and invoke them. i've tried an got here till yet.....here is my code,........


now what to do if i am having multiple methods in the same class.....thanks in advance...
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
duhit Choudhary wrote:now what to do if i am having multiple methods in the same class.....thanks in advance...

What exactly is your problem ? You know how to call the showDetails method. What's stopping you from calling the other methods in the same way ?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My preference is to have the classes I am loading in implement an interface which contains the methods I will be calling. Then I don't have to continue to use reflection to call those methods:





Of course, that only works when:
1) All the objects you will be loading in via reflection have the same interface
2) You know the methods you want to call (i.e. those don't come from a configuration file as well).

If you need to use reflection to call a second method ... then you do it the same way you called the first method.

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code you have right now isn't particularly useful.

Line 23 - you get a name from the properties file
Line 25 - you add the name to a hard-coded package name; this limits the usefulness of the code because it assumes you are dealing with classes in the student package
Line 27 - here you call a constructor directly to instantiate an Employee; this further limits the code's usefulness because now it will only work with that class
Line 28 - here you hard-code the method you want to get a reference to; this makes the code even more specific to the implementation. This code won't work for any class that does not have a showDetails method.

So why use a properties file at all if you're going to limit the utility of the rest of the code like this? What exactly are you trying to achieve here?

More importantly: are you just messing around with the Reflection API or is this for real-world code? If it's for the former, fine; read up on the Reflection API some more.

However, if it's for real-world code, STOP IT! I suspect there is something seriously lacking in your analysis and design if you have to resort to reflection and introspection when dealing with a class like Employee.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
duhit Choudhary wrote:...here is my code,...

Duhit,

I broke up that very long line in your code. Please re-read the UseCodeTags page. Thoroughly.

Thanks

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
duhit Choudhary wrote:i am trying to read properties file through which i can print the class and its methods and invoke them.

To what end? Without more information, this just sounds like one of those "I'd like to do anything with anything" scenarios, which
(a) Almost never exist.
(b) If you've found one, Java is NOT the right language for.

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