Search in sources :

Example 91 with ConstantExpression

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

the class AnnotationConstantsVisitor method revertType.

private Expression revertType(Expression val, ClassNode returnWrapperType) {
    ConstantExpression ce = (ConstantExpression) val;
    if (ClassHelper.Character_TYPE.equals(returnWrapperType) && ClassHelper.STRING_TYPE.equals(val.getType())) {
        return configure(val, Verifier.transformToPrimitiveConstantIfPossible((ConstantExpression) val));
    }
    ClassNode valWrapperType = ClassHelper.getWrapper(val.getType());
    if (ClassHelper.Integer_TYPE.equals(valWrapperType)) {
        Integer i = (Integer) ce.getValue();
        if (ClassHelper.Character_TYPE.equals(returnWrapperType)) {
            return configure(val, new ConstantExpression((char) i.intValue(), true));
        }
        if (ClassHelper.Short_TYPE.equals(returnWrapperType)) {
            return configure(val, new ConstantExpression(i.shortValue(), true));
        }
        if (ClassHelper.Byte_TYPE.equals(returnWrapperType)) {
            return configure(val, new ConstantExpression(i.byteValue(), true));
        }
    }
    if (ClassHelper.BigDecimal_TYPE.equals(valWrapperType)) {
        BigDecimal bd = (BigDecimal) ce.getValue();
        if (ClassHelper.Float_TYPE.equals(returnWrapperType)) {
            return configure(val, new ConstantExpression(bd.floatValue(), true));
        }
        if (ClassHelper.Double_TYPE.equals(returnWrapperType)) {
            return configure(val, new ConstantExpression(bd.doubleValue(), true));
        }
    }
    return null;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) BigDecimal(java.math.BigDecimal)

Example 92 with ConstantExpression

use of org.codehaus.groovy.ast.expr.ConstantExpression 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 93 with ConstantExpression

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

the class ControllerActionTransformer method initializeStringParameter.

protected void initializeStringParameter(final ClassNode classNode, final BlockStatement wrapper, final Parameter param, final String requestParameterName) {
    final ClassNode paramTypeClassNode = param.getType();
    final String methodParamName = param.getName();
    Expression getParamsExpression = buildGetPropertyExpression(new VariableExpression("this"), "params", classNode);
    final Expression paramsContainsKeyMethodArguments = new ArgumentListExpression(new ConstantExpression(requestParameterName));
    final BooleanExpression containsKeyExpression = new BooleanExpression(applyDefaultMethodTarget(new MethodCallExpression(getParamsExpression, "containsKey", paramsContainsKeyMethodArguments), Map.class));
    final Statement initializeParameterStatement = new ExpressionStatement(new DeclarationExpression(new VariableExpression(methodParamName, paramTypeClassNode), Token.newSymbol(Types.EQUALS, 0, 0), new TernaryExpression(containsKeyExpression, buildGetMapExpression(getParamsExpression, requestParameterName), new ConstantExpression(null))));
    wrapper.addStatement(initializeParameterStatement);
}
Also used : TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) 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) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) GrailsASTUtils.buildGetPropertyExpression(org.grails.compiler.injection.GrailsASTUtils.buildGetPropertyExpression) GrailsASTUtils.buildSetPropertyExpression(org.grails.compiler.injection.GrailsASTUtils.buildSetPropertyExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) GrailsASTUtils.buildGetMapExpression(org.grails.compiler.injection.GrailsASTUtils.buildGetMapExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) Map(java.util.Map) TypeConvertingMap(grails.util.TypeConvertingMap) HashMap(java.util.HashMap)

Example 94 with ConstantExpression

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

the class GrailsApplicationCompilerAutoConfiguration method createGrabAnnotation.

public static AnnotationNode createGrabAnnotation(String group, String module, String version, String classifier, String type, boolean transitive) {
    AnnotationNode annotationNode = new AnnotationNode(new ClassNode(Grab.class));
    annotationNode.addMember("group", new ConstantExpression(group));
    annotationNode.addMember("module", new ConstantExpression(module));
    annotationNode.addMember("version", new ConstantExpression(version));
    if (classifier != null) {
        annotationNode.addMember("classifier", new ConstantExpression(classifier));
    }
    if (type != null) {
        annotationNode.addMember("type", new ConstantExpression(type));
    }
    annotationNode.addMember("transitive", new ConstantExpression(transitive));
    annotationNode.addMember("initClass", new ConstantExpression(false));
    return annotationNode;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Grab(groovy.lang.Grab)

Example 95 with ConstantExpression

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

the class TestMixinTransformation method addSpockFieldMetadata.

private void addSpockFieldMetadata(FieldNode field, int ordinal) {
    AnnotationNode ann = new AnnotationNode(ClassHelper.make(FieldMetadata.class));
    ann.setMember(FieldMetadata.NAME, new ConstantExpression(field.getName()));
    ann.setMember(FieldMetadata.ORDINAL, new ConstantExpression(ordinal));
    ann.setMember(FieldMetadata.LINE, new ConstantExpression(field.getLineNumber()));
    field.addAnnotation(ann);
}
Also used : FieldMetadata(org.spockframework.runtime.model.FieldMetadata) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression)

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