Есть ли способ сохранить текущее состояние класса, который будет использоваться в другом классе, где я хочу проверить метод getData.
public class Test1 {
private static ThreadLocal t = new ThreadLocal();
public static Map getData() {
Map m = (Map) t.get();
if (m == null) {
m = new HashMap();
t.set(m);
}
return (Map) m;
}
}
Я пытался добиться этого, используя следующие отражения для доступалокальные переменные потока в другом, но это не сработало:
public class Test2 {
Test3 t3 = new Test3();
t3.setTest1(Test1.class);
System.out.Println(Test1.getData()); //Able to retrieve the value
}
public class Test3 {
private Class<Test1> test1;
//getter and setters
System.out.Println(test1.getDeclaredMethod("getData").invoke(null).toString()); // always return null as it picks the current thread local value and not the one available at the time of capturing
}