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:jdroidremote.ServerFrame.java

public static void typeCharacter(String letter) {
    System.out.println(letter);//from www . j  a v a 2 s  .c o  m
    Robot robot = null;
    try {
        robot = new Robot();
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    if (Character.isLetter(letter.charAt(0))) {
        try {
            boolean upperCase = Character.isUpperCase(letter.charAt(0));
            String variableName = "VK_" + letter.toUpperCase();

            KeyEvent ke = new KeyEvent(new JTextField(), 0, 0, 0, 0, ' ');
            Class clazz = ke.getClass();
            Field field = clazz.getField(variableName);
            int keyCode = field.getInt(ke);

            //System.out.println(keyCode + " = keyCode");
            robot.delay(80);

            if (upperCase) {
                robot.keyPress(KeyEvent.VK_SHIFT);
            }

            robot.keyPress(keyCode);
            robot.keyRelease(keyCode);

            if (upperCase) {
                robot.keyRelease(KeyEvent.VK_SHIFT);
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    } else if (letter.equals(".")) {
        robot.keyPress(KeyEvent.VK_PERIOD); //keyCode 46
        robot.keyRelease(KeyEvent.VK_PERIOD);
    } else if (letter.equals("!")) {
        robot.keyPress(KeyEvent.VK_SHIFT); //keyCode 16
        robot.keyPress(KeyEvent.VK_1); //keycode 49
        robot.keyRelease(KeyEvent.VK_1);
        robot.keyRelease(KeyEvent.VK_SHIFT);
    } else if (letter.equals(" ")) {
        robot.keyPress(KeyEvent.VK_SPACE);
        robot.keyRelease(KeyEvent.VK_SPACE);
    } else if (letter.equals("?")) {
        robot.keyPress(KeyEvent.VK_SHIFT); //keyCode 16
        robot.keyPress(KeyEvent.VK_SLASH); //keyCode 47
        robot.keyRelease(KeyEvent.VK_SLASH);
        robot.keyRelease(KeyEvent.VK_SHIFT);
    } else if (letter.equals(",")) {
        robot.keyPress(KeyEvent.VK_COMMA);
        robot.keyRelease(KeyEvent.VK_COMMA);
    } else if (letter.equals("@enter")) {
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);
    } else if (letter.equals("@backspace")) {
        robot.keyPress(KeyEvent.VK_BACK_SPACE);
        robot.keyRelease(KeyEvent.VK_BACK_SPACE);
    }
}

From source file:com.sunchenbin.store.feilong.core.lang.reflect.FieldUtil.java

/**
 * .// w  w w  .j ava2s . c  om
 *
 * @param owner
 *            the owner
 * @param fieldName
 *            
 * @param value
 *            
 * @see java.lang.Object#getClass()
 * @see java.lang.Class#getField(String)
 * @see java.lang.reflect.Field#set(Object, Object)
 * 
 * @see org.apache.commons.lang3.reflect.FieldUtils#writeField(Field, Object, Object, boolean)
 * @since 1.4.0
 */
public static void setFieldValue(Object owner, String fieldName, Object value) {
    try {
        Class<?> ownerClass = owner.getClass();
        Field field = ownerClass.getField(fieldName);
        field.set(ownerClass, value);
    } catch (Exception e) {
        LOGGER.error(e.getClass().getName(), e);
        throw new ReflectException(e);
    }
}

From source file:com.sunchenbin.store.feilong.core.lang.reflect.FieldUtil.java

/**
 * ?.//from  w w  w .  ja v a  2s . c  o  m
 *
 * @param <T>
 *            the generic type
 * @param owner
 *            the owner
 * @param fieldName
 *            the field name
 * @return 
 * @see java.lang.Object#getClass()
 * @see java.lang.Class#getField(String)
 * @see java.lang.reflect.Field#get(Object)
 * @since 1.4.0
 */
@SuppressWarnings("unchecked")
public static <T> T getFieldValue(Object owner, String fieldName) {
    try {
        Class<?> ownerClass = owner.getClass();
        Field field = ownerClass.getField(fieldName);
        return (T) field.get(owner);
    } catch (Exception e) {
        String formatMessage = Slf4jUtil.formatMessage("owner:[{}],fieldName:[{}]", owner, fieldName);
        LOGGER.error(formatMessage + e.getClass().getName(), e);
        throw new ReflectException(formatMessage, e);
    }
}

From source file:com.aw.support.reflection.MethodInvoker.java

public static Field getAttribute(Object target, String fielName) {
    Class cls = target.getClass();
    Field field = null;//ww  w .j  a  v  a  2 s . c om
    try {
        field = cls.getField(fielName);
    } catch (NoSuchFieldException e) {
        field = null;
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }
    return field;
}

From source file:com.bobomee.android.common.util.ScreenUtil.java

/**
 * get the height of status */*from   ww  w.  j a va 2  s .  c  om*/
 */
public static int getStatusH(Context ctx) {
    int statusHeight = -1;
    try {
        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 = ctx.getResources().getDimensionPixelSize(height);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return statusHeight;
}

From source file:com.sunchenbin.store.feilong.core.lang.reflect.FieldUtil.java

/**
 * ???.//ww w .j ava  2s.  c o  m
 * 
 * <pre>
 * {@code
 * example1 :
 * 
 * FieldUtil.getStaticProperty("com.sunchenbin.store.feilong.core.io.ImageType", "JPG")
 *  :jpg
 * }
 * </pre>
 *
 * @param <T>
 *            the generic type
 * @param className
 *            ??,e.g com.sunchenbin.store.feilong.core.io.ImageType
 * @param fieldName
 *            ??
 * @return 
 * @see ClassUtil#loadClass(String)
 * @see java.lang.Class#getField(String)
 * @see java.lang.reflect.Field#get(Object)
 * 
 * @see org.apache.commons.lang3.reflect.FieldUtils#getField(Class, String)
 * @since 1.4.0
 */
@SuppressWarnings("unchecked")
public static <T> T getStaticFieldValue(String className, String fieldName) {
    try {
        Class<?> ownerClass = ClassUtil.loadClass(className);
        Field field = ownerClass.getField(fieldName);
        return (T) field.get(ownerClass);
    } catch (Exception e) {
        String formatMessage = Slf4jUtil.formatMessage("className:[{}],fieldName:[{}]", className, fieldName);
        LOGGER.error(formatMessage + e.getClass().getName(), e);
        throw new ReflectException(formatMessage, e);
    }
}

From source file:org.apache.cordova.X5CoreAndroid.java

public static Object getBuildConfigValue(Context ctx, String key) {
    try {//from  w w  w  .  j a  v  a2 s  .  c  o  m
        Class<?> clazz = Class.forName(ctx.getPackageName() + ".BuildConfig");
        Field field = clazz.getField(key);
        return field.get(null);
    } catch (ClassNotFoundException e) {
        LOG.d(TAG, "Unable to get the BuildConfig, is this built with ANT?");
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        LOG.d(TAG, key + " is not a valid field. Check your build.gradle");
    } catch (IllegalAccessException e) {
        LOG.d(TAG, "Illegal Access Exception: Let's print a stack trace.");
        e.printStackTrace();
    }

    return null;
}

From source file:run.ace.Utils.java

public static Object getField(Class c, Object instance, String fieldName) {
    try {//from   w w  w  .  j  a  v a2s  .  c om
        Field f = c.getField(fieldName);
        return f.get(instance);
    } catch (NoSuchFieldException ex) {
        throw new RuntimeException(c.getSimpleName() + " does not have a " + fieldName + " field");
    } catch (IllegalAccessException ex) {
        throw new RuntimeException(c.getSimpleName() + "'s " + fieldName + " field is inaccessible");
    }
}

From source file:org.apache.abdera.util.AbderaConfiguration.java

private static String getName(Class<? extends StreamWriter> sw) {
    String name = null;//from ww w.  j a  va 2 s.c  o m
    try {
        Field field = sw.getField("NAME");
        if (Modifier.isStatic(field.getModifiers())) {
            name = (String) field.get(null);
        }
    } catch (Exception e) {
    }
    return name;
}

From source file:com.austin.base.commons.util.ReflectUtil.java

/**
 * @param className ??//  www.j a  va  2s.  c o  m
 * @param fieldName ?17
 * @return 171717
 */
public static Object getStaticProperty(String className, String fieldName)

{
    Object property = null;
    try {
        Class ownerClass = Class.forName(className);
        Field field = ownerClass.getField(fieldName);
        property = field.get(ownerClass);
    } catch (Exception ex) {
        log.error(ex);
    }
    return property;
}