修改非静态变量的值 - 反射

时间:2016-09-22 13:02:22

标签: java android reflection

在:

中定义一个非静态int变量
MyException::DEVISION_BY_ZERO

这是来自SharedBind

 public class MainActivity extends AppCompatActivity {


    @SharedField(key = "sample")
    public  int data = 2;

    SharedBind sharedBind = new SharedBind();

    @SharedMethod(key = "sample")
    public void methodTest() {
        Log.e("test", "" + data);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sharedBind.bind(this); // to run the binding

}

public void onButtonClick(){
sharedBind.runTest("sample");
}
}

这是来自public class SharedBind { HashMap<String, ArrayList<BindHandler>> fieldBindHandlerHashMap = new HashMap(); HashMap<String, ArrayList<BindHandler>> methodBindHandlerHashMap = new HashMap(); public void bind(Object target) { Class<?> obj = target.getClass(); for (Method method : obj.getDeclaredMethods()) { // if method is annotated with @Test if (method.isAnnotationPresent(SharedMethod.class)) { Annotation annotation = method.getAnnotation(SharedMethod.class); SharedMethod sharedMethod = (SharedMethod) annotation; ArrayList<BindHandler> methodBindHandlers = methodBindHandlerHashMap.get(sharedMethod.key()); if (methodBindHandlers == null) methodBindHandlers = new ArrayList<>(); methodBindHandlers.add(new BindHandler(method, target, sharedMethod)); methodBindHandlerHashMap.put(sharedMethod.key(), methodBindHandlers); } } for (Field field : obj.getDeclaredFields()) { if (field.isAnnotationPresent(SharedField.class)) { Annotation annotation = field.getAnnotation(SharedField.class); SharedField sharedView = (SharedField) annotation; ArrayList<BindHandler> fieldBindHandlers = fieldBindHandlerHashMap.get(sharedView.key()); if (fieldBindHandlers == null) fieldBindHandlers = new ArrayList<>(); fieldBindHandlers.add(new BindHandler(field, target, sharedView)); fieldBindHandlerHashMap.put(sharedView.key(), fieldBindHandlers); Log.e("done", "mdf"); Log.e("result", field.getGenericType().toString()); } } } public void runTest(String key) { ArrayList<BindHandler> viewBinds = fieldBindHandlerHashMap.get(key); if (viewBinds != null) { for (BindHandler bindHandler : viewBinds) { bindHandler.getTargetField().setAccessible(true); try { bindHandler.getTargetField().setInt(bindHandler.getTarget(), 234); } catch (IllegalAccessException e) { e.printStackTrace(); } } } ArrayList<BindHandler> methodBinds = methodBindHandlerHashMap.get(key); if (methodBinds != null) { for (BindHandler bindHandler : methodBinds) { try { bindHandler.getMethod().invoke(bindHandler.getTarget().getClass().newInstance()); } catch (Exception e) { e.printStackTrace(); } } } } }

BindHandler

我试图将内容修改为:

public class BindHandler {
    private Field targetField;
    private OperationType operationType;
    private SharedObj sharedObj;
    private Method method;
    private Object target;


    public BindHandler(Field targetField, Object target, SharedField sharedView) {
        this.targetField = targetField;
        this.target = target;
        this.sharedObj = sharedView.dataType();
        this.operationType = sharedView.operationType();
    }
}

这不起作用。但是当我将MainActivity中的变量更改为:

bindHandler.getTargetField().setInt(bindHandler.getTarget(), 234);

有效。如何使其适用于非静态变量。感谢

0 个答案:

没有答案
相关问题