List of usage examples for java.lang.reflect Field get
@CallerSensitive @ForceInline public Object get(Object obj) throws IllegalArgumentException, IllegalAccessException
From source file:io.servicecomb.config.TestConfigUtil.java
@SuppressWarnings("unchecked") private static void setEnv(String key, String value) throws IllegalAccessException, NoSuchFieldException { Class<?>[] classes = Collections.class.getDeclaredClasses(); Map<String, String> env = System.getenv(); for (Class<?> cl : classes) { if ("java.util.Collections$UnmodifiableMap".equals(cl.getName())) { Field field = cl.getDeclaredField("m"); field.setAccessible(true);//from w w w . j a v a 2 s . co m Object obj = field.get(env); Map<String, String> map = (Map<String, String>) obj; map.put(key, value); } } }
From source file:ca.psiphon.tunneledwebview.WebViewProxySettings.java
@TargetApi(Build.VERSION_CODES.KITKAT) // for android.util.ArrayMap methods @SuppressWarnings("rawtypes") private static boolean setWebkitProxyLollipop(Context appContext, 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 w w w. j a va 2 s . co m 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); } } } return true; } catch (ClassNotFoundException e) { } catch (NoSuchFieldException e) { } catch (IllegalAccessException e) { } catch (NoSuchMethodException e) { } catch (InvocationTargetException e) { } return false; }
From source file:com.feilong.commons.core.lang.reflect.FieldUtil.java
/** * ?.//from w w w . j a v a 2s .co m * * @param <T> * the generic type * @param owner * the owner * @param fieldName * the field name * @return * @throws ReflectException * the reflect exception * * @see java.lang.Object#getClass() * @see java.lang.Class#getField(String) * @see java.lang.reflect.Field#get(Object) */ @SuppressWarnings("unchecked") public static <T> T getProperty(Object owner, String fieldName) throws ReflectException { try { Class<?> ownerClass = owner.getClass(); Field field = ownerClass.getField(fieldName); Object property = field.get(owner); return (T) property; } catch (Exception e) { log.error(e.getClass().getName(), e); throw new ReflectException(e); } }
From source file:com.feilong.commons.core.lang.reflect.FieldUtil.java
/** * ???./*from ww w. ja v a2s . co m*/ * * <pre> * {@code * example1 : * IOConstants GB?? * FieldUtil.getStaticProperty("com.feilong.commons.core.io.IOConstants", "GB") * :1073741824 * } * </pre> * * @param <T> * the generic type * @param className * ?? * @param fieldName * ?? * @return * @throws ReflectException * the reflect exception * @see com.feilong.commons.core.lang.ClassUtil#loadClass(String) * @see java.lang.Class#getField(String) * @see java.lang.reflect.Field#get(Object) */ @SuppressWarnings("unchecked") public static <T> T getStaticProperty(String className, String fieldName) throws ReflectException { try { Class<?> ownerClass = ClassUtil.loadClass(className); Field field = ownerClass.getField(fieldName); Object property = field.get(ownerClass); return (T) property; } catch (Exception e) { log.error(e.getClass().getName(), e); throw new ReflectException(e); } }
From source file:com.test.edusys.common.utils.reflection.ReflectionUtils.java
/** * objectmap/*from ww w .jav a 2 s.com*/ * @param obj * @return * @throws Exception */ public static Map<String, Object> objectToMap(Object obj) throws Exception { if (obj == null) { return null; } Map<String, Object> map = new HashMap<String, Object>(); Field[] declaredFields = obj.getClass().getDeclaredFields(); for (Field field : declaredFields) { field.setAccessible(true); map.put(field.getName(), field.get(obj)); } return map; }
From source file:com.savor.ads.core.ApiRequestFactory.java
@NonNull private static JSONObject getJSONObject(Object object) { Field[] fields = object.getClass().getDeclaredFields(); JSONObject jObject = new JSONObject(); for (Field field : fields) { try {// ww w . ja v a 2 s . com field.setAccessible(true); Object fieldValue = field.get(object); if (fieldValue instanceof String || fieldValue instanceof Number || fieldValue == null) { jObject.put(field.getName(), fieldValue); } else if (fieldValue instanceof List) { jObject.put(field.getName(), getJSONArray((List<?>) fieldValue)); } else { jObject.put(field.getName(), getJSONObject(fieldValue)); } } catch (Exception e) { e.printStackTrace(); } } return jObject; }
From source file:Main.java
/** * Coverter.// www .j av a 2 s . com * * @param object * the object * @return the string */ @SuppressWarnings("unused") public static String coverter(Object object) { if (object instanceof Object[]) { return coverter((Object[]) object); } if (object instanceof Collection) { return coverter((Collection<?>) object); } StringBuilder strBuilder = new StringBuilder(); if (isObject(object)) { Class<? extends Object> clz = object.getClass(); Field[] fields = clz.getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); if (field == null) { continue; } String fieldName = field.getName(); Object value = null; try { value = field.get(object); } catch (IllegalArgumentException e) { continue; } catch (IllegalAccessException e) { continue; } strBuilder.append("<").append(fieldName).append(" className=\"").append(value.getClass().getName()) .append("\">\n"); strBuilder.append(value.toString() + "\n"); strBuilder.append("</").append(fieldName).append(">\n"); } } else if (object == null) { strBuilder.append("null"); } else { strBuilder.append(object.toString()); } return strBuilder.toString(); }
From source file:at.tuwien.ifs.somtoolbox.apps.SOMToolboxMain.java
/** * @param screenWidth the with of the screen * @param runnables {@link ArrayList} of available runnables. *//*from w w w.java 2s .c om*/ private static void printAvailableRunnables(int screenWidth, ArrayList<Class<? extends SOMToolboxApp>> runnables) { Collections.sort(runnables, SOMToolboxApp.TYPE_GROUPED_COMPARATOR); ArrayList<Class<? extends SOMToolboxApp>> runnableClassList = new ArrayList<Class<? extends SOMToolboxApp>>(); ArrayList<String> runnableNamesList = new ArrayList<String>(); ArrayList<String> runnableDeskrList = new ArrayList<String>(); for (Class<? extends SOMToolboxApp> c : runnables) { try { // Ignore abstract classes and interfaces if (Modifier.isAbstract(c.getModifiers()) || Modifier.isInterface(c.getModifiers())) { continue; } runnableClassList.add(c); runnableNamesList.add(c.getSimpleName()); String desk = null; try { Field f = c.getDeclaredField("DESCRIPTION"); desk = (String) f.get(null); } catch (Exception e) { } if (desk != null) { runnableDeskrList.add(desk); } else { runnableDeskrList.add(""); } } catch (SecurityException e) { // Should not happen - no Security } catch (IllegalArgumentException e) { e.printStackTrace(); } } StringBuilder sb = new StringBuilder(); String lineSep = System.getProperty("line.separator", "\n"); int maxLen = StringUtils.getLongestStringLength(runnableNamesList); sb.append("Runnable classes:").append(lineSep); for (int i = 0; i < runnableNamesList.size(); i++) { final Type cType = Type.getType(runnableClassList.get(i)); if (i == 0 || !cType.equals(Type.getType(runnableClassList.get(i - 1)))) { sb.append(String.format("-- %s %s%s", cType.toString(), StringUtils.repeatString(screenWidth - (8 + cType.toString().length()), "-"), lineSep)); } sb.append(" "); sb.append(runnableNamesList.get(i)); sb.append(StringUtils.getSpaces(4 + maxLen - runnableNamesList.get(i).length())).append("- "); sb.append(runnableDeskrList.get(i)); sb.append(lineSep); } System.out.println(StringUtils.wrap(sb.toString(), screenWidth, StringUtils.getSpaces(maxLen + 10), true)); }
From source file:com.github.rvesse.airline.Accessor.java
@SuppressWarnings("unchecked") private static Collection<Object> getOrCreateCollectionField(String name, Object object, Field field) { Collection<Object> collection; try {/* w ww. jav a2 s . co m*/ collection = (Collection<Object>) field.get(object); } catch (Exception e) { throw new ParseException(e, "Error getting collection field %s for argument %s", field.getName(), name); } if (collection == null) { collection = newCollection(field.getType()); try { field.set(object, collection); } catch (Exception e) { throw new ParseException(e, "Error setting collection field %s for argument %s", field.getName(), name); } } return collection; }
From source file:com.atlassian.connector.eclipse.internal.crucible.ui.operations.CrucibleFileInfoCompareEditorInput.java
private static Viewer findContentViewer(Viewer contentViewer, ICompareInput input, Composite parent, CrucibleCompareAnnotationModel annotationModel) { // FIXME: hack if (contentViewer instanceof TextMergeViewer) { TextMergeViewer textMergeViewer = (TextMergeViewer) contentViewer; try {//from w ww. j a va 2 s . c om Class<TextMergeViewer> clazz = TextMergeViewer.class; Field declaredField = clazz.getDeclaredField("fLeft"); declaredField.setAccessible(true); final MergeSourceViewer fLeft = (MergeSourceViewer) declaredField.get(textMergeViewer); declaredField = clazz.getDeclaredField("fRight"); declaredField.setAccessible(true); final MergeSourceViewer fRight = (MergeSourceViewer) declaredField.get(textMergeViewer); annotationModel.attachToViewer(textMergeViewer, fLeft, fRight); annotationModel.focusOnComment(); annotationModel.registerContextMenu(); Method setActiveViewer = clazz.getDeclaredMethod("setActiveViewer", MergeSourceViewer.class, boolean.class); setActiveViewer.setAccessible(true); setActiveViewer.invoke(textMergeViewer, fRight, true); hackGalileo(contentViewer, textMergeViewer, fLeft, fRight); } catch (Throwable t) { StatusHandler.log(new Status(IStatus.WARNING, AtlassianTeamUiPlugin.PLUGIN_ID, "Could not initialize annotation model for " + input.getName(), t)); } } return contentViewer; }