Hi have a class[many] for which I create object dynamically during run time. now I want to set value for the fields which are private fields
. How do I set them.
I have seen many examples which explain this but we need to know the field name and only than the values can be set.
for my case I have some set of default values for set of primitive and non primitive types and find the field type during run time and set the default values for them.
For example:
LoginBean loginBean = new LoginBean();
Method setUserName = loginBean.getClass().getMethod("setUserName", new Class[]{String.class});
setUserName.invoke(loginBean, "myLogin");
My case is different and i don't even know the field name
but have to set the default value according to field type.
how to do this using reflection or even better in spring.
-
you might want to explain on why you want to do this?Eugene– Eugene2013年04月29日 08:59:27 +00:00Commented Apr 29, 2013 at 8:59
3 Answers 3
You can say yourBean.class.getFields();
which will give array of Field.
Using Field
you can find its name
and type
, and do the desired work (setting some value, if its type is == some primitive type)
This example sets default values on several fields within a class using reflection. The fields have private access, which is toggled on and off via reflection. Field.set()
is used to set the values of the field on a particular instance instead of using the setter method.
import java.lang.reflect.Field;
import java.util.Date;
public class StackExample {
private Integer field1 = 3;
private String field2 = "Something";
private Date field3;
public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException {
StackExample se = new StackExample();
Field[] fields = se.getClass().getDeclaredFields();
for(Field f:fields){
if(!f.isAccessible()){
f.setAccessible(true);
Class<?> type = f.getType();
if(type.equals(Integer.class)){
f.set(se, 100); //Set Default value
}else if(type.equals(String.class)){
f.set(se, "Default");
}else if (type.equals(Date.class)){
f.set(se, new Date());
}
f.setAccessible(false);
}
System.out.println(f.get(se)); //print fields with reflection
}
}
}
-
@Anto if you have any questions let me knowKevin Bowersox– Kevin Bowersox2013年04月29日 09:03:16 +00:00Commented Apr 29, 2013 at 9:03
-
Hi Kevin, Thanks it helped me. if i want to print and see the values i have set how do i do that. since you know the field name you have printed them by name. supposing i don't know the field since they are created dynamically.Java Questions– Java Questions2013年04月29日 09:12:28 +00:00Commented Apr 29, 2013 at 9:12
-
@Anto I updated the code sample to print the field values using reflection. Basically its iterating through the fields and using
field.get()
Kevin Bowersox– Kevin Bowersox2013年04月29日 09:16:22 +00:00Commented Apr 29, 2013 at 9:16 -
Hi Kevin i get this
Class com.hexgen.tools.JsonConverter can not access a member of class java.lang.Class with modifiers "private static final"
Java Questions– Java Questions2013年04月29日 09:20:24 +00:00Commented Apr 29, 2013 at 9:20 -
Kevin the values are not setJava Questions– Java Questions2013年04月29日 09:44:18 +00:00Commented Apr 29, 2013 at 9:44
1) By Using Spring Constructor/Setter Injection. You dont need to know the attribute name , just type will do. Like as following:
<bean id="myBean" class="myBean">
<constructor-arg type="int"><value>1</value></constructor-arg>
</bean>
-
With java reflection, you may get all the fields and their types. but you wont get the names of the fieldsVineet Singla– Vineet Singla2013年04月29日 09:01:11 +00:00Commented Apr 29, 2013 at 9:01
-
Field
has a propertyname
which is the name of the field!sanbhat– sanbhat2013年04月29日 09:03:51 +00:00Commented Apr 29, 2013 at 9:03 -
Field name only provides the information of public fields, you cannot access private fields from there.Vineet Singla– Vineet Singla2013年04月29日 09:12:45 +00:00Commented Apr 29, 2013 at 9:12