Java Code Examples for org.apache.commons.lang3.reflect.ConstructorUtils#getAccessibleConstructor()
The following examples show how to use
org.apache.commons.lang3.reflect.ConstructorUtils#getAccessibleConstructor() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractObjectToStringConverter.java From yarg with Apache License 2.0 | 6 votes |
protected Object convertFromStringUnresolved(Class<?> parameterClass, String paramValueStr) { try { Constructor constructor = ConstructorUtils.getAccessibleConstructor(parameterClass, String.class); if (constructor != null) { return constructor.newInstance(paramValueStr); } else { Method valueOf = MethodUtils.getAccessibleMethod(parameterClass, "valueOf", String.class); if (valueOf != null) { return valueOf.invoke(null, paramValueStr); } } } catch (ReflectiveOperationException e) { throw new ReportingException( String.format("Could not instantiate object with class [%s] from [%s] string.", parameterClass.getCanonicalName(), paramValueStr)); } return paramValueStr; }
Example 2
Source File: SimpleClassWriterTest.java From coroutines with GNU Lesser General Public License v3.0 | 4 votes |
@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); /** * 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), throwRuntimeException("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, GenericGenerators.loadVar(listVar)), pop(), // discard results of call returnVoid() ); 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); } }
Example 3
Source File: SimpleVerifierTest.java From coroutines with GNU Lesser General Public License v3.0 | 4 votes |
@Test public void testVertificationWithoutUsingClassLoader() 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); VariableTable.Variable testArg = varTable.getArgument(1); VariableTable.Variable listVar = varTable.acquireExtra(iterableType); /** * 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), throwRuntimeException("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, loadVar(listVar)), pop(), // discard results of call returnVoid() ); methodNode.instructions = methodInsnList; // write out class and read it back in again so maxes and frames can be properly computed for frame analyzer SimpleClassWriter writer = new SimpleClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS, classRepo); classNode.accept(writer); byte[] data = writer.toByteArray(); ClassReader cr = new ClassReader(data); classNode = new SimpleClassNode(); cr.accept(classNode, 0); methodNode = classNode.methods.get(1); // analyze Frame<BasicValue>[] frames; try { frames = new Analyzer<>(new SimpleVerifier(classRepo)).analyze(classNode.name, methodNode); } catch (AnalyzerException ae) { throw new IllegalArgumentException("Analyzer failed to analyze method", ae); } // get last frame Frame frame = frames[frames.length - 1]; BasicValue basicValue = (BasicValue) frame.getLocal(listVar.getIndex()); // ensure that that the local variable for the collection we created in the switch blocks is an abstract type assertEquals("java/util/AbstractCollection", basicValue.getType().getInternalName()); }