List of usage examples for java.lang.reflect Field setAccessible
@Override @CallerSensitive public void setAccessible(boolean flag)
From source file:com.dwdesign.tweetings.util.WebViewProxySettings.java
private static void setDeclaredField(Object obj, String name, Object value) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { final Field f = obj.getClass().getDeclaredField(name); f.setAccessible(true); f.set(obj, value);/* w ww. java2 s . c o m*/ }
From source file:org.onebusaway.io.client.test.UriAssert.java
protected static URI getUriFromRequest(RequestBase actualRequest) throws NoSuchFieldException, IllegalAccessException { Field uriField = RequestBase.class.getDeclaredField("mUri"); uriField.setAccessible(true); URI actualUri = (URI) uriField.get(actualRequest); return actualUri; }
From source file:com.funtl.framework.smoke.core.modules.act.service.creator.RuntimeActivityCreatorSupport.java
private static void copyFields(Object source, Object target, String... fieldNames) { Assert.assertNotNull(source);/*from w ww .j a va2 s.co m*/ Assert.assertNotNull(target); Assert.assertSame(source.getClass(), target.getClass()); for (String fieldName : fieldNames) { try { Field field = FieldUtils.getField(source.getClass(), fieldName, true); field.setAccessible(true); field.set(target, field.get(source)); } catch (Exception e) { e.printStackTrace(); } } }
From source file:com.gargoylesoftware.htmlunit.TestCaseTest.java
/** * Returns list of all host classes defined. * @return the list//from ww w .j ava2 s .c om * @throws Exception if an error occurs. */ public static List<String> getAllClassNames() throws Exception { final Field field = JavaScriptConfiguration.class.getDeclaredField("CLASSES_"); field.setAccessible(true); final List<String> list = new ArrayList<>(); for (final Class<?> c : (Class<?>[]) field.get(null)) { final String name = c.getSimpleName(); list.add(name); } list.add(Intl.class.getSimpleName()); list.add("Error"); return list; }
From source file:Main.java
public static void dumphreadLocals() { try {//www . j a v a 2 s . co m // Get a reference to the thread locals table of the current thread Thread thread = Thread.currentThread(); Field threadLocalsField = Thread.class.getDeclaredField("threadLocals"); threadLocalsField.setAccessible(true); Object threadLocalTable = threadLocalsField.get(thread); // Get a reference to the array holding the thread local variables // inside the // ThreadLocalMap of the current thread @SuppressWarnings("rawtypes") Class threadLocalMapClass = Class.forName("java.lang.ThreadLocal$ThreadLocalMap"); Field tableField = threadLocalMapClass.getDeclaredField("table"); tableField.setAccessible(true); Object[] table = (Object[]) tableField.get(threadLocalTable); @SuppressWarnings("rawtypes") Class threadLocalMapEntryClass = Class.forName("java.lang.ThreadLocal$ThreadLocalMap$Entry"); Field entryValueField = threadLocalMapEntryClass.getDeclaredField("value"); entryValueField.setAccessible(true); // The key to the ThreadLocalMap is a WeakReference object. The // referent field of this object // is a reference to the actual ThreadLocal variable Field referentField = Reference.class.getDeclaredField("referent"); referentField.setAccessible(true); for (Object entry : table) { // Each entry in the table array of ThreadLocalMap is an Entry // object // representing the thread local reference and its value if (entry != null) { Object tlcValue = entryValueField.get(entry); ThreadLocal threadLocal = (ThreadLocal) referentField.get(entry); // System.out.println("thread local value "+tlcValue); // if(threadLocal) printObject(threadLocal, tlcValue); } } System.out.println("__________________________"); } catch (Exception e) { // We will tolerate an exception here and just log it throw new IllegalStateException(e); } }
From source file:Main.java
/** * @param clazz// w ww . ja v a2s. c om * @return namespace of root element qname or null if this is not object does not represent a root element */ public static String getEnumValue(Enum<?> myEnum) { Field f; String value; try { f = myEnum.getClass().getField(myEnum.name()); f.setAccessible(true); XmlEnumValue xev = (XmlEnumValue) getAnnotation(f, XmlEnumValue.class); if (xev == null) { value = f.getName(); } else { value = xev.value(); } } catch (SecurityException e) { value = null; } catch (NoSuchFieldException e) { value = null; } return value; }
From source file:com.taobao.itest.spring.context.SpringContextManager.java
private static boolean hasBeanInjected(Field field, Object object) { field.setAccessible(true); try {// ww w . ja va2 s .c o m return field.get(object) != null; } catch (Exception e) { e.printStackTrace(); return false; } }
From source file:com.ejisto.modules.web.util.FieldSerializationUtil.java
private static Object safeGet(Field f, Object container) { try {//from w ww. j a v a2 s.com f.setAccessible(true); return f.get(container); } catch (IllegalAccessException e) { log.log(Level.SEVERE, "unexpected exception", e); return null; } }
From source file:fi.jumi.test.JumiBootstrapTest.java
private static Object getDaemonOutput(JumiBootstrap bootstrap) throws Exception { Object out = getFieldValue(bootstrap, "daemonOutput"); // JumiBootstrap wraps System.err into a CloseShieldOutputStream, so this code will unwrap it FilterOutputStream wrapper = (FilterOutputStream) out; Field f = FilterOutputStream.class.getDeclaredField("out"); f.setAccessible(true); return f.get(wrapper); }
From source file:Main.java
public static Map<String, Object> convertObjToMap(Object object, String token) { Map<String, Object> map = new HashMap<>(); if (object == null) { return null; }//from www.jav a 2 s.c om Field[] fields = object.getClass().getSuperclass().getDeclaredFields(); Field[] ff = object.getClass().getDeclaredFields(); try { for (int i = 0; i < fields.length; i++) { Field field = object.getClass().getSuperclass().getDeclaredField(fields[i].getName()); field.setAccessible(true); Annotation[] annotations = field.getDeclaredAnnotations(); String key = ""; for (Annotation annotation : annotations) { if (annotation instanceof SerializedName) { key = ((SerializedName) annotation).value(); } } if (TextUtils.isEmpty(key)) { key = fields[i].getName(); } Object o = field.get(object); map.put(key, o); } for (int i = 0; i < ff.length; i++) { Field field = object.getClass().getDeclaredField(ff[i].getName()); field.setAccessible(true); Annotation[] annotations = field.getDeclaredAnnotations(); String key = ""; for (Annotation annotation : annotations) { if (annotation instanceof SerializedName) { key = ((SerializedName) annotation).value(); } } if (TextUtils.isEmpty(key)) { key = ff[i].getName(); } Object o = field.get(object); map.put(key, o); } } catch (Exception e) { e.printStackTrace(); } map.remove("token"); map.put("token", token); Log.d(".......", "convert success"); return map; }