use of org.codehaus.groovy.ast.stmt.ExpressionStatement 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);
}
});
}
use of org.codehaus.groovy.ast.stmt.ExpressionStatement 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));
}
}
use of org.codehaus.groovy.ast.stmt.ExpressionStatement in project grails-core by grails.
the class ASTValidationErrorsHelper method addSetErrorsMethod.
protected void addSetErrorsMethod(final ClassNode paramTypeClassNode) {
final String errorsArgumentName = "$errorsArg";
MethodNode setErrorsMethod = paramTypeClassNode.getMethod(SET_ERRORS_METHOD_NAME, new Parameter[] { new Parameter(ERRORS_CLASS_NODE, errorsArgumentName) });
if (setErrorsMethod == null) {
final Expression assignErrorsExpression = new BinaryExpression(ERRORS_EXPRESSION, EQUALS_SYMBOL, new VariableExpression(errorsArgumentName));
setErrorsMethod = new MethodNode(SET_ERRORS_METHOD_NAME, Modifier.PUBLIC, ClassHelper.VOID_TYPE, new Parameter[] { new Parameter(ERRORS_CLASS_NODE, errorsArgumentName) }, GrailsArtefactClassInjector.EMPTY_CLASS_ARRAY, new ExpressionStatement(assignErrorsExpression));
paramTypeClassNode.addMethod(setErrorsMethod);
}
}
use of org.codehaus.groovy.ast.stmt.ExpressionStatement in project gradle by gradle.
the class RuleVisitor method visitRuleClosure.
public void visitRuleClosure(ClosureExpression expression, Expression invocation, String invocationDisplayName) {
InputReferences parentInputs = inputs;
VariableExpression parentInputsVariable = inputsVariable;
try {
inputs = new InputReferences();
inputsVariable = new VariableExpression("__rule_inputs_var_" + (counter++), POTENTIAL_INPUTS);
inputsVariable.setClosureSharedVariable(true);
super.visitClosureExpression(expression);
BlockStatement code = (BlockStatement) expression.getCode();
code.setNodeMetaData(AST_NODE_METADATA_LOCATION_KEY, new SourceLocation(location, scriptSourceDescription, invocationDisplayName, invocation.getLineNumber(), invocation.getColumnNumber()));
code.setNodeMetaData(AST_NODE_METADATA_INPUTS_KEY, inputs);
if (parentInputsVariable != null) {
expression.getVariableScope().putReferencedLocalVariable(parentInputsVariable);
}
code.getVariableScope().putDeclaredVariable(inputsVariable);
if (parentInputsVariable == null) {
// <inputs-lvar> = <inputs-field>
DeclarationExpression variableDeclaration = new DeclarationExpression(inputsVariable, ASSIGN, new VariableExpression(INPUTS_FIELD_NAME));
code.getStatements().add(0, new ExpressionStatement(variableDeclaration));
} else {
// <inputs-lvar> = <inputs-field> ?: <parent-inputs-lvar>
DeclarationExpression variableDeclaration = new DeclarationExpression(inputsVariable, ASSIGN, new ElvisOperatorExpression(new VariableExpression(INPUTS_FIELD_NAME), parentInputsVariable));
code.getStatements().add(0, new ExpressionStatement(variableDeclaration));
}
// Move default values into body of closure, so they can use <inputs-lvar>
for (Parameter parameter : expression.getParameters()) {
if (parameter.hasInitialExpression()) {
code.getStatements().add(1, new ExpressionStatement(new BinaryExpression(new VariableExpression(parameter.getName()), ASSIGN, parameter.getInitialExpression())));
parameter.setInitialExpression(ConstantExpression.NULL);
}
}
} finally {
if (parentInputs != null) {
parentInputs.addNestedReferences(inputs);
}
inputs = parentInputs;
inputsVariable = parentInputsVariable;
}
}
use of org.codehaus.groovy.ast.stmt.ExpressionStatement in project grails-core by grails.
the class GroovyPageOptimizerVisitor method introduceThisObjectVariable.
// TODO: Research why http://jira.grails.org/browse/GRAILS-8679 happens with this enabled. See ElvisAndClosureGroovyPageTests
// @Override
// public void visitClosureExpression(ClosureExpression expression) {
// innerClosures.push(expression);
// introduceThisObjectVariable(expression);
// super.visitClosureExpression(expression);
// innerClosures.pop();
// }
@SuppressWarnings("unused")
private void introduceThisObjectVariable(ClosureExpression closureExpression) {
if (closureExpression.getCode() instanceof BlockStatement) {
List<Statement> oldBlock = ((BlockStatement) closureExpression.getCode()).getStatements();
BlockStatement newBlock = new BlockStatement();
newBlock.addStatement(new ExpressionStatement(thisObjectDeclaration));
newBlock.addStatements(oldBlock);
closureExpression.setCode(newBlock);
}
}
Aggregations