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:org.apache.axis2.util.PrettyPrinter.java

/**
 * Pretty prints contents of the java source file.
 *
 * @param file/*from   ww  w  . j  a v  a  2 s . c  o m*/
 */
public static void prettify(File file) {
    // If the user has set "axis2.jalopy=false" on the system property,
    // then just return back to caller
    String property = System.getProperty("axis2.jalopy");
    if ((property == null) || !JavaUtils.isTrueExplicitly(property)) {
        return;
    }
    PrintStream backupOutputStream = System.out;
    PrintStream backupErrorStream = System.err;
    System.setOut(new PrintStream(new ByteArrayOutputStream()));
    System.setErr(new PrintStream(new ByteArrayOutputStream()));
    try {
        Class clazzConfigurator = Loader.loadClass("org.apache.log4j.PropertyConfigurator");
        Method configure = clazzConfigurator.getMethod("configure", new Class[] { Properties.class });
        Properties properties = new Properties();
        properties.setProperty("log4j.logger.de.hunsicker.jalopy.io",
                System.getProperty("log4j.logger.de.hunsicker.jalopy.io", "FATAL"));
        configure.invoke(null, new Object[] { properties });

        // Create an instance of the Jalopy bean
        Class clazz = Loader.loadClass("de.hunsicker.jalopy.Jalopy");
        Object prettifier = clazz.newInstance();

        // Set the input file
        Method input = clazz.getMethod("setInput", new Class[] { File.class });
        input.invoke(prettifier, new Object[] { file });

        // Set the output file
        Method output = clazz.getMethod("setOutput", new Class[] { File.class });
        output.invoke(prettifier, new Object[] { file });

        Class clazz2 = Loader.loadClass("de.hunsicker.jalopy.storage.Convention");
        Method instance = clazz2.getMethod("getInstance", new Class[] {});
        Object settings = instance.invoke(null, new Object[] {});

        Class clazz3 = Loader.loadClass("de.hunsicker.jalopy.storage.ConventionKeys");
        Field field = clazz3.getField("COMMENT_FORMAT_MULTI_LINE");
        Object key = field.get(null);
        Method put = clazz2.getMethod("put", new Class[] { key.getClass(), String.class });
        put.invoke(settings, new Object[] { key, "true" });

        // format and overwrite the given input file
        Method format = clazz.getMethod("format", new Class[] {});
        format.invoke(prettifier, new Object[] {});
        log.debug("Pretty printed file : " + file);
    } catch (ClassNotFoundException e) {
        log.debug("Jalopy/Log4j not found - unable to pretty print " + file);
    } catch (Exception e) {
        log.warn("Exception occurred while trying to pretty print file " + file, e);
    } catch (Throwable t) {
        log.debug("Exception occurred while trying to pretty print file " + file, t);
    } finally {
        System.setOut(backupOutputStream);
        System.setErr(backupErrorStream);
    }
}

From source file:Main.java

public static int getStatusBarHeight(Activity context) {
    int statusHeight = 0;
    Rect frame = new Rect();
    context.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
    statusHeight = frame.top;/*w  w w .j  a  v  a2s .c o m*/
    if (0 == statusHeight) {
        Class<?> localClass;
        try {
            localClass = Class.forName("com.android.internal.R$dimen");
            Object localObject = localClass.newInstance();
            int i5 = Integer.parseInt(localClass.getField("status_bar_height").get(localObject).toString());
            statusHeight = context.getResources().getDimensionPixelSize(i5);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return statusHeight;
}

From source file:Main.java

public static int getStatusBarHeight(Context context) {
    Class<?> c = null;
    Object obj = null;/*from   www  . java  2  s .c o  m*/
    Field field = null;
    int x = 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());
        return context.getResources().getDimensionPixelSize(x);
    } catch (Exception e1) {
        e1.printStackTrace();
        return 75;
    }
}

From source file:Main.java

/**
 * @param context/*w  ww. j  a va 2  s  . c  o  m*/
 * @return
 */
public static int getStatusHeight(Context context) {
    int statusHeight = 0;
    Rect localRect = new Rect();
    ((Activity) context).getWindow().getDecorView().getWindowVisibleDisplayFrame(localRect);
    statusHeight = localRect.top;
    if (0 == statusHeight) {
        Class<?> localClass;
        try {
            localClass = Class.forName("com.android.internal.R$dimen");
            Object localObject = localClass.newInstance();
            int i5 = Integer.parseInt(localClass.getField("status_bar_height").get(localObject).toString());
            statusHeight = context.getResources().getDimensionPixelSize(i5);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return statusHeight;
}

From source file:Main.java

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

From source file:Main.java

public static int getStatusBarHeight(Context context) {
    Class<?> c = null;
    Object obj = null;/*from  w  w  w .ja  v a2s  .  co m*/
    Field field = null;
    int x = 0, sbar = 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());
        sbar = context.getResources().getDimensionPixelSize(x);
    } catch (Exception e1) {
        e1.printStackTrace();
    }
    return sbar;
}

From source file:Main.java

private static int getStatusBarHeight(Activity activity) {
    Class<?> c = null;
    Object obj = null;/*from  w  w  w  .  j  a  v a2s . c  om*/
    Field field = null;
    int x = 0, sbar = 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());
        sbar = activity.getResources().getDimensionPixelSize(x);
        return sbar;
    } catch (Exception e1) {
        //       loge("get status bar height fail");
        e1.printStackTrace();
        return 0;
    }
}

From source file:Main.java

/**
 * @param activity/*from w ww  .  j av  a2s .  c om*/
 * @return > 0 success; <= 0 fail
 */
public static int getStatusHeight(Activity activity) {
    int statusHeight = 0;
    Rect localRect = new Rect();
    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(localRect);
    statusHeight = localRect.top;
    if (0 == statusHeight) {
        Class<?> localClass;
        try {
            localClass = Class.forName("com.android.internal.R$dimen");
            Object localObject = localClass.newInstance();
            int i5 = Integer.parseInt(localClass.getField("status_bar_height").get(localObject).toString());
            statusHeight = activity.getResources().getDimensionPixelSize(i5);
        } catch (ClassNotFoundException | IllegalAccessException | InstantiationException
                | IllegalArgumentException | SecurityException | NoSuchFieldException e) {
            e.printStackTrace();
        }
    }
    return statusHeight;
}

From source file:Main.java

public static Field getField(Class<?> sourceClass, String fieldName, boolean isFindDeclaredField,
        boolean isUpwardFind) {
    Field field = null;//from   w w  w .  j  a  va  2  s .  c  om
    try {
        field = isFindDeclaredField ? sourceClass.getDeclaredField(fieldName) : sourceClass.getField(fieldName);
    } catch (NoSuchFieldException e1) {
        if (isUpwardFind) {
            Class<?> classs = sourceClass.getSuperclass();
            while (field == null && classs != null) {
                try {
                    field = isFindDeclaredField ? classs.getDeclaredField(fieldName)
                            : classs.getField(fieldName);
                } catch (NoSuchFieldException e11) {
                    classs = classs.getSuperclass();
                }
            }
        }
    }
    return field;
}

From source file:Main.java

public static int getStatusBarHeight(Context context) {
    Class<?> c = null;
    Object obj = null;//  w ww .  jav a  2s  . c om
    Field field = null;
    int x = 0, sbar = 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());
        sbar = context.getResources().getDimensionPixelSize(x);
    } catch (Exception e1) {
        Log.e("getStatusBarHight()", "get status bar height fail");
        e1.printStackTrace();
    }
    int statusBarHeight = sbar;
    Log.i("onPreDraw", "statusBarHeight: " + statusBarHeight);
    return statusBarHeight;
}