• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to access private constructor using java reflection ?  RSS feed

 
Jackson Martin
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am trying to access private constructor using java reflection but not able to access. Please tell me where I am making mistake.
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is the Constructor class (at Line 18) defined?
 
Knute Snortum
Sheriff
Posts: 4081
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a couple of problems. First, getConstructor() looks for public constructors. Use getDeclaredConstructor instead.

Second, as Stevens implies, you are looking for a constructor that takes one parameter of type privateCon. There is no such constructor.

Third, although it doesn't produce an error, naming your class starting with a lower case letter is confusing.

Fourth, Constructor is a generic class. It should take a type.

So, putting it all together:

 
Jackson Martin
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevens Miller wrote:Where is the Constructor class (at Line 18) defined?


Constructor class ??
Its Java reflection class so its imported

Sorry I haven't shown that imports here.
 
Jackson Martin
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am able to access private constructor using getDeclaredConstructors() method. It is working properly. Question arise here then why it was not working with getConstructors() Method?
 
Jackson Martin
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:There are a couple of problems. First, getConstructor() looks for public constructors. Use getDeclaredConstructor instead.

Second, as Stevens implies, you are looking for a constructor that takes one parameter of type privateCon. There is no such constructor.

Third, although it doesn't produce an error, naming your class starting with a lower case letter is confusing.

Fourth, Constructor is a generic class. It should take a type.

So, putting it all together:



Thanks got it
I will keep these things in mind.
Thanks once again.
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice work, everyone. Marking this one resolved.
 
Tim Cooke
Marshal
Posts: 3873
233
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do hope that this is an academic exercise in learning about the Reflection library.

If this is a real live problem and you're trying to do this for real in a real application then I have to ask the question: why why why why why why why are you trying to access a private constructor? If it's private for a good reason then you should respect that and leave it alone. If it's private for no good reason, make it public.
 
Campbell Ritchie
Marshal
Posts: 55768
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree. If the constructor is private then you are breaching the general contract of the application you are using, and you are liable to produce errors if you run it like that.
 
Jackson Martin
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:I do hope that this is an academic exercise in learning about the Reflection library.

If this is a real live problem and you're trying to do this for real in a real application then I have to ask the question: why why why why why why why are you trying to access a private constructor? If it's private for a good reason then you should respect that and leave it alone. If it's private for no good reason, make it public.



Yes It is academic exercise in learning about the Reflection
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That being the case, can anyone suggest a reason why getDeclaredConstructor even exists? Is there any valid practical use for it?
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[They] were so preoccupied with whether or not they could, they didn't stop to think if they should.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevens Miller wrote:That being the case, can anyone suggest a reason why getDeclaredConstructor even exists? Is there any valid practical use for it?


I assume you're asking why there is a method that returns private members at all, rather than asking why they split out the other method to return only public members.

I think there are practical uses of it. For example take frameworks like Hibernate. This product takes data in a database and automatically turns it into Java objects and vice versa. Hibernate creates the java objects using a no-args constructor and then populates the fields by using reflection to find and set them too.

Hibernate uses the getDeclaredX() variants of the reflection methods. This means that when you design the classes it is going to populate you can make the no-args constructor private and also keep all the fields private. This means that if another programmer wants to create an instance of this class outside of Hibernate then the interface is sensible. You can provide public constructors with parameters so the objects are in a valid state, and the fields can't be modified directly because they aren't public.

So I guess the creators of reflection allowed access to private members so that they didn't limit creativity. You can set security restrictions that prevent access to private members if you need to restrict this ability.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!