use of org.codehaus.groovy.ast.expr.BooleanExpression in project groovy-core by groovy.
the class AssertionWriter method writeSourcelessAssertText.
private void writeSourcelessAssertText(AssertStatement statement) {
MethodVisitor mv = controller.getMethodVisitor();
OperandStack operandStack = controller.getOperandStack();
BooleanExpression booleanExpression = statement.getBooleanExpression();
// push expression string onto stack
String expressionText = booleanExpression.getText();
List<String> list = new ArrayList<String>();
addVariableNames(booleanExpression, list);
if (list.isEmpty()) {
mv.visitLdcInsn(expressionText);
} else {
boolean first = true;
// let's create a new expression
mv.visitTypeInsn(NEW, "java/lang/StringBuffer");
mv.visitInsn(DUP);
mv.visitLdcInsn(expressionText + ". Values: ");
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuffer", "<init>", "(Ljava/lang/String;)V", false);
// TODO: maybe use more special type StringBuffer here
operandStack.push(ClassHelper.OBJECT_TYPE);
int tempIndex = controller.getCompileStack().defineTemporaryVariable("assert", true);
for (String name : list) {
String text = name + " = ";
if (first) {
first = false;
} else {
text = ", " + text;
}
mv.visitVarInsn(ALOAD, tempIndex);
mv.visitLdcInsn(text);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "append", "(Ljava/lang/Object;)Ljava/lang/StringBuffer;", false);
mv.visitInsn(POP);
mv.visitVarInsn(ALOAD, tempIndex);
new VariableExpression(name).visit(controller.getAcg());
operandStack.box();
mv.visitMethodInsn(INVOKESTATIC, "org/codehaus/groovy/runtime/InvokerHelper", "toString", "(Ljava/lang/Object;)Ljava/lang/String;", false);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;", false);
mv.visitInsn(POP);
operandStack.remove(1);
}
mv.visitVarInsn(ALOAD, tempIndex);
controller.getCompileStack().removeVar(tempIndex);
}
}
use of org.codehaus.groovy.ast.expr.BooleanExpression 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.expr.BooleanExpression in project grails-core by grails.
the class ControllerActionTransformer method initializeStringParameter.
protected void initializeStringParameter(final ClassNode classNode, final BlockStatement wrapper, final Parameter param, final String requestParameterName) {
final ClassNode paramTypeClassNode = param.getType();
final String methodParamName = param.getName();
Expression getParamsExpression = buildGetPropertyExpression(new VariableExpression("this"), "params", classNode);
final Expression paramsContainsKeyMethodArguments = new ArgumentListExpression(new ConstantExpression(requestParameterName));
final BooleanExpression containsKeyExpression = new BooleanExpression(applyDefaultMethodTarget(new MethodCallExpression(getParamsExpression, "containsKey", paramsContainsKeyMethodArguments), Map.class));
final Statement initializeParameterStatement = new ExpressionStatement(new DeclarationExpression(new VariableExpression(methodParamName, paramTypeClassNode), Token.newSymbol(Types.EQUALS, 0, 0), new TernaryExpression(containsKeyExpression, buildGetMapExpression(getParamsExpression, requestParameterName), new ConstantExpression(null))));
wrapper.addStatement(initializeParameterStatement);
}
Aggregations