Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Vinayak Sharma
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vinayak Sharma
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic