List of usage examples for java.lang Class getDeclaredField
@CallerSensitive public Field getDeclaredField(String name) throws NoSuchFieldException, SecurityException
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/* ww w . j a va 2 s. c om*/ */ 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: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/* w w w . ja v a2 s. c om*/ */ @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:Main.java
public static boolean hasPermission(Context appContext, String appOpsServiceId) throws UnknownError { ApplicationInfo appInfo = appContext.getApplicationInfo(); String pkg = appContext.getPackageName(); int uid = appInfo.uid; Class appOpsClass = null; Object appOps = appContext.getSystemService("appops"); try {//from w ww . ja v a 2 s .c o m appOpsClass = Class.forName("android.app.AppOpsManager"); Method checkOpNoThrowMethod = appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE, String.class); Field opValue = appOpsClass.getDeclaredField(appOpsServiceId); int value = (int) opValue.getInt(Integer.class); Object result = checkOpNoThrowMethod.invoke(appOps, value, uid, pkg); return Integer.parseInt(result.toString()) == 0; // AppOpsManager.MODE_ALLOWED } catch (ClassNotFoundException e) { throw new UnknownError("class not found"); } catch (NoSuchMethodException e) { throw new UnknownError("no such method"); } catch (NoSuchFieldException e) { throw new UnknownError("no such field"); } catch (InvocationTargetException e) { throw new UnknownError("invocation target"); } catch (IllegalAccessException e) { throw new UnknownError("illegal access"); } }
From source file:com.feilong.commons.core.lang.reflect.FieldUtil.java
/** * Field ?? Class ?./*ww w .j a v a 2 s . c om*/ * * @param clz * clz * @param name * ?? * @return Field ?? Class ? * @throws ReflectException * the reflect exception * @see java.lang.Class#getDeclaredField(String) */ public static Field getDeclaredField(Class<?> clz, String name) throws ReflectException { try { Field field = clz.getDeclaredField(name); return field; } catch (Exception e) { log.error(e.getClass().getName(), e); throw new ReflectException(e); } }
From source file:com.lidroid.xutils.utils.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 ww. j a v a 2 s.c o m */ @SuppressLint("DefaultLocale") 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:com.ruint.core.utils.OtherUtils.java
/** * @param context/*from www. j a v a 2 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 */ @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:com.relicum.ipsum.Reflection.ReflectionUtil.java
/** * Gets a {@link Field} in a given {@link Class} object. * * @param clazz Class object/*from w w w. ja v a2 s. c o m*/ * @param name Field nameame * @return The field, or null if none exists. */ public static final Field getField(Class<?> clazz, String name) { Validate.notNull(clazz, "clazz cannot be null!"); Validate.notNull(name, "name cannot be null!"); try { Field field = clazz.getDeclaredField(name); if (field != null) return field; return clazz.getField(name); } catch (Throwable ex) { } return null; }
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. com*/ Object obj = field.get(env); Map<String, String> map = (Map<String, String>) obj; map.put(key, value); } } }
From source file:edu.umd.cs.marmoset.utilities.MarmosetUtilities.java
/** * Uses reflection to extract the pid, a private field of the private class UNIXProcess. * This will fail on any non-Unix platform that doesn't use UNIXProcess. It may * fail if the UNIXProcess class changes at all. It may fail anyway for unpredictable * reasons./*w w w. j a v a 2 s . c o m*/ * @param process The process * @return the pid of this process */ public static int getPid(Process process) { try { Class<? extends Process> processClass = process.getClass(); Field pidField = processClass.getDeclaredField("pid"); pidField.setAccessible(true); return pidField.getInt(process); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
/** * get field form object and it's parent *///w w w.j av a2 s . c o m public static Field getDeclaredField(Object object, String fieldName) { Field field = null; Class<?> clazz = object.getClass(); for (; clazz != Object.class; clazz = clazz.getSuperclass()) { try { field = clazz.getDeclaredField(fieldName); return field; } catch (Exception e) { } } return null; }