List of usage examples for java.lang.reflect Field get
@CallerSensitive @ForceInline public Object get(Object obj) throws IllegalArgumentException, IllegalAccessException
From source file:com.facebook.hiveio.log.HiveLogHelpers.java
/** * Add class logger to the list of logs// ww w . j a v a 2 s . co m * * @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:com.mycompany.client.bank.utils.CryptMessage.java
public static Object toInternal(String SuperSecret, Object criptStrings) { Class curClass = criptStrings.getClass(); byte[] syperSecretBytes = null; try {//from w ww .ja va 2 s .c o m syperSecretBytes = SuperSecret.getBytes("utf-8"); } catch (UnsupportedEncodingException ex) { Logger.getLogger(CryptMessage.class.getName()).log(Level.SEVERE, null, ex); } for (Field field : curClass.getDeclaredFields()) { try { String str = null; if (field.get(criptStrings) != null) str = field.get(criptStrings).toString(); if (str != null) { byte[] mybyte = str.getBytes("utf-8"); int i = 0; ByteTransform(syperSecretBytes, mybyte, i); field.set(criptStrings, new String(mybyte, "utf-8")); } } catch (UnsupportedEncodingException | IllegalArgumentException | IllegalAccessException ex) { Logger.getLogger(CryptMessage.class.getName()).log(Level.SEVERE, null, ex); } } return criptStrings; }
From source file:Main.java
public static void dumphreadLocals() throws NoSuchFieldException, IllegalAccessException, ClassNotFoundException { Thread thread = Thread.currentThread(); Field threadLocalField = thread.getClass().getDeclaredField("threadLocals"); threadLocalField.setAccessible(true); Object threadLocalTable = threadLocalField.get(thread); Class threadLocalMapClass = Class.forName("java.lang.ThreadLocal$ThreadLocalMap"); //Class threadLocalMapClass = Class.forName(threadLocalField.getType().getName()); Field tableField = threadLocalMapClass.getDeclaredField("table"); tableField.setAccessible(true);// ww w . j a va 2 s. c o m Object[] table = (Object[]) tableField.get(threadLocalTable); Class threadLocalMapEntryClass = Class.forName("java.lang.ThreadLocal$ThreadLocalMap$Entry"); Field entryValueField = threadLocalMapEntryClass.getDeclaredField("value"); entryValueField.setAccessible(true); //Field referenceField = Reference.class.getDeclaredField("referent") ; //referenceField.setAccessible(true); for (Object entry : table) { if (entry != null) { Object threadLocalValue = entryValueField.get(entry); printObject(threadLocalValue); //ThreadLocal threadLocal = (ThreadLocal)referenceField.get(entry); //System.out.println("thread local value "+threadLocal); } } }
From source file:Main.java
@TargetApi(Build.VERSION_CODES.KITKAT) private static boolean setProxyLollipop(final Context context, String host, int port) { System.setProperty("http.proxyHost", host); System.setProperty("http.proxyPort", port + ""); System.setProperty("https.proxyHost", host); System.setProperty("https.proxyPort", port + ""); try {//from ww w. j av a2 s .c om Context appContext = context.getApplicationContext(); Class applictionClass = Class.forName("android.app.Application"); Field mLoadedApkField = applictionClass.getDeclaredField("mLoadedApk"); mLoadedApkField.setAccessible(true); Object mloadedApk = mLoadedApkField.get(appContext); Class loadedApkClass = Class.forName("android.app.LoadedApk"); Field mReceiversField = loadedApkClass.getDeclaredField("mReceivers"); mReceiversField.setAccessible(true); ArrayMap receivers = (ArrayMap) mReceiversField.get(mloadedApk); for (Object receiverMap : receivers.values()) { for (Object receiver : ((ArrayMap) receiverMap).keySet()) { Class clazz = receiver.getClass(); if (clazz.getName().contains("ProxyChangeListener")) { Method onReceiveMethod = clazz.getDeclaredMethod("onReceive", Context.class, Intent.class); Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION); onReceiveMethod.invoke(receiver, appContext, intent); } } } } catch (Exception e) { e.printStackTrace(); return false; } return true; }
From source file:com.erinors.hpb.tests.HpbTestUtils.java
public static void assertEqualsByCloneableFields(Object o1, Object o2) { if (o1 == null || o2 == null || o1.getClass() != o2.getClass()) { throw new IllegalArgumentException( "Non-null objects of same class expected but got: " + o1 + ", " + o2); }//from w w w . j a v a 2 s. com List<Field> fields = new LinkedList<Field>(); ClassUtils.collectCloneableFields(o1.getClass(), fields); for (Field field : fields) { try { ReflectionUtils.makeAccessible(field); Object fieldValue1 = field.get(o1); Object fieldValue2 = field.get(o2); if (!equal(fieldValue1, fieldValue2)) { throw new AssertionError("Field value mismatch: " + field + ", value1=" + fieldValue1 + ", value2=" + fieldValue2); } } catch (Exception e) { throw new RuntimeException("Cannot copy field: " + field, e); } } }
From source file:Main.java
public static String[][] returnTable(Collection E) { if ((E == null) || E.isEmpty()) { System.out.println("The collection is empty!"); }/*from w w w.ja v a 2 s .co m*/ Set<Field> collectionFields = new TreeSet<>(new Comparator<Field>() { @Override public int compare(Field o1, Field o2) { return o1.getName().compareTo(o2.getName()); } }); for (Object o : E) { Class c = o.getClass(); createSetOfFields(collectionFields, c); while (c.getSuperclass() != null) { c = c.getSuperclass(); createSetOfFields(collectionFields, c); } } String[][] exitText = new String[E.size() + 1][collectionFields.size() + 1]; exitText[0][0] = String.format("%20s", "Class"); int indexOfColumn = 0; for (Field f : collectionFields) { exitText[0][indexOfColumn + 1] = String.format("%20s", f.getName()); indexOfColumn++; } int indexOfRow = 0; for (Object o : E) { indexOfColumn = 0; exitText[indexOfRow + 1][0] = String.format("%20s", o.getClass().getSimpleName()); for (Field field : collectionFields) { try { field.setAccessible(true); if (field.get(o) instanceof Date) { exitText[indexOfRow + 1][indexOfColumn + 1] = String.format("%20tD", field.get(o)); } else { String temp = String.valueOf(field.get(o)); exitText[indexOfRow + 1][indexOfColumn + 1] = String.format("%20s", temp); } } catch (Exception e) { exitText[indexOfRow + 1][indexOfColumn + 1] = String.format("%20s", "-"); } indexOfColumn++; } indexOfRow++; } return exitText; }
From source file:Main.java
public static LinkedHashMap<String, String> convertBeans(Object bean) { if (bean == null) return null; try {// ww w. ja va2 s. c o m LinkedHashMap<String, String> returnMap = new LinkedHashMap<String, String>(); Class<? extends Object> clazz = bean.getClass(); List<Field> fleids = new ArrayList<Field>(); for (Class<?> c = clazz; c != Object.class; c = c.getSuperclass()) { fleids.addAll(Arrays.asList(c.getDeclaredFields())); } for (Field field : fleids) { String value = ""; field.setAccessible(true); try { Object result = field.get(bean); if (result == null) continue; value = result.toString(); } catch (IllegalAccessException e) { e.printStackTrace(); } // MLogUtil.e("field.getName() "+field.getName()); // MLogUtil.e("value "+value); returnMap.put(field.getName(), value); field.setAccessible(false); } return returnMap; } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:Main.java
public static <T> T getVar(@NonNull Object obj, @NonNull String name) 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);//from w w w .j ava 2 s . c o m //noinspection unchecked return (T) f.get(obj); } } } throw new RuntimeException("no var matching " + name); }
From source file:Main.java
/** * @param context/* w w w. j a v a2 s . c o m*/ * if null, use the default format (Mozilla/5.0 (Linux; U; * Android %s) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 * %sSafari/534.30). * @return */ public static String getUserAgent(Context context) { String webUserAgent = null; if (context != null) { try { Class<?> sysResCls = Class.forName("com.android.internal.R$string"); Field webUserAgentField = sysResCls.getDeclaredField("web_user_agent"); Integer resId = (Integer) webUserAgentField.get(null); webUserAgent = context.getString(resId); } catch (Throwable ignored) { } } if (TextUtils.isEmpty(webUserAgent)) { webUserAgent = "Mozilla/5.0 (Linux; U; Android %s) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 %sSafari/533.1"; } Locale locale = Locale.getDefault(); StringBuffer buffer = new StringBuffer(); // Add version final String version = Build.VERSION.RELEASE; if (version.length() > 0) { buffer.append(version); } else { // default to "1.0" buffer.append("1.0"); } buffer.append("; "); final String language = locale.getLanguage(); if (language != null) { buffer.append(language.toLowerCase()); final String country = locale.getCountry(); if (country != null) { buffer.append("-"); buffer.append(country.toLowerCase()); } } else { // default to "en" buffer.append("en"); } // add the model for the release build if ("REL".equals(Build.VERSION.CODENAME)) { final String model = Build.MODEL; if (model.length() > 0) { buffer.append("; "); buffer.append(model); } } final String id = Build.ID; if (id.length() > 0) { buffer.append(" Build/"); buffer.append(id); } return String.format(webUserAgent, buffer, "Mobile "); }
From source file:Main.java
public static Map<String, String> convertBean(Object bean) { if (bean == null) return null; try {//from ww w . j ava 2s . co m Class<? extends Object> clazz = bean.getClass(); Map<String, String> returnMap = new HashMap<String, String>(); List<Field> fleids = new ArrayList<Field>(); for (Class<?> c = clazz; c != Object.class; c = c.getSuperclass()) { fleids.addAll(Arrays.asList(c.getDeclaredFields())); } for (Field field : fleids) { String value = ""; field.setAccessible(true); try { Object result = field.get(bean); if (result == null) continue; value = result.toString(); } catch (IllegalAccessException e) { e.printStackTrace(); } // MLogUtil.e("field.getName() "+field.getName()); // MLogUtil.e("value "+value); returnMap.put(field.getName(), value); field.setAccessible(false); } return returnMap; } catch (Exception e) { e.printStackTrace(); return null; } }