• Post Reply Bookmark Topic Watch Topic
  • New Topic

Simple Reflection Trouble  RSS feed

 
Daniel Prene
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was experimenting with reflection last night and ran into some trouble...
I've looked over the code several times but still have been unable to figure out what's going wrong. I get the following exception at runtime:




I'm asuming that this means there isn't a constructor with a string parameter, but there definitely is:



It all happens here:


The reflection code seems pretty lame... is there a more 1.5 way of doing it?

Any help would be appreciated.

Thanks for atleast reading my post,
-D.P.
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the API:
The constructor to reflect is the public constructor of the class represented by this Class object whose formal parameter types match those specified by parameterTypes.
Your constructor is protected however - try making it public.
 
Daniel Prene
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh woah! I didn't even see that!
Thanks for the help!

Is there a better way to do this?

I really appreciate it,
- D.P.
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, this way only makes sense if the Cat subtype is unknown at compile time - I'm guessing this is just example code.
Because if you know the class, you can
1) use cat.LivingCat.class instead of Class.forName(), and
2) just use new cat.LivingCat(color).

But if the Cat subtype is not known (e.g. it's a parameter) than reflection is (unfortunately) the only way to go.
 
Daniel Prene
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if the Cat subtype is not known (e.g. it's a parameter) than reflection is (unfortunately) the only way to go.

It is indeed. What do you mean by "unfortunately?" How expensive is it? What are the drawbacks of reflection

Thanks again,
-D.P.
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reflection is considered quite evil by many Java programmers.
There is an artical on the Ranch itself: http://radio.javaranch.com/val/2004/05/18/1084891793000.html
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But this sort of reflection -- creating and configuring objects dynamically -- is the very foundation of frameworks like Spring, tools like Eclipse, and indeed most of the J2EE platform itself. Val's just saying that sneaking around and breaking the invariants of a class using reflection is evil. Using reflection in the way we're discussing it here is perfectly valid.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In any case, the problem is not that the Cat constructor needs to be public; it's that the Class.getConstructor() method will only find public constructors. Use Class.getDeclaredConstructor() instead, and you'll have no problem finding a protected constructor in the same package.
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whether a tool or technique is morally charged is always a matter of opinion. Showing the misuses of a tool, as I see it, is no counterargument to its benefits. I think it's good to know, however, whether a particular tool can be deemed double-edged.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!