List of usage examples for java.lang.reflect Field get
@CallerSensitive @ForceInline public Object get(Object obj) throws IllegalArgumentException, IllegalAccessException
From source file:ca.psiphon.tunneledwebview.WebViewProxySettings.java
@TargetApi(Build.VERSION_CODES.KITKAT) @SuppressWarnings("rawtypes") private static boolean setWebkitProxyKitKat(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 {/* www .j a v a 2s . com*/ Class applicationClass = Class.forName("android.app.Application"); Field loadedApkField = applicationClass.getDeclaredField("mLoadedApk"); loadedApkField.setAccessible(true); Object loadedApk = loadedApkField.get(appContext); Class loadedApkClass = Class.forName("android.app.LoadedApk"); Field receiversField = loadedApkClass.getDeclaredField("mReceivers"); receiversField.setAccessible(true); ArrayMap receivers = (ArrayMap) receiversField.get(loadedApk); for (Object receiverMap : receivers.values()) { for (Object receiver : ((ArrayMap) receiverMap).keySet()) { Class receiverClass = receiver.getClass(); if (receiverClass.getName().contains("ProxyChangeListener")) { Method onReceiveMethod = receiverClass.getDeclaredMethod("onReceive", Context.class, Intent.class); Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION); final String CLASS_NAME = "android.net.ProxyProperties"; Class proxyPropertiesClass = Class.forName(CLASS_NAME); Constructor constructor = proxyPropertiesClass.getConstructor(String.class, Integer.TYPE, String.class); constructor.setAccessible(true); Object proxyProperties = constructor.newInstance(host, port, null); intent.putExtra("proxy", (Parcelable) proxyProperties); onReceiveMethod.invoke(receiver, appContext, intent); } } } return true; } catch (ClassNotFoundException e) { } catch (NoSuchFieldException e) { } catch (IllegalAccessException e) { } catch (IllegalArgumentException e) { } catch (NoSuchMethodException e) { } catch (InvocationTargetException e) { } catch (InstantiationException e) { } return false; }
From source file:Main.java
public static void setDataEnabled(Context context, boolean enabled) { ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); Class<?> conMgrClass = null; Field iConMgrField = null; Object iConMgr = null;/*from w w w . jav a 2 s .c o m*/ Class<?> iConMgrClass = null; Method setMobileDataEnabledMethod = null; try { conMgrClass = Class.forName(conMgr.getClass().getName()); iConMgrField = conMgrClass.getDeclaredField("mService"); iConMgrField.setAccessible(true); iConMgr = iConMgrField.get(conMgr); iConMgrClass = Class.forName(iConMgr.getClass().getName()); setMobileDataEnabledMethod = iConMgrClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE); setMobileDataEnabledMethod.setAccessible(true); setMobileDataEnabledMethod.invoke(iConMgr, enabled); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.libframework.annotation.util.OtherUtils.java
/** * @param context 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/*from ww w. ja v a 2 s.co m*/ */ 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(Locale.getDefault())); final String country = locale.getCountry(); if (country != null) { buffer.append("-"); buffer.append(country.toLowerCase(Locale.getDefault())); } } 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:edu.mayo.cts2.framework.webapp.rest.view.jsp.Beans.java
/** * Inspect.// w w w .j a v a 2 s .c o m * * @param bean the bean * @return the list */ public static List<Map.Entry<String, Object>> inspect(Object bean) { Map<String, Object> props = new LinkedHashMap<String, Object>(); Class<?> clazz = bean.getClass(); while (clazz != null) { for (Field field : clazz.getDeclaredFields()) { field.setAccessible(true); String name = field.getName(); Object value; try { value = field.get(bean); } catch (Exception e) { throw new RuntimeException(e); } if (value != null) { props.put(name, value); } } clazz = clazz.getSuperclass(); } List<Map.Entry<String, Object>> list = new ArrayList<Map.Entry<String, Object>>(props.entrySet()); Collections.sort(list, BEAN_COMPARATOR); return list; }
From source file:com.chiorichan.util.StringUtil.java
public static Color parseColor(String color) { Pattern c = Pattern.compile("rgb *\\( *([0-9]+), *([0-9]+), *([0-9]+) *\\)"); Matcher m = c.matcher(color); // First try to parse RGB(0,0,0); if (m.matches()) return new Color(Integer.valueOf(m.group(1)), // r Integer.valueOf(m.group(2)), // g Integer.valueOf(m.group(3))); // b try {/*from www.java2 s. co m*/ Field field = Class.forName("java.awt.Color").getField(color.trim().toUpperCase()); return (Color) field.get(null); } catch (Exception e) { } try { return Color.decode(color); } catch (Exception e) { } return null; }
From source file:com.iflytek.edu.cloud.frame.web.filter.CheckOpenServiceFilterTest.java
@BeforeClass public static void init() { InitializingService initializingService = new InitializingService(); try {/*w w w.j a v a 2s .co m*/ initializingService.afterPropertiesSet(); } catch (Exception e1) { e1.printStackTrace(); } ErrorRequestMessageConverter messageConverter = new ErrorRequestMessageConverter(); messageConverter.setJsonMessageConverter(new MappingJackson2HttpMessageConverter()); messageConverter.setXmlMessageConverter(new Jaxb2RootElementHttpMessageConverterExt()); filter = new CheckOpenServiceFilter(); filter.setMessageConverter(messageConverter); try { Field field = filter.getClass().getDeclaredField("methodVersionMap"); field.setAccessible(true); Multimap<String, String> methodVersionMap = (Multimap<String, String>) field.get(filter); methodVersionMap.put("user.get", "1.0.0"); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.cloudbees.plugins.credentials.ContextMenuIconUtils.java
/** * Gets the qualified URL of the specified icon. * * @param icon the icon.// w ww.jav a 2 s .c om * @return the qualified URL of the icon. */ @CheckForNull public static String getQualifiedUrl(@CheckForNull Icon icon) { if (icon == null) { return null; } try { Field iconType = Icon.class.getDeclaredField("iconType"); iconType.setAccessible(true); IconType type = (IconType) iconType.get(icon); switch (type) { case CORE: { return Functions.getResourcePath() + "/images/" + icon.getUrl(); } case PLUGIN: { return Functions.getResourcePath() + "/plugin/" + icon.getUrl(); } } return null; } catch (NoSuchFieldException e) { // ignore we'll use a JellyContext } catch (IllegalAccessException e) { // ignore we'll use a JellyContext } JellyContext ctx = new JellyContext(); ctx.setVariable("resURL", Functions.getResourcePath()); return icon.getQualifiedUrl(ctx); }
From source file:com.google.code.ddom.commons.cl.ClassLoaderLocal.java
private static Map<ClassLoaderLocal<?>, Object> getClassLoaderLocalMap(ClassLoader cl) { if (cl == ClassLoaderLocal.class.getClassLoader()) { return ClassLoaderLocalMapHolder.locals; } else {// www . jav a 2 s . c om Class<?> holderClass; try { holderClass = cl.loadClass(holderClassName); if (holderClass.getClassLoader() != cl) { holderClass = null; } } catch (ClassNotFoundException ex) { holderClass = null; } if (holderClass == null) { try { holderClass = (Class<?>) defineClassMethod.invoke(cl, holderClassName, holderClassDef, 0, holderClassDef.length); } catch (Exception ex) { // TODO: can we get here? maybe if two threads call getClassLoaderLocalMap concurrently? throw new Error(ex); } } Field field; try { field = holderClass.getDeclaredField("locals"); } catch (NoSuchFieldException ex) { throw new NoSuchFieldError(ex.getMessage()); } try { return (Map<ClassLoaderLocal<?>, Object>) field.get(0); } catch (IllegalAccessException ex) { throw new Error(ex); } } }
From source file:com.ddiiyy.xydz.xutils.util.OtherUtils.java
/** * @param context 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//from w w w .j a v a 2 s.co m */ @SuppressLint("DefaultLocale") @SuppressWarnings("rawtypes") 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:Debug.java
public static Debug getDebugLevel(Class cls) throws NoSuchFieldException { try {/*w ww .j ava 2 s .c o m*/ Field fld = cls.getField("debug"); if (fld.getType() != Debug.class || !Modifier.isStatic(fld.getModifiers())) throw new NoSuchFieldException(); return (Debug) fld.get(null); } catch (IllegalArgumentException e) { throw new NoSuchFieldException(); } catch (IllegalAccessException e) { throw new NoSuchFieldException(); } catch (SecurityException e) { throw new NoSuchFieldException(); } }