List of usage examples for java.lang.reflect Field getName
public String getName()
From source file:Main.java
public static String getFields(Class clazz, Object object) { try {// w w w. ja v a 2s. co m StringBuilder builder = new StringBuilder(); Field[] fs = clazz.getDeclaredFields(); for (Field f : fs) { f.setAccessible(true); builder.append(f.getName() + "=" + f.get(object) + "\r\n"); } return builder.toString(); } catch (Exception e) { return null; } }
From source file:Main.java
private static Method getColumnGetMethod(Class<?> entityType, Field field, String suffix) { String fieldName = field.getName(); Method getMethod = null;/*from w w w . j ava2 s . c o m*/ if (field.getType() == boolean.class) { getMethod = getBooleanColumnGetMethod(entityType, fieldName, suffix); } if (getMethod == null) { String methodName = "get" + fieldName.substring(0, 1).toUpperCase(Locale.getDefault()) + fieldName.substring(1) + suffix; try { getMethod = entityType.getDeclaredMethod(methodName); } catch (NoSuchMethodException e) { Log.d("T", methodName + " not exist"); } } return getMethod; }
From source file:Main.java
public static Method getColumnGetMethod(Class<?> entityType, Field field) { String fieldName = field.getName(); Method getMethod = null;/*from ww w .java2s .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
/** * Returns the field with the given name on the type lowest in the class hierarchy. * //from w ww . j a va 2s . co m * @param name The name of the field to get * @param type The class on which to search for the field. * @return The field with this name lowest in the hierarchy, otherwise null if the field does not exist in the class hierarchy. */ public static Field getField(String name, Class<?> type) { for (Field field : type.getDeclaredFields()) { if (field.getName().equals(name)) { return field; } } if (type.getSuperclass() != null) { return getField(name, type.getSuperclass()); } return null; }
From source file:Main.java
public static Method getColumnGetMethod(Class<?> entityType, Field field) { String fieldName = field.getName(); Method getMethod = null;/* ww w.j ava 2s .c o 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;/*from w ww .j av a2s .c o 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) { //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.j av a 2 s . c o 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:Main.java
/** * @param type/*w ww . j ava 2 s .c om*/ * @param fieldName * @return */ private static Field getField(Class<?> type, String fieldName) { Class<?> currentType = type; while (!currentType.equals(Object.class)) { for (Field field : currentType.getDeclaredFields()) { if (field.getName().equals(fieldName)) { return field; } } currentType = currentType.getSuperclass(); } return null; }
From source file:Main.java
public static ContentValues objectToContentValues(Object object) { if (object == null) { throw new IllegalArgumentException("please check your argument"); }// www . ja v a 2 s . co m ContentValues contentValues = new ContentValues(); try { Field[] fields = object.getClass().getDeclaredFields(); for (Field field : fields) { String fieldName = field.getName(); Type type = field.getType(); field.setAccessible(true); if (type.equals(String.class)) { contentValues.put(fieldName, field.get(object).toString()); } else if (type.equals(Integer.class) || type.equals(Integer.TYPE)) { contentValues.put(fieldName, field.getInt(object)); } else if (type.equals(Float.class) || type.equals(Float.TYPE)) { contentValues.put(fieldName, field.getFloat(object)); } else if (type.equals(Long.class) || type.equals(Long.TYPE)) { contentValues.put(fieldName, field.getLong(object)); } else if (type.equals(Double.class) || type.equals(Double.TYPE)) { contentValues.put(fieldName, field.getDouble(object)); } else if (type.equals(Boolean.class) || type.equals(Boolean.TYPE)) { contentValues.put(fieldName, field.getBoolean(object)); } } } catch (IllegalAccessException | IllegalArgumentException e) { e.printStackTrace(); } return contentValues; }
From source file:Main.java
public static Field findFieldByClassAndTypeAndName(Class<?> targetObject, Class<?> fieldType, String fieldName) {//from w w w . ja v a2 s. co m Class clazz = targetObject; do { for (Field field : clazz.getDeclaredFields()) { if (field.getType() == fieldType && field.getName().equals(fieldName)) { field.setAccessible(true); return field; } } clazz = clazz.getSuperclass(); } while (clazz != null); throw new NoSuchFieldError("Field of type " + fieldType.getName() + " in class " + targetObject.getName()); }