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);
}
});
}
Aggregations