Example usage for java.lang.reflect Constructor getModifiers

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

Introduction

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

Prototype

@Override
public int getModifiers() 

Source Link

Usage

From source file:Main.java

public static void main(String... args) throws Exception {
    Class<?> c = Class.forName("java.lang.String");
    Constructor[] allConstructors = c.getDeclaredConstructors();
    for (Constructor ctor : allConstructors) {
        int searchMod = Modifier.INTERFACE;
        int mods = accessModifiers(ctor.getModifiers());
        if (searchMod == mods) {
            System.out.println(ctor);
        }// ww w.  j a  va 2  s .  c o m
    }
}

From source file:Main.java

public static void main(String... args) throws Exception {
    Class<?> c = Class.forName("java.lang.String");
    Constructor[] allConstructors = c.getDeclaredConstructors();
    for (Constructor ctor : allConstructors) {
        int searchMod = Modifier.PROTECTED;
        int mods = accessModifiers(ctor.getModifiers());
        if (searchMod == mods) {
            System.out.println(ctor);
        }/*w  w  w . j ava 2  s  . c  om*/
    }
}

From source file:Main.java

public static void main(String... args) throws Exception {
    Class<?> c = Class.forName("java.lang.String");
    Constructor[] allConstructors = c.getDeclaredConstructors();
    for (Constructor ctor : allConstructors) {
        int searchMod = Modifier.TRANSIENT;
        int mods = accessModifiers(ctor.getModifiers());
        if (searchMod == mods) {
            System.out.println(ctor);
        }/*from w  ww  . ja  v a 2s .co m*/
    }
}

From source file:Main.java

public static void main(String... args) throws Exception {
    Class<?> c = Class.forName("java.lang.String");
    Constructor[] allConstructors = c.getDeclaredConstructors();
    for (Constructor ctor : allConstructors) {
        int searchMod = Modifier.SYNCHRONIZED;
        int mods = accessModifiers(ctor.getModifiers());
        if (searchMod == mods) {
            System.out.println(ctor);
        }//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<?> 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());
        }//from www . ja v  a 2 s  .  co m
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Constructor[] constructors = String.class.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].getCanonicalName());
        }//from  ww  w. j  ava  2 s. co m
        System.out.println(");");
    }
}

From source file:ConstructorAccess.java

public static void main(String... args) {
    try {//w w  w  .j a  v a  2 s.com
        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());
            }
        }

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

From source file:Main.java

/**
 * set constructor is accessible/*from  w ww.java 2 s. c om*/
 * @param constructor {@link java.lang.reflect.Constructor}
 * @param <T> t
 */
public static <T> void makeAccessible(final Constructor<T> constructor) {
    if (!Modifier.isPublic(constructor.getModifiers())
            || !Modifier.isPublic(constructor.getDeclaringClass().getModifiers())) {
        constructor.setAccessible(true);
    }
}

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  ww  . j a  va 2 s .  c  o  m
        System.out.println(");");
    }
}

From source file:Main.java

/**
 * Takes a class type and constructs it. If the class does not have an empty constructor this
 * will//from  www .j  a v a2 s  .c o  m
 * find the parametrized constructor and use nulls and default values to construct the class.
 *
 * @param clazz The class to construct.
 * @param <T> The type of the class to construct.
 * @return The constructed object.
 */
public static <T> T createInstance(Class<T> clazz) {
    T created = null;
    Constructor<?>[] constructors = clazz.getConstructors();
    for (Constructor<?> constructor : constructors) {
        if (!Modifier.isPrivate(constructor.getModifiers())) {
            Class<?>[] parameterTypes = constructor.getParameterTypes();
            List<Object> params = new ArrayList<Object>();
            for (Class<?> parameterType : parameterTypes)
                if (!parameterType.isPrimitive()) {
                    params.add(null);
                } else {
                    if (parameterType == boolean.class) {
                        params.add(false);
                    } else {
                        params.add(0);
                    }
                }

            try {
                @SuppressWarnings("unchecked")
                T newObject = (T) constructor.newInstance(params.toArray());
                created = newObject;
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            } catch (InstantiationException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
            break;
        }
    }
    return created;
}