use of org.codehaus.groovy.ast.stmt.IfStatement in project groovy by apache.
the class AntlrParserPlugin method ifStatement.
protected Statement ifStatement(AST ifNode) {
AST node = ifNode.getFirstChild();
assertNodeType(EXPR, node);
BooleanExpression booleanExpression = booleanExpression(node);
node = node.getNextSibling();
Statement ifBlock = statement(node);
Statement elseBlock = EmptyStatement.INSTANCE;
node = node.getNextSibling();
if (node != null) {
elseBlock = statement(node);
}
IfStatement ifStatement = new IfStatement(booleanExpression, ifBlock, elseBlock);
configureAST(ifStatement, ifNode);
return ifStatement;
}
use of org.codehaus.groovy.ast.stmt.IfStatement in project groovy-core by groovy.
the class StaticTypeCheckingVisitor method visitIfElse.
@Override
public void visitIfElse(final IfStatement ifElse) {
Map<VariableExpression, List<ClassNode>> oldTracker = pushAssignmentTracking();
try {
// create a new temporary element in the if-then-else type info
typeCheckingContext.pushTemporaryTypeInfo();
visitStatement(ifElse);
ifElse.getBooleanExpression().visit(this);
ifElse.getIfBlock().visit(this);
// pop if-then-else temporary type info
typeCheckingContext.popTemporaryTypeInfo();
// GROOVY-6099: restore assignment info as before the if branch
restoreTypeBeforeConditional();
Statement elseBlock = ifElse.getElseBlock();
if (elseBlock instanceof EmptyStatement) {
// dispatching to EmptyStatement will not call back visitor,
// must call our visitEmptyStatement explicitly
visitEmptyStatement((EmptyStatement) elseBlock);
} else {
elseBlock.visit(this);
}
} finally {
popAssignmentTracking(oldTracker);
}
}
use of org.codehaus.groovy.ast.stmt.IfStatement in project groovy-core by groovy.
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.IfStatement in project groovy-core by groovy.
the class EnumVisitor method addMethods.
private void addMethods(ClassNode enumClass, FieldNode values) {
List<MethodNode> methods = enumClass.getMethods();
boolean hasNext = false;
boolean hasPrevious = false;
for (MethodNode m : methods) {
if (m.getName().equals("next") && m.getParameters().length == 0)
hasNext = true;
if (m.getName().equals("previous") && m.getParameters().length == 0)
hasPrevious = true;
if (hasNext && hasPrevious)
break;
}
ClassNode enumRef = enumClass.getPlainNodeReference();
{
// create values() method
MethodNode valuesMethod = new MethodNode("values", PUBLIC_FS, enumRef.makeArray(), Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null);
valuesMethod.setSynthetic(true);
BlockStatement code = new BlockStatement();
MethodCallExpression cloneCall = new MethodCallExpression(new FieldExpression(values), "clone", MethodCallExpression.NO_ARGUMENTS);
cloneCall.setMethodTarget(values.getType().getMethod("clone", Parameter.EMPTY_ARRAY));
code.addStatement(new ReturnStatement(cloneCall));
valuesMethod.setCode(code);
enumClass.addMethod(valuesMethod);
}
if (!hasNext) {
// create next() method, code:
// Day next() {
// int ordinal = ordinal().next()
// if (ordinal >= values().size()) ordinal = 0
// return values()[ordinal]
// }
Token assign = Token.newSymbol(Types.ASSIGN, -1, -1);
Token ge = Token.newSymbol(Types.COMPARE_GREATER_THAN_EQUAL, -1, -1);
MethodNode nextMethod = new MethodNode("next", Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, enumRef, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null);
nextMethod.setSynthetic(true);
BlockStatement code = new BlockStatement();
BlockStatement ifStatement = new BlockStatement();
ifStatement.addStatement(new ExpressionStatement(new BinaryExpression(new VariableExpression("ordinal"), assign, new ConstantExpression(0))));
code.addStatement(new ExpressionStatement(new DeclarationExpression(new VariableExpression("ordinal"), assign, new MethodCallExpression(new MethodCallExpression(VariableExpression.THIS_EXPRESSION, "ordinal", MethodCallExpression.NO_ARGUMENTS), "next", MethodCallExpression.NO_ARGUMENTS))));
code.addStatement(new IfStatement(new BooleanExpression(new BinaryExpression(new VariableExpression("ordinal"), ge, new MethodCallExpression(new FieldExpression(values), "size", MethodCallExpression.NO_ARGUMENTS))), ifStatement, EmptyStatement.INSTANCE));
code.addStatement(new ReturnStatement(new MethodCallExpression(new FieldExpression(values), "getAt", new VariableExpression("ordinal"))));
nextMethod.setCode(code);
enumClass.addMethod(nextMethod);
}
if (!hasPrevious) {
// create previous() method, code:
// Day previous() {
// int ordinal = ordinal().previous()
// if (ordinal < 0) ordinal = values().size() - 1
// return values()[ordinal]
// }
Token assign = Token.newSymbol(Types.ASSIGN, -1, -1);
Token lt = Token.newSymbol(Types.COMPARE_LESS_THAN, -1, -1);
MethodNode nextMethod = new MethodNode("previous", Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, enumRef, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null);
nextMethod.setSynthetic(true);
BlockStatement code = new BlockStatement();
BlockStatement ifStatement = new BlockStatement();
ifStatement.addStatement(new ExpressionStatement(new BinaryExpression(new VariableExpression("ordinal"), assign, new MethodCallExpression(new MethodCallExpression(new FieldExpression(values), "size", MethodCallExpression.NO_ARGUMENTS), "minus", new ConstantExpression(1)))));
code.addStatement(new ExpressionStatement(new DeclarationExpression(new VariableExpression("ordinal"), assign, new MethodCallExpression(new MethodCallExpression(VariableExpression.THIS_EXPRESSION, "ordinal", MethodCallExpression.NO_ARGUMENTS), "previous", MethodCallExpression.NO_ARGUMENTS))));
code.addStatement(new IfStatement(new BooleanExpression(new BinaryExpression(new VariableExpression("ordinal"), lt, new ConstantExpression(0))), ifStatement, EmptyStatement.INSTANCE));
code.addStatement(new ReturnStatement(new MethodCallExpression(new FieldExpression(values), "getAt", new VariableExpression("ordinal"))));
nextMethod.setCode(code);
enumClass.addMethod(nextMethod);
}
{
// create valueOf
Parameter stringParameter = new Parameter(ClassHelper.STRING_TYPE, "name");
MethodNode valueOfMethod = new MethodNode("valueOf", PS, enumRef, new Parameter[] { stringParameter }, ClassNode.EMPTY_ARRAY, null);
ArgumentListExpression callArguments = new ArgumentListExpression();
callArguments.addExpression(new ClassExpression(enumClass));
callArguments.addExpression(new VariableExpression("name"));
BlockStatement code = new BlockStatement();
code.addStatement(new ReturnStatement(new MethodCallExpression(new ClassExpression(ClassHelper.Enum_Type), "valueOf", callArguments)));
valueOfMethod.setCode(code);
valueOfMethod.setSynthetic(true);
enumClass.addMethod(valueOfMethod);
}
}
use of org.codehaus.groovy.ast.stmt.IfStatement in project groovy-core by groovy.
the class FinalVariableAnalyzer method visitIfElse.
@Override
public void visitIfElse(final IfStatement ifElse) {
visitStatement(ifElse);
ifElse.getBooleanExpression().visit(this);
Map<Variable, VariableState> ifState = pushState();
ifElse.getIfBlock().visit(this);
popState();
Statement elseBlock = ifElse.getElseBlock();
Map<Variable, VariableState> elseState = pushState();
if (elseBlock instanceof EmptyStatement) {
// dispatching to EmptyStatement will not call back visitor,
// must call our visitEmptyStatement explicitly
visitEmptyStatement((EmptyStatement) elseBlock);
} else {
elseBlock.visit(this);
}
popState();
// merge if/else branches
Map<Variable, VariableState> curState = getState();
Set<Variable> allVars = new HashSet<Variable>();
allVars.addAll(curState.keySet());
allVars.addAll(ifState.keySet());
allVars.addAll(elseState.keySet());
for (Variable var : allVars) {
VariableState beforeValue = curState.get(var);
VariableState ifValue = ifState.get(var);
VariableState elseValue = elseState.get(var);
// merge if and else values
VariableState mergedIfElse;
mergedIfElse = ifValue != null && elseValue != null && ifValue.isFinal && elseValue.isFinal ? VariableState.is_final : VariableState.is_var;
if (beforeValue == null) {
curState.put(var, mergedIfElse);
} else {
if (beforeValue == VariableState.is_uninitialized) {
curState.put(var, mergedIfElse);
} else if (ifValue != null || elseValue != null) {
curState.put(var, VariableState.is_var);
}
}
}
}
Aggregations