Example usage for java.lang.reflect Field getType

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

Introduction

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

Prototype

public Class<?> getType() 

Source Link

Document

Returns a Class object that identifies the declared type for the field represented by this Field object.

Usage

From source file:Main.java

public static Method getColumnGetMethod(Class<?> entityType, Field field) {
    String fieldName = field.getName();
    Method getMethod = null;/*from w w  w. jav  a 2 s  . co m*/
    if (field.getType() == boolean.class) {
        getMethod = getBooleanColumnGetMethod(entityType, fieldName);
    }
    if (getMethod == null) {
        String methodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
        try {
            getMethod = entityType.getDeclaredMethod(methodName);
        } catch (NoSuchMethodException e) {
            //Logger.d(methodName + " not exist");
        }
    }

    if (getMethod == null && !Object.class.equals(entityType.getSuperclass())) {
        return getColumnGetMethod(entityType.getSuperclass(), field);
    }
    return getMethod;
}

From source file:Main.java

public static Method getColumnGetMethod(Class<?> entityType, Field field) {
    String fieldName = field.getName();
    Method getMethod = null;/*from  w ww . j ava 2 s.  co m*/
    if (field.getType() == boolean.class) {
        getMethod = getBooleanColumnGetMethod(entityType, fieldName);
    }
    if (getMethod == null) {
        String methodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
        try {
            getMethod = entityType.getDeclaredMethod(methodName);
        } catch (NoSuchMethodException e) {
            Log.d("getColumnGetMethod", methodName + " not exist");
        }
    }

    if (getMethod == null && !Object.class.equals(entityType.getSuperclass())) {
        return getColumnGetMethod(entityType.getSuperclass(), field);
    }
    return getMethod;
}

From source file:Main.java

public static Method getColumnSetMethod(Class<?> entityType, Field field) {
    String fieldName = field.getName();
    Method setMethod = null;// w  w  w .ja  v a  2  s .  com
    if (field.getType() == boolean.class) {
        setMethod = getBooleanColumnSetMethod(entityType, field);
    }
    if (setMethod == null) {
        String methodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
        try {
            setMethod = entityType.getDeclaredMethod(methodName, field.getType());
        } catch (NoSuchMethodException e) {
            //Logger.d(methodName + " not exist");
        }
    }

    if (setMethod == null && !Object.class.equals(entityType.getSuperclass())) {
        return getColumnSetMethod(entityType.getSuperclass(), field);
    }
    return setMethod;
}

From source file:Main.java

public static Method getColumnSetMethod(Class<?> entityType, Field field) {
    String fieldName = field.getName();
    Method setMethod = null;//from  ww w .  ja v a2s. co  m
    if (field.getType() == boolean.class) {
        setMethod = getBooleanColumnSetMethod(entityType, field);
    }
    if (setMethod == null) {
        String methodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
        try {
            setMethod = entityType.getDeclaredMethod(methodName, field.getType());
        } catch (NoSuchMethodException e) {
            Log.d("getColumnSetMethod", methodName + " not exist");
        }
    }

    if (setMethod == null && !Object.class.equals(entityType.getSuperclass())) {
        return getColumnSetMethod(entityType.getSuperclass(), field);
    }
    return setMethod;
}

From source file:com.amazonaws.services.iot.client.shadow.AwsIotJsonDeserializer.java

private static void updateDeviceProperty(ObjectMapper jsonObjectMapper, JsonNode node,
        AbstractAwsIotDevice device, Field field) throws IOException {
    Object value = jsonObjectMapper.treeToValue(node, field.getType());
    invokeSetterMethod(device, field.getName(), field.getType(), value);
}

From source file:Main.java

public static Object getResource(Context context, Field field, int value) {
    Resources resources = context.getResources();
    Class type = field.getType();

    if (type.isAssignableFrom(Boolean.TYPE) || type.isAssignableFrom(Boolean.class))
        return resources.getBoolean(value);
    else if (type.isAssignableFrom(Integer.TYPE) || type.isAssignableFrom(Integer.class)) {
        return resources.getInteger(value);
    } else if (type.isAssignableFrom(ColorStateList.class))
        return resources.getColorStateList(value);
    else if (type.isAssignableFrom(XmlResourceParser.class))
        return resources.getXml(value);
    else if (type.isAssignableFrom(Float.TYPE) || type.isAssignableFrom(Float.class))
        return resources.getDimension(value);
    else if (type.isAssignableFrom(Drawable.class))
        return resources.getDrawable(value);
    else if (type.isAssignableFrom(Animation.class))
        return AnimationUtils.loadAnimation(context, value);
    else if (type.isAssignableFrom(Movie.class))
        return resources.getMovie(value);
    else if (type.isAssignableFrom(String.class))
        return resources.getString(value);
    else if (type.isArray()) {
        if (type.getName().equals("[I")) {
            return resources.getIntArray(value);
        } else if (type.isAssignableFrom(String[].class)) {
            return resources.getStringArray(value);
        }/*  w  w w .  j  ava 2  s .c  om*/
    }

    return null;
}

From source file:Main.java

public static Method getAppMethod(Class cls, Field field) throws Exception {
    char[] ca = field.getName().toCharArray();
    ca[0] = Character.toUpperCase(ca[0]);
    return cls.getMethod("set" + new String(ca), field.getType());
}

From source file:MySuperClass.java

public static ArrayList<String> getFieldsDesciption(Field[] fields) {
    ArrayList<String> fieldList = new ArrayList<>();

    for (Field f : fields) {
        int mod = f.getModifiers() & Modifier.fieldModifiers();
        String modifiers = Modifier.toString(mod);

        Class<?> type = f.getType();
        String typeName = type.getSimpleName();

        String fieldName = f.getName();

        fieldList.add(modifiers + "  " + typeName + "  " + fieldName);
    }/* w ww. j a  va2s . c  om*/

    return fieldList;
}

From source file:Main.java

private static Method getColumnSetMethod(Class<?> entityType, Field field, Class<?> typeClass, String suffix) {
    String fieldName = field.getName();
    Method setMethod = null;//from   www  .j  a v a 2  s  .  c  o  m
    if (field.getType() == boolean.class) {
        setMethod = getBooleanColumnSetMethod(entityType, field, typeClass, suffix);
    }
    if (setMethod == null) {
        String methodName = "set" + fieldName.substring(0, 1).toUpperCase(Locale.getDefault())
                + fieldName.substring(1) + suffix;
        try {
            setMethod = entityType.getDeclaredMethod(methodName, typeClass);
        } catch (NoSuchMethodException e) {
            Log.d("T", methodName + " not exist");
        }
    }
    return setMethod;
}

From source file:com.jim.im.offline.repo.MessageConverter.java

private static Object getValue(Field field, Object sourceValue) {
    if (sourceValue == null)
        return null;

    if (field.getType() == MsgType.class) {
        return MsgType.valueOf(sourceValue.toString());
    } else if (field.getType() == TopicOwnerType.class) {
        return TopicOwnerType.valueOf(sourceValue.toString());
    } else if (field.getType() == BigInteger.class) {
        return new BigInteger(sourceValue.toString(), 16);
    }//w ww. j a v a 2s . c  o  m
    return sourceValue;
}