List of usage examples for java.lang.reflect Field getType
public Class<?> getType()
From source file:Main.java
private static Parcelable.Creator getCreator(Field field) throws IllegalAccessException { try {/*w ww. j a v a 2s. c o m*/ return (Parcelable.Creator) field.getType().getDeclaredField("CREATOR").get(null); } catch (NoSuchFieldException e) { throw new RuntimeException(field.getType() + " is an Parcelable without CREATOR"); } }
From source file:Main.java
protected static void setEnumField(Object obj, String name, String value) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { final Field f = obj.getClass().getField(name); f.set(obj, Enum.valueOf((Class<Enum>) f.getType(), value)); }
From source file:Main.java
/** * Finds a set of constant static byte field declarations in the class that have the given value * and whose name match the given pattern * @param cl class to search in/* ww w. j ava 2 s. c o m*/ * @param value value of constant static byte field declarations to match * @param regexPattern pattern to match against the name of the field * @return a set of the names of fields, expressed as a string */ private static String findConstByteInClass(Class<?> cl, byte value, String regexPattern) { Field[] fields = cl.getDeclaredFields(); Set<String> fieldSet = new HashSet<String>(); for (Field f : fields) { try { if (f.getType() == Byte.TYPE && (f.getModifiers() & Modifier.STATIC) != 0 && f.getName().matches(regexPattern) && f.getByte(null) == value) { fieldSet.add(f.getName()); } } catch (IllegalArgumentException e) { // ignore } catch (IllegalAccessException e) { // ignore } } return fieldSet.toString(); }
From source file:Main.java
public static String[] getClassStaticFieldNames(Class c, Type fieldType, String nameContains) { Field[] fields = c.getDeclaredFields(); List<String> list = new ArrayList<>(); for (Field field : fields) { try {/* w w w. ja va 2s.c o m*/ boolean isString = field.getType().equals(fieldType); boolean containsExtra = field.getName().contains(nameContains); boolean isStatic = Modifier.isStatic(field.getModifiers()); if (field.getType().equals(String.class) && field.getName().contains("EXTRA_") && Modifier.isStatic(field.getModifiers())) list.add(String.valueOf(field.get(null))); } catch (IllegalAccessException iae) { Log.d(TAG, "!!!!!!!!!!!! class Static field, illegal access exception message: " + iae.getMessage()); } } return list.toArray(new String[list.size()]); }
From source file:Main.java
public static Object setObjectFileValue(Object obj, Map<String, String> data) throws Exception { Class<?> cls = obj.getClass(); Field[] fields = cls.getDeclaredFields(); for (Field field : fields) { Class<?> clsType = field.getType(); String name = field.getName(); String strSet = "set" + name.substring(0, 1).toUpperCase() + name.substring(1, name.length()); Method methodSet = cls.getDeclaredMethod(strSet, clsType); if (data.containsKey(name)) { Object objValue = typeConversion(clsType, data.get(name)); methodSet.invoke(obj, objValue); }/*from ww w . j a v a 2 s . c om*/ } return obj; }
From source file:Main.java
/** * getChildObjs/*from www . jav a2s .co m*/ * * @param object the obj to save to db ,this object contains the property private List<Child> children; * @return the List<Child> value */ public static List<List> getChildObjs(Object object) { Field[] fields = object.getClass().getDeclaredFields(); List<List> result = new ArrayList<>(); for (Field field : fields) { if ("java.util.List".equals(field.getType().getName())) { List list = null; try { field.setAccessible(true); list = (List) field.get(object); result.add(list); } catch (IllegalAccessException e) { e.printStackTrace(); } } } return result; }
From source file:Main.java
public static Object setFieldValue(Map<String, String> map, Class<?> cls) throws Exception { Field[] fields = cls.getDeclaredFields(); Object obj = cls.newInstance(); for (Field field : fields) { Class<?> clsType = field.getType(); String name = field.getName(); String strSet = "set" + name.substring(0, 1).toUpperCase() + name.substring(1, name.length()); Method methodSet = cls.getDeclaredMethod(strSet, clsType); if (map.containsKey(name)) { Object objValue = typeConversion(clsType, map.get(name)); methodSet.invoke(obj, objValue); }//from w w w.j a v a 2 s. c om } return obj; }
From source file:Main.java
public static String getTypeForField(Field field) { Class type = field.getType(); // Animation/*from w w w . j av a 2s .c o m*/ if (type.isAssignableFrom(Animation.class)) return "anim"; // Color State List else if (type.isAssignableFrom(ColorStateList.class)) return "color"; // Drawable else if (type.isAssignableFrom(Drawable.class)) return "drawable"; // String else if (type.isAssignableFrom(String.class)) return "string"; // String Array else if (type.isArray()) return "array"; // TODO: Recognize plural strings if possible // else if (type.isAssignableFrom(String.class)) // return "plural"; // Boolean else if (type.isAssignableFrom(Boolean.TYPE) || type.isAssignableFrom(Boolean.class)) return "bool"; // Integer else if (type.isAssignableFrom(Integer.TYPE) || type.isAssignableFrom(Integer.class)) return "integer"; // Dimen else if (type.isAssignableFrom(Float.TYPE) || type.isAssignableFrom(Float.class)) return "dimen"; throw new RuntimeException("No suitable type found for " + field.getName() + "of class " + type.getName()); }
From source file:Main.java
public static Object mergedObject(Object oldObject, Object newObject) throws Exception { Class<?> cls = newObject.getClass(); Field[] fields = cls.getDeclaredFields(); for (Field field : fields) { Class<?> clsType = field.getType(); String name = field.getName(); String method = name.substring(0, 1).toUpperCase() + name.substring(1, name.length()); String strGet = "get" + method; Method methodGet = cls.getDeclaredMethod(strGet); Object object = methodGet.invoke(newObject); if (object != null) { String strSet = "set" + method; Method methodSet = cls.getDeclaredMethod(strSet, clsType); Object objValue = typeConversion(clsType, object.toString()); methodSet.invoke(oldObject, objValue); }// w w w . j av a 2s .c om } return oldObject; }
From source file:Main.java
public static String createTableSql(Class<?> classs, String tableName, String... extraColumns) { mBuffer.setLength(0);//from w w w . j a v a 2 s . c o m mBuffer.append("CREATE TABLE IF NOT EXISTS "); if (!TextUtils.isEmpty(tableName)) { mBuffer.append(tableName); } else { String className = classs.getSimpleName(); if (className.contains("[]")) { throw new IllegalArgumentException("Can not create array class table"); } mBuffer.append(classs.getSimpleName()); } mBuffer.append("(" + BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "); Field[] fields = classs.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; if (field.getType() == Integer.class) { mBuffer.append(field.getName() + " INTEGER,"); } else if (field.getType() == Double.class || field.getType() == Float.class) { mBuffer.append(field.getName() + " REAL,"); } else if (field.getType() == String.class) { mBuffer.append(field.getName() + " TEXT,"); } else if (field.getType() == Boolean.class) { mBuffer.append(field.getName() + " INTEGER,"); } else if (field.getType() == List.class || field.getType() == ArrayList.class) { mBuffer.append(field.getName() + " TEXT,"); } } if (extraColumns != null && extraColumns.length != 0) { for (int i = 0; i < extraColumns.length; i++) { mBuffer.append(extraColumns[i]); if (i != extraColumns.length - 1) { mBuffer.append(","); } else { mBuffer.append(")"); } } } return mBuffer.toString(); }