Search in sources :

Example 46 with ConstructorNode

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

the class AntlrParserPlugin method constructorDef.

protected void constructorDef(AST constructorDef) {
    List<AnnotationNode> annotations = new ArrayList<AnnotationNode>();
    AST node = constructorDef.getFirstChild();
    int modifiers = Opcodes.ACC_PUBLIC;
    if (isType(MODIFIERS, node)) {
        modifiers = modifiers(node, annotations, modifiers);
        checkNoInvalidModifier(constructorDef, "Constructor", modifiers, Opcodes.ACC_STATIC, "static");
        checkNoInvalidModifier(constructorDef, "Constructor", modifiers, Opcodes.ACC_FINAL, "final");
        checkNoInvalidModifier(constructorDef, "Constructor", modifiers, Opcodes.ACC_ABSTRACT, "abstract");
        checkNoInvalidModifier(constructorDef, "Constructor", modifiers, Opcodes.ACC_NATIVE, "native");
        node = node.getNextSibling();
    }
    assertNodeType(PARAMETERS, node);
    Parameter[] parameters = parameters(node);
    if (parameters == null)
        parameters = Parameter.EMPTY_ARRAY;
    node = node.getNextSibling();
    ClassNode[] exceptions = ClassNode.EMPTY_ARRAY;
    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();
    }
    assertNodeType(SLIST, node);
    boolean syntheticPublic = ((modifiers & Opcodes.ACC_SYNTHETIC) != 0);
    modifiers &= ~Opcodes.ACC_SYNTHETIC;
    ConstructorNode constructorNode = classNode.addConstructor(modifiers, parameters, exceptions, null);
    MethodNode oldMethod = methodNode;
    methodNode = constructorNode;
    Statement code = statementList(node);
    methodNode = oldMethod;
    constructorNode.setCode(code);
    constructorNode.setSyntheticPublic(syntheticPublic);
    constructorNode.addAnnotations(annotations);
    configureAST(constructorNode, constructorDef);
}
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) ConstructorNode(org.codehaus.groovy.ast.ConstructorNode) Parameter(org.codehaus.groovy.ast.Parameter)

Example 47 with ConstructorNode

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

the class StaticTypeCheckingVisitor method typeCheckMapConstructor.

protected MethodNode typeCheckMapConstructor(final ConstructorCallExpression call, final ClassNode receiver, final Expression arguments) {
    MethodNode node = null;
    if (arguments instanceof TupleExpression) {
        TupleExpression texp = (TupleExpression) arguments;
        List<Expression> expressions = texp.getExpressions();
        if (expressions.size() == 1) {
            Expression expression = expressions.get(0);
            if (expression instanceof MapExpression) {
                MapExpression argList = (MapExpression) expression;
                checkGroovyConstructorMap(call, receiver, argList);
                node = new ConstructorNode(Opcodes.ACC_PUBLIC, new Parameter[] { new Parameter(MAP_TYPE, "map") }, ClassNode.EMPTY_ARRAY, GENERATED_EMPTY_STATEMENT);
                node.setDeclaringClass(receiver);
            }
        }
    }
    return node;
}
Also used : MethodNode(org.codehaus.groovy.ast.MethodNode) ConstructorNode(org.codehaus.groovy.ast.ConstructorNode) Parameter(org.codehaus.groovy.ast.Parameter)

Example 48 with ConstructorNode

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

the class StaticTypeCheckingVisitor method checkGroovyStyleConstructor.

/**
     * Checks that a constructor style expression is valid regarding the number of arguments and the argument types.
     *
     * @param node      the class node for which we will try to find a matching constructor
     * @param arguments the constructor arguments
     */
protected MethodNode checkGroovyStyleConstructor(final ClassNode node, final ClassNode[] arguments, final ASTNode source) {
    if (node.equals(ClassHelper.OBJECT_TYPE) || node.equals(ClassHelper.DYNAMIC_TYPE)) {
        // in that case, we are facing a list constructor assigned to a def or object
        return null;
    }
    List<ConstructorNode> constructors = node.getDeclaredConstructors();
    if (constructors.isEmpty() && arguments.length == 0) {
        return null;
    }
    List<MethodNode> constructorList = findMethod(node, "<init>", arguments);
    if (constructorList.isEmpty()) {
        if (isBeingCompiled(node) && arguments.length == 1 && LINKEDHASHMAP_CLASSNODE.equals(arguments[0])) {
            // there will be a default hash map constructor added later
            ConstructorNode cn = new ConstructorNode(Opcodes.ACC_PUBLIC, new Parameter[] { new Parameter(LINKEDHASHMAP_CLASSNODE, "args") }, ClassNode.EMPTY_ARRAY, EmptyStatement.INSTANCE);
            return cn;
        } else {
            addStaticTypeError("No matching constructor found: " + node + toMethodParametersString("<init>", arguments), source);
            return null;
        }
    } else if (constructorList.size() > 1) {
        addStaticTypeError("Ambiguous constructor call " + node + toMethodParametersString("<init>", arguments), source);
        return null;
    }
    return constructorList.get(0);
}
Also used : MethodNode(org.codehaus.groovy.ast.MethodNode) ConstructorNode(org.codehaus.groovy.ast.ConstructorNode) Parameter(org.codehaus.groovy.ast.Parameter)

Example 49 with ConstructorNode

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

the class StaticInvocationWriter method writeSpecialConstructorCall.

@Override
public void writeSpecialConstructorCall(final ConstructorCallExpression call) {
    MethodNode mn = call.getNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET);
    if (mn == null) {
        super.writeSpecialConstructorCall(call);
        return;
    }
    controller.getCompileStack().pushInSpecialConstructorCall();
    ConstructorNode cn;
    if (mn instanceof ConstructorNode) {
        cn = (ConstructorNode) mn;
    } else {
        cn = new ConstructorNode(mn.getModifiers(), mn.getParameters(), mn.getExceptions(), mn.getCode());
        cn.setDeclaringClass(mn.getDeclaringClass());
    }
    // load "this"
    controller.getMethodVisitor().visitVarInsn(ALOAD, 0);
    String ownerDescriptor = BytecodeHelper.getClassInternalName(cn.getDeclaringClass());
    TupleExpression args = makeArgumentList(call.getArguments());
    int before = controller.getOperandStack().getStackLength();
    loadArguments(args.getExpressions(), cn.getParameters());
    finnishConstructorCall(cn, ownerDescriptor, controller.getOperandStack().getStackLength() - before);
    // on a special call, there's no object on stack
    controller.getOperandStack().remove(1);
    controller.getCompileStack().pop();
}
Also used : ExtensionMethodNode(org.codehaus.groovy.transform.stc.ExtensionMethodNode) MethodNode(org.codehaus.groovy.ast.MethodNode) ConstructorNode(org.codehaus.groovy.ast.ConstructorNode)

Example 50 with ConstructorNode

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

the class MapConstructorASTTransformation method createConstructor.

public static void createConstructor(ClassNode cNode, boolean includeFields, boolean includeProperties, boolean includeSuperProperties, boolean useSetters, List<String> excludes, List<String> includes, ClosureExpression pre, ClosureExpression post, SourceUnit source, boolean allNames) {
    List<ConstructorNode> constructors = cNode.getDeclaredConstructors();
    boolean foundEmpty = constructors.size() == 1 && constructors.get(0).getFirstStatement() == null;
    // HACK: JavaStubGenerator could have snuck in a constructor we don't want
    if (foundEmpty)
        constructors.remove(0);
    List<FieldNode> superList = new ArrayList<FieldNode>();
    if (includeSuperProperties) {
        superList.addAll(getSuperPropertyFields(cNode.getSuperClass()));
    }
    List<FieldNode> list = new ArrayList<FieldNode>();
    if (includeProperties) {
        list.addAll(getInstancePropertyFields(cNode));
    }
    if (includeFields) {
        list.addAll(getInstanceNonPropertyFields(cNode));
    }
    Parameter map = param(MAP_TYPE, "args");
    final BlockStatement body = new BlockStatement();
    ClassCodeExpressionTransformer transformer = makeMapTypedArgsTransformer();
    if (pre != null) {
        ClosureExpression transformed = (ClosureExpression) transformer.transform(pre);
        copyStatementsWithSuperAdjustment(transformed, body);
    }
    final BlockStatement inner = new BlockStatement();
    for (FieldNode fNode : superList) {
        String name = fNode.getName();
        if (shouldSkip(name, excludes, includes, allNames))
            continue;
        assignField(useSetters, map, inner, name);
    }
    for (FieldNode fNode : list) {
        String name = fNode.getName();
        if (shouldSkip(name, excludes, includes, allNames))
            continue;
        assignField(useSetters, map, inner, name);
    }
    body.addStatement(ifS(notNullX(varX("args")), inner));
    if (post != null) {
        ClosureExpression transformed = (ClosureExpression) transformer.transform(post);
        body.addStatement(transformed.getCode());
    }
    cNode.addConstructor(new ConstructorNode(ACC_PUBLIC, params(map), ClassNode.EMPTY_ARRAY, body));
}
Also used : FieldNode(org.codehaus.groovy.ast.FieldNode) ConstructorNode(org.codehaus.groovy.ast.ConstructorNode) ArrayList(java.util.ArrayList) Parameter(org.codehaus.groovy.ast.Parameter) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassCodeExpressionTransformer(org.codehaus.groovy.ast.ClassCodeExpressionTransformer)

Aggregations

ConstructorNode (org.codehaus.groovy.ast.ConstructorNode)59 Parameter (org.codehaus.groovy.ast.Parameter)30 ClassNode (org.codehaus.groovy.ast.ClassNode)28 MethodNode (org.codehaus.groovy.ast.MethodNode)21 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)19 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)19 FieldNode (org.codehaus.groovy.ast.FieldNode)13 Expression (org.codehaus.groovy.ast.expr.Expression)10 ArrayList (java.util.ArrayList)8 Statement (org.codehaus.groovy.ast.stmt.Statement)8 PropertyNode (org.codehaus.groovy.ast.PropertyNode)6 MapExpression (org.codehaus.groovy.ast.expr.MapExpression)6 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)6 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)6 MethodVisitor (org.objectweb.asm.MethodVisitor)6 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)5 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)5 EmptyStatement (org.codehaus.groovy.ast.stmt.EmptyStatement)5 ThrowStatement (org.codehaus.groovy.ast.stmt.ThrowStatement)5 LinkedList (java.util.LinkedList)4