Constructor: toGenericString() : Constructor « java.lang.reflect « Java by API






Constructor: toGenericString()

  

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.Constructor: getExceptionTypes()
2.Constructor: getParameterTypes()
3.Constructor: getModifiers()
4.Constructor: getName()
5.Constructor: newInstance(Object... initargs)
6.Constructor: setAccessible(boolean flag)