Example usage for java.lang Class getField

List of usage examples for java.lang Class getField

Introduction

In this page you can find the example usage for java.lang Class getField.

Prototype

@CallerSensitive
public Field getField(String name) throws NoSuchFieldException, SecurityException 

Source Link

Document

Returns a Field object that reflects the specified public member field of the class or interface represented by this Class object.

Usage

From source file:Main.java

public static int getStatusHeight(Context context) {

    int statusHeight = -1;
    try {/*  ww w . j a va 2  s  . c o m*/
        Class<?> clazz = Class.forName("com.android.internal.R$dimen");
        Object object = clazz.newInstance();
        int height = Integer.parseInt(clazz.getField("status_bar_height").get(object).toString());
        statusHeight = context.getResources().getDimensionPixelSize(height);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return statusHeight;
}

From source file:Main.java

public static Field getField(Class<?> targetClass, String name) {
    if (targetClass == null || TextUtils.isEmpty(name))
        return null;
    try {//from   w  w  w  . j  a v a2  s .  c o m
        return targetClass.getField(name);
    } catch (SecurityException e) {
        // ignore
    } catch (NoSuchFieldException e) {
        // ignore
    }
    return null;
}

From source file:Main.java

public static Field getField(Class<?> targetClass, String name) {
    if (targetClass == null || TextUtils.isEmpty(name))
        return null;
    try {/*  w w w.  j a v  a  2 s . c  o m*/
        return targetClass.getField(name);
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Object getStaticField(Class clz, String fieldName, int type) {
    if (null != clz) {
        try {//  w  ww. j  ava2 s.  c o m
            Field field = clz.getField(fieldName);
            switch (type) {
            case TYPE_OBJECT:
                return field.get(clz);
            case TYPE_INT:
                return field.getInt(clz);
            case TYPE_SHORT:
                return field.getShort(clz);
            case TYPE_BYTE:
                return field.getByte(clz);
            case TYPE_BOOLEAN:
                return field.getBoolean(clz);
            case TYPE_FLOAT:
                return field.getFloat(clz);
            case TYPE_LONG:
                return field.getLong(clz);
            case TYPE_DOUBLE:
                return field.getDouble(clz);
            default:
                return field.get(clz);
            }
        } catch (Exception e) {
        }
        return (clz == Object.class ? getDefault(type) : getStaticField(clz.getSuperclass(), fieldName, type));
    }
    return getDefault(type);
}

From source file:Main.java

public static int getStatusBarHeight(Context context) {
    try {/*from  w ww  .  ja  va  2 s.c o m*/
        @SuppressWarnings("rawtypes")
        Class clazz = Class.forName("com.android.internal.R$dimen");
        Object object = clazz.newInstance();
        Field field = clazz.getField("status_bar_height");

        int id = Integer.parseInt(field.get(object).toString());
        return context.getResources().getDimensionPixelSize(id);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return 0;
}

From source file:Main.java

public static Field getField(final Class<?> targetClass, final String name) {
    if (targetClass == null || TextUtils.isEmpty(name))
        return null;
    try {/*from www .  ja  v a 2  s.  c o  m*/
        return targetClass.getField(name);
    } catch (SecurityException e) {
        // ignore
    } catch (NoSuchFieldException e) {
        // ignore
    }
    return null;
}

From source file:Main.java

public static Field getField(final Class<?> targetClass, final String name) {
    if (targetClass == null || TextUtils.isEmpty(name)) {
        return null;
    }//from   w w  w.  jav a 2 s .c o  m
    try {
        return targetClass.getField(name);
    } catch (final SecurityException | NoSuchFieldException e) {
        // ignore
    }
    return null;
}

From source file:Main.java

@SuppressLint("NewApi")
@SuppressWarnings("all")
private static boolean setProxyKK(WebView webView, String host, int port, String applicationClassName) {
    Context appContext = webView.getContext().getApplicationContext();
    if (null == host) {
        System.clearProperty("http.proxyHost");
        System.clearProperty("http.proxyPort");
        System.clearProperty("https.proxyHost");
        System.clearProperty("https.proxyPort");
    } else {// ww w.j av  a2 s. com
        System.setProperty("http.proxyHost", host);
        System.setProperty("http.proxyPort", port + "");
        System.setProperty("https.proxyHost", host);
        System.setProperty("https.proxyPort", port + "");
    }
    try {
        Class applictionCls = Class.forName(applicationClassName);
        Field loadedApkField = applictionCls.getField("mLoadedApk");
        loadedApkField.setAccessible(true);
        Object loadedApk = loadedApkField.get(appContext);
        Class loadedApkCls = Class.forName("android.app.LoadedApk");
        Field receiversField = loadedApkCls.getDeclaredField("mReceivers");
        receiversField.setAccessible(true);
        Map receivers = (Map) receiversField.get(loadedApk);
        for (Object receiverMap : receivers.values()) {
            for (Object rec : ((Map) receiverMap).keySet()) {
                Class clazz = rec.getClass();
                if (clazz.getName().contains("ProxyChangeListener")) {
                    Method onReceiveMethod = clazz.getDeclaredMethod("onReceive", Context.class, Intent.class);
                    Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);

                    /*********** optional, may be need in future *************/
                    final String CLASS_NAME = "android.net.ProxyProperties";
                    Class cls = Class.forName(CLASS_NAME);
                    Constructor constructor = cls.getConstructor(String.class, Integer.TYPE, String.class);
                    constructor.setAccessible(true);
                    Object proxyProperties = constructor.newInstance(host, port, null);
                    intent.putExtra("proxy", (Parcelable) proxyProperties);
                    /*********** optional, may be need in future *************/

                    onReceiveMethod.invoke(rec, appContext, intent);
                }
            }
        }
        Log.d(LOG_TAG, "Setting proxy with >= 4.4 API successful!");
        return true;
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (InstantiationException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:Main.java

public static String getChannelNum(Context context) {
    Resources resources = context.getResources();
    try {/*  w ww. j a va2s .c om*/
        Class<?> className = Class.forName(
                (new StringBuilder(String.valueOf(context.getPackageName()))).append(".R$raw").toString());
        Field field = className.getField("parent");
        Integer result = (Integer) field.get(className);
        InputStream num = resources.openRawResource(result);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int i = 1;
        try {
            while ((i = num.read()) != -1) {
                baos.write(i);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            resources = null;
            if (num != null) {
                try {
                    num.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return new String(baos.toByteArray());
    } catch (Exception e) {
    }
    return "0";
}

From source file:Main.java

public static int getStatusBarHeight(Context context) {
    Class<?> c;
    Object obj;//from w  ww.j  a va  2s .c om
    Field field;
    int x, statusBarHeight = 0;
    try {
        c = Class.forName("com.android.internal.R$dimen");
        obj = c.newInstance();
        field = c.getField("status_bar_height");
        x = Integer.parseInt(field.get(obj).toString());
        statusBarHeight = context.getResources().getDimensionPixelSize(x);
    } catch (Exception e1) {
        e1.printStackTrace();
    }
    return statusBarHeight;
}