Search in sources :

Example 46 with ConstantExpression

use of org.codehaus.groovy.ast.expr.ConstantExpression in project spring-boot by spring-projects.

the class ResolveDependencyCoordinatesTransformationTests method createGrabAnnotation.

private AnnotationNode createGrabAnnotation() {
    ClassNode classNode = new ClassNode(Grab.class);
    AnnotationNode annotationNode = new AnnotationNode(classNode);
    annotationNode.addMember("value", new ConstantExpression("spring-core"));
    return annotationNode;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression)

Example 47 with ConstantExpression

use of org.codehaus.groovy.ast.expr.ConstantExpression in project groovy by apache.

the class TraitComposer method createDelegatingForwarder.

private static Statement createDelegatingForwarder(final MethodNode forwarderMethod, final ClassNode next) {
    // generates --> next$Trait$Helper.method(this, arg1, arg2)
    TraitHelpersTuple helpers = Traits.findHelpers(next);
    ArgumentListExpression args = new ArgumentListExpression();
    args.addExpression(new VariableExpression("this"));
    Parameter[] forwarderMethodParameters = forwarderMethod.getParameters();
    for (final Parameter forwarderMethodParameter : forwarderMethodParameters) {
        args.addExpression(new VariableExpression(forwarderMethodParameter));
    }
    StaticMethodCallExpression delegateCall = new StaticMethodCallExpression(helpers.getHelper(), forwarderMethod.getName(), args);
    Statement result;
    if (ClassHelper.VOID_TYPE.equals(forwarderMethod.getReturnType())) {
        BlockStatement stmt = new BlockStatement();
        stmt.addStatement(new ExpressionStatement(delegateCall));
        stmt.addStatement(new ReturnStatement(new ConstantExpression(null)));
        result = stmt;
    } else {
        result = new ReturnStatement(delegateCall);
    }
    return result;
}
Also used : IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) Parameter(org.codehaus.groovy.ast.Parameter) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 48 with ConstantExpression

use of org.codehaus.groovy.ast.expr.ConstantExpression in project groovy by apache.

the class Verifier method addFieldInitialization.

protected void addFieldInitialization(List list, List staticList, FieldNode fieldNode, boolean isEnumClassNode, List initStmtsAfterEnumValuesInit, Set explicitStaticPropsInEnum) {
    Expression expression = fieldNode.getInitialExpression();
    if (expression != null) {
        final FieldExpression fe = new FieldExpression(fieldNode);
        if (fieldNode.getType().equals(ClassHelper.REFERENCE_TYPE) && ((fieldNode.getModifiers() & Opcodes.ACC_SYNTHETIC) != 0)) {
            fe.setUseReferenceDirectly(true);
        }
        ExpressionStatement statement = new ExpressionStatement(new BinaryExpression(fe, Token.newSymbol(Types.EQUAL, fieldNode.getLineNumber(), fieldNode.getColumnNumber()), expression));
        if (fieldNode.isStatic()) {
            // GROOVY-3311: pre-defined constants added by groovy compiler for numbers/characters should be
            // initialized first so that code dependent on it does not see their values as empty
            Expression initialValueExpression = fieldNode.getInitialValueExpression();
            if (initialValueExpression instanceof ConstantExpression) {
                ConstantExpression cexp = (ConstantExpression) initialValueExpression;
                cexp = transformToPrimitiveConstantIfPossible(cexp);
                if (fieldNode.isFinal() && ClassHelper.isStaticConstantInitializerType(cexp.getType()) && cexp.getType().equals(fieldNode.getType())) {
                    // GROOVY-5150: primitive type constants will be initialized directly
                    return;
                }
                staticList.add(0, statement);
            } else {
                staticList.add(statement);
            }
            // to avoid double initialization in case of several constructors
            fieldNode.setInitialValueExpression(null);
            /*
                 * If it is a statement for an explicitly declared static field inside an enum, store its
                 * reference. For enums, they need to be handled differently as such init statements should
                 * come after the enum values have been initialized inside <clinit> block. GROOVY-3161.
                 */
            if (isEnumClassNode && explicitStaticPropsInEnum.contains(fieldNode.getName())) {
                initStmtsAfterEnumValuesInit.add(statement);
            }
        } else {
            list.add(statement);
        }
    }
}
Also used : BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression)

Example 49 with ConstantExpression

use of org.codehaus.groovy.ast.expr.ConstantExpression in project groovy by apache.

the class Verifier method addGroovyObjectInterfaceAndMethods.

protected void addGroovyObjectInterfaceAndMethods(ClassNode node, final String classInternalName) {
    if (!node.isDerivedFromGroovyObject())
        node.addInterface(ClassHelper.make(GroovyObject.class));
    FieldNode metaClassField = getMetaClassField(node);
    if (!node.hasMethod("getMetaClass", Parameter.EMPTY_ARRAY)) {
        metaClassField = setMetaClassFieldIfNotExists(node, metaClassField);
        addMethod(node, !isAbstract(node.getModifiers()), "getMetaClass", ACC_PUBLIC, ClassHelper.METACLASS_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, new BytecodeSequence(new BytecodeInstruction() {

            public void visit(MethodVisitor mv) {
                Label nullLabel = new Label();
                /**
                             *  the code is:
                             *  if (this.metaClass==null) {
                             *      this.metaClass = this.$getStaticMetaClass
                             *      return this.metaClass
                             *  } else {
                             *      return this.metaClass
                             *  }
                             *  with the optimization that the result of the
                             *  first this.metaClass is duped on the operand
                             *  stack and reused for the return in the else part
                             */
                mv.visitVarInsn(ALOAD, 0);
                mv.visitFieldInsn(GETFIELD, classInternalName, "metaClass", "Lgroovy/lang/MetaClass;");
                mv.visitInsn(DUP);
                mv.visitJumpInsn(IFNULL, nullLabel);
                mv.visitInsn(ARETURN);
                mv.visitLabel(nullLabel);
                mv.visitInsn(POP);
                mv.visitVarInsn(ALOAD, 0);
                mv.visitInsn(DUP);
                mv.visitMethodInsn(INVOKEVIRTUAL, classInternalName, "$getStaticMetaClass", "()Lgroovy/lang/MetaClass;", false);
                mv.visitFieldInsn(PUTFIELD, classInternalName, "metaClass", "Lgroovy/lang/MetaClass;");
                mv.visitVarInsn(ALOAD, 0);
                mv.visitFieldInsn(GETFIELD, classInternalName, "metaClass", "Lgroovy/lang/MetaClass;");
                mv.visitInsn(ARETURN);
            }
        }));
    }
    Parameter[] parameters = new Parameter[] { new Parameter(ClassHelper.METACLASS_TYPE, "mc") };
    if (!node.hasMethod("setMetaClass", parameters)) {
        metaClassField = setMetaClassFieldIfNotExists(node, metaClassField);
        Statement setMetaClassCode;
        if (isFinal(metaClassField.getModifiers())) {
            ConstantExpression text = new ConstantExpression("cannot set read-only meta class");
            ConstructorCallExpression cce = new ConstructorCallExpression(ClassHelper.make(IllegalArgumentException.class), text);
            setMetaClassCode = new ExpressionStatement(cce);
        } else {
            List list = new ArrayList();
            list.add(new BytecodeInstruction() {

                public void visit(MethodVisitor mv) {
                    /**
                         * the code is (meta class is stored in 1):
                         * this.metaClass = <1>
                         */
                    mv.visitVarInsn(ALOAD, 0);
                    mv.visitVarInsn(ALOAD, 1);
                    mv.visitFieldInsn(PUTFIELD, classInternalName, "metaClass", "Lgroovy/lang/MetaClass;");
                    mv.visitInsn(RETURN);
                }
            });
            setMetaClassCode = new BytecodeSequence(list);
        }
        addMethod(node, !isAbstract(node.getModifiers()), "setMetaClass", ACC_PUBLIC, ClassHelper.VOID_TYPE, SET_METACLASS_PARAMS, ClassNode.EMPTY_ARRAY, setMetaClassCode);
    }
    if (!node.hasMethod("invokeMethod", INVOKE_METHOD_PARAMS)) {
        VariableExpression vMethods = new VariableExpression("method");
        VariableExpression vArguments = new VariableExpression("arguments");
        VariableScope blockScope = new VariableScope();
        blockScope.putReferencedLocalVariable(vMethods);
        blockScope.putReferencedLocalVariable(vArguments);
        addMethod(node, !isAbstract(node.getModifiers()), "invokeMethod", ACC_PUBLIC, ClassHelper.OBJECT_TYPE, INVOKE_METHOD_PARAMS, ClassNode.EMPTY_ARRAY, new BytecodeSequence(new BytecodeInstruction() {

            public void visit(MethodVisitor mv) {
                mv.visitVarInsn(ALOAD, 0);
                mv.visitMethodInsn(INVOKEVIRTUAL, classInternalName, "getMetaClass", "()Lgroovy/lang/MetaClass;", false);
                mv.visitVarInsn(ALOAD, 0);
                mv.visitVarInsn(ALOAD, 1);
                mv.visitVarInsn(ALOAD, 2);
                mv.visitMethodInsn(INVOKEINTERFACE, "groovy/lang/MetaClass", "invokeMethod", "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object;", true);
                mv.visitInsn(ARETURN);
            }
        }));
    }
    if (!node.hasMethod("getProperty", GET_PROPERTY_PARAMS)) {
        addMethod(node, !isAbstract(node.getModifiers()), "getProperty", ACC_PUBLIC, ClassHelper.OBJECT_TYPE, GET_PROPERTY_PARAMS, ClassNode.EMPTY_ARRAY, new BytecodeSequence(new BytecodeInstruction() {

            public void visit(MethodVisitor mv) {
                mv.visitVarInsn(ALOAD, 0);
                mv.visitMethodInsn(INVOKEVIRTUAL, classInternalName, "getMetaClass", "()Lgroovy/lang/MetaClass;", false);
                mv.visitVarInsn(ALOAD, 0);
                mv.visitVarInsn(ALOAD, 1);
                mv.visitMethodInsn(INVOKEINTERFACE, "groovy/lang/MetaClass", "getProperty", "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;", true);
                mv.visitInsn(ARETURN);
            }
        }));
    }
    if (!node.hasMethod("setProperty", SET_PROPERTY_PARAMS)) {
        addMethod(node, !isAbstract(node.getModifiers()), "setProperty", ACC_PUBLIC, ClassHelper.VOID_TYPE, SET_PROPERTY_PARAMS, ClassNode.EMPTY_ARRAY, new BytecodeSequence(new BytecodeInstruction() {

            public void visit(MethodVisitor mv) {
                mv.visitVarInsn(ALOAD, 0);
                mv.visitMethodInsn(INVOKEVIRTUAL, classInternalName, "getMetaClass", "()Lgroovy/lang/MetaClass;", false);
                mv.visitVarInsn(ALOAD, 0);
                mv.visitVarInsn(ALOAD, 1);
                mv.visitVarInsn(ALOAD, 2);
                mv.visitMethodInsn(INVOKEINTERFACE, "groovy/lang/MetaClass", "setProperty", "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;)V", true);
                mv.visitInsn(RETURN);
            }
        }));
    }
}
Also used : BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Label(org.objectweb.asm.Label) ArrayList(java.util.ArrayList) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) MethodVisitor(org.objectweb.asm.MethodVisitor) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) List(java.util.List) ArrayList(java.util.ArrayList)

Example 50 with ConstantExpression

use of org.codehaus.groovy.ast.expr.ConstantExpression in project groovy by apache.

the class BinaryExpressionHelper method assignToArray.

protected void assignToArray(Expression parent, Expression receiver, Expression index, Expression rhsValueLoader) {
    // let's replace this assignment to a subscript operator with a
    // method call
    // e.g. x[5] = 10
    // -> (x, [], 5), =, 10
    // -> methodCall(x, "putAt", [5, 10])
    ArgumentListExpression ae = new ArgumentListExpression(index, rhsValueLoader);
    controller.getInvocationWriter().makeCall(parent, receiver, new ConstantExpression("putAt"), ae, InvocationWriter.invokeMethod, false, false, false);
    controller.getOperandStack().pop();
    // return value of assignment
    rhsValueLoader.visit(controller.getAcg());
}
Also used : ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression)

Aggregations

ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)157 Expression (org.codehaus.groovy.ast.expr.Expression)92 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)88 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)73 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)72 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)67 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)59 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)55 ClassNode (org.codehaus.groovy.ast.ClassNode)51 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)43 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)42 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)41 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)39 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)37 BooleanExpression (org.codehaus.groovy.ast.expr.BooleanExpression)36 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)35 MapExpression (org.codehaus.groovy.ast.expr.MapExpression)33 Statement (org.codehaus.groovy.ast.stmt.Statement)33 StaticMethodCallExpression (org.codehaus.groovy.ast.expr.StaticMethodCallExpression)31 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)29