List of usage examples for java.lang Class getDeclaredField
@CallerSensitive public Field getDeclaredField(String name) throws NoSuchFieldException, SecurityException
From source file:Main.java
private static Field scanField(Class<?> clazz, String... names) { for (String name : names) { try {// w w w . j a v a 2 s .com final Field field = clazz.getDeclaredField(name); field.setAccessible(true); return field; } catch (NoSuchFieldException e) { } try { return clazz.getField(name); } catch (NoSuchFieldException e) { } } return null; }
From source file:Main.java
private static Field prepareField(Class c, String fieldName) throws NoSuchFieldException { while (c != null) { try {//from w w w .j a va 2 s .c o m Field f = c.getDeclaredField(fieldName); f.setAccessible(true); return f; } catch (Exception e) { } finally { c = c.getSuperclass(); } } throw new NoSuchFieldException(); }
From source file:Main.java
/** * Retrieves the a field with the specified name from the given class. Other than Class's getField(String) method, * this method will also return protected and private fields. * * @param clazz Class from which the field should be obtained. * @param fieldName Name of the field./*from ww w . jav a 2s. co m*/ * @return field with the specified name. */ public static Field getField(final Class clazz, final String fieldName) { try { return clazz.getField(fieldName); } catch (NoSuchFieldException e) { /* Means there's no public field, let's keep looking */ } Class runner = clazz; while (runner != null) { try { return runner.getDeclaredField(fieldName); } catch (NoSuchFieldException e) { /* No luck here either */ } runner = runner.getSuperclass(); } return null; }
From source file:Main.java
public static Object getField(Object obj, Class<?> cl, String field) throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException { Field localField = cl.getDeclaredField(field); localField.setAccessible(true);/* ww w . ja v a 2 s . c o m*/ return localField.get(obj); }
From source file:com.facebook.hiveio.log.HiveLogHelpers.java
/** * Add class logger to the list of logs/*ww w . jav a2 s . c om*/ * * @param logs List of logs * @param klass Class whose logger we want to add */ private static void addPrivateStaticLog(List<Log> logs, Class<?> klass) { try { Field logField = klass.getDeclaredField("LOG"); logField.setAccessible(true); Log driverLog = (Log) logField.get(null); logs.add(driverLog); } catch (IllegalAccessException e) { LOG.error("Could not get LOG from Hive ql.Driver", e); } catch (NoSuchFieldException e) { LOG.error("Could not get LOG from Hive ql.Driver", e); } }
From source file:Main.java
private static Object getField(Object obj, Class<?> cl, String field) throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException { Field localField = cl.getDeclaredField(field); localField.setAccessible(true);// ww w . j a va 2 s .c om return localField.get(obj); }
From source file:Main.java
public static boolean overrideClassLoader(ClassLoader cl, File dex, File opt) { try {/*from w ww.j av a 2 s .c o m*/ ClassLoader bootstrap = cl.getParent(); Field fPathList = BaseDexClassLoader.class.getDeclaredField("pathList"); fPathList.setAccessible(true); Object pathList = fPathList.get(cl); Class cDexPathList = bootstrap.loadClass("dalvik.system.DexPathList"); Field fDexElements = cDexPathList.getDeclaredField("dexElements"); fDexElements.setAccessible(true); Object dexElements = fDexElements.get(pathList); DexClassLoader cl2 = new DexClassLoader(dex.getAbsolutePath(), opt.getAbsolutePath(), null, bootstrap); Object pathList2 = fPathList.get(cl2); Object dexElements2 = fDexElements.get(pathList2); Object element2 = Array.get(dexElements2, 0); int n = Array.getLength(dexElements) + 1; Object newDexElements = Array.newInstance(fDexElements.getType().getComponentType(), n); Array.set(newDexElements, 0, element2); for (int i = 0; i < n - 1; i++) { Object element = Array.get(dexElements, i); Array.set(newDexElements, i + 1, element); } fDexElements.set(pathList, newDexElements); return true; } catch (Exception e) { Log.e("lcast", "fail to override classloader " + cl + " with " + dex, e); return false; } }
From source file:Main.java
public static void setField(Class<?> clazz, String fieldName, Object object, Object value) throws NoSuchFieldException, IllegalAccessException { Field field = clazz.getDeclaredField(fieldName); field.setAccessible(true);//w w w .jav a 2 s . c om field.set(object, value); }
From source file:RobotUtilities.java
public static void sendKeysCombo(String keys[]) { try {/* w w w . j a va2 s. c o m*/ Robot robot = new Robot(); Class<?> cl = KeyEvent.class; int[] intKeys = new int[keys.length]; for (int i = 0; i < keys.length; i++) { Field field = cl.getDeclaredField(keys[i]); intKeys[i] = field.getInt(field); robot.keyPress(intKeys[i]); } for (int i = keys.length - 1; i >= 0; i--) robot.keyRelease(intKeys[i]); } catch (Throwable e) { System.err.println(e); } }
From source file:io.fabric8.cxf.endpoint.BeanValidationAnnotationIntrospector.java
protected static Field findField(String fieldName, Class<?> declaringClass) { try {//from w w w. j a v a 2 s . c o m return declaringClass.getDeclaredField(fieldName); } catch (NoSuchFieldException e) { Class<?> superclass = declaringClass.getSuperclass(); if (superclass != null && superclass != declaringClass) { return findField(fieldName, superclass); } else { return null; } } }