/** * Creates a ClassNode using a given class. * Unlike make(String) this method will not use the cache * to create the ClassNode. This means the ClassNode created * from this method using the same name will have a different * reference * * @param name of the class the ClassNode is representing * @see #make(String) */ public static ClassNode makeWithoutCaching(String name) { ClassNode cn = new ClassNode(name, Opcodes.ACC_PUBLIC, OBJECT_TYPE); cn.isPrimaryNode = false; return cn; }
public static ClassNode makeCached(Class c) { final SoftReference<ClassNode> classNodeSoftReference = ClassHelperCache.classCache.get(c); ClassNode classNode; if (classNodeSoftReference == null || (classNode = classNodeSoftReference.get()) == null) { classNode = new ClassNode(c); ClassHelperCache.classCache.put(c, new SoftReference<ClassNode>(classNode)); VMPluginFactory.getPlugin().setAdditionalClassInformation(classNode); } return classNode; }
public void testIsDynamicTypedExplicitObject() { VariableExpression intExpression = new VariableExpression("foo", new ClassNode(Object.class)); assertFalse(intExpression.isDynamicTyped()); }
public ClassNode getPlainNodeReference() { if (ClassHelper.isPrimitiveType(this)) return this; ClassNode n = new ClassNode(name, modifiers, superClass,null,null); n.isPrimaryNode = false; n.setRedirect(redirect()); if (isArray()) { n.componentType = redirect().getComponentType(); } return n; }
private void checkVisitErrors(String name, int modifiers, boolean expectedToFail) { ClassNode node = new ClassNode(name, modifiers, ClassHelper.OBJECT_TYPE); verifier.visitClass(node); assertTrue(source.getErrorCollector().hasErrors() == expectedToFail); }
public void testPackageName() { assertEquals("Package", null, classNode.getPackageName()); ClassNode packageNode = new ClassNode("com.acme.Foo", ACC_PUBLIC, ClassHelper.OBJECT_TYPE); assertEquals("Package", "com.acme", packageNode.getPackageName()); } }
public static ClassNode makeEnumNode(String name, int modifiers, ClassNode[] interfaces, ClassNode outerClass) { modifiers = modifiers | Opcodes.ACC_FINAL | Opcodes.ACC_ENUM; ClassNode enumClass; if (outerClass==null) { enumClass = new ClassNode(name,modifiers,null,interfaces,MixinNode.EMPTY_ARRAY); } else { name = outerClass.getName() + "$" + name; modifiers |= Opcodes.ACC_STATIC; enumClass = new InnerClassNode(outerClass,name,modifiers,null,interfaces,MixinNode.EMPTY_ARRAY); } // set super class and generics info // "enum X" -> class X extends Enum<X> GenericsType gt = new GenericsType(enumClass); ClassNode superClass = ClassHelper.makeWithoutCaching("java.lang.Enum"); superClass.setGenericsTypes(new GenericsType[]{gt}); enumClass.setSuperClass(superClass); superClass.setRedirect(ClassHelper.Enum_Type); return enumClass; }
private TryCatchStatement surroundSpecialTryCatch(Expression executeAndVerify) { final TryCatchStatement tryCatchStatement = new TryCatchStatement( new ExpressionStatement(executeAndVerify), EmptyStatement.INSTANCE ); tryCatchStatement.addCatch( new CatchStatement( new Parameter(new ClassNode(Throwable.class), "throwable"), new ExpressionStatement( AstUtil.createDirectMethodCall( new ClassExpression(resources.getAstNodeCache().SpockRuntime), resources.getAstNodeCache().SpockRuntime_GroupConditionFailedWithException, new ArgumentListExpression(Arrays.<Expression>asList( new VariableExpression(SpockNames.ERROR_COLLECTOR), new VariableExpression("throwable") // throwable )) ) ) ) ); return tryCatchStatement; }
public void testProperties() throws Exception { ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC + ACC_SUPER, ClassHelper.OBJECT_TYPE); classNode.addProperty(new PropertyNode("bar", ACC_PUBLIC, ClassHelper.STRING_TYPE, classNode, null, null, null)); Class fooClass = loadClass(classNode); assertTrue("Loaded a new class", fooClass != null); Object bean = fooClass.newInstance(); assertTrue("Managed to create bean", bean != null); assertField(fooClass, "bar", 0, ClassHelper.STRING_TYPE); assertGetProperty(bean, "bar", null); assertSetProperty(bean, "bar", "newValue"); }
public void testNonLoop() throws Exception { ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, ClassHelper.OBJECT_TYPE); classNode.addConstructor(new ConstructorNode(ACC_PUBLIC, null)); Parameter[] parameters = {new Parameter(ClassHelper.OBJECT_TYPE, "coll")}; Statement statement = createPrintlnStatement(new VariableExpression("coll")); classNode.addMethod(new MethodNode("oneParamDemo", ACC_PUBLIC, ClassHelper.VOID_TYPE, parameters, ClassNode.EMPTY_ARRAY, statement)); Class fooClass = loadClass(classNode); assertTrue("Loaded a new class", fooClass != null); Object bean = fooClass.newInstance(); assertTrue("Managed to create bean", bean != null); System.out.println("################ Now about to invoke a method without looping"); Object value = new Integer(10000); try { InvokerHelper.invokeMethod(bean, "oneParamDemo", new Object[]{value}); } catch (InvokerInvocationException e) { System.out.println("Caught: " + e.getCause()); e.getCause().printStackTrace(); fail("Should not have thrown an exception"); } System.out.println("################ Done"); }
private void checkDetectsDuplicateMethods(int modifiers, String expectedErrorMessage, Parameter[] params) { ClassNode node = new ClassNode("zzz", modifiers, ClassHelper.OBJECT_TYPE); node.addMethod(new MethodNode("xxx", ACC_PUBLIC, ClassHelper.OBJECT_TYPE, params, ClassNode.EMPTY_ARRAY, null)); node.addMethod(new MethodNode("xxx", ACC_PUBLIC, ClassHelper.OBJECT_TYPE, params, ClassNode.EMPTY_ARRAY, null)); verifier.visitClass(node); checkErrorCount(2); checkErrorMessage(expectedErrorMessage); }
public void testLoop() throws Exception { ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, ClassHelper.OBJECT_TYPE); classNode.addConstructor(new ConstructorNode(ACC_PUBLIC, null)); Parameter[] parameters = {new Parameter(ClassHelper.OBJECT_TYPE.makeArray(), "coll")}; Statement loopStatement = createPrintlnStatement(new VariableExpression("i")); ForStatement statement = new ForStatement(new Parameter(ClassHelper.OBJECT_TYPE, "i"), new VariableExpression("coll"), loopStatement); classNode.addMethod(new MethodNode("iterateDemo", ACC_PUBLIC, ClassHelper.VOID_TYPE, parameters, ClassNode.EMPTY_ARRAY, statement)); Class fooClass = loadClass(classNode); assertTrue("Loaded a new class", fooClass != null); Object bean = fooClass.newInstance(); assertTrue("Managed to create bean", bean != null); System.out.println("################ Now about to invoke a method with looping"); Object[] array = {new Integer(1234), "abc", "def"}; try { InvokerHelper.invokeMethod(bean, "iterateDemo", new Object[]{array}); } catch (InvokerInvocationException e) { System.out.println("Caught: " + e.getCause()); e.getCause().printStackTrace(); fail("Should not have thrown an exception"); } System.out.println("################ Done"); }
public void testFields() throws Exception { ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, ClassHelper.OBJECT_TYPE); classNode.addField("x", ACC_PUBLIC, ClassHelper.OBJECT_TYPE, null); classNode.addField("y", ACC_PUBLIC, ClassHelper.Integer_TYPE, null); classNode.addField("z", ACC_PRIVATE, ClassHelper.STRING_TYPE, null); Class fooClass = loadClass(classNode); assertTrue("Loaded a new class", fooClass != null); assertField(fooClass, "x", Modifier.PUBLIC, ClassHelper.OBJECT_TYPE); assertField(fooClass, "y", Modifier.PUBLIC, ClassHelper.Integer_TYPE); assertField(fooClass, "z", Modifier.PRIVATE, ClassHelper.STRING_TYPE); }
private TryCatchStatement surroundWithTryCatch(Expression condition, Expression message, Expression executeAndVerify) { final TryCatchStatement tryCatchStatement = new TryCatchStatement( new ExpressionStatement(executeAndVerify), EmptyStatement.INSTANCE ); tryCatchStatement.addCatch( new CatchStatement( new Parameter(new ClassNode(Throwable.class), "throwable"), new ExpressionStatement( AstUtil.createDirectMethodCall( new ClassExpression(resources.getAstNodeCache().SpockRuntime), resources.getAstNodeCache().SpockRuntime_ConditionFailedWithException, new ArgumentListExpression(Arrays.asList( new VariableExpression(SpockNames.ERROR_COLLECTOR), new VariableExpression(SpockNames.VALUE_RECORDER), // recorder new ConstantExpression(resources.getSourceText(condition)), // text new ConstantExpression(condition.getLineNumber()), // line new ConstantExpression(condition.getColumnNumber()), // column message == null ? ConstantExpression.NULL : message, // message new VariableExpression("throwable") // throwable )) ) ) ) ); return tryCatchStatement; }
public void testInheritedProperties() throws Exception { ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC + ACC_SUPER, ClassHelper.make(DummyBean.class)); classNode.addProperty(new PropertyNode("bar", ACC_PUBLIC, ClassHelper.STRING_TYPE, classNode, null, null, null)); Class fooClass = loadClass(classNode); assertTrue("Loaded a new class", fooClass != null); Object bean = fooClass.newInstance(); assertTrue("Managed to create bean", bean != null); assertField(fooClass, "bar", 0, ClassHelper.STRING_TYPE); assertGetProperty(bean, "name", "James"); assertSetProperty(bean, "name", "Bob"); assertGetProperty(bean, "bar", null); assertSetProperty(bean, "bar", "newValue"); } }
public void testDetectsCorrectMethodModifiersInClass() throws Exception { // can't check volatile here as it doubles up with bridge ClassNode node = new ClassNode("zzz", ACC_PUBLIC, ClassHelper.OBJECT_TYPE); node.addMethod(new MethodNode("st", ACC_STRICT, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null)); node.addMethod(new MethodNode("na", ACC_NATIVE, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null)); node.addMethod(new MethodNode("sy", ACC_SYNCHRONIZED, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null)); addDummyConstructor(node); verifier.visitClass(node); checkErrorCount(0); }
public void testDetectsFinalAndStaticMethodsInInterface() throws Exception { ClassNode node = new ClassNode("zzz", ACC_ABSTRACT | ACC_INTERFACE, ClassHelper.OBJECT_TYPE); node.addMethod(new MethodNode("xxx", ACC_PUBLIC | ACC_FINAL, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null)); node.addMethod(new MethodNode("yyy", ACC_PUBLIC | ACC_STATIC, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null)); addDummyConstructor(node); verifier.visitClass(node); checkErrorCount(2); checkErrorMessage(EXPECTED_INTERFACE_FINAL_METHOD_ERROR_MESSAGE); checkErrorMessage(EXPECTED_INTERFACE_STATIC_METHOD_ERROR_MESSAGE); }
public void testDetectsIncorrectMethodModifiersInInterface() throws Exception { // can't check volatile here as it doubles up with bridge ClassNode node = new ClassNode("zzz", ACC_ABSTRACT | ACC_INTERFACE, ClassHelper.OBJECT_TYPE); node.addMethod(new MethodNode("st", ACC_STRICT, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null)); node.addMethod(new MethodNode("na", ACC_NATIVE, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null)); node.addMethod(new MethodNode("sy", ACC_SYNCHRONIZED, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null)); addDummyConstructor(node); verifier.visitClass(node); checkErrorCount(3); checkErrorMessage(EXPECTED_STRICT_METHOD_ERROR_MESSAGE); checkErrorMessage(EXPECTED_NATIVE_METHOD_ERROR_MESSAGE); checkErrorMessage(EXPECTED_SYNCHRONIZED_METHOD_ERROR_MESSAGE); }
public void testDetectsIncorrectMemberVisibilityInInterface() throws Exception { ClassNode node = new ClassNode("zzz", ACC_ABSTRACT | ACC_INTERFACE, ClassHelper.OBJECT_TYPE); node.addMethod(new MethodNode("prim", ACC_PRIVATE, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null)); node.addMethod(new MethodNode("prom", ACC_PROTECTED, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null)); node.addField("prif", ACC_PRIVATE, ClassHelper.OBJECT_TYPE, null); node.addField("prof", ACC_PROTECTED, ClassHelper.OBJECT_TYPE, null); addDummyConstructor(node); verifier.visitClass(node); checkErrorCount(4); checkErrorMessage(EXPECTED_PROTECTED_FIELD_ERROR_MESSAGE); checkErrorMessage(EXPECTED_PRIVATE_FIELD_ERROR_MESSAGE); checkErrorMessage(EXPECTED_PROTECTED_METHOD_ERROR_MESSAGE); checkErrorMessage(EXPECTED_PRIVATE_METHOD_ERROR_MESSAGE); }
public void testMethods() throws Exception { ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, ClassHelper.OBJECT_TYPE); classNode.addConstructor(new ConstructorNode(ACC_PUBLIC, null)); Statement statementA = new ReturnStatement(new ConstantExpression("calledA")); Statement statementB = new ReturnStatement(new ConstantExpression("calledB")); Statement emptyStatement = new BlockStatement(); classNode.addMethod(new MethodNode("a", ACC_PUBLIC, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, statementA)); classNode.addMethod(new MethodNode("b", ACC_PUBLIC, null, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, statementB)); classNode.addMethod(new MethodNode("noReturnMethodA", ACC_PUBLIC, null, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, emptyStatement)); classNode.addMethod(new MethodNode("noReturnMethodB", ACC_PUBLIC, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, emptyStatement)); classNode.addMethod(new MethodNode("c", ACC_PUBLIC, ClassHelper.VOID_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, emptyStatement)); Class fooClass = loadClass(classNode); assertTrue("Loaded a new class", fooClass != null); Object bean = fooClass.newInstance(); assertTrue("Created instance of class: " + bean, bean != null); assertCallMethod(bean, "a", "calledA"); assertCallMethod(bean, "b", "calledB"); assertCallMethod(bean, "noReturnMethodA", null); assertCallMethod(bean, "noReturnMethodB", null); assertCallMethod(bean, "c", null); }