Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set field value with reflection

I'm working with one project which is not opensource and I need to modify one or more its classes.

In one class is following collection:

private Map<Integer, TTP> ttp = new HashMap<>();  

All what I need to do is use reflection and use concurrenthashmap here. I've tried following code but it doesnt work.

Field f = ..getClass().getDeclaredField("ttp"); f.setAccessible(true); f.set(null, new ConcurrentHashMap<>()); 
like image 243
user2749903 Avatar asked Jun 07 '14 07:06

user2749903


People also ask

How do you set a field value?

SetFieldValue(value = 10000.0); The FieldByName method returns the field object that represents the given field (salary_fld, in this example), and the SetFieldValue methods uses that field object to set the value of the field. If this method is successful, it returns ER_OK.

How do you set a field value in Java?

The set() method of java. lang. reflect. Field is used to set the value of the field represented by this Field object on the specified object argument to the specified new value passed as parameter.


2 Answers

Hope this is something what you are trying to do :

import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap;  public class Test {      private Map ttp = new HashMap();       public  void test() {         Field declaredField =  null;         try {              declaredField = Test.class.getDeclaredField("ttp");             boolean accessible = declaredField.isAccessible();              declaredField.setAccessible(true);              ConcurrentHashMap<Object, Object> concHashMap = new ConcurrentHashMap<Object, Object>();             concHashMap.put("key1", "value1");             declaredField.set(this, concHashMap);             Object value = ttp.get("key1");              System.out.println(value);              declaredField.setAccessible(accessible);          } catch (NoSuchFieldException                  | SecurityException                 | IllegalArgumentException                  | IllegalAccessException e) {             e.printStackTrace();         }      }      public static void main(String... args) {         Test test = new Test();         test.test();      } } 

It prints :

value1 
like image 116
Visruth Avatar answered Sep 19 '22 00:09

Visruth


It's worth reading Oracle Java Tutorial - Getting and Setting Field Values

Field#set(Object object, Object value) sets the field represented by this Field object on the specified object argument to the specified new value.

It should be like this

f.set(objectOfTheClass, new ConcurrentHashMap<>()); 

You can't set any value in null Object If tried then it will result in NullPointerException


Note: Setting a field's value via reflection has a certain amount of performance overhead because various operations must occur such as validating access permissions. From the runtime's point of view, the effects are the same, and the operation is as atomic as if the value was changed in the class code directly.

like image 20
Braj Avatar answered Sep 19 '22 00:09

Braj