use of org.codehaus.groovy.ast.stmt.ExpressionStatement in project groovy-core by groovy.
the class Verifier method addFieldInitialization.
protected void addFieldInitialization(List list, List staticList, FieldNode fieldNode, boolean isEnumClassNode, List initStmtsAfterEnumValuesInit, Set explicitStaticPropsInEnum) {
Expression expression = fieldNode.getInitialExpression();
if (expression != null) {
final FieldExpression fe = new FieldExpression(fieldNode);
if (fieldNode.getType().equals(ClassHelper.REFERENCE_TYPE) && ((fieldNode.getModifiers() & Opcodes.ACC_SYNTHETIC) != 0)) {
fe.setUseReferenceDirectly(true);
}
ExpressionStatement statement = new ExpressionStatement(new BinaryExpression(fe, Token.newSymbol(Types.EQUAL, fieldNode.getLineNumber(), fieldNode.getColumnNumber()), expression));
if (fieldNode.isStatic()) {
// GROOVY-3311: pre-defined constants added by groovy compiler for numbers/characters should be
// initialized first so that code dependent on it does not see their values as empty
Expression initialValueExpression = fieldNode.getInitialValueExpression();
if (initialValueExpression instanceof ConstantExpression) {
ConstantExpression cexp = (ConstantExpression) initialValueExpression;
cexp = transformToPrimitiveConstantIfPossible(cexp);
if (fieldNode.isFinal() && ClassHelper.isStaticConstantInitializerType(cexp.getType()) && cexp.getType().equals(fieldNode.getType())) {
// GROOVY-5150: primitive type constants will be initialized directly
return;
}
staticList.add(0, statement);
} else {
staticList.add(statement);
}
// to avoid double initialization in case of several constructors
fieldNode.setInitialValueExpression(null);
/*
* If it is a statement for an explicitly declared static field inside an enum, store its
* reference. For enums, they need to be handled differently as such init statements should
* come after the enum values have been initialized inside <clinit> block. GROOVY-3161.
*/
if (isEnumClassNode && explicitStaticPropsInEnum.contains(fieldNode.getName())) {
initStmtsAfterEnumValuesInit.add(statement);
}
} else {
list.add(statement);
}
}
}
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 groovy by apache.
the class TupleListTest method assertIterate.
protected void assertIterate(String methodName, Expression listExpression) throws Exception {
ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, ClassHelper.OBJECT_TYPE);
classNode.addConstructor(new ConstructorNode(ACC_PUBLIC, null));
classNode.addProperty(new PropertyNode("bar", ACC_PUBLIC, ClassHelper.STRING_TYPE, classNode, null, null, null));
Statement loopStatement = createPrintlnStatement(new VariableExpression("i"));
BlockStatement block = new BlockStatement();
block.addStatement(new ExpressionStatement(new DeclarationExpression(new VariableExpression("list"), Token.newSymbol("=", 0, 0), listExpression)));
block.addStatement(new ForStatement(new Parameter(ClassHelper.DYNAMIC_TYPE, "i"), new VariableExpression("list"), loopStatement));
classNode.addMethod(new MethodNode(methodName, ACC_PUBLIC, ClassHelper.VOID_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, block));
Class fooClass = loadClass(classNode);
assertTrue("Loaded a new class", fooClass != null);
Object bean = fooClass.newInstance();
assertTrue("Managed to create bean", bean != null);
System.out.println("################ Now about to invoke method");
try {
InvokerHelper.invokeMethod(bean, methodName, null);
} catch (InvokerInvocationException e) {
System.out.println("Caught: " + e.getCause());
e.getCause().printStackTrace();
fail("Should not have thrown an exception");
}
System.out.println("################ Done");
}
Aggregations