0

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.

SpaceCowboy
5638 silver badges17 bronze badges
asked Apr 29, 2013 at 8:50
1
  • you might want to explain on why you want to do this? Commented Apr 29, 2013 at 8:59

3 Answers 3

2

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)

answered Apr 29, 2013 at 8:55
1

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
 }
 }
}
answered Apr 29, 2013 at 8:58
9
  • @Anto if you have any questions let me know Commented 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. Commented 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() Commented 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" Commented Apr 29, 2013 at 9:20
  • Kevin the values are not set Commented Apr 29, 2013 at 9:44
0

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>
answered Apr 29, 2013 at 8:59
3
  • With java reflection, you may get all the fields and their types. but you wont get the names of the fields Commented Apr 29, 2013 at 9:01
  • Field has a property name which is the name of the field! Commented Apr 29, 2013 at 9:03
  • Field name only provides the information of public fields, you cannot access private fields from there. Commented Apr 29, 2013 at 9:12

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.