Modifier: PUBLIC : Modifier « java.lang.reflect « Java by API






Modifier: PUBLIC

 
import static java.lang.System.out;

import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;

public class Main {
  public static void main(String... args) throws Exception {
    Class<?> c = Class.forName(args[0]);
    Constructor[] allConstructors = c.getDeclaredConstructors();
    for (Constructor ctor : allConstructors) {
      int searchMod = modifierFromString(args[1]);
      int mods = accessModifiers(ctor.getModifiers());
      if (searchMod == mods) {
        out.format("%s%n", ctor.toGenericString());
        out.format("  [ synthetic=%-5b var_args=%-5b ]%n", ctor.isSynthetic(), ctor.isVarArgs());
      }
    }
  }

  private static int accessModifiers(int m) {
    return m & (Modifier.PUBLIC | Modifier.PRIVATE | Modifier.PROTECTED);
  }

  private static int modifierFromString(String s) {
    if ("public".equals(s))
      return Modifier.PUBLIC;
    else if ("protected".equals(s))
      return Modifier.PROTECTED;
    else if ("private".equals(s))
      return Modifier.PRIVATE;
    else if ("package-private".equals(s))
      return 0;
    else
      return -1;
  }
}

   
  








Related examples in the same category

1.Modifier: PRIVATE
2.Modifier: PROTECTED
3.Modifier.isPrivate(int mod)
4.Modifier.isPublic(int mod)
5.Modifier.isAbstract(int mod)
6.Modifier.isNative(int mod)
7.Modifier.isInterface(int mod)
8.Modifier.isTransient(int mod)
9.Modifier.isVolatile(int mod)
10.Modifier: toString(int mod)