my dog learned polymorphism*
The moose likes Java in General and the fly likes Not able to get handle to non-public members using Class class & Reflection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Not able to get handle to non-public members using Class class & Reflection" Watch "Not able to get handle to non-public members using Class class & Reflection" New topic
Author

Not able to get handle to non-public members using Class class & Reflection

Vinayak Sharma
Greenhorn

Joined: Jan 28, 2003
Posts: 13
Hi,

Using the "Class" class, I can get the Members as follows

Class.getField(String fld) - will get me the Field by name "fld" which is a PUBLIC field for the class or its superclasses upto Object class.

Class.getDecalredField(String fld) - will get me Field by name "fld" (public, private, protected etc.) for the class, but not the superclasses.

I want to have access to all the fields (pvt., public, protected etc.) declared in the object and its superclasses upto Object Class. For same I used "Class.getSuperClass()" recursively and used "Class.getDecalredField(String fld)" to find the needed field.

Now the problem comes when I try to get the object from Field instance. Issue is with variables in superclasses.

// Object whose superclass's field I want to query
ChildClass childClsObj = new ChildClass();

// This stmt can be replaced by a method which recursively traverses
// the hierarchy and gets me the field, where ever it is, here keeping it
//simple
Field fld = class.getDeclaredField("superClassField");
fld.setAccessible(true);

// This code fails at runtime, where an IllegalArgument exception
// is thrown at runtime
SuperClassFieldClass inst = (SuperClassFieldClass) fld.get(childClsObj );

// If I try typecasting the object to Base Class, then I get a ClassCastException
// at runtime, reason being this object and the object being queried are loaded by
// separate ClassLoaders. Follow the thread for details
// http://www.coderanch.com/t/257386/Applets/java/getClass-instanceof-typecast-inconsistency-while
SuperClassFieldClass inst = (SuperClassFieldClass) fld.get((BaseClass)childClsObj );

Is there way I can get over with this issue and access all the fields, members for a class and its superClasses.

Thanks,
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Given that the environment you're in is fighting you every step of the way: can you tell us what you're trying to accomplish? Not what the code you're trying to write is supposed to do, but what your actual goal is; maybe there's a better way to go about it.


[Jess in Action][AskingGoodQuestions]
Vinayak Sharma
Greenhorn

Joined: Jan 28, 2003
Posts: 13
I am actually trying to build an Automated Test Case Execution Framework for an Applet/AWT based application.

Basically, the code for the Applet based application is already written and it is really not possible to make changes to the code, so as to provide hooks for the automation framework, as we have pretty huge code base and not much knowledge and clarity on how the application internally operates.

So basic idea is not to make any changes to the exisiting code (as it may destabalise the application) and develop an Autmation Framework, which can hook to the application, by just knowing where the object is declared and accessible from in the application's class hierarchy.

We have already investigated some of the existing freely avaialble Automation tools like Jemmy, which did not fit our requirements. We do not want to go for co-ordinate based Automation tools, as we would be testing the application on different platforms. Also tools like Winrunner do not fit in our budget.

:-)
Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Not able to get handle to non-public members using Class class & Reflection
 
Similar Threads
Finding annotations from subclass or child class
Constructing subclasses from superclass instance
hashcode()
Need help on JTree
converting a string color to an object color