I have a method:
public void extractStringFromField(Class<?> classToInspect) {
Field[] allFields = classToInspect.getDeclaredFields();
for(Field field : allFields) {
if(field.getType().isAssignableFrom(String.class)) {
System.out.println("Field name: " + field.getName());
// How to get the actual value of the string?!?!
// String strValue = ???
}
}
}
When this runs I get output like:
Field name: java.lang.String
Now how do I extract the actual string value into strValue, using reflection?
System.out.println("Field name: " + field.getName());classToInspectis aWidget, and theWidgetclass has a String field calledfizz, and the value of thatWidget#fizzinstance is "buzz", then I want to get thebuzzstring into an actualStringinstance.nullwhen it's static) with theField#get(Object)method. Internally, it looks likeObject.Fieldto retrieve the value.