aspose file tools*
The moose likes Java in General and the fly likes Problem while playing with Reflection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem while playing with Reflection" Watch "Problem while playing with Reflection" New topic
Author

Problem while playing with Reflection

Balasubramanian Chandrasekaran
Ranch Hand

Joined: Nov 28, 2007
Posts: 215

Hi all,

I was just playing with Reflection and got into trouble.

I have two classes Class A and Class B.

Class A Code:
=============


Class B Code:
=============


Then i tried to run Class B it resulted in exception


java.lang.IllegalAccessException: Class B can not access a member of class A with modifiers "private"
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65)
at java.lang.Class.newInstance0(Class.java:349)
at java.lang.Class.newInstance(Class.java:308)
at B.doOperation(B.java:27)
at B.main(B.java:43)


Then i changed the code on both the class as follows

Class A Code:
=============


Class B Code:
=============



This code still gives Exception

java.lang.IllegalAccessException: Class B can not access a member of class A with modifiers "private"
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65)
at java.lang.Class.newInstance0(Class.java:349)
at java.lang.Class.newInstance(Class.java:308)
at B.doOperation(B.java:27)
at B.main(B.java:43)


I just want to call callThis() method in Class A dynamically without changing that private constructor on Class A. Where i am going wrong
Any help please...
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Surely, you just don't need this line: -


Incidentally, there is no need to use Class.forName() to get the Class object of a class that's available at compile-time. Just use the "class literal". For instance: -


You may be able to use Class<Integer> here - I dunno without trying, which I don't have time for right now. If Class<Integer> does work, it's better code.
[ April 24, 2008: Message edited by: Peter Chase ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Balasubramanian Chandrasekaran
Ranch Hand

Joined: Nov 28, 2007
Posts: 215

Thanks Peter Chase.

I have changed the things which you have quoted and it works as intended. Happy with the way i am learning

Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
this horse might already be out of the barn, but just for future reference I think we ought to slide this over to the intermediate forum, I mean really... reflection?


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Balasubramanian Chandrasekaran
Ranch Hand

Joined: Nov 28, 2007
Posts: 215

Hi all,

I just want to change my class A code to my first implementation

Class A:
========



I studied in a article that we can dynamically call a method which has been declared as private by overriding java language access checking.

(i.e.,) by using setAccessible(boolean) method in AccessibleObject class in reflection.

Likewise, is there a way i could use some method to get reference for the Singleton Class A which i have shown above.

Waiting for your answers!!!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19781
    
  20

By calling the private constructor, you're ensuring class A is NOT a singleton class!

If you want it to be a singleton class, all you need to do is have a static reference and a method to get the single object:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Balasubramanian Chandrasekaran
Ranch Hand

Joined: Nov 28, 2007
Posts: 215

First of all sorry for this late reply.
Rob thanks for your reply,

But, here my problem is that i will be getting the class name "A" during runtime only. So the reference for A cannot able to call getInstance() method directly because of the presence of private constructor. In that case what can i do?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19781
    
  20

If class A does not have a non-private constructor and does not have a (static) factory method to create objects of type A, then either class A is flawed or it is not meant to be instantiated.

Do you have the API for class A? If so, is there a factory method?
If so, you can use reflection to call that method:
Balasubramanian Chandrasekaran
Ranch Hand

Joined: Nov 28, 2007
Posts: 215

Thanks Rob, Now i have changed the code to call callThis method by following your implementation.
 
 
subject: Problem while playing with Reflection