Back to project page AndroidPlugin.
The source code is released under:
MIT License
If you think the Android project AndroidPlugin listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.limemobile.app.plugin.internal; /*from w w w.j a v a 2 s .c o m*/ import java.lang.reflect.Field; public class ReflectFieldAccessor<T> { private Object mObject; private String mFieldName; private boolean isInited; private Field mField; public ReflectFieldAccessor(Object obj, String fieldName) { if (obj == null) { throw new IllegalArgumentException("obj cannot be null"); } this.mObject = obj; this.mFieldName = fieldName; } private void prepare() { if (isInited) return; isInited = true; Class<?> c = mObject.getClass(); while (c != null) { try { Field f = c.getDeclaredField(mFieldName); f.setAccessible(true); mField = f; return; } catch (Exception e) { } finally { c = c.getSuperclass(); } } } public T get() throws NoSuchFieldException, IllegalAccessException, IllegalArgumentException { prepare(); if (mField == null) throw new NoSuchFieldException(); try { @SuppressWarnings("unchecked") T r = (T) mField.get(mObject); return r; } catch (ClassCastException e) { throw new IllegalArgumentException("unable to cast object"); } } public void set(T val) throws NoSuchFieldException, IllegalAccessException, IllegalArgumentException { prepare(); if (mField == null) throw new NoSuchFieldException(); mField.set(mObject, val); } }