Search in sources :

Example 51 with ClassExpression

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

the class SuperCallTraitTransformer method transformMethodCallExpression.

private Expression transformMethodCallExpression(final MethodCallExpression exp) {
    Expression objectExpression = transform(exp.getObjectExpression());
    ClassNode traitReceiver = objectExpression.getNodeMetaData(SuperCallTraitTransformer.class);
    if (traitReceiver != null) {
        TraitHelpersTuple helpers = Traits.findHelpers(traitReceiver);
        // (SomeTrait.super).foo() --> SomeTrait$Helper.foo(this)
        ClassExpression receiver = new ClassExpression(helpers.getHelper());
        ArgumentListExpression newArgs = new ArgumentListExpression();
        Expression arguments = exp.getArguments();
        newArgs.addExpression(new VariableExpression("this"));
        if (arguments instanceof TupleExpression) {
            List<Expression> expressions = ((TupleExpression) arguments).getExpressions();
            for (Expression expression : expressions) {
                newArgs.addExpression(transform(expression));
            }
        } else {
            newArgs.addExpression(arguments);
        }
        MethodCallExpression result = new MethodCallExpression(receiver, exp.getMethod(), newArgs);
        result.setImplicitThis(false);
        result.setSpreadSafe(exp.isSpreadSafe());
        result.setSafe(exp.isSafe());
        result.setSourcePosition(exp);
        return result;
    }
    return super.transform(exp);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Example 52 with ClassExpression

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

the class TraitASTTransformation method processField.

private void processField(final FieldNode field, final MethodNode initializer, final MethodNode staticInitializer, final ClassNode fieldHelper, final ClassNode trait, final Set<String> knownFields) {
    Expression initialExpression = field.getInitialExpression();
    MethodNode selectedMethod = field.isStatic() ? staticInitializer : initializer;
    if (initialExpression != null) {
        VariableExpression thisObject = new VariableExpression(selectedMethod.getParameters()[0]);
        ExpressionStatement initCode = new ExpressionStatement(initialExpression);
        processBody(thisObject, selectedMethod, initCode, trait, fieldHelper, knownFields);
        BlockStatement code = (BlockStatement) selectedMethod.getCode();
        MethodCallExpression mce;
        if (field.isStatic()) {
            mce = new MethodCallExpression(new ClassExpression(INVOKERHELPER_CLASSNODE), "invokeStaticMethod", new ArgumentListExpression(thisObject, new ConstantExpression(Traits.helperSetterName(field)), initCode.getExpression()));
        } else {
            mce = new MethodCallExpression(new CastExpression(createReceiverType(field.isStatic(), fieldHelper), thisObject), Traits.helperSetterName(field), new CastExpression(field.getOriginType(), initCode.getExpression()));
        }
        mce.setImplicitThis(false);
        mce.setSourcePosition(initialExpression);
        code.addStatement(new ExpressionStatement(mce));
    }
    // define setter/getter helper methods
    fieldHelper.addMethod(Traits.helperSetterName(field), ACC_PUBLIC | ACC_ABSTRACT, field.getOriginType(), new Parameter[] { new Parameter(field.getOriginType(), "val") }, ClassNode.EMPTY_ARRAY, null);
    fieldHelper.addMethod(Traits.helperGetterName(field), ACC_PUBLIC | ACC_ABSTRACT, field.getOriginType(), Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null);
    // dummy fields are only used to carry annotations if instance field
    // and to differentiate from static fields otherwise
    String dummyFieldName = (field.isStatic() ? Traits.STATIC_FIELD_PREFIX : Traits.FIELD_PREFIX) + (field.isPublic() ? Traits.PUBLIC_FIELD_PREFIX : Traits.PRIVATE_FIELD_PREFIX) + Traits.remappedFieldName(field.getOwner(), field.getName());
    FieldNode dummyField = new FieldNode(dummyFieldName, ACC_STATIC | ACC_PUBLIC | ACC_FINAL | ACC_SYNTHETIC, field.getOriginType(), fieldHelper, null);
    // copy annotations from field to dummy field
    List<AnnotationNode> copied = new LinkedList<AnnotationNode>();
    List<AnnotationNode> notCopied = new LinkedList<AnnotationNode>();
    GeneralUtils.copyAnnotatedNodeAnnotations(field, copied, notCopied);
    dummyField.addAnnotations(copied);
    fieldHelper.addField(dummyField);
}
Also used : FieldNode(org.codehaus.groovy.ast.FieldNode) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) LinkedList(java.util.LinkedList) MethodNode(org.codehaus.groovy.ast.MethodNode) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) 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) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) Parameter(org.codehaus.groovy.ast.Parameter) CastExpression(org.codehaus.groovy.ast.expr.CastExpression)

Example 53 with ClassExpression

use of org.codehaus.groovy.ast.expr.ClassExpression in project grails-core by grails.

the class TestMixinTransformation method weaveMixinsIntoClass.

public void weaveMixinsIntoClass(ClassNode classNode, ListExpression values, ClassNode applicationClassNode) {
    if (values == null) {
        return;
    }
    Junit3TestFixtureMethodHandler junit3MethodHandler = isJunit3Test(classNode) ? new Junit3TestFixtureMethodHandler(classNode) : null;
    for (Expression current : values.getExpressions()) {
        if (current instanceof ClassExpression) {
            ClassExpression ce = (ClassExpression) current;
            ClassNode mixinClassNode = ce.getType();
            mixinClassNode.getAllInterfaces();
            weaveMixinIntoClass(classNode, mixinClassNode, junit3MethodHandler, applicationClassNode);
        }
    }
    if (junit3MethodHandler != null) {
        junit3MethodHandler.postProcessClassNode();
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression)

Example 54 with ClassExpression

use of org.codehaus.groovy.ast.expr.ClassExpression in project grails-core by grails.

the class ControllerActionTransformer method annotateActionMethod.

protected void annotateActionMethod(ClassNode controllerClassNode, final Parameter[] parameters, final MethodNode methodNode) {
    if (isCommandObjectAction(parameters)) {
        ListExpression initArray = new ListExpression();
        for (Parameter parameter : parameters) {
            initArray.addExpression(new ClassExpression(parameter.getType()));
        }
        AnnotationNode paramActionAnn = new AnnotationNode(new ClassNode(Action.class));
        paramActionAnn.setMember(ACTION_MEMBER_TARGET, initArray);
        methodNode.addAnnotation(paramActionAnn);
    } else {
        methodNode.addAnnotation(ACTION_ANNOTATION_NODE);
    }
}
Also used : Action(grails.web.Action) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) RequestParameter(grails.web.RequestParameter) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression)

Example 55 with ClassExpression

use of org.codehaus.groovy.ast.expr.ClassExpression in project grails-core by grails.

the class TestMixinTransformation method addJunitRuleFields.

protected void addJunitRuleFields(ClassNode classNode) {
    if (classNode.getField(JUNIT_ADAPTER_FIELD_NAME) != null) {
        return;
    }
    ClassNode junitAdapterType = ClassHelper.make(TestRuntimeJunitAdapter.class);
    FieldNode junitAdapterFieldNode = classNode.addField(JUNIT_ADAPTER_FIELD_NAME, Modifier.STATIC, junitAdapterType, new ConstructorCallExpression(junitAdapterType, MethodCallExpression.NO_ARGUMENTS));
    boolean spockTest = isSpockTest(classNode);
    FieldNode staticRuleFieldNode = classNode.addField(RULE_FIELD_NAME_BASE + "StaticClassRule", Modifier.PRIVATE | Modifier.STATIC, ClassHelper.make(TestRule.class), new MethodCallExpression(new FieldExpression(junitAdapterFieldNode), "newClassRule", new ClassExpression(classNode)));
    AnnotationNode classRuleAnnotation = new AnnotationNode(ClassHelper.make(ClassRule.class));
    if (spockTest) {
        // @ClassRule must be added to @Shared field in spock
        FieldNode spockSharedRuleFieldNode = classNode.addField(RULE_FIELD_NAME_BASE + "SharedClassRule", Modifier.PUBLIC, ClassHelper.make(TestRule.class), new FieldExpression(staticRuleFieldNode));
        spockSharedRuleFieldNode.addAnnotation(classRuleAnnotation);
        spockSharedRuleFieldNode.addAnnotation(new AnnotationNode(ClassHelper.make(Shared.class)));
        addSpockFieldMetadata(spockSharedRuleFieldNode, 0);
    } else {
        staticRuleFieldNode.setModifiers(Modifier.PUBLIC | Modifier.STATIC);
        staticRuleFieldNode.addAnnotation(classRuleAnnotation);
    }
    FieldNode ruleFieldNode = classNode.addField(RULE_FIELD_NAME_BASE + "Rule", Modifier.PUBLIC, ClassHelper.make(TestRule.class), new MethodCallExpression(new FieldExpression(junitAdapterFieldNode), "newRule", new VariableExpression("this")));
    ruleFieldNode.addAnnotation(new AnnotationNode(ClassHelper.make(Rule.class)));
    if (spockTest) {
        addSpockFieldMetadata(ruleFieldNode, 0);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) TestRule(org.junit.rules.TestRule) FieldNode(org.codehaus.groovy.ast.FieldNode) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ClassRule(org.junit.ClassRule) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression)

Aggregations

ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)97 Expression (org.codehaus.groovy.ast.expr.Expression)59 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)57 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)55 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)52 ClassNode (org.codehaus.groovy.ast.ClassNode)48 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)41 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)40 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)37 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)33 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)31 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)27 MethodNode (org.codehaus.groovy.ast.MethodNode)18 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)17 StaticMethodCallExpression (org.codehaus.groovy.ast.expr.StaticMethodCallExpression)17 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)17 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)16 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)16 CastExpression (org.codehaus.groovy.ast.expr.CastExpression)16 FieldNode (org.codehaus.groovy.ast.FieldNode)15