List of usage examples for java.lang Class getDeclaredField
@CallerSensitive public Field getDeclaredField(String name) throws NoSuchFieldException, SecurityException
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;//from w ww. jav a 2 s .co m Object iConMgr = null; 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.shrj.util.ReflectionUtils.java
public static Class getClassGenricType(final Class clazz, final int index) { try {/*from w ww. j ava 2 s.c o m*/ Field f = clazz.getDeclaredField("typeList"); Type t = f.getGenericType(); if (t instanceof ParameterizedType) { Type[] params = ((ParameterizedType) t).getActualTypeArguments(); if (index >= params.length || index < 0) { return Object.class; } // System.out.println(params[index]); return null; } } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchFieldException e) { // TODO Auto-generated catch block e.printStackTrace(); } return Object.class; }
From source file:com.jb.statistics.common.utils.ReflectionUtils.java
public static Class getClassGenricType(final Class clazz, final int index) { try {/*from ww w . j av a 2 s .co m*/ Field f = clazz.getDeclaredField("typeList"); Type t = f.getGenericType(); if (t instanceof ParameterizedType) { Type[] params = ((ParameterizedType) t).getActualTypeArguments(); if (index >= params.length || index < 0) { return Object.class; } // System.out.println(params[index]); return null; } } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchFieldException e) { // TODO Auto-generated catch block e.printStackTrace(); } return Object.class; }
From source file:com.qualogy.qafe.business.integration.adapter.MappingAdapter.java
private static Object adaptToJavaObject(String mappingId, String attributeName, Object result, Object valueToAdapt, ClassLoader classLoader) { try {/*from w w w . j a v a2 s. co m*/ if (valueToAdapt != null) { Class resultClazz = classLoader.loadClass(result.getClass().getName()); Field field = resultClazz.getDeclaredField(attributeName); field.setAccessible(true); String fieldName = field.getType().getName(); String valueName = valueToAdapt.getClass().getName(); logger.info(field.getType().getName()); if (!fieldName.equals(valueName)) { if (PredefinedClassTypeConverter.isPredefined(field.getType())) valueToAdapt = PredefinedAdapterFactory.create(field.getType()).convert(valueToAdapt); else throw new IllegalArgumentException("Object passed [" + valueToAdapt + "] cannot be converted to type wanted[" + field.getType() + "] for field[" + field.getName() + "] in adapter[" + mappingId + "]"); } else { if (!PredefinedClassTypeConverter.isPredefined(field.getType())) { Class paramClazz = classLoader.loadClass(fieldName); valueToAdapt = paramClazz.cast(valueToAdapt); } } field.set(result, valueToAdapt); } } catch (IllegalArgumentException e) { throw new UnableToAdaptException( "arg [" + valueToAdapt + "] is illegal for field with name [" + attributeName + "]", e); } catch (SecurityException e) { throw new UnableToAdaptException(e); } catch (IllegalAccessException e) { throw new UnableToAdaptException( "field [" + attributeName + "] does not accessible on class [" + result.getClass() + "]", e); } catch (NoSuchFieldException e) { logger.log(Level.WARNING, "field [" + attributeName + "] does not exist on class [" + result.getClass() + "]", e); } catch (ClassNotFoundException e) { throw new UnableToAdaptException( "field [" + attributeName + "] class not found [" + result.getClass() + "]", e); } return result; }
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 ww w .j a va 2 s . c om 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:Main.java
public static String getResourceString(String recourceName, Activity context) { Class rStringClass = null; try {// www. j a v a2 s.c o m if (rStringClass == null) { rStringClass = Class.forName( new StringBuilder().append(context.getPackageName()).append(".R$string").toString()); } return context.getResources() .getString(((Integer) rStringClass.getDeclaredField(recourceName).get(null)).intValue()); } catch (Exception e) { Log.e(context.getPackageName(), e.getMessage(), e); context.finish(); } return ""; }
From source file:com.sixt.service.framework.servicetest.helper.DockerComposeHelper.java
@SuppressWarnings("unchecked") private static void setEnv(Map<String, String> newEnv) { try {//ww w. j a va 2 s. c o m Class<?> processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment"); Field theEnvironmentField = processEnvironmentClass.getDeclaredField("theEnvironment"); theEnvironmentField.setAccessible(true); Map<String, String> env = (Map<String, String>) theEnvironmentField.get(null); env.putAll(newEnv); Field theCaseInsensitiveEnvironmentField = processEnvironmentClass .getDeclaredField("theCaseInsensitiveEnvironment"); theCaseInsensitiveEnvironmentField.setAccessible(true); Map<String, String> cienv = (Map<String, String>) theCaseInsensitiveEnvironmentField.get(null); cienv.putAll(newEnv); } catch (NoSuchFieldException e) { try { 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); Object obj = field.get(env); Map<String, String> map = (Map<String, String>) obj; map.clear(); map.putAll(newEnv); } } } catch (Exception e2) { e2.printStackTrace(); } } catch (Exception e1) { e1.printStackTrace(); } }
From source file:shiver.me.timbers.spring.security.WrappedUsernamePasswordAuthenticationFilterTest.java
private static Object extractFiledValue(Class type, Object object, String fieldName) throws IllegalAccessException { if (Object.class.equals(type)) { return null; }/*from w w w .ja va2 s . co m*/ try { final Field field = type.getDeclaredField(fieldName); field.setAccessible(true); return field.get(object); } catch (NoSuchFieldException e) { return extractFiledValue(type.getSuperclass(), object, fieldName); } }
From source file:com.cloudera.recordservice.tests.ClusterController.java
/** * This method allows the caller to add environment variables to the JVM. * There is no easy way to do this through a simple call, such as there is to * read env variables using System.getEnv(variableName). Much of the method * was written with guidance from stack overflow: * http://stackoverflow.com/questions/*from ww w. j a v a2s. c o m*/ * /318239/how-do-i-set-environment-variables-from-java */ protected static void setEnv(Map<String, String> newenv) { try { Class<?> processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment"); Field theEnvironmentField = processEnvironmentClass.getDeclaredField("theEnvironment"); theEnvironmentField.setAccessible(true); Map<String, String> env = (Map<String, String>) theEnvironmentField.get(null); env.putAll(newenv); Field theCaseInsensitiveEnvironmentField = processEnvironmentClass .getDeclaredField("theCaseInsensitiveEnvironment"); theCaseInsensitiveEnvironmentField.setAccessible(true); Map<String, String> cienv = (Map<String, String>) theCaseInsensitiveEnvironmentField.get(null); cienv.putAll(newenv); } catch (NoSuchFieldException e) { try { 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); Object obj = field.get(env); Map<String, String> map = (Map<String, String>) obj; map.clear(); map.putAll(newenv); } } } catch (Exception e2) { e2.printStackTrace(); } } catch (Exception e1) { e1.printStackTrace(); } }
From source file:com.elasticbox.jenkins.k8s.util.TestUtils.java
public static void setEnv(Map<String, String> newenv) { try {//w w w .j ava 2 s. c om Class<?> processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment"); Field theEnvironmentField = processEnvironmentClass.getDeclaredField("theEnvironment"); theEnvironmentField.setAccessible(true); Map<String, String> env = (Map<String, String>) theEnvironmentField.get(null); env.putAll(newenv); Field theCaseInsensitiveEnvironmentField = processEnvironmentClass .getDeclaredField("theCaseInsensitiveEnvironment"); theCaseInsensitiveEnvironmentField.setAccessible(true); Map<String, String> cienv = (Map<String, String>) theCaseInsensitiveEnvironmentField.get(null); cienv.putAll(newenv); } catch (NoSuchFieldException e) { try { 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); Object obj = field.get(env); Map<String, String> map = (Map<String, String>) obj; map.putAll(newenv); } } } catch (Exception e2) { e2.printStackTrace(); } } catch (Exception e1) { e1.printStackTrace(); } }