List of usage examples for java.lang.reflect Field getModifiers
public int getModifiers()
From source file:Main.java
private static void init() { try {//from w ww .j ava2 s.com // read all com.android.internal.R Class clazz = Class.forName("com.android.internal.R$layout"); Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { // public static final if (Modifier.isPublic(field.getModifiers()) && Modifier.isStatic(field.getModifiers()) && Modifier.isFinal(field.getModifiers())) { try { int id = field.getInt(null); sSystemLayoutResIds.put(id, field.getName()); } catch (IllegalAccessException e) { } } } } catch (Exception e) { } }
From source file:ShowClass.java
/** Print the modifiers, type, and name of a field */ public static void print_field(Field f) { System.out.println(" " + modifiers(f.getModifiers()) + typename(f.getType()) + " " + f.getName() + ";"); }
From source file:Main.java
/** * Get a list of the name of variables of the class received * @param klass Class to get the variable names * @return List<String>[] of length 3 with variable names: [0]: primitives, [1]: arrays, [2]: objects *//* w w w . j a v a 2 s .com*/ public static List<String>[] getVariableNames(Class klass) { //array to return List<String>[] varNames = new List[3]; for (int i = 0; i < 3; i++) { varNames[i] = new ArrayList<>(); } //add all valid fields do { Field[] fields = klass.getDeclaredFields(); for (Field field : fields) { if (!Modifier.isTransient(field.getModifiers())) { //get the type Class type = field.getType(); if (type.isPrimitive() || (type == Integer.class) || (type == Float.class) || (type == Double.class) || (type == Boolean.class) || (type == String.class)) { varNames[0].add(field.getName()); } else if (type.isArray()) { varNames[1].add(field.getName()); } else { varNames[2].add(field.getName()); } } } klass = klass.getSuperclass(); } while (klass != null); //return array return varNames; }
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//from w ww. jav a2 s .com * @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 List<Field> getPublicFields(Class<?> clazz) { List<Field> publicList = new ArrayList<Field>(); List<Field> accessibleList = getAccessibleFields(clazz, Object.class); for (Field f : accessibleList) { if (Modifier.isPublic(f.getModifiers()) == true) { publicList.add(f);//w w w . j ava 2s . co m } } accessibleList.clear(); return publicList; }
From source file:springobjectmapper.ReflectionHelper.java
private static boolean isEntityField(Field field) { return !field.isSynthetic() && !Modifier.isStatic(field.getModifiers()) && !Modifier.isTransient(field.getModifiers()); }
From source file:Main.java
public static Field[] getAllFiedFromClassAndSuper(Class clazz, boolean needStatic) { ArrayList<Field> fields = new ArrayList<>(); if (clazz != null) { Field[] classFields = clazz.getDeclaredFields(); if (classFields != null) { for (Field field : classFields) { boolean isStatic = Modifier.isStatic(field.getModifiers()); if (isStatic && !needStatic) { continue; }//from w ww. j av a2 s . com fields.add(field); } } Field[] superFields = getAllFiedFromClassAndSuper(clazz.getSuperclass(), needStatic); if (superFields != null) { for (Field field : superFields) { boolean isStatic = Modifier.isStatic(field.getModifiers()); if (isStatic && !needStatic) { continue; } fields.add(field); } } } return fields.toArray(new Field[fields.size()]); }
From source file:Main.java
/** * Converts a color into a string. If the color is equal to one of the defined * constant colors, that name is returned instead. Otherwise the color is * returned as hex-string./*from w w w.j a va 2s . co m*/ * * @param c * the color. * @return the string for this color. */ public static String colorToString(final Color c) { try { final Field[] fields = Color.class.getFields(); for (int i = 0; i < fields.length; i++) { final Field f = fields[i]; if (Modifier.isPublic(f.getModifiers()) && Modifier.isFinal(f.getModifiers()) && Modifier.isStatic(f.getModifiers())) { final String name = f.getName(); final Object oColor = f.get(null); if (oColor instanceof Color) { if (c.equals(oColor)) { return name; } } } } } catch (Exception e) { // } // no defined constant color, so this must be a user defined color final String color = Integer.toHexString(c.getRGB() & 0x00ffffff); final StringBuffer retval = new StringBuffer(7); retval.append("#"); final int fillUp = 6 - color.length(); for (int i = 0; i < fillUp; i++) { retval.append("0"); } retval.append(color); return retval.toString(); }
From source file:com.netflix.astyanax.util.StringUtils.java
public static <T> String joinClassAttributeValues(final T object, String name, Class<T> clazz) { Field[] fields = clazz.getDeclaredFields(); StringBuilder sb = new StringBuilder(); sb.append(name).append("["); sb.append(org.apache.commons.lang.StringUtils.join(Collections2.transform( // Filter any field that does not start with lower case // (we expect constants to start with upper case) Collections2.filter(Arrays.asList(fields), new Predicate<Field>() { @Override/*from w ww.jav a 2 s. com*/ public boolean apply(Field field) { if ((field.getModifiers() & Modifier.STATIC) == Modifier.STATIC) return false; return Character.isLowerCase(field.getName().charAt(0)); } }), // Convert field to "name=value". value=*** on error new Function<Field, String>() { @Override public String apply(Field field) { Object value; try { value = field.get(object); } catch (Exception e) { value = "***"; } return field.getName() + "=" + value; } }), ",")); sb.append("]"); return sb.toString(); }
From source file:ColorUtils.java
private static boolean isConstantColorField(Field field) { return Modifier.isPublic(field.getModifiers()) && Modifier.isStatic(field.getModifiers()) && Color.class == field.getType(); }