Java Reflection Question

I am working on a project that uses reflection to get the fields of a running Java application.

I managed to get the fields, but I cannot read or write them. This is an example that I found on the Internet:

Class aClass = MyObject.class Field field = aClass.getField("someField"); MyObject objectInstance = new MyObject(); Object value = field.get(objectInstance); field.set(objetInstance, value); 

The problem is that I use the classes from the working jar file, and the classes that I'm trying to manipulate come from the Loader class. So instead of "MyObject.class" I have ".class". To get "MyObject", I tried using ClassLoader, but that didn't work.

If I just use ".class":

 Object value = field.get(theLoadedClass); 

I will get this error:

 java.lang.IllegalArgumentException: Can not set int field myClass.field to java.lang.Class 

Thanks.

+6
source share
6 answers

This should help:

 Class aClass = myClassLoader.loadClass("MyObject"); // use your class loader and fully qualified class name Field field = aClass.getField("someField"); // you can not use "MyObject objectInstance = new MyObject()" since its class would be loaded by a different classloader to the one used to obtain "aClass" // instead, use "newInstance()" method of the class Object objectInstance = aClass.newInstance(); Object value = field.get(objectInstance); field.set(objetInstance, value); 
+3
source

You need an instance of the appropriate class to pass to field.get / set methods.

To get an instance from class , you can try the following options:

 Class<?> clazz = MyObject.class; // How to call the default constructor from the class: MyObject myObject1 = clazz.newInstance(); // Example of calling a custom constructor from the class: MyObject myObject2 = clazz.getConstructor(String.class, Integer.class).newInstance("foo", 1); 
+2
source

From the documentation: java.lang.IllegalArgumentException selected:

If, after expansion, the new value cannot be converted to the base field type by conversion identifier or extension, the method throws an IllegalArgumentException.

This means that the type of object (object) that you are trying to set for the field cannot be converted to the actual type. Do not try to use the object.

Unconnected, looking at your code, I would change

 Class aClass = MyObject.class; 

fragment:

 Class aClass = Class.forName("fullyQualifiedMyObjectClassName.egcom.bla.bla.MyObject"); 
+2
source

Your questions are not very clear, but I think you are asking how to read the value of a field from an object using reflection.

If you look in the JavaDoc of Field.get, you will see that the Field.get argument must be an instance of the object you are trying to read in a field (not a class object). Therefore, it should be something like:

 Object value = field.get(someInstanceOfTheLoadedClass); 

It seems that you are erros trying to assign something of type Class to a class of type int. You must use Field.setInt to set the int fields.

It doesn't matter if you get a Class object using .class or using Class.forName.

0
source

If you do not know the type at compile time:

 Class = objectInstance.getClass(); 

Also, like other posters, you need to know which type of field and use the correct type accordingly.

To determine this runtime, use Field.getType () and after that use the correct getter and setter.

0
source

It works?

 Class aClass = MyObject.class; Field field = aClass.getDeclaredField("someField"); field.setAccessible(true); MyObject objectInstance = new MyObject(); Object value = field.get(objectInstance); field.set(objectInstance, value); 
0
source

Source: https://habr.com/ru/post/892553/


All Articles