use of org.codehaus.groovy.ast.stmt.ExpressionStatement in project groovy by apache.
the class IfElseTest method testLoop.
public void testLoop() 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));
classNode.addProperty(new PropertyNode("result", ACC_PUBLIC, ClassHelper.STRING_TYPE, classNode, null, null, null));
BooleanExpression expression = new BooleanExpression(new BinaryExpression(new FieldExpression(new FieldNode("bar", ACC_PRIVATE, ClassHelper.STRING_TYPE, classNode, ConstantExpression.NULL)), Token.newSymbol("==", 0, 0), new ConstantExpression("abc")));
Statement trueStatement = new ExpressionStatement(new BinaryExpression(new FieldExpression(new FieldNode("result", ACC_PRIVATE, ClassHelper.STRING_TYPE, classNode, ConstantExpression.NULL)), Token.newSymbol("=", 0, 0), new ConstantExpression("worked")));
Statement falseStatement = createPrintlnStatement(new ConstantExpression("false"));
IfStatement statement = new IfStatement(expression, trueStatement, falseStatement);
classNode.addMethod(new MethodNode("ifDemo", ACC_PUBLIC, ClassHelper.VOID_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, statement));
Class fooClass = loadClass(classNode);
assertTrue("Loaded a new class", fooClass != null);
Object bean = fooClass.newInstance();
assertTrue("Managed to create bean", bean != null);
assertSetProperty(bean, "bar", "abc");
System.out.println("################ Now about to invoke method");
Object[] array = {};
InvokerHelper.invokeMethod(bean, "ifDemo", array);
System.out.println("################ Done");
assertGetProperty(bean, "result", "worked");
}
use of org.codehaus.groovy.ast.stmt.ExpressionStatement in project groovy by apache.
the class JavaAwareResolveVisitor method getConstructorCall.
private static Expression getConstructorCall(Statement code) {
if (code == null)
return null;
if (code instanceof BlockStatement) {
BlockStatement bs = (BlockStatement) code;
if (bs.isEmpty())
return null;
return getConstructorCall(bs.getStatements().get(0));
}
if (!(code instanceof ExpressionStatement))
return null;
ExpressionStatement es = (ExpressionStatement) code;
Expression exp = es.getExpression();
if (!(exp instanceof ConstructorCallExpression))
return null;
ConstructorCallExpression cce = (ConstructorCallExpression) exp;
if (!cce.isSpecialCall())
return null;
return cce;
}
use of org.codehaus.groovy.ast.stmt.ExpressionStatement in project groovy by apache.
the class AntlrParserPlugin method variableDef.
protected Statement variableDef(AST variableDef) {
ExpressionStatement expressionStatement = new ExpressionStatement(declarationExpression(variableDef));
configureAST(expressionStatement, variableDef);
return expressionStatement;
}
use of org.codehaus.groovy.ast.stmt.ExpressionStatement in project groovy by apache.
the class AntlrParserPlugin method declarationExpression.
protected Expression declarationExpression(AST variableDef) {
AST node = variableDef.getFirstChild();
ClassNode type = null;
List<AnnotationNode> annotations = new ArrayList<AnnotationNode>();
int modifiers = 0;
if (isType(MODIFIERS, node)) {
// force check of modifier conflicts
modifiers = modifiers(node, annotations, 0);
node = node.getNextSibling();
}
if (isType(TYPE, node)) {
type = makeTypeWithArguments(node);
node = node.getNextSibling();
}
Expression leftExpression;
Expression rightExpression = EmptyExpression.INSTANCE;
AST right;
if (isType(ASSIGN, node)) {
node = node.getFirstChild();
AST left = node.getFirstChild();
ArgumentListExpression alist = new ArgumentListExpression();
for (AST varDef = left; varDef != null; varDef = varDef.getNextSibling()) {
assertNodeType(VARIABLE_DEF, varDef);
DeclarationExpression de = (DeclarationExpression) declarationExpression(varDef);
alist.addExpression(de.getVariableExpression());
}
leftExpression = alist;
right = node.getNextSibling();
if (right != null)
rightExpression = expression(right);
} else {
String name = identifier(node);
VariableExpression ve = new VariableExpression(name, type);
ve.setModifiers(modifiers);
leftExpression = ve;
right = node.getNextSibling();
if (right != null) {
assertNodeType(ASSIGN, right);
rightExpression = expression(right.getFirstChild());
}
}
configureAST(leftExpression, node);
Token token = makeToken(Types.ASSIGN, variableDef);
DeclarationExpression expression = new DeclarationExpression(leftExpression, token, rightExpression);
expression.addAnnotations(annotations);
configureAST(expression, variableDef);
ExpressionStatement expressionStatement = new ExpressionStatement(expression);
configureAST(expressionStatement, variableDef);
return expression;
}
use of org.codehaus.groovy.ast.stmt.ExpressionStatement in project groovy by apache.
the class TraitComposer method createDelegatingForwarder.
private static Statement createDelegatingForwarder(final MethodNode forwarderMethod, final ClassNode next) {
// generates --> next$Trait$Helper.method(this, arg1, arg2)
TraitHelpersTuple helpers = Traits.findHelpers(next);
ArgumentListExpression args = new ArgumentListExpression();
args.addExpression(new VariableExpression("this"));
Parameter[] forwarderMethodParameters = forwarderMethod.getParameters();
for (final Parameter forwarderMethodParameter : forwarderMethodParameters) {
args.addExpression(new VariableExpression(forwarderMethodParameter));
}
StaticMethodCallExpression delegateCall = new StaticMethodCallExpression(helpers.getHelper(), forwarderMethod.getName(), args);
Statement result;
if (ClassHelper.VOID_TYPE.equals(forwarderMethod.getReturnType())) {
BlockStatement stmt = new BlockStatement();
stmt.addStatement(new ExpressionStatement(delegateCall));
stmt.addStatement(new ReturnStatement(new ConstantExpression(null)));
result = stmt;
} else {
result = new ReturnStatement(delegateCall);
}
return result;
}
Aggregations