Example usage for java.lang.reflect Field getChar

List of usage examples for java.lang.reflect Field getChar

Introduction

In this page you can find the example usage for java.lang.reflect Field getChar.

Prototype

@CallerSensitive
@ForceInline 
public char getChar(Object obj) throws IllegalArgumentException, IllegalAccessException 

Source Link

Document

Gets the value of a static or instance field of type char or of another primitive type convertible to type char via a widening conversion.

Usage

From source file:MyClass.java

public static void main(String[] args) throws Exception {
    Class<?> clazz = Class.forName("MyClass");
    MyClass x = (MyClass) clazz.newInstance();

    Field f = clazz.getField("i");
    System.out.println(f.getChar(x));

    f.setChar(x, 'a');
    System.out.println(f.getBoolean(x));

}

From source file:org.apache.openjpa.enhance.Reflection.java

/**
 * Return the value of the given field in the given object.
 *///www.j a  v a 2s  .co  m
public static char getChar(Object target, Field field) {
    if (target == null || field == null)
        return (char) 0;
    makeAccessible(field, field.getModifiers());
    try {
        return field.getChar(target);
    } catch (Throwable t) {
        throw wrapReflectionException(t, _loc.get("get-field", target, field));
    }
}

From source file:org.acoveo.tools.Reflection.java

/**
 * Return the value of the given field in the given object.
 */// w  ww  . jav a 2 s . c  o m
public static char getChar(Object target, Field field) {
    if (target == null || field == null)
        return (char) 0;
    makeAccessible(field, field.getModifiers());
    try {
        return field.getChar(target);
    } catch (Throwable t) {
        throw wrapReflectionException(t);
    }
}

From source file:net.larry1123.elec.util.test.config.AbstractConfigTest.java

public void characterTest(String fieldName, Field testField) {
    try {/*  w w w  .j av  a2  s .  c  o  m*/
        Assert.assertTrue(getPropertiesFile().getCharacter(fieldName) == testField.getChar(getConfigBase()));
    } catch (IllegalAccessException e) {
        assertFailFieldError(fieldName);
    }
}

From source file:org.evosuite.regression.ObjectFields.java

private static Object getFieldValue(Field field, Object p) {
    try {/*from  w w  w  .ja v a2s  . c  o m*/
        /*Class objClass = p.getClass();
        if(p instanceof java.lang.String){
           ((String) p).hashCode();
        }*/
        Class<?> fieldType = field.getType();
        field.setAccessible(true);
        if (fieldType.isPrimitive()) {
            if (fieldType.equals(Boolean.TYPE)) {
                return field.getBoolean(p);
            }
            if (fieldType.equals(Integer.TYPE)) {
                return field.getInt(p);
            }
            if (fieldType.equals(Byte.TYPE)) {
                return field.getByte(p);
            }
            if (fieldType.equals(Short.TYPE)) {
                return field.getShort(p);
            }
            if (fieldType.equals(Long.TYPE)) {
                return field.getLong(p);
            }
            if (fieldType.equals(Double.TYPE)) {
                return field.getDouble(p);
            }
            if (fieldType.equals(Float.TYPE)) {
                return field.getFloat(p);
            }
            if (fieldType.equals(Character.TYPE)) {
                return field.getChar(p);
            }
            throw new UnsupportedOperationException("Primitive type " + fieldType + " not implemented!");
        }
        return field.get(p);
    } catch (IllegalAccessException exc) {
        throw new RuntimeException(exc);
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        if (MAX_RECURSION != 0)
            MAX_RECURSION = 0;
        else
            throw new RuntimeErrorException(e);
        return getFieldValue(field, p);
    }
}

From source file:com.googlecode.android_scripting.facade.AndroidFacade.java

@Rpc(description = "Get list of constants (static final fields) for a class")
public Bundle getConstants(
        @RpcParameter(name = "classname", description = "Class to get constants from") String classname)
        throws Exception {
    Bundle result = new Bundle();
    int flags = Modifier.FINAL | Modifier.PUBLIC | Modifier.STATIC;
    Class<?> clazz = Class.forName(classname);
    for (Field field : clazz.getFields()) {
        if ((field.getModifiers() & flags) == flags) {
            Class<?> type = field.getType();
            String name = field.getName();
            if (type == int.class) {
                result.putInt(name, field.getInt(null));
            } else if (type == long.class) {
                result.putLong(name, field.getLong(null));
            } else if (type == double.class) {
                result.putDouble(name, field.getDouble(null));
            } else if (type == char.class) {
                result.putChar(name, field.getChar(null));
            } else if (type instanceof Object) {
                result.putString(name, field.get(null).toString());
            }/*from  www .  j  av  a 2 s  .  c o  m*/
        }
    }
    return result;
}

From source file:org.rhq.enterprise.gui.legacy.taglib.ConstantsTag.java

public int doEndTag() throws JspException {
    try {/*from w  w w.j  a v  a2 s .  c om*/
        JspWriter out = pageContext.getOut();
        if (className == null) {
            className = pageContext.getServletContext().getInitParameter(constantsClassNameParam);
        }

        if (validate(out)) {
            // we're misconfigured. getting this far
            // is a matter of what our failure mode is;
            // if we haven't thrown an Error, carry on
            log.debug("constants tag misconfigured");
            return EVAL_PAGE;
        }

        Map<String, String> fieldMap;
        if (constants.containsKey(className)) {
            // we cache the result of the constant's class
            // reflection field walk as a map
            fieldMap = (Map<String, String>) constants.get(className);
        } else {
            fieldMap = new HashMap<String, String>();
            Class typeClass = Class.forName(className);
            if (typeClass.isEnum()) {
                for (Object enumConstantObj : typeClass.getEnumConstants()) {
                    Enum enumConstant = (Enum) enumConstantObj;

                    // Set name *and* value to enum name (e.g. name of ResourceCategory.PLATFORM = "PLATFORM")
                    // NOTE: We do not set the value to enumConstant.ordinal(), because there is no way to
                    // convert the ordinal value back to an Enum (i.e. no Enum.valueOf(int ordinal) method).
                    fieldMap.put(enumConstant.name(), enumConstant.name());
                }
            } else {
                Object instance = typeClass.newInstance();
                Field[] fields = typeClass.getFields();
                for (Field field : fields) {
                    // string comparisons of class names should be cheaper
                    // than reflective Class comparisons, the asumption here
                    // is that most constants are Strings, ints and booleans
                    // but a minimal effort is made to accomadate all types
                    // and represent them as String's for our tag's output
                    String fieldType = field.getType().getName();
                    String strVal;
                    if (fieldType.equals("java.lang.String")) {
                        strVal = (String) field.get(instance);
                    } else if (fieldType.equals("int")) {
                        strVal = Integer.toString(field.getInt(instance));
                    } else if (fieldType.equals("boolean")) {
                        strVal = Boolean.toString(field.getBoolean(instance));
                    } else if (fieldType.equals("char")) {
                        strVal = Character.toString(field.getChar(instance));
                    } else if (fieldType.equals("double")) {
                        strVal = Double.toString(field.getDouble(instance));
                    } else if (fieldType.equals("float")) {
                        strVal = Float.toString(field.getFloat(instance));
                    } else if (fieldType.equals("long")) {
                        strVal = Long.toString(field.getLong(instance));
                    } else if (fieldType.equals("short")) {
                        strVal = Short.toString(field.getShort(instance));
                    } else if (fieldType.equals("byte")) {
                        strVal = Byte.toString(field.getByte(instance));
                    } else {
                        strVal = field.get(instance).toString();
                    }

                    fieldMap.put(field.getName(), strVal);
                }
            }

            // cache the result
            constants.put(className, fieldMap);
        }

        if ((symbol != null) && !fieldMap.containsKey(symbol)) {
            // tell the developer that he's being a dummy and what
            // might be done to remedy the situation
            // TODO: what happens if the constants change?
            // do we need to throw a JspException, here? - mtk
            String err1 = symbol + " was not found in " + className + "\n";
            String err2 = err1 + "use <constants:diag classname=\"" + className + "\"/>\n"
                    + "to figure out what you're looking for";
            log.error(err2);
            die(out, err1);
        }

        if (varSpecified) {
            doSet(fieldMap);
        } else {
            doOutput(fieldMap, out);
        }
    } catch (JspException e) {
        throw e;
    } catch (Exception e) {
        log.debug("doEndTag() failed: ", e);
        throw new JspException("Could not access constants tag", e);
    }

    return EVAL_PAGE;
}

From source file:cn.edu.zafu.corepage.base.BaseActivity.java

/**
 * ??/*from  w  w w. jav  a 2s. c o  m*/
 *
 * @param outState Bundle
 */
@Override
protected void onSaveInstanceState(Bundle outState) {
    Field[] fields = this.getClass().getDeclaredFields();
    Field.setAccessible(fields, true);
    Annotation[] ans;
    for (Field f : fields) {
        ans = f.getDeclaredAnnotations();
        for (Annotation an : ans) {
            if (an instanceof SaveWithActivity) {
                try {
                    Object o = f.get(this);
                    if (o == null) {
                        continue;
                    }
                    String fieldName = f.getName();
                    if (o instanceof Integer) {
                        outState.putInt(fieldName, f.getInt(this));
                    } else if (o instanceof String) {
                        outState.putString(fieldName, (String) f.get(this));
                    } else if (o instanceof Long) {
                        outState.putLong(fieldName, f.getLong(this));
                    } else if (o instanceof Short) {
                        outState.putShort(fieldName, f.getShort(this));
                    } else if (o instanceof Boolean) {
                        outState.putBoolean(fieldName, f.getBoolean(this));
                    } else if (o instanceof Byte) {
                        outState.putByte(fieldName, f.getByte(this));
                    } else if (o instanceof Character) {
                        outState.putChar(fieldName, f.getChar(this));
                    } else if (o instanceof CharSequence) {
                        outState.putCharSequence(fieldName, (CharSequence) f.get(this));
                    } else if (o instanceof Float) {
                        outState.putFloat(fieldName, f.getFloat(this));
                    } else if (o instanceof Double) {
                        outState.putDouble(fieldName, f.getDouble(this));
                    } else if (o instanceof String[]) {
                        outState.putStringArray(fieldName, (String[]) f.get(this));
                    } else if (o instanceof Parcelable) {
                        outState.putParcelable(fieldName, (Parcelable) f.get(this));
                    } else if (o instanceof Serializable) {
                        outState.putSerializable(fieldName, (Serializable) f.get(this));
                    } else if (o instanceof Bundle) {
                        outState.putBundle(fieldName, (Bundle) f.get(this));
                    }
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    super.onSaveInstanceState(outState);
}