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:org.force66.beantester.utils.InstantiationUtils.java

public static Constructor<?> findPublicConstructor(Class<?> klass) {
    Validate.notNull(klass, "Null class not allowed.");
    Constructor<?> nullConstructor = ConstructorUtils.getAccessibleConstructor(klass, new Class<?>[0]);
    if (nullConstructor != null) {
        return nullConstructor;
    }/*w w  w.  j  a v a2  s  .  co  m*/

    Constructor<?>[] constructorArray = klass.getConstructors();
    for (Constructor<?> constructor : constructorArray) {
        if (Modifier.isPublic(constructor.getModifiers())) {
            return constructor;
        }
    }

    return null;
}

From source file:ReflectionTest.java

/**
 * Prints all constructors of a class//from w w  w.j  a  v  a  2 s  .  c om
 * @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(");");
    }
}

From source file:com.haulmont.cuba.core.config.type.TypeFactory.java

@Nullable
private static TypeFactory getInferred(Class<?> returnType) {
    // special case due to Character.valueOf(char) does not accept String
    if (returnType == Character.class) {
        return new CharTypeFactory();
    }/*w  ww.j  a  va2s  .  co m*/

    for (String methodName : FACTORY_METHOD_NAMES) {
        try {
            Method factoryMethod = returnType.getMethod(methodName, String.class);
            if (isAcceptableMethod(returnType, factoryMethod)) {
                return new StaticTypeFactory(factoryMethod);
            }
        } catch (NoSuchMethodException ex) {
            // Ignore failure.
        }
    }
    try {
        Constructor ctor = returnType.getConstructor(String.class);
        if (Modifier.isPublic(ctor.getModifiers())) {
            return new ConstructorTypeFactory(ctor);
        }
    } catch (NoSuchMethodException e) {
        return null;
    }
    return null;
}

From source file:edu.mit.media.funf.config.DefaultRuntimeTypeAdapterFactory.java

public static boolean isTypeInstatiable(Class<?> type) {
    int modifiers = type.getModifiers();
    if (!(Modifier.isAbstract(modifiers) || Modifier.isInterface(modifiers))) {
        try {//from   ww  w.  j a va2  s .  c om
            Constructor<?> noArgConstructor = type.getConstructor();
            return Modifier.isPublic(noArgConstructor.getModifiers());
        } catch (SecurityException e) {
        } catch (NoSuchMethodException e) {
        }
    }
    return false;
}

From source file:org.openehealth.ipf.commons.lbs.utils.NiceClass.java

/**
 * Ensures that all constructors of a class are null safe.
 * <p>//  w  w w . jav a  2s .  c o  m
 * Test involves public constructors.
 * @param obj
 *          object to test
 * @param args
 *          dummy arguments of all different parameter types that any of the constructors have
 * @param optional
 *          optional dummy arguments that can also be set to {@code null} without breaking
 *          null-safety
 * @throws any thrown exception that were not expected. Never an {@link IllegalArgumentException}.
 */
public static void checkConstructorsAreNullSafe(Class<?> clazz, List<?> args, List<?> optional)
        throws Exception {
    for (Constructor<?> constructor : clazz.getDeclaredConstructors()) {
        if (Modifier.isPublic(constructor.getModifiers())) {
            checkConstructorIsNullSafe(constructor, args, optional);
        }
    }
}

From source file:org.evosuite.setup.TestClusterUtils.java

public static void makeAccessible(Constructor<?> constructor) {
    if (!Modifier.isPublic(constructor.getModifiers())
            || !Modifier.isPublic(constructor.getDeclaringClass().getModifiers())) {
        constructor.setAccessible(true);
    }/*w  w w .  j av a2 s. co m*/
}

From source file:Main.java

public static <T> Constructor<T> findConstructor(Class<T> paramClass, boolean paramBoolean) {
    Constructor localConstructor;
    try {/*  w  w  w  .  ja  v a  2 s. com*/
        localConstructor = paramClass.getDeclaredConstructor(new Class[0]);
        if (paramBoolean) {
            checkAndFixAccess(localConstructor);
            return localConstructor;
        }
        if (!Modifier.isPublic(localConstructor.getModifiers()))
            throw new IllegalArgumentException("Default constructor for " + paramClass.getName()
                    + " is not accessible (non-public?): not allowed to try modify access via Reflection: can not instantiate type");
    } catch (NoSuchMethodException localNoSuchMethodException) {
        return null;
    } catch (Exception localException) {
        while (true)
            unwrapAndThrowAsIAE(localException, "Failed to find default constructor of class "
                    + paramClass.getName() + ", problem: " + localException.getMessage());
    }
    return localConstructor;
}

From source file:Main.java

public static <T> Constructor<T> findConstructor(Class<T> paramClass, boolean paramBoolean)
        throws IllegalArgumentException {
    Constructor localConstructor;
    try {//from  w w  w.  j a v  a 2 s . c  om
        localConstructor = paramClass.getDeclaredConstructor(new Class[0]);
        if (paramBoolean) {
            checkAndFixAccess(localConstructor);
            return localConstructor;
        }
        if (!Modifier.isPublic(localConstructor.getModifiers()))
            throw new IllegalArgumentException("Default constructor for " + paramClass.getName()
                    + " is not accessible (non-public?): not allowed to try modify access via Reflection: can not instantiate type");
    } catch (NoSuchMethodException localNoSuchMethodException) {
        return null;
    } catch (Exception localException) {
        while (true)
            unwrapAndThrowAsIAE(localException, "Failed to find default constructor of class "
                    + paramClass.getName() + ", problem: " + localException.getMessage());
    }
    return localConstructor;
}

From source file:com.github.juanmf.java2plant.Parser.java

protected static void addUses(Set<Relation> relations, Class<?> fromType) {
    Method[] methods = fromType.getDeclaredMethods();
    for (Method m : methods) {
        if (!Modifier.isPrivate(m.getModifiers())) {
            addMethodUses(relations, fromType, m);
        }/*  w w w  .  j  a va 2s .  c o  m*/
    }
    Constructor<?>[] constructors = fromType.getDeclaredConstructors();
    for (Constructor<?> c : constructors) {
        if (!Modifier.isPrivate(c.getModifiers())) {
            addConstructorUses(relations, fromType, c);
        }
    }
}

From source file:com.link_intersystems.beans.BeanClass.java

/**
 * Has the clazz a public default constructor?
 *
 * @param clazz/*  w w w .  j  av a 2s .com*/
 * @return
 */
private static boolean hasBeanConstructor(Class<?> clazz) {
    try {
        Constructor<?> defaultConstructor = clazz.getDeclaredConstructor();
        int modifiers = defaultConstructor.getModifiers();
        return Modifier.isPublic(modifiers);
    } catch (NoSuchMethodException e) {
        return false;
    }
}