Retrieving Field Values Using Reflection
I cannot get the value of the field. What I am trying to do is get the object at runtime. Please let me know where I am going wrong.
Test.class
import java.lang.reflect.Field;
public class Test {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, SecurityException,
IllegalArgumentException, IllegalAccessException {
final Field field = Class.forName("com.logging.EX").getDeclaredField("value");
field.setAccessible(true);
field.get(Class.forName("com.logging.EX"));
}
}
EX.class
public class EX {
private String value;
public EX(){
value="data";
}
/**
* @return the value
*/
public String getValue() {
return value;
}
/**
* @param value
* the value to set
*/
public void setValue(String value) {
this.value = value;
}
}
source to share
Something like that...
import java.lang.reflect.Field;
public class Test {
public static void main(String... args) {
try {
Foobar foobar = new Foobar("Peter");
System.out.println("Name: " + foobar.getName());
Class<?> clazz = Class.forName("com.csa.mdm.Foobar");
System.out.println("Class: " + clazz);
Field field = clazz.getDeclaredField("name");
field.setAccessible(true);
String value = (String) field.get(foobar);
System.out.println("Value: " + value);
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Foobar {
private final String name;
public Foobar(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
Or you can use a class method newInstance
to get an instance of your object at runtime. You will still have to set this instance variable first, otherwise it won't make any difference.
eg.
Class<?> clazz = Class.forName("com.something.Foobar");
Object object = clazz.newInstance();
Or, where it has two parameters in its constructor, String and int, for example ...
Class<?> clazz = Class.forName("com.something.Foobar");
Constructor<?> constructor = clazz.getConstructor(String.class, int.class);
Object obj = constructor.newInstance("Meaning Of Life", 42);
Or you can query it for your constructors at runtime using clazz.getConstructors()
NB I deliberately omitted the casting of the object created here in the expected view as this will defeat the reflection point as you already know about this class if you do, which negates the need for reflection in the first place.
source to share
You can instantiate from a class object and which can be used in the get value field.
Class modelClass = Class.forName("com.gati.stackoverflow.EX");
final Field field = modelClass.getDeclaredField("value");
field.setAccessible(true);
Object modelInstance=modelClass.newInstance();
field.get(modelInstance);
source to share
So, we got the answer below. It works fine for now. Not sure if this is the best one.
Test class:
public class Test {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, SecurityException,
IllegalArgumentException, IllegalAccessException, InstantiationException {
Field[] fields = Class.forName("com.logging.EX").newInstance().getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
System.out.println(field.getName() + " : " + field.get(Class.forName("com.logging.EX").newInstance()));
}
}
}
I am extracting all the fields into an array by calling an instance com.logging.EX
and then iterating over all the fields and retrieving the name and value that the field has. Didn't code any field name here.
There are a few security warnings with mine as I accessed the variable with an access modifier private
but always exists with reflection. Just a disclaimer!
Hope this helps!
source to share