List of usage examples for java.lang Class getDeclaredFields
@CallerSensitive public Field[] getDeclaredFields() throws SecurityException
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 {/* www .j a v a 2 s. co 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 ArrayList<String> getFields(Class<?> cls) { ArrayList<String> fieldList = new ArrayList<String>(); Field[] fields = cls.getDeclaredFields(); for (Field f : fields) { fieldList.add(f.getName());//www. j a v a 2s . c om } return fieldList; }
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 www. j a v a 2 s.co m } return obj; }
From source file:Main.java
public static Field[] findFields(Class<?> clazz) { Field[] result = null;/*from ww w . ja va2 s. c o m*/ if (clazz != null) { result = clazz.getDeclaredFields(); } return result; }
From source file:com.thinkbiganalytics.metadata.jpa.support.QueryDslPathInspector.java
/** * returns a Map of the Field Name and the respective Field object for a given class *//*w w w.j av a2 s .co m*/ public static Map<String, Field> getFields(Class<?> cl) { return Arrays.asList(cl.getDeclaredFields()).stream().collect(Collectors.toMap(f -> f.getName(), f -> f)); }
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); }/* w ww . j a v a 2s. co m*/ } return obj; }
From source file:Main.java
public static void classInspector(Object object) { Class cls = object.getClass(); System.out.println(cls.toString()); Field[] fieldlist = cls.getDeclaredFields(); for (int i = 0; i < fieldlist.length; i++) { Field fld = fieldlist[i]; System.out.println("name = " + fld.getName()); System.out.println("decl class = " + fld.getDeclaringClass()); System.out.println("type = " + fld.getType()); System.out.println("-----"); }/*from w ww. j a v a 2 s . c om*/ }
From source file:Main.java
/** * Set the fields' value.//from ww w .ja v a 2 s .co m * * @param bean * @param valMap */ public static void setFieldValues(Object bean, Map<String, Object> valMap) { Class<?> cls = bean.getClass(); //Get all fields. Field[] fields = cls.getDeclaredFields(); for (Field field : fields) { if (valMap.containsKey(field.getName())) { field.setAccessible(true); try { field.set(bean, valMap.get(field.getName())); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } } }
From source file:Main.java
public static <T> T converMap2Object(Map<String, String> map, Class<T> cls) { Field[] fields = cls.getDeclaredFields(); T t = null;/* w w w .j a v a 2 s.co m*/ try { t = (T) cls.newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } for (Field f : fields) { f.setAccessible(true); invokeSet(t, f.getName(), map.get(f.getName())); } return t; }
From source file:ReflectApp.java
static void describeClassOrInterface(Class className, String name) { displayModifiers(className.getModifiers()); displayFields(className.getDeclaredFields()); displayMethods(className.getDeclaredMethods()); if (className.isInterface()) { System.out.println("Interface: " + name); } else {/*from w w w . j ava2s . co m*/ System.out.println("Class: " + name); displayInterfaces(className.getInterfaces()); displayConstructors(className.getDeclaredConstructors()); } }