Search in sources :

Example 96 with ClassNode

use of org.codehaus.groovy.ast.ClassNode in project groovy by apache.

the class PropertyTest method testProperties.

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");
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) PropertyNode(org.codehaus.groovy.ast.PropertyNode)

Example 97 with ClassNode

use of org.codehaus.groovy.ast.ClassNode in project groovy by apache.

the class ClassCompletionVerifierTest method testDetectsIncorrectMemberVisibilityInInterface.

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);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode)

Example 98 with ClassNode

use of org.codehaus.groovy.ast.ClassNode in project groovy by apache.

the class ClassCompletionVerifierTest method testDetectsFinalAndStaticMethodsInInterface.

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);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode)

Example 99 with ClassNode

use of org.codehaus.groovy.ast.ClassNode in project groovy by apache.

the class ClassCompletionVerifierTest method testDetectsIncorrectMethodModifiersInInterface.

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);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode)

Example 100 with ClassNode

use of org.codehaus.groovy.ast.ClassNode in project groovy by apache.

the class StaticImportVisitor method transformInlineConstants.

// resolve constant-looking expressions statically (do here as gets transformed away later)
private Expression transformInlineConstants(Expression exp) {
    if (exp instanceof PropertyExpression) {
        PropertyExpression pe = (PropertyExpression) exp;
        if (pe.getObjectExpression() instanceof ClassExpression) {
            ClassExpression ce = (ClassExpression) pe.getObjectExpression();
            ClassNode type = ce.getType();
            if (type.isEnum())
                return exp;
            Expression constant = findConstant(getField(type, pe.getPropertyAsString()));
            if (constant != null)
                return constant;
        }
    } else if (exp instanceof ListExpression) {
        ListExpression le = (ListExpression) exp;
        ListExpression result = new ListExpression();
        for (Expression e : le.getExpressions()) {
            result.addExpression(transformInlineConstants(e));
        }
        return result;
    }
    return exp;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) NamedArgumentListExpression(org.codehaus.groovy.ast.expr.NamedArgumentListExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) NamedArgumentListExpression(org.codehaus.groovy.ast.expr.NamedArgumentListExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression)

Aggregations

ClassNode (org.codehaus.groovy.ast.ClassNode)850 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)329 MethodNode (org.codehaus.groovy.ast.MethodNode)192 GenericsType (org.codehaus.groovy.ast.GenericsType)149 LowestUpperBoundClassNode (org.codehaus.groovy.ast.tools.WideningCategories.LowestUpperBoundClassNode)148 Expression (org.codehaus.groovy.ast.expr.Expression)140 Parameter (org.codehaus.groovy.ast.Parameter)133 FieldNode (org.codehaus.groovy.ast.FieldNode)125 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)114 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)112 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)107 ArrayList (java.util.ArrayList)95 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)95 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)86 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)73 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)71 LinkedList (java.util.LinkedList)70 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)69 AnnotatedNode (org.codehaus.groovy.ast.AnnotatedNode)59 MethodVisitor (org.objectweb.asm.MethodVisitor)57