Search in sources :

Example 71 with MethodNode

use of org.codehaus.groovy.ast.MethodNode in project groovy-core by groovy.

the class PackageScopeASTTransformation method visit.

public void visit(ASTNode[] nodes, SourceUnit source) {
    init(nodes, source);
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode node = (AnnotationNode) nodes[0];
    boolean legacyMode = LEGACY_TYPE_NAME.equals(node.getClassNode().getName());
    if (!MY_TYPE.equals(node.getClassNode()) && !legacyMode)
        return;
    Expression value = node.getMember("value");
    if (parent instanceof ClassNode) {
        List<groovy.transform.PackageScopeTarget> targets;
        if (value == null)
            targets = Collections.singletonList(legacyMode ? PackageScopeTarget.FIELDS : PackageScopeTarget.CLASS);
        else
            targets = determineTargets(value);
        visitClassNode((ClassNode) parent, targets);
        parent.getAnnotations();
    } else {
        if (value != null) {
            addError("Error during " + MY_TYPE_NAME + " processing: " + TARGET_CLASS_NAME + " only allowed at class level.", parent);
            return;
        }
        if (parent instanceof MethodNode) {
            visitMethodNode((MethodNode) parent);
        } else if (parent instanceof FieldNode) {
            visitFieldNode((FieldNode) parent);
        }
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) PackageScopeTarget(groovy.transform.PackageScopeTarget) MethodNode(org.codehaus.groovy.ast.MethodNode) FieldNode(org.codehaus.groovy.ast.FieldNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) Expression(org.codehaus.groovy.ast.expr.Expression) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode)

Example 72 with MethodNode

use of org.codehaus.groovy.ast.MethodNode in project groovy-core by groovy.

the class SortableASTTransformation method createComparatorFor.

private static void createComparatorFor(ClassNode classNode, PropertyNode property) {
    String propName = property.getName();
    String className = classNode.getName() + "$" + StringGroovyMethods.capitalize(propName) + "Comparator";
    ClassNode superClass = makeClassSafeWithGenerics(AbstractComparator.class, classNode);
    InnerClassNode cmpClass = new InnerClassNode(classNode, className, ACC_PRIVATE | ACC_STATIC, superClass);
    classNode.getModule().addClass(cmpClass);
    cmpClass.addMethod(new MethodNode("compare", ACC_PUBLIC, ClassHelper.int_TYPE, params(param(newClass(classNode), ARG0), param(newClass(classNode), ARG1)), ClassNode.EMPTY_ARRAY, createCompareMethodBody(property)));
    String fieldName = "this$" + StringGroovyMethods.capitalize(propName) + "Comparator";
    // private final Comparator this$<property>Comparator = new <type>$<property>Comparator();
    FieldNode cmpField = classNode.addField(fieldName, ACC_STATIC | ACC_FINAL | ACC_PRIVATE | ACC_SYNTHETIC, COMPARATOR_TYPE, ctorX(cmpClass));
    classNode.addMethod(new MethodNode("comparatorBy" + StringGroovyMethods.capitalize(propName), ACC_PUBLIC | ACC_STATIC, COMPARATOR_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, returnS(fieldX(cmpField))));
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) FieldNode(org.codehaus.groovy.ast.FieldNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode)

Example 73 with MethodNode

use of org.codehaus.groovy.ast.MethodNode in project groovy-core by groovy.

the class ClassCompletionVerifierTest method checkDetectsDuplicateMethods.

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

Example 74 with MethodNode

use of org.codehaus.groovy.ast.MethodNode in project groovy-core by groovy.

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 75 with MethodNode

use of org.codehaus.groovy.ast.MethodNode in project groovy-core by groovy.

the class ClassCompletionVerifierTest method testDetectsCorrectMethodModifiersInClass.

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

Aggregations

MethodNode (org.codehaus.groovy.ast.MethodNode)294 ClassNode (org.codehaus.groovy.ast.ClassNode)193 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)94 Parameter (org.codehaus.groovy.ast.Parameter)79 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)65 FieldNode (org.codehaus.groovy.ast.FieldNode)57 LinkedList (java.util.LinkedList)50 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)50 Expression (org.codehaus.groovy.ast.expr.Expression)49 LowestUpperBoundClassNode (org.codehaus.groovy.ast.tools.WideningCategories.LowestUpperBoundClassNode)47 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)43 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)39 ArrayList (java.util.ArrayList)36 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)36 Statement (org.codehaus.groovy.ast.stmt.Statement)34 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)33 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)30 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)29 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)27 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)27