org.codehaus.groovy.ast.expr.BooleanExpression Java Examples
The following examples show how to use
org.codehaus.groovy.ast.expr.BooleanExpression.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StatementWriter.java From groovy with Apache License 2.0 | 6 votes |
private void visitConditionOfLoopingStatement(final BooleanExpression expression, final Label breakLabel, final MethodVisitor mv) { boolean boolHandled = false; if (expression.getExpression() instanceof ConstantExpression) { ConstantExpression constant = (ConstantExpression) expression.getExpression(); if (constant.getValue() == Boolean.TRUE) { boolHandled = true; // do nothing } else if (constant.getValue() == Boolean.FALSE) { boolHandled = true; mv.visitJumpInsn(GOTO, breakLabel); } } if (!boolHandled) { expression.visit(controller.getAcg()); controller.getOperandStack().jump(IFEQ, breakLabel); } }
Example #2
Source File: AssertionWriter.java From groovy with Apache License 2.0 | 5 votes |
private void addVariableNames(Expression expression, List<String> list) { if (expression instanceof BooleanExpression) { BooleanExpression boolExp = (BooleanExpression) expression; addVariableNames(boolExp.getExpression(), list); } else if (expression instanceof BinaryExpression) { BinaryExpression binExp = (BinaryExpression) expression; addVariableNames(binExp.getLeftExpression(), list); addVariableNames(binExp.getRightExpression(), list); } else if (expression instanceof VariableExpression) { VariableExpression varExp = (VariableExpression) expression; list.add(varExp.getName()); } }
Example #3
Source File: TryWithResourcesASTTransformation.java From groovy with Apache License 2.0 | 5 votes |
private BlockStatement createFinallyBlockForNewTryCatchStatement(String primaryExcName, String firstResourceIdentifierName) { BlockStatement finallyBlock = new BlockStatement(); // primaryExc != null BooleanExpression conditionExpression = new BooleanExpression( new BinaryExpression( new VariableExpression(primaryExcName), newSymbol(Types.COMPARE_NOT_EQUAL, -1, -1), new ConstantExpression(null))); // try-catch statement TryCatchStatement newTryCatchStatement = new TryCatchStatement( astBuilder.createBlockStatement(this.createCloseResourceStatement(firstResourceIdentifierName)), // { Identifier?.close(); } EmptyStatement.INSTANCE); String suppressedExcName = this.genSuppressedExcName(); newTryCatchStatement.addCatch( // catch (Throwable #suppressedExc) { .. } new CatchStatement( new Parameter(ClassHelper.make(Throwable.class), suppressedExcName), astBuilder.createBlockStatement(this.createAddSuppressedStatement(primaryExcName, suppressedExcName)) // #primaryExc.addSuppressed(#suppressedExc); ) ); // if (#primaryExc != null) { ... } IfStatement ifStatement = new IfStatement( conditionExpression, newTryCatchStatement, this.createCloseResourceStatement(firstResourceIdentifierName) // Identifier?.close(); ); astBuilder.appendStatementsToBlockStatement(finallyBlock, ifStatement); return astBuilder.createBlockStatement(finallyBlock); }
Example #4
Source File: GeneralUtils.java From groovy with Apache License 2.0 | 5 votes |
public static IfStatement ifElseS(final Expression cond, final Statement thenStmt, final Statement elseStmt) { return new IfStatement( cond instanceof BooleanExpression ? (BooleanExpression) cond : boolX(cond), thenStmt, elseStmt ); }
Example #5
Source File: AsmClassGenerator.java From groovy with Apache License 2.0 | 5 votes |
@Override public void visitBooleanExpression(final BooleanExpression expression) { int mark = controller.getOperandStack().getStackLength(); expression.getExpression().visit(this); controller.getOperandStack().castToBool(mark, true); }
Example #6
Source File: BooleanExpressionTransformer.java From groovy with Apache License 2.0 | 5 votes |
Expression transformBooleanExpression(final BooleanExpression booleanExpression) { if (booleanExpression instanceof NotExpression) { return transformer.superTransform(booleanExpression); } final Expression expression = booleanExpression.getExpression(); if (!(expression instanceof BinaryExpression)) { StaticTypesTypeChooser typeChooser = transformer.getTypeChooser(); final ClassNode type = typeChooser.resolveType(expression, transformer.getClassNode()); BooleanExpression transformed = new OptimizingBooleanExpression(transformer.transform(expression),type); transformed.setSourcePosition(booleanExpression); transformed.copyNodeMetaData(booleanExpression); return transformed; } return transformer.superTransform(booleanExpression); }
Example #7
Source File: StaticCompilationTransformer.java From groovy with Apache License 2.0 | 5 votes |
@Override public Expression transform(Expression expr) { if (expr instanceof StaticMethodCallExpression) { return staticMethodCallExpressionTransformer.transformStaticMethodCallExpression((StaticMethodCallExpression) expr); } if (expr instanceof BinaryExpression) { return binaryExpressionTransformer.transformBinaryExpression((BinaryExpression)expr); } if (expr instanceof MethodCallExpression) { return methodCallExpressionTransformer.transformMethodCallExpression((MethodCallExpression) expr); } if (expr instanceof ClosureExpression) { return closureExpressionTransformer.transformClosureExpression((ClosureExpression) expr); } if (expr instanceof ConstructorCallExpression) { return constructorCallTransformer.transformConstructorCall((ConstructorCallExpression) expr); } if (expr instanceof BooleanExpression) { return booleanExpressionTransformer.transformBooleanExpression((BooleanExpression)expr); } if (expr instanceof VariableExpression) { return variableExpressionTransformer.transformVariableExpression((VariableExpression)expr); } if (expr instanceof RangeExpression) { return rangeExpressionTransformer.transformRangeExpression(((RangeExpression)expr)); } if (expr instanceof ListExpression) { return listExpressionTransformer.transformListExpression((ListExpression) expr); } if (expr instanceof CastExpression) { return castExpressionTransformer.transformCastExpression(((CastExpression)expr)); } return super.transform(expr); }
Example #8
Source File: TraitComposer.java From groovy with Apache License 2.0 | 5 votes |
private static Statement createSuperFallback(MethodNode forwarderMethod, ClassNode returnType) { ArgumentListExpression args = new ArgumentListExpression(); Parameter[] forwarderMethodParameters = forwarderMethod.getParameters(); for (final Parameter forwarderMethodParameter : forwarderMethodParameters) { args.addExpression(new VariableExpression(forwarderMethodParameter)); } BinaryExpression instanceOfExpr = new BinaryExpression(new VariableExpression("this"), Token.newSymbol(Types.KEYWORD_INSTANCEOF, -1, -1), new ClassExpression(Traits.GENERATED_PROXY_CLASSNODE)); MethodCallExpression superCall = new MethodCallExpression( new VariableExpression("super"), forwarderMethod.getName(), args ); superCall.setImplicitThis(false); CastExpression proxyReceiver = new CastExpression(Traits.GENERATED_PROXY_CLASSNODE, new VariableExpression("this")); MethodCallExpression getProxy = new MethodCallExpression(proxyReceiver, "getProxyTarget", ArgumentListExpression.EMPTY_ARGUMENTS); getProxy.setImplicitThis(true); StaticMethodCallExpression proxyCall = new StaticMethodCallExpression( ClassHelper.make(InvokerHelper.class), "invokeMethod", new ArgumentListExpression(getProxy, new ConstantExpression(forwarderMethod.getName()), new ArrayExpression(ClassHelper.OBJECT_TYPE, args.getExpressions())) ); IfStatement stmt = new IfStatement( new BooleanExpression(instanceOfExpr), new ExpressionStatement(new CastExpression(returnType,proxyCall)), new ExpressionStatement(superCall) ); return stmt; }
Example #9
Source File: TraitReceiverTransformer.java From groovy with Apache License 2.0 | 5 votes |
private TernaryExpression createStaticReceiver(final Expression receiver) { return new TernaryExpression( new BooleanExpression(new BinaryExpression( receiver, Token.newSymbol(Types.KEYWORD_INSTANCEOF, -1, -1), new ClassExpression(ClassHelper.CLASS_Type) )), receiver, new MethodCallExpression(createFieldHelperReceiver(), "getClass", ArgumentListExpression.EMPTY_ARGUMENTS) ); }
Example #10
Source File: ASTNodeVisitor.java From groovy-language-server with Apache License 2.0 | 5 votes |
public void visitBooleanExpression(BooleanExpression node) { pushASTNode(node); try { super.visitBooleanExpression(node); } finally { popASTNode(); } }
Example #11
Source File: GeneralUtils.java From groovy with Apache License 2.0 | 4 votes |
public static BooleanExpression isInstanceOfX(final Expression objectExpression, final ClassNode cNode) { return boolX(binX(objectExpression, INSTANCEOF, classX(cNode))); }
Example #12
Source File: GeneralUtils.java From groovy with Apache License 2.0 | 4 votes |
public static BooleanExpression notNullX(final Expression argExpr) { return boolX(binX(argExpr, NE, nullX())); }
Example #13
Source File: GeneralUtils.java From groovy with Apache License 2.0 | 4 votes |
public static BooleanExpression hasSamePropertyX(final PropertyNode pNode, final Expression other) { ClassNode cNode = pNode.getDeclaringClass(); return sameX(getterThisX(cNode, pNode), getterX(cNode, other, pNode)); }
Example #14
Source File: GeneralUtils.java From groovy with Apache License 2.0 | 4 votes |
public static BooleanExpression hasSameFieldX(final FieldNode fNode, final Expression other) { return sameX(varX(fNode), propX(other, fNode.getName())); }
Example #15
Source File: GeneralUtils.java From groovy with Apache License 2.0 | 4 votes |
public static BooleanExpression equalsNullX(final Expression argExpr) { return boolX(eqX(argExpr, nullX())); }
Example #16
Source File: GeneralUtils.java From groovy with Apache License 2.0 | 4 votes |
public static BooleanExpression boolX(final Expression expr) { return new BooleanExpression(expr); }
Example #17
Source File: CodeVisitorSupport.java From groovy with Apache License 2.0 | 4 votes |
@Override public void visitBooleanExpression(BooleanExpression expression) { expression.getExpression().visit(this); }
Example #18
Source File: IfStatement.java From groovy with Apache License 2.0 | 4 votes |
public void setBooleanExpression(BooleanExpression booleanExpression) { this.booleanExpression = booleanExpression; }
Example #19
Source File: GeneralUtils.java From groovy with Apache License 2.0 | 4 votes |
/** * Alias for {@link #equalsNullX(Expression)} */ public static BooleanExpression isNullX(final Expression expr) { return equalsNullX(expr); }
Example #20
Source File: GeneralUtils.java From groovy with Apache License 2.0 | 4 votes |
public static BooleanExpression isOneX(final Expression expr) { return boolX(binX(expr, EQ, constX(1))); }
Example #21
Source File: GeneralUtils.java From groovy with Apache License 2.0 | 4 votes |
public static BooleanExpression isTrueX(final Expression argExpr) { return boolX(binX(argExpr, EQ, constX(Boolean.TRUE))); }
Example #22
Source File: GeneralUtils.java From groovy with Apache License 2.0 | 4 votes |
public static BooleanExpression isZeroX(final Expression expr) { return boolX(binX(expr, EQ, constX(0))); }
Example #23
Source File: AssertStatement.java From groovy with Apache License 2.0 | 4 votes |
public void setBooleanExpression(BooleanExpression booleanExpression) { this.booleanExpression = booleanExpression; }
Example #24
Source File: GeneralUtils.java From groovy with Apache License 2.0 | 4 votes |
public static NotExpression notX(final Expression expr) { return new NotExpression(expr instanceof BooleanExpression ? expr : boolX(expr)); }
Example #25
Source File: GeneralUtils.java From groovy with Apache License 2.0 | 4 votes |
public static BooleanExpression sameX(final Expression self, final Expression other) { return boolX(callX(self, "is", args(other))); }
Example #26
Source File: GeneralUtils.java From groovy with Apache License 2.0 | 4 votes |
public static TernaryExpression ternaryX(final Expression cond, final Expression trueExpr, final Expression elseExpr) { return new TernaryExpression( cond instanceof BooleanExpression ? (BooleanExpression) cond : boolX(cond), trueExpr, elseExpr); }
Example #27
Source File: AssertionWriter.java From groovy with Apache License 2.0 | 4 votes |
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); } }
Example #28
Source File: IfElseTest.java From groovy with Apache License 2.0 | 4 votes |
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.getDeclaredConstructor().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"); }
Example #29
Source File: DoWhileStatement.java From groovy with Apache License 2.0 | 4 votes |
public DoWhileStatement(BooleanExpression booleanExpression, Statement loopBlock) { this.booleanExpression = booleanExpression; this.loopBlock = loopBlock; }
Example #30
Source File: PathFinderVisitor.java From netbeans with Apache License 2.0 | 4 votes |
@Override public void visitBooleanExpression(BooleanExpression node) { if (isInside(node, line, column)) { super.visitBooleanExpression(node); } }