Example usage for org.apache.commons.lang3.reflect ConstructorUtils getAccessibleConstructor

List of usage examples for org.apache.commons.lang3.reflect ConstructorUtils getAccessibleConstructor

Introduction

In this page you can find the example usage for org.apache.commons.lang3.reflect ConstructorUtils getAccessibleConstructor.

Prototype

public static <T> Constructor<T> getAccessibleConstructor(final Constructor<T> ctor) 

Source Link

Document

Checks if the specified constructor is accessible.

This simply ensures that the constructor is accessible.

Usage

From source file:com.robertsmieja.test.utils.junit.Internal.java

static <T> T createObjectFromDefaultConstructor(Class<T> aClass) throws ObjectFactoryException {
    Constructor<T> constructor = ConstructorUtils.getAccessibleConstructor(aClass);
    if (constructor == null) {
        throw new ObjectFactoryException("Unable to find a public no-arg constructor for <" + aClass + ">");
    }/*from  ww w.j a  v a2s.  co  m*/

    try {
        return constructor.newInstance();
    } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
        throw new ObjectFactoryException(
                "Exception encountered while trying to create an instance of <" + aClass + ">", e);
    }
}

From source file:com.offbynull.coroutines.instrumenter.asm.SimpleClassWriterTest.java

@Test
public void testCustomGetCommonSuperClassImplementation() throws Exception {
    // augment method to take in a single int argument
    methodNode.desc = Type.getMethodDescriptor(Type.VOID_TYPE, Type.INT_TYPE);

    // augment method instructions
    VariableTable varTable = new VariableTable(classNode, methodNode);

    Class<?> iterableClass = Iterable.class;
    Constructor arrayListConstructor = ConstructorUtils.getAccessibleConstructor(ArrayList.class);
    Constructor linkedListConstructor = ConstructorUtils.getAccessibleConstructor(LinkedList.class);
    Constructor hashSetConstructor = ConstructorUtils.getAccessibleConstructor(HashSet.class);
    Method iteratorMethod = MethodUtils.getAccessibleMethod(iterableClass, "iterator");

    Type iterableType = Type.getType(iterableClass);

    Variable testArg = varTable.getArgument(1);
    Variable listVar = varTable.acquireExtra(iterableType);

    /**/*from  w w  w .  ja va 2s . c  o  m*/
     * Collection it;
     * switch(arg1) {
     *     case 0:
     *         it = new ArrayList()
     *         break;
     *     case 1:
     *         it = new LinkedList()
     *         break;
     *     case 2:
     *         it = new HashSet()
     *         break;
     *     default: throw new RuntimeException("must be 0 or 1");
     * }
     * list.iterator();
     */
    LabelNode invokePoint = new LabelNode();
    InsnList methodInsnList = merge(
            tableSwitch(loadVar(testArg), throwException("must be 0 or 1"), 0,
                    merge(construct(arrayListConstructor), saveVar(listVar), jumpTo(invokePoint)),
                    merge(construct(linkedListConstructor), saveVar(listVar), jumpTo(invokePoint)),
                    merge(construct(hashSetConstructor), saveVar(listVar), jumpTo(invokePoint))),
            addLabel(invokePoint), call(iteratorMethod, InstructionUtils.loadVar(listVar)), pop(), // discard results of call
            returnDummy(Type.VOID_TYPE));

    methodNode.instructions = methodInsnList;

    // attemp to write out class -- since we're branching like this it should call getCommonSuperClass() with the types specified in
    // each of the switch cases. getCommonsuperClass() is the logic we explictly override and are testing. createJarAndLoad uses
    // simpleclasswriter
    try (URLClassLoader cl = createJarAndLoad(classNode)) {
        Object obj = cl.loadClass("SimpleStub").newInstance();

        // there should not be any verifer errors here
        MethodUtils.invokeMethod(obj, "fillMeIn", 0);
        MethodUtils.invokeMethod(obj, "fillMeIn", 1);
        MethodUtils.invokeMethod(obj, "fillMeIn", 2);
    }
}

From source file:com.carmanconsulting.cassidy.util.CassidyUtils.java

public static boolean isInstantiable(Class<?> type) {
    return ConstructorUtils.getAccessibleConstructor(type) != null;
}

From source file:therian.operator.convert.CopyingConverter.java

private static <T> Constructor<T> requireDefaultConstructor(Class<T> type) {
    return Validate.notNull(ConstructorUtils.getAccessibleConstructor(type),
            "Could not find default constructor for %s", type);
}