Example usage for java.lang.reflect Modifier toString

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

Introduction

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

Prototype

public static String toString(int mod) 

Source Link

Document

Return a string describing the access modifier flags in the specified modifier.

Usage

From source file:MainClass.java

public static void printMethods(Class cl) {
    Method[] methods = cl.getDeclaredMethods();

    for (int i = 0; i < methods.length; i++) {
        Method m = methods[i];/*from   ww  w .j  av  a  2 s . c  om*/
        Class retType = m.getReturnType();
        Class[] paramTypes = m.getParameterTypes();
        String name = m.getName();
        System.out.print(Modifier.toString(m.getModifiers()));
        System.out.print(" " + retType.getName() + " " + name + "(");
        for (int j = 0; j < paramTypes.length; j++) {
            if (j > 0)
                System.out.print(", ");
            System.out.print(paramTypes[j].getName());
        }
        System.out.println(");");
    }
}

From source file:ReflectionTest.java

public static void printConstructors(Class cl) {
    Constructor[] constructors = cl.getDeclaredConstructors();

    for (int i = 0; i < constructors.length; i++) {
        Constructor c = constructors[i];
        Class[] paramTypes = c.getParameterTypes();
        String name = c.getName();
        System.out.print(Modifier.toString(c.getModifiers()));
        System.out.print(" " + name + "(");
        for (int j = 0; j < paramTypes.length; j++) {
            if (j > 0)
                System.out.print(", ");
            System.out.print(paramTypes[j].getName());
        }//from w  w  w . java 2  s  .  c o  m
        System.out.println(");");
    }
}

From source file:ReflectApp.java

static void displayModifiers(int m) {
    System.out.println("Modifiers: " + Modifier.toString(m));
}

From source file:ShowClass.java

/**
 * Display the modifiers, name, superclass and interfaces of a class or
 * interface. Then go and list all constructors, fields, and methods.
 *///  w w  w .java2s  .  c  o  m
public static void print_class(Class c) {
    // Print modifiers, type (class or interface), name and superclass.
    if (c.isInterface()) {
        // The modifiers will include the "interface" keyword here...
        System.out.print(Modifier.toString(c.getModifiers()) + " " + typename(c));
    } else if (c.getSuperclass() != null) {
        System.out.print(Modifier.toString(c.getModifiers()) + " class " + typename(c) + " extends "
                + typename(c.getSuperclass()));
    } else {
        System.out.print(Modifier.toString(c.getModifiers()) + " class " + typename(c));
    }

    // Print interfaces or super-interfaces of the class or interface.
    Class[] interfaces = c.getInterfaces();
    if ((interfaces != null) && (interfaces.length > 0)) {
        if (c.isInterface())
            System.out.print(" extends ");
        else
            System.out.print(" implements ");
        for (int i = 0; i < interfaces.length; i++) {
            if (i > 0)
                System.out.print(", ");
            System.out.print(typename(interfaces[i]));
        }
    }

    System.out.println(" {"); // Begin class member listing.

    // Now look up and display the members of the class.
    System.out.println("  // Constructors");
    Constructor[] constructors = c.getDeclaredConstructors();
    for (int i = 0; i < constructors.length; i++)
        // Display constructors.
        print_method_or_constructor(constructors[i]);

    System.out.println("  // Fields");
    Field[] fields = c.getDeclaredFields(); // Look up fields.
    for (int i = 0; i < fields.length; i++)
        // Display them.
        print_field(fields[i]);

    System.out.println("  // Methods");
    Method[] methods = c.getDeclaredMethods(); // Look up methods.
    for (int i = 0; i < methods.length; i++)
        // Display them.
        print_method_or_constructor(methods[i]);

    System.out.println("}"); // End class member listing.
}

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

    return fieldList;
}

From source file:MyClass.java

public static String getClassDescription(Class c) {
    StringBuilder classDesc = new StringBuilder();
    int modifierBits = 0;
    String keyword = "";
    if (c.isInterface()) {
        modifierBits = c.getModifiers() & Modifier.interfaceModifiers();
        if (c.isAnnotation()) {
            keyword = "@interface";
        } else {// w ww .j  a v  a 2  s . c  om
            keyword = "interface";
        }
    } else if (c.isEnum()) {
        modifierBits = c.getModifiers() & Modifier.classModifiers();
        keyword = "enum";
    }
    modifierBits = c.getModifiers() & Modifier.classModifiers();
    keyword = "class";

    String modifiers = Modifier.toString(modifierBits);
    classDesc.append(modifiers);
    classDesc.append(" " + keyword);
    String simpleName = c.getSimpleName();
    classDesc.append(" " + simpleName);

    String genericParms = getGenericTypeParams(c);
    classDesc.append(genericParms);

    Class superClass = c.getSuperclass();
    if (superClass != null) {
        String superClassSimpleName = superClass.getSimpleName();
        classDesc.append("  extends " + superClassSimpleName);
    }
    String interfaces = Main.getClassInterfaces(c);
    if (interfaces != null) {
        classDesc.append("  implements " + interfaces);
    }
    return classDesc.toString();
}

From source file:Main.java

public static String modifiers(int m) {
    if (m == 0)//from ww w  .  j av  a 2 s. c o  m
        return "";
    else
        return Modifier.toString(m) + " ";
}

From source file:MyClass.java

public static ArrayList<String> getParameters(Executable exec) {
    Parameter[] parms = exec.getParameters();
    ArrayList<String> parmList = new ArrayList<>();
    for (int i = 0; i < parms.length; i++) {

        int mod = parms[i].getModifiers() & Modifier.parameterModifiers();
        String modifiers = Modifier.toString(mod);
        String parmType = parms[i].getType().getSimpleName();
        String parmName = parms[i].getName();
        String temp = modifiers + "  " + parmType + "  " + parmName;
        if (temp.trim().length() == 0) {
            continue;
        }//  w  w  w .java2  s.  c om
        parmList.add(temp.trim());
    }
    return parmList;
}

From source file:GenericReflectionTest.java

public static void printMethod(Method m) {
    String name = m.getName();//from w w  w. ja v a2  s  .  co  m
    System.out.print(Modifier.toString(m.getModifiers()));
    System.out.print(" ");
    printTypes(m.getTypeParameters(), "<", ", ", "> ", true);

    printType(m.getGenericReturnType(), false);
    System.out.print(" ");
    System.out.print(name);
    System.out.print("(");
    printTypes(m.getGenericParameterTypes(), "", ", ", "", false);
    System.out.println(")");
}

From source file:ReflectionTest.java

/**
 * Prints all constructors of a class// w w w .j  a  v a 2s.  c o m
 * @param cl a class
 */
public static void printConstructors(Class cl) {
    Constructor[] constructors = cl.getDeclaredConstructors();

    for (Constructor c : constructors) {
        String name = c.getName();
        System.out.print("   ");
        String modifiers = Modifier.toString(c.getModifiers());
        if (modifiers.length() > 0)
            System.out.print(modifiers + " ");
        System.out.print(name + "(");

        // print parameter types
        Class[] paramTypes = c.getParameterTypes();
        for (int j = 0; j < paramTypes.length; j++) {
            if (j > 0)
                System.out.print(", ");
            System.out.print(paramTypes[j].getName());
        }
        System.out.println(");");
    }
}