Example usage for java.lang.reflect Field getModifiers

List of usage examples for java.lang.reflect Field getModifiers

Introduction

In this page you can find the example usage for java.lang.reflect Field getModifiers.

Prototype

public int getModifiers() 

Source Link

Document

Returns the Java language modifiers for the field represented by this Field object, as an integer.

Usage

From source file:Main.java

public static void main(String[] args) throws IllegalAccessException {
    Class clazz = Color.class;
    Field[] colorFields = clazz.getDeclaredFields();

    HashMap<String, Color> singleColors = new HashMap<String, Color>();
    for (Field cf : colorFields) {
        int modifiers = cf.getModifiers();
        if (!Modifier.isPublic(modifiers))
            continue;

        Color c = (Color) cf.get(null);
        if (!singleColors.values().contains(c))
            singleColors.put(cf.getName(), c);
    }/*from   www.j  a  v  a 2s .c o  m*/

    for (String k : singleColors.keySet()) {
        System.out.println(k + ": " + singleColors.get(k));
    }
}

From source file:org.apache.flink.configuration.ConfigDocsCompletenessChecker.java

public static void main(String[] args) throws Exception {

    String configFileContents = FileUtils.readFileToString(new File("docs/setup/config.md"));
    Field[] fields = ConfigConstants.class.getFields();

    for (Field field : fields) {
        if (Modifier.isStatic(field.getModifiers()) && field.getType().equals(String.class)
                && !field.getName().startsWith("DEFAULT")) {
            Object val = field.get(null);
            if (!configFileContents.contains((String) val)) {
                System.out.println("++++ " + val + " is not mentioned in the configuration file!!!");
            }//from   w  ww  .  j av a 2 s  .  co m
        }
    }
}

From source file:Main.java

public static void main(String[] args) {
    Field[] fields = java.lang.Integer.class.getDeclaredFields();

    for (int i = 0; i < fields.length; i++) {
        Field f = fields[i];
        Class type = f.getType();
        String name = f.getName();
        System.out.print(Modifier.toString(f.getModifiers()));
        System.out.println(" " + type.getCanonicalName() + " " + name + ";");
    }//from  w  w w.j ava  2s.c om
}

From source file:Spy.java

public static void main(String... args) {
    try {//from w  w w  .ja v  a2s.co m
        Class<?> c = Class.forName(args[0]);
        int searchMods = 0x0;
        for (int i = 1; i < args.length; i++) {
            searchMods |= modifierFromString(args[i]);
        }

        Field[] flds = c.getDeclaredFields();
        out.format("Fields in Class '%s' containing modifiers:  %s%n", c.getName(),
                Modifier.toString(searchMods));
        boolean found = false;
        for (Field f : flds) {
            int foundMods = f.getModifiers();
            // Require all of the requested modifiers to be present
            if ((foundMods & searchMods) == searchMods) {
                out.format("%-8s [ synthetic=%-5b enum_constant=%-5b ]%n", f.getName(), f.isSynthetic(),
                        f.isEnumConstant());
                found = true;
            }
        }

        if (!found) {
            out.format("No matching fields%n");
        }

        // production code should handle this exception more gracefully
    } catch (ClassNotFoundException x) {
        x.printStackTrace();
    }
}

From source file:Main.java

public static boolean fieldCanBeModify(Field field) {
    return field != null && ((field.getModifiers() & Modifier.FINAL) == 0);
}

From source file:Main.java

public static boolean fieldIsInvalid(Field field) {
    return (Modifier.isStatic(field.getModifiers()) && Modifier.isFinal(field.getModifiers()))
            || field.isSynthetic();/* ww w .  j  a v a 2  s .co  m*/
}

From source file:Main.java

protected static void makeAccessible(Field field) {
    if (!Modifier.isPublic(field.getModifiers())
            || !Modifier.isPublic(field.getDeclaringClass().getModifiers())) {
        field.setAccessible(true);// www  .  j  av a 2s  .c om
    }
}

From source file:Main.java

protected static void makeAccessible(final Field field) {
    if (!Modifier.isPublic(field.getModifiers())
            || !Modifier.isPublic(field.getDeclaringClass().getModifiers())) {
        field.setAccessible(true);//from  w w w. j av a  2  s. co m
    }
}

From source file:MySuperClass.java

public static ArrayList<String> getFieldsDesciption(Field[] fields) {
    ArrayList<String> fieldList = new ArrayList<>();

    for (Field f : fields) {
        int mod = f.getModifiers() & Modifier.fieldModifiers();
        String modifiers = Modifier.toString(mod);

        Class<?> type = f.getType();
        String typeName = type.getSimpleName();

        String fieldName = f.getName();

        fieldList.add(modifiers + "  " + typeName + "  " + fieldName);
    }//from  w  w  w  .  j a  va  2 s  .  co m

    return fieldList;
}

From source file:Main.java

public static boolean isPublicField(Field field) {
    final int mod = field.getModifiers();
    return Modifier.isPublic(mod);
}