我正在使用一个不是开源的项目,我需要修改一个或多个类。
在一个课程中是关注集合:
private Map<Integer, TTP> ttp = new HashMap<>();
我需要做的就是使用反射并在这里使用concurrenthashmap。 我试过跟随代码,但它不起作用。
Field f = ..getClass().getDeclaredField("ttp");
f.setAccessible(true);
f.set(null, new ConcurrentHashMap<>());
答案 0 :(得分:29)
希望这是你想要做的事情:
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();
}
}
打印:
value1
答案 1 :(得分:17)
值得一读Oracle Java Tutorial - Getting and Setting Field Values
Field#set(Object object, Object value)将指定对象参数上此Field
对象所表示的字段设置为指定的新值。
应该是这样的
f.set(objectOfTheClass, new ConcurrentHashMap<>());
您无法在null
对象中设置任何值如果尝试过,则会产生NullPointerException
注意:通过反射设置字段值会产生一定量的性能开销,因为必须进行各种操作,例如验证访问权限。从运行时的角度来看,效果是相同的,并且操作就像在类代码中直接更改值一样原子化。
答案 2 :(得分:6)
即使字段位于超类
中,下面的方法也会在对象上设置字段/**
* Sets a field value on a given object
*
* @param targetObject the object to set the field value on
* @param fieldName exact name of the field
* @param fieldValue value to set on the field
* @return true if the value was successfully set, false otherwise
*/
public static boolean setField(Object targetObject, String fieldName, Object fieldValue) {
Field field;
try {
field = targetObject.getClass().getDeclaredField(fieldName);
} catch (NoSuchFieldException e) {
field = null;
}
Class superClass = targetObject.getClass().getSuperclass();
while (field == null && superClass != null) {
try {
field = superClass.getDeclaredField(fieldName);
} catch (NoSuchFieldException e) {
superClass = superClass.getSuperclass();
}
}
if (field == null) {
return false;
}
field.setAccessible(true);
try {
field.set(targetObject, fieldValue);
return true;
} catch (IllegalAccessException e) {
return false;
}
}
答案 3 :(得分:0)
你可以试试这个:
//Your class instance
Publication publication = new Publication();
//Get class with full path(with package name)
Class<?> c = Class.forName("com.example.publication.models.Publication");
//Get method
Method method = c.getDeclaredMethod ("setTitle", String.class);
//set value
method.invoke (publication, "Value to want to set here...");
答案 4 :(得分:0)
您可以尝试以下方法:
static class Student {
private int age;
private int number;
public Student(int age, int number) {
this.age = age;
this.number = number;
}
public Student() {
}
}
public static void main(String[] args) throws IllegalAccessException, NoSuchFieldException {
Student student1=new Student();
// Class g=student1.getClass();
Field[]fields=student1.getClass().getDeclaredFields();
Field age=student1.getClass().getDeclaredField("age");
age.setAccessible(true);
age.setInt(student1,13);
Field number=student1.getClass().getDeclaredField("number");
number.setAccessible(true);
number.setInt(student1,936);
for (Field f:fields
) {
f.setAccessible(true);
System.out.println(f.getName()+" "+f.getInt(student1));
}
}
}