Example usage for java.lang.reflect Field setAccessible

List of usage examples for java.lang.reflect Field setAccessible

Introduction

In this page you can find the example usage for java.lang.reflect Field setAccessible.

Prototype

@Override
@CallerSensitive
public void setAccessible(boolean flag) 

Source Link

Usage

From source file:Main.java

public static void setVar(@NonNull Object obj, @NonNull String name, @Nullable Object val) throws Exception {
    for (Class cls = obj.getClass(); //
            cls != null && cls != Object.class; //
            cls = cls.getSuperclass()) {
        for (Field f : cls.getDeclaredFields()) {
            if (f.getName().equals(name)) {
                f.setAccessible(true);
                f.set(obj, val);
                return;
            }/*w ww .  jav  a  2s  . co  m*/
        }
    }
    throw new RuntimeException("no var matching " + name);
}

From source file:Main.java

public static void setListViewEdgeEffectColor(AbsListView listView, int color) {
    if (Build.VERSION.SDK_INT >= 21) {
        try {/*from  www  .  j a  v  a2  s .  co m*/
            Field field = AbsListView.class.getDeclaredField("mEdgeGlowTop");
            field.setAccessible(true);
            EdgeEffect mEdgeGlowTop = (EdgeEffect) field.get(listView);
            if (mEdgeGlowTop != null) {
                mEdgeGlowTop.setColor(color);
            }

            field = AbsListView.class.getDeclaredField("mEdgeGlowBottom");
            field.setAccessible(true);
            EdgeEffect mEdgeGlowBottom = (EdgeEffect) field.get(listView);
            if (mEdgeGlowBottom != null) {
                mEdgeGlowBottom.setColor(color);
            }
        } catch (Exception e) {
            //                FileLog.e("tmessages", e);
        }
    }
}

From source file:Main.java

static Field getField(Class<?> clz, final String fldName) {
    if (clz == null || TextUtils.isEmpty(fldName)) {
        return null;
    }/* w w  w.j a v a2 s. c  o  m*/
    Field fld = null;
    try {
        fld = clz.getDeclaredField(fldName);
        fld.setAccessible(true);
    } catch (NoSuchFieldException e) {
        Log.d(TAG, e.getMessage(), e);
    }
    return fld;
}

From source file:com.streametry.json.JsonSerializer.java

public static Object getFieldValue(Field f, Object o) {
    try {//from w ww  .  j  a  v a2  s .c  o m
        f.setAccessible(true);
        return f.get(o);
    } catch (IllegalAccessException e) {
        throw new IllegalStateException(e);
    }
}

From source file:Main.java

/**
 * Returns the first field of the given type in a class.
 * Might be useful for Proguard'ed classes to identify fields with unique types.
 * If no matching field was not found, a {@link NoSuchFieldError} will be thrown.
 *//*from ww  w  .j  a v a  2s.co m*/
public static Field findFirstFieldByExactType(Class<?> clazz, Class<?> type) {
    Class<?> clz = clazz;
    do {
        for (Field field : clz.getDeclaredFields()) {
            if (field.getType() == type) {
                field.setAccessible(true);
                return field;
            }
        }
    } while ((clz = clz.getSuperclass()) != null);

    throw new NoSuchFieldError("Field of type " + type.getName() + " in class " + clazz.getName());
}

From source file:Main.java

/**
 * Set the fields' value.//from w  ww  .j  a  v  a 2  s .c  o  m
 * 
 * @param bean
 * @param valMap
 */
public static void setFieldValues(Object bean, Map<String, Object> valMap) {
    Class<?> cls = bean.getClass();
    //Get all fields.
    Field[] fields = cls.getDeclaredFields();

    for (Field field : fields) {
        if (valMap.containsKey(field.getName())) {
            field.setAccessible(true);
            try {
                field.set(bean, valMap.get(field.getName()));
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:Main.java

public static <T> T getValue(ThreadLocal<T> variable, Thread key) {
    try {//w ww . j  a  v  a 2 s .c o m
        Field field = Thread.class.getDeclaredField("threadLocals");
        field.setAccessible(true);
        Object entryMap = field.get(key);
        if (entryMap == null)
            return null;
        Method getEntryMethod = entryMap.getClass().getDeclaredMethod("getEntry", ThreadLocal.class);
        getEntryMethod.setAccessible(true);
        Object entry = getEntryMethod.invoke(entryMap, variable);
        if (entry == null)
            return null;
        Field valueField = entry.getClass().getDeclaredField("value");
        valueField.setAccessible(true);

        return (T) valueField.get(entry);
    } catch (NoSuchFieldException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(e);
    } catch (InvocationTargetException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static <T> T getPrivateConstantField(Class<?> outerClass, String innerClassName, String fieldname,
        Class<T> returnType) throws Exception {
    // Get all inner classes
    Class<?> innerClasses[] = outerClass.getDeclaredClasses();
    // find the inner class that matches the order
    Class<?> innerClass = null;

    for (int index = 0; index < innerClasses.length; index++) {
        if (innerClassName.equals(innerClasses[index].getSimpleName())) {
            innerClass = innerClasses[index];
        }/*from  w w  w  .  j a va 2 s . c o  m*/
    }
    T returnValue = null;
    if (innerClass != null) {
        Field field;
        field = innerClass.getDeclaredField(fieldname);
        field.setAccessible(true);
        returnValue = (T) field.get(innerClass);
    }
    return returnValue;
}

From source file:Main.java

public static boolean creteEntity(Object entity, String fileName) {
    boolean flag = false;
    try {/*from w  ww  .  j  ava  2 s  .  com*/
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse(fileName);

        Class clazz = entity.getClass();
        Field[] fields = clazz.getDeclaredFields();
        Node EntityElement = document.getElementsByTagName(clazz.getSimpleName() + "s").item(0);

        Element newEntity = document.createElement(clazz.getSimpleName());
        EntityElement.appendChild(newEntity);

        for (Field field : fields) {
            field.setAccessible(true);
            Element element = document.createElement(field.getName());
            element.appendChild(document.createTextNode(field.get(entity).toString()));
            newEntity.appendChild(element);
        }

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource domSource = new DOMSource(document);
        StreamResult streamResult = new StreamResult(new File(fileName));
        transformer.transform(domSource, streamResult);
        flag = true;
    } catch (ParserConfigurationException pce) {
        System.out.println(pce.getLocalizedMessage());
        pce.printStackTrace();
    } catch (TransformerException te) {
        System.out.println(te.getLocalizedMessage());
        te.printStackTrace();
    } catch (IOException ioe) {
        System.out.println(ioe.getLocalizedMessage());
        ioe.printStackTrace();
    } catch (SAXException sae) {
        System.out.println(sae.getLocalizedMessage());
        sae.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    return flag;
}

From source file:com.sun.faces.application.ResetUniqueRequestIdBean.java

public static void setPrivateField(String fieldName, Class containingClass, Object target, Object value) {
    try {/*from   w ww  .j  a  va2s .  co m*/
        Field field = containingClass.getDeclaredField(fieldName);
        field.setAccessible(true);
        field.set(target, value);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}