Example usage for java.lang.reflect Modifier isPublic

List of usage examples for java.lang.reflect Modifier isPublic

Introduction

In this page you can find the example usage for java.lang.reflect Modifier isPublic.

Prototype

public static boolean isPublic(int mod) 

Source Link

Document

Return true if the integer argument includes the public modifier, false otherwise.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    int mods = String.class.getModifiers();
    if (Modifier.isPublic(mods)) {
        System.out.println("class is public");
    }//w  w  w .  j  av  a 2s. com
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    int mods = String.class.getDeclaredMethods()[0].getModifiers();
    if (Modifier.isPublic(mods)) {
        System.out.println("member is public");
    }/*from  ww  w .  j  a  va  2 s. c  o  m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class<?> clazz = String.class;
    int modifier = clazz.getModifiers();

    if (Modifier.isPublic(modifier)) {
        System.out.println("isPublic");
    }//w  w w. j  a va2s.  co m
}

From source file:ReflectionDemo2.java

public static void main(String args[]) {
    try {// www  .ja v a  2  s  .c o  m
        A a = new A();
        Class c = a.getClass();
        System.out.println("Public Methods:");
        Method methods[] = c.getDeclaredMethods();
        for (int i = 0; i < methods.length; i++) {
            int modifiers = methods[i].getModifiers();
            if (Modifier.isPublic(modifiers)) {
                System.out.println(" " + methods[i].getName());
            }
        }
    } catch (Exception e) {
        System.out.println("Exception: " + e);
    }
}

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 w w w .  ja v  a 2s .c o m*/

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

From source file:MainClass.java

public static void main(String args[]) {
    try {// w w w.  j av a  2  s .c  om
        MyClass a = new MyClass();
        Class c = a.getClass();
        System.out.println("Public Methods:");
        Method methods[] = c.getDeclaredMethods();
        for (int i = 0; i < methods.length; i++) {
            int modifiers = methods[i].getModifiers();
            if (Modifier.isPublic(modifiers)) {
                System.out.println(" " + methods[i].getName());
            }
        }
    } catch (Exception e) {
        System.out.println("Exception: " + e);
    }
}

From source file:org.apache.openejb.util.ListSetters.java

public static void main(final String[] args) throws Exception {
    final Options options = new Options();
    options.addOption(OptionBuilder.isRequired(true).hasArg(true).withLongOpt("class")
            .withDescription("the class to introspect").create("c"));

    final CommandLine line;
    try {/*  w ww. ja va2s.c  o m*/
        line = new PosixParser().parse(options, args);
    } catch (final ParseException exp) {
        help(options);
        throw new SystemExitException(-1);
    }

    if (line.hasOption("help")) {
        help(options);
        return;
    }

    final String clazz = line.getOptionValue("class");
    final Collection<Class<?>> ancestors = Classes
            .ancestors(Thread.currentThread().getContextClassLoader().loadClass(clazz));
    final List<String> list = new LinkedList<>();
    for (final Class<?> c : ancestors) {
        for (final Method m : c.getDeclaredMethods()) {
            if (!Modifier.isPublic(m.getModifiers())) {
                continue;
            }

            if (!m.getName().startsWith("set")) {
                continue;
            }

            if (m.getGenericParameterTypes().length != 1) {
                continue;
            }

            list.add(m.getName().substring(3));
        }
    }
    Collections.sort(list);
    for (final String s : list) {
        System.out.println("- " + s);
    }
}

From source file:Main.java

protected static void makeAccessible(Field field) {
    if (!Modifier.isPublic(field.getModifiers())
            || !Modifier.isPublic(field.getDeclaringClass().getModifiers())) {
        field.setAccessible(true);/*from w  w w.jav  a2  s . com*/
    }
}

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  a  v a  2s  .  co m*/
    }
}

From source file:Main.java

public static void setField(Field field, Object target, Object value) {
    if (!Modifier.isPublic(field.getModifiers())) {
        field.setAccessible(true);/*from w w  w .  j ava2 s  .c o  m*/
    }
    try {
        field.set(target, value);
    } catch (IllegalAccessException iae) {
        throw new IllegalArgumentException("Could not set field " + field, iae);
    }
}