Search in sources :

Example 1 with MethodNode

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

the class BindableASTTransformation method wrapSetterMethod.

/*
     * Wrap an existing setter.
     */
private static void wrapSetterMethod(ClassNode classNode, String propertyName) {
    String getterName = "get" + MetaClassHelper.capitalize(propertyName);
    MethodNode setter = classNode.getSetterMethod("set" + MetaClassHelper.capitalize(propertyName));
    if (setter != null) {
        // Get the existing code block
        Statement code = setter.getCode();
        Expression oldValue = varX("$oldValue");
        Expression newValue = varX("$newValue");
        BlockStatement block = new BlockStatement();
        // create a local variable to hold the old value from the getter
        block.addStatement(declS(oldValue, callThisX(getterName)));
        // call the existing block, which will presumably set the value properly
        block.addStatement(code);
        // get the new value to emit in the event
        block.addStatement(declS(newValue, callThisX(getterName)));
        // add the firePropertyChange method call
        block.addStatement(stmt(callThisX("firePropertyChange", args(constX(propertyName), oldValue, newValue))));
        // replace the existing code block with our new one
        setter.setCode(block);
    }
}
Also used : MethodNode(org.codehaus.groovy.ast.MethodNode) Expression(org.codehaus.groovy.ast.expr.Expression) Statement(org.codehaus.groovy.ast.stmt.Statement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 2 with MethodNode

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

the class VetoableASTTransformation method wrapSetterMethod.

/**
     * Wrap an existing setter.
     */
private static void wrapSetterMethod(ClassNode classNode, boolean bindable, String propertyName) {
    String getterName = "get" + MetaClassHelper.capitalize(propertyName);
    MethodNode setter = classNode.getSetterMethod("set" + MetaClassHelper.capitalize(propertyName));
    if (setter != null) {
        // Get the existing code block
        Statement code = setter.getCode();
        Expression oldValue = varX("$oldValue");
        Expression newValue = varX("$newValue");
        Expression proposedValue = varX(setter.getParameters()[0].getName());
        BlockStatement block = new BlockStatement();
        // create a local variable to hold the old value from the getter
        block.addStatement(declS(oldValue, callThisX(getterName)));
        // add the fireVetoableChange method call
        block.addStatement(stmt(callThisX("fireVetoableChange", args(constX(propertyName), oldValue, proposedValue))));
        // call the existing block, which will presumably set the value properly
        block.addStatement(code);
        if (bindable) {
            // get the new value to emit in the event
            block.addStatement(declS(newValue, callThisX(getterName)));
            // add the firePropertyChange method call
            block.addStatement(stmt(callThisX("firePropertyChange", args(constX(propertyName), oldValue, newValue))));
        }
        // replace the existing code block with our new one
        setter.setCode(block);
    }
}
Also used : MethodNode(org.codehaus.groovy.ast.MethodNode) Expression(org.codehaus.groovy.ast.expr.Expression) Statement(org.codehaus.groovy.ast.stmt.Statement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 3 with MethodNode

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

the class DefaultStrategy method createBuildMethod.

private static MethodNode createBuildMethod(AnnotationNode anno, ClassNode buildee, List<FieldNode> fields) {
    String buildMethodName = getMemberStringValue(anno, "buildMethodName", "build");
    final BlockStatement body = new BlockStatement();
    body.addStatement(returnS(initializeInstance(buildee, fields, body)));
    return new MethodNode(buildMethodName, ACC_PUBLIC, newClass(buildee), NO_PARAMS, NO_EXCEPTIONS, body);
}
Also used : MethodNode(org.codehaus.groovy.ast.MethodNode) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 4 with MethodNode

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

the class AntlrParserPlugin method methodDef.

protected void methodDef(AST methodDef) {
    MethodNode oldNode = methodNode;
    List<AnnotationNode> annotations = new ArrayList<AnnotationNode>();
    AST node = methodDef.getFirstChild();
    GenericsType[] generics = null;
    if (isType(TYPE_PARAMETERS, node)) {
        generics = makeGenericsType(node);
        node = node.getNextSibling();
    }
    int modifiers = Opcodes.ACC_PUBLIC;
    if (isType(MODIFIERS, node)) {
        modifiers = modifiers(node, annotations, modifiers);
        checkNoInvalidModifier(methodDef, "Method", modifiers, Opcodes.ACC_VOLATILE, "volatile");
        node = node.getNextSibling();
    }
    if (isAnInterface()) {
        modifiers |= Opcodes.ACC_ABSTRACT;
    }
    ClassNode returnType = null;
    if (isType(TYPE, node)) {
        returnType = makeTypeWithArguments(node);
        node = node.getNextSibling();
    }
    String name = identifier(node);
    if (classNode != null && !classNode.isAnnotationDefinition()) {
        if (classNode.getNameWithoutPackage().equals(name)) {
            if (isAnInterface()) {
                throw new ASTRuntimeException(methodDef, "Constructor not permitted within an interface.");
            }
            throw new ASTRuntimeException(methodDef, "Invalid constructor format. Remove '" + returnType.getName() + "' as the return type if you want a constructor, or use a different name if you want a method.");
        }
    }
    node = node.getNextSibling();
    Parameter[] parameters = Parameter.EMPTY_ARRAY;
    ClassNode[] exceptions = ClassNode.EMPTY_ARRAY;
    if (classNode == null || !classNode.isAnnotationDefinition()) {
        assertNodeType(PARAMETERS, node);
        parameters = parameters(node);
        if (parameters == null)
            parameters = Parameter.EMPTY_ARRAY;
        node = node.getNextSibling();
        if (isType(LITERAL_throws, node)) {
            AST throwsNode = node.getFirstChild();
            List<ClassNode> exceptionList = new ArrayList<ClassNode>();
            throwsList(throwsNode, exceptionList);
            exceptions = exceptionList.toArray(exceptions);
            node = node.getNextSibling();
        }
    }
    boolean hasAnnotationDefault = false;
    Statement code = null;
    boolean syntheticPublic = ((modifiers & Opcodes.ACC_SYNTHETIC) != 0);
    modifiers &= ~Opcodes.ACC_SYNTHETIC;
    methodNode = new MethodNode(name, modifiers, returnType, parameters, exceptions, code);
    if ((modifiers & Opcodes.ACC_ABSTRACT) == 0) {
        if (node == null) {
            throw new ASTRuntimeException(methodDef, "You defined a method without body. Try adding a body, or declare it abstract.");
        }
        assertNodeType(SLIST, node);
        code = statementList(node);
    } else if (node != null && classNode.isAnnotationDefinition()) {
        code = statement(node);
        hasAnnotationDefault = true;
    } else if ((modifiers & Opcodes.ACC_ABSTRACT) > 0) {
        if (node != null) {
            throw new ASTRuntimeException(methodDef, "Abstract methods do not define a body.");
        }
    }
    methodNode.setCode(code);
    methodNode.addAnnotations(annotations);
    methodNode.setGenericsTypes(generics);
    methodNode.setAnnotationDefault(hasAnnotationDefault);
    methodNode.setSyntheticPublic(syntheticPublic);
    configureAST(methodNode, methodDef);
    if (classNode != null) {
        classNode.addMethod(methodNode);
    } else {
        output.addMethod(methodNode);
    }
    methodNode = oldNode;
}
Also used : EnumConstantClassNode(org.codehaus.groovy.ast.EnumConstantClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) AST(antlr.collections.AST) CaseStatement(org.codehaus.groovy.ast.stmt.CaseStatement) ForStatement(org.codehaus.groovy.ast.stmt.ForStatement) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) AssertStatement(org.codehaus.groovy.ast.stmt.AssertStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) WhileStatement(org.codehaus.groovy.ast.stmt.WhileStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) ContinueStatement(org.codehaus.groovy.ast.stmt.ContinueStatement) BreakStatement(org.codehaus.groovy.ast.stmt.BreakStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) SynchronizedStatement(org.codehaus.groovy.ast.stmt.SynchronizedStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) SwitchStatement(org.codehaus.groovy.ast.stmt.SwitchStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) ArrayList(java.util.ArrayList) MethodNode(org.codehaus.groovy.ast.MethodNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) GenericsType(org.codehaus.groovy.ast.GenericsType) Parameter(org.codehaus.groovy.ast.Parameter)

Example 5 with MethodNode

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

the class ClassNodeUtils method getDeclaredMethodMapsFromInterfaces.

public static Map<String, MethodNode> getDeclaredMethodMapsFromInterfaces(ClassNode classNode) {
    Map<String, MethodNode> result = new HashMap<String, MethodNode>();
    ClassNode[] interfaces = classNode.getInterfaces();
    for (ClassNode iface : interfaces) {
        result.putAll(iface.getDeclaredMethodsMap());
    }
    return result;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) HashMap(java.util.HashMap)

Aggregations

MethodNode (org.codehaus.groovy.ast.MethodNode)430 ClassNode (org.codehaus.groovy.ast.ClassNode)274 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)144 Parameter (org.codehaus.groovy.ast.Parameter)140 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)112 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)108 Expression (org.codehaus.groovy.ast.expr.Expression)105 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)98 FieldNode (org.codehaus.groovy.ast.FieldNode)89 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)85 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)79 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)75 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)71 ArrayList (java.util.ArrayList)69 LinkedList (java.util.LinkedList)67 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)67 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)67 Statement (org.codehaus.groovy.ast.stmt.Statement)64 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)60 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)52