Search in sources :

Example 1 with RestrictiveCodeVisitor

use of org.gradle.groovy.scripts.internal.RestrictiveCodeVisitor 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)

Aggregations

ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)1 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)1 Expression (org.codehaus.groovy.ast.expr.Expression)1 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)1 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)1 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)1 Statement (org.codehaus.groovy.ast.stmt.Statement)1 RestrictiveCodeVisitor (org.gradle.groovy.scripts.internal.RestrictiveCodeVisitor)1 InvalidPluginIdException (org.gradle.plugin.internal.InvalidPluginIdException)1 PluginDependencySpec (org.gradle.plugin.use.PluginDependencySpec)1