Not able to get handle to non-public members using Class class & Reflection
Joined: Jan 28, 2003
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 );
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.
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.