List of usage examples for java.lang.reflect Field getName
public String getName()
From source file:Main.java
/** * Dynamically load R.styleable.name/*w ww . jav a2 s . c o m*/ * @param context * @param name * @return */ public static int getStyleableResourceInt(Context context, String name) { if (context == null) return 0; try { //use reflection to access the resource class Field[] fields2 = Class.forName(context.getPackageName() + ".R$styleable").getFields(); //browse all fields for (Field f : fields2) { //pick matching field if (f.getName().equals(name)) { //return as int array int ret = (Integer) f.get(null); return ret; } } } catch (Throwable t) { /*no op*/ } return 0; }
From source file:Main.java
/** * Get a Hashtable of all class member fields (including private) of the * given object/*www . j ava2 s . c o m*/ * * @param objectInstance Object to get data from * @return Hashtable of all values * * @throws IllegalArgumentException * @throws IllegalAccessException */ public static Map<String, Object> getAllFields(Object objectInstance) throws IllegalArgumentException, IllegalAccessException // NOSONAR { HashMap<String, Object> map = new HashMap<String, Object>(); Class<? extends Object> clazz = objectInstance.getClass(); Field fields[] = clazz.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field f = fields[i]; f.setAccessible(true); String fieldName = f.getName(); Object fieldValue = f.get(objectInstance); map.put(fieldName, fieldValue); } return map; }
From source file:Main.java
public static Method findIsSelectedMethodForField(Field field, Class<?> objectClass) throws NoSuchMethodException { String methodName = "is" + field.getName().toUpperCase().substring(0, 1) + field.getName().substring(1) + "Selected"; return objectClass.getMethod(methodName, (java.lang.Class[]) null); }
From source file:Main.java
/** * Returns the Jarvis Api Version or -1 if it is not supported * @return api version//from ww w . j a v a 2s .c o m */ public static int getJarvisApiVersion() { Field[] declaredFields = Build.class.getDeclaredFields(); for (Field field : declaredFields) { if (Modifier.isStatic(field.getModifiers()) && field.getName().equals("JARVIS_VERSION")) { try { return field.getInt(null); } catch (IllegalAccessException ex) { ex.printStackTrace(); } catch (IllegalArgumentException ex) { ex.printStackTrace(); } } } return -1; }
From source file:Main.java
/********************************************************************************* * Returns the resource-IDs for all attributes specified in the * given <declare-styleable>-resource tag as an int array. * * @param context The current application context. * @param name The name of the <declare-styleable>-resource-tag to pick. * @return All resource-IDs of the child-attributes for the given * <declare-styleable>-resource or <code>null</code> if * this tag could not be found or an error occured. *********************************************************************************/ public static final int[] getResourceDeclareStyleableIntArray(Context context, String name) { try {/*from w w w.j a va 2s. c o m*/ //use reflection to access the resource class Field[] fields2 = Class.forName(context.getPackageName() + ".R$styleable").getFields(); //browse all fields for (Field f : fields2) { //pick matching field if (f.getName().equals(name)) { //return as int array int[] ret = (int[]) f.get(null); return ret; } } } catch (Throwable t) { } return null; }
From source file:Main.java
/** * Returns a {@code Class} object that identifies the * declared class for the field represented by the given {@code String name} parameter inside * the invoked {@code Class<?> clazz} parameter. * * @param clazz the {@code Class} object whose declared fields to be * checked for a certain field.//w w w . j av a 2s . co m * @param name the field name as {@code String} to be * compared with {@link Field#getName()} * @return the {@code Class} object representing the type of given field name. * * @see {@link Class#getDeclaredFields()} * @see {@link Field#getType()} */ public static Class<?> getFieldClass(Class<?> clazz, String name) { if (clazz == null || name == null || name.isEmpty()) { return null; } Class<?> propertyClass = null; for (Field field : clazz.getDeclaredFields()) { field.setAccessible(true); if (field.getName().equalsIgnoreCase(name)) { propertyClass = field.getType(); break; } } return propertyClass; }
From source file:Main.java
/** * @param type//from ww w.ja v a 2 s . com * @param object * @param field * @param value */ public static <T> void setFieldValue(Class<? extends T> type, T object, Field field, Object value) { try { Method m = type.getMethod("set" + getFirstLetterUppercased(field.getName()), field.getType()); m.invoke(object, value); } catch (Exception e) { } }
From source file:Main.java
public static Object getProperty(Object o, String field) { try {// w ww . j ava 2 s . co m Field f = o.getClass().getDeclaredField(field); f.setAccessible(true); String name = f.getName(); name = name.replaceFirst(name.substring(0, 1), name.substring(0, 1).toUpperCase(Locale.US)); Method m = o.getClass().getMethod("get" + name); // return f.get(o); return (Object) m.invoke(o); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static String[][] returnTable(Collection E) { if ((E == null) || E.isEmpty()) { System.out.println("The collection is empty!"); }//from w w w. j av a 2 s . c o m Set<Field> collectionFields = new TreeSet<>(new Comparator<Field>() { @Override public int compare(Field o1, Field o2) { return o1.getName().compareTo(o2.getName()); } }); for (Object o : E) { Class c = o.getClass(); createSetOfFields(collectionFields, c); while (c.getSuperclass() != null) { c = c.getSuperclass(); createSetOfFields(collectionFields, c); } } String[][] exitText = new String[E.size() + 1][collectionFields.size() + 1]; exitText[0][0] = String.format("%20s", "Class"); int indexOfColumn = 0; for (Field f : collectionFields) { exitText[0][indexOfColumn + 1] = String.format("%20s", f.getName()); indexOfColumn++; } int indexOfRow = 0; for (Object o : E) { indexOfColumn = 0; exitText[indexOfRow + 1][0] = String.format("%20s", o.getClass().getSimpleName()); for (Field field : collectionFields) { try { field.setAccessible(true); if (field.get(o) instanceof Date) { exitText[indexOfRow + 1][indexOfColumn + 1] = String.format("%20tD", field.get(o)); } else { String temp = String.valueOf(field.get(o)); exitText[indexOfRow + 1][indexOfColumn + 1] = String.format("%20s", temp); } } catch (Exception e) { exitText[indexOfRow + 1][indexOfColumn + 1] = String.format("%20s", "-"); } indexOfColumn++; } indexOfRow++; } return exitText; }
From source file:Main.java
public static Field findFiledWithName(Field[] fields, String filedName) { if (fields != null) { for (Field field : fields) { field.setAccessible(true);//from w w w. j ava 2 s . com if (field.getName().equals(filedName)) { return field; } } } return null; }