Search in sources :

Example 86 with ConstantExpression

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

the class Verifier method transformToPrimitiveConstantIfPossible.

/**
     * When constant expressions are created, the value is always wrapped to a non primitive type.
     * Some constant expressions are optimized to return primitive types, but not all primitives are
     * handled. This method guarantees to return a similar constant expression but with a primitive type
     * instead of a boxed type.
     *
     * Additionaly, single char strings are converted to 'char' types.
     *
     * @param constantExpression a constant expression
     * @return the same instance of constant expression if the type is already primitive, or a primitive
     * constant if possible.
     */
public static ConstantExpression transformToPrimitiveConstantIfPossible(ConstantExpression constantExpression) {
    Object value = constantExpression.getValue();
    if (value == null)
        return constantExpression;
    ConstantExpression result;
    ClassNode type = constantExpression.getType();
    if (ClassHelper.isPrimitiveType(type))
        return constantExpression;
    if (value instanceof String && ((String) value).length() == 1) {
        result = new ConstantExpression(((String) value).charAt(0));
        result.setType(ClassHelper.char_TYPE);
    } else {
        type = ClassHelper.getUnwrapper(type);
        result = new ConstantExpression(value, true);
        result.setType(type);
    }
    return result;
}
Also used : ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) GroovyObject(groovy.lang.GroovyObject)

Example 87 with ConstantExpression

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

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)

Example 88 with ConstantExpression

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

the class PluginUseScriptBlockMetadataExtractor method extract.

public void extract(SourceUnit sourceUnit, ScriptBlock scriptBlock) {
    ClosureExpression closureArg = scriptBlock.getClosureExpression();
    closureArg.getCode().visit(new RestrictiveCodeVisitor(sourceUnit, formatErrorMessage(BASE_MESSAGE)) {

        @Override
        public void visitBlockStatement(BlockStatement block) {
            for (Statement statement : block.getStatements()) {
                statement.visit(this);
            }
        }

        @Override
        public void visitMethodCallExpression(MethodCallExpression call) {
            if (!call.isImplicitThis()) {
                Expression target = call.getObjectExpression();
                if (!(target instanceof MethodCallExpression)) {
                    restrict(target, formatErrorMessage(BASE_MESSAGE));
                    return;
                }
                visitMethodCallExpression((MethodCallExpression) target);
            }
            if (call.getMethod() instanceof ConstantExpression) {
                ConstantExpression methodName = (ConstantExpression) call.getMethod();
                if (isOfType(methodName, String.class)) {
                    String methodNameText = methodName.getText();
                    if (methodNameText.equals("id") || methodNameText.equals("version")) {
                        ConstantExpression argumentExpression = hasSingleConstantArgOfType(call, String.class);
                        if (argumentExpression == null) {
                            restrict(call, formatErrorMessage(NEED_SINGLE_STRING));
                            return;
                        }
                        String argStringValue = argumentExpression.getValue().toString();
                        if (argStringValue.length() == 0) {
                            restrict(argumentExpression, formatErrorMessage(NEED_SINGLE_STRING));
                            return;
                        }
                        if (methodName.getText().equals("id")) {
                            if (call.isImplicitThis()) {
                                try {
                                    DefaultPluginId.validate(argStringValue);
                                    call.setNodeMetaData(PluginDependencySpec.class, pluginRequestCollector.createSpec(call.getLineNumber()).id(argStringValue));
                                } catch (InvalidPluginIdException e) {
                                    restrict(argumentExpression, formatErrorMessage(e.getReason()));
                                }
                            } else {
                                restrict(call, formatErrorMessage(BASE_MESSAGE));
                            }
                        }
                        if (methodName.getText().equals("version")) {
                            PluginDependencySpec spec = getSpecFor(call);
                            if (spec == null) {
                                return;
                            }
                            spec.version(argStringValue);
                            call.setNodeMetaData(PluginDependencySpec.class, spec);
                        }
                    } else if (methodNameText.equals("apply")) {
                        ConstantExpression arguments = hasSingleConstantArgOfType(call, boolean.class);
                        if (arguments == null) {
                            restrict(call, formatErrorMessage(NEED_SINGLE_BOOLEAN));
                            return;
                        }
                        PluginDependencySpec spec = getSpecFor(call);
                        if (spec == null) {
                            return;
                        }
                        spec.apply((Boolean) arguments.getValue());
                    } else {
                        if (!call.isImplicitThis()) {
                            restrict(methodName, formatErrorMessage(EXTENDED_MESSAGE));
                        } else {
                            restrict(methodName, formatErrorMessage(BASE_MESSAGE));
                        }
                    }
                } else {
                    restrict(methodName, formatErrorMessage(NOT_LITERAL_ID_METHOD_NAME));
                }
            } else {
                restrict(call);
            }
        }

        private PluginDependencySpec getSpecFor(MethodCallExpression call) {
            Expression objectExpression = call.getObjectExpression();
            if (objectExpression instanceof MethodCallExpression) {
                return objectExpression.getNodeMetaData(PluginDependencySpec.class);
            } else {
                restrict(call, formatErrorMessage(BASE_MESSAGE));
                return null;
            }
        }

        @Override
        public void visitExpressionStatement(ExpressionStatement statement) {
            statement.getExpression().visit(this);
        }
    });
}
Also used : MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) RestrictiveCodeVisitor(org.gradle.groovy.scripts.internal.RestrictiveCodeVisitor) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) InvalidPluginIdException(org.gradle.plugin.internal.InvalidPluginIdException) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) PluginDependencySpec(org.gradle.plugin.use.PluginDependencySpec) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression)

Example 89 with ConstantExpression

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

the class AbstractGrailsArtefactTransformer method addApiLookupFieldAndSetter.

protected void addApiLookupFieldAndSetter(ClassNode classNode, ClassNode implementationNode, String apiProperty, Expression initialValueExpression) {
    FieldNode fieldNode = classNode.getField(apiProperty);
    if (fieldNode == null || !fieldNode.getDeclaringClass().equals(classNode)) {
        fieldNode = new FieldNode(apiProperty, Modifier.PRIVATE | Modifier.STATIC, implementationNode, classNode, initialValueExpression);
        classNode.addField(fieldNode);
        String setterName = "set" + MetaClassHelper.capitalize(apiProperty);
        Parameter setterParameter = new Parameter(implementationNode, apiProperty);
        BlockStatement setterBody = new BlockStatement();
        setterBody.addStatement(new ExpressionStatement(new BinaryExpression(new AttributeExpression(new ClassExpression(classNode), new ConstantExpression(apiProperty)), Token.newSymbol(Types.EQUAL, 0, 0), new VariableExpression(setterParameter))));
        GrailsASTUtils.addCompileStaticAnnotation(classNode.addMethod(setterName, Modifier.PUBLIC | Modifier.STATIC, ClassHelper.VOID_TYPE, new Parameter[] { setterParameter }, null, setterBody));
    }
}
Also used : FieldNode(org.codehaus.groovy.ast.FieldNode) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Parameter(org.codehaus.groovy.ast.Parameter) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) AttributeExpression(org.codehaus.groovy.ast.expr.AttributeExpression)

Example 90 with ConstantExpression

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

the class ArtefactTypeAstTransformation method postProcess.

protected void postProcess(SourceUnit sourceUnit, AnnotationNode annotationNode, ClassNode classNode, String artefactType) {
    if (!getAnnotationType().equals(annotationNode.getClassNode())) {
        // add @Artefact annotation to resulting class so that "short cut" annotations like @TagLib 
        // also produce an @Artefact annotation in the resulting class file
        AnnotationNode annotation = new AnnotationNode(getAnnotationType());
        annotation.addMember("value", new ConstantExpression(artefactType));
        classNode.addAnnotation(annotation);
    }
}
Also used : 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