com.github.javaparser.ast.expr.BinaryExpr Java Examples

The following examples show how to use com.github.javaparser.ast.expr.BinaryExpr. 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: DecisionContainerGenerator.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Override
public List<Statement> setupStatements() {
    return Collections.singletonList(
            new IfStmt(
                    new BinaryExpr(
                            new MethodCallExpr(new MethodCallExpr(null, "config"), "decision"),
                            new NullLiteralExpr(),
                            BinaryExpr.Operator.NOT_EQUALS
                    ),
                    new BlockStmt().addStatement(new ExpressionStmt(new MethodCallExpr(
                            new NameExpr("decisionModels"), "init", NodeList.nodeList(new ThisExpr())
                    ))),
                    null
            )
    );
}
 
Example #2
Source File: SpringDependencyInjectionAnnotator.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Override
public Expression getMultiInstance(String fieldName) {
    return new ConditionalExpr(
            new BinaryExpr(new NameExpr(fieldName), new NullLiteralExpr(), BinaryExpr.Operator.NOT_EQUALS),
            new NameExpr(fieldName),
            new MethodCallExpr(new TypeExpr(new ClassOrInterfaceType(null, Collections.class.getCanonicalName())), "emptyList")
    );
}
 
Example #3
Source File: PrettyPrintVisitor.java    From stategen with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void visit(final BinaryExpr n, final Void arg) {
    printJavaComment(n.getComment(), arg);
    n.getLeft().accept(this, arg);
    printer.print(" ");
    printer.print(n.getOperator().asString());
    printer.print(" ");
    n.getRight().accept(this, arg);
}
 
Example #4
Source File: BinaryExprMerger.java    From dolphin with Apache License 2.0 5 votes vote down vote up
@Override public BinaryExpr doMerge(BinaryExpr first, BinaryExpr second) {
  BinaryExpr be = new BinaryExpr();

  be.setOperator(first.getOperator());
  be.setLeft(mergeSingle(first.getLeft(),second.getLeft()));
  be.setRight(mergeSingle(first.getRight(),second.getRight()));

  return be;
}
 
Example #5
Source File: BinaryExprMerger.java    From dolphin with Apache License 2.0 5 votes vote down vote up
@Override public boolean doIsEquals(BinaryExpr first, BinaryExpr second) {

    if(!isEqualsUseMerger(first.getLeft(),second.getLeft())) return false;
    if(!isEqualsUseMerger(first.getRight(),second.getRight())) return false;
    if(!first.getOperator().equals(second.getOperator())) return false;

    return true;
  }
 
Example #6
Source File: TestCodeVisitor.java    From CodeDefenders with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void visit(final BinaryExpr stmt, Void args) {
    if (!isValid) {
        return;
    }
    final BinaryExpr.Operator operator = stmt.getOperator();
    if (operator == BinaryExpr.Operator.AND || operator == BinaryExpr.Operator.OR) {
        messages.add("Test contains an invalid statement: " + stmt.toString());
        isValid = false;
    }
    super.visit(stmt, args);
}
 
Example #7
Source File: CompilerJarReferenceTest.java    From turin-programming-language with Apache License 2.0 5 votes vote down vote up
@Test
public void compileFunctionUsinStaticMethodFromJar() throws IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchMethodException, IOException {
    Method invoke = compileFunction("use_jar_static_method", new Class[]{}, ImmutableList.of("src/test/resources/jars/javaparser-core-2.2.1.jar"));
    Expression expression = (Expression) invoke.invoke(null);
    assertEquals(true, expression instanceof BinaryExpr);
    BinaryExpr binaryExpr = (BinaryExpr)expression;
    assertEquals(BinaryExpr.Operator.plus, binaryExpr.getOperator());
}
 
Example #8
Source File: MigrateJCas.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
   * Visitor for if stmts
   *   - removes feature missing test
   */
  @Override
  public void visit(IfStmt n, Object ignore) {
    do {
      // if (get_set_method == null) break;  // sometimes, these occur outside of recogn. getters/setters
      
      Expression c = n.getCondition(), e;
      BinaryExpr be, be2; 
      List<Statement> stmts;
      if ((c instanceof BinaryExpr) &&
          ((be = (BinaryExpr)c).getLeft() instanceof FieldAccessExpr) &&
          ((FieldAccessExpr)be.getLeft()).getNameAsString().equals("featOkTst")) {
        // remove the feature missing if statement
        
        // verify the remaining form 
        if (! (be.getRight() instanceof BinaryExpr) 
         || ! ((be2 = (BinaryExpr)be.getRight()).getRight() instanceof NullLiteralExpr) 
         || ! (be2.getLeft() instanceof FieldAccessExpr)

         || ! ((e = getExpressionFromStmt(n.getThenStmt())) instanceof MethodCallExpr)
         || ! (((MethodCallExpr)e).getNameAsString()).equals("throwFeatMissing")) {
          reportDeletedCheckModified("The featOkTst was modified:\n" + n.toString() + '\n');
        }
              
        BlockStmt parent = (BlockStmt) n.getParentNode().get();
        stmts = parent.getStatements();
        stmts.set(stmts.indexOf(n), new EmptyStmt()); //dont remove
                                            // otherwise iterators fail
//        parent.getStmts().remove(n);
        return;
      }
    } while (false);
    
    super.visit(n,  ignore);
  }
 
Example #9
Source File: CDIDependencyInjectionAnnotator.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Override
public Expression optionalInstanceExists(String fieldName) {
    MethodCallExpr condition = new MethodCallExpr(new NameExpr(fieldName), "isUnsatisfied");
    return new BinaryExpr(condition, new BooleanLiteralExpr(false), BinaryExpr.Operator.EQUALS);
}
 
Example #10
Source File: RuleUnitInstanceGenerator.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
private MethodDeclaration bindMethod() {
    MethodDeclaration methodDeclaration = new MethodDeclaration();

    BlockStmt methodBlock = new BlockStmt();
    methodDeclaration.setName("bind")
            .addAnnotation( "Override" )
            .addModifier(Modifier.Keyword.PROTECTED)
            .addParameter(KieSession.class.getCanonicalName(), "runtime")
            .addParameter(ruleUnitDescription.getRuleUnitName(), "value")
            .setType(void.class)
            .setBody(methodBlock);

    try {


        for (RuleUnitVariable m : ruleUnitDescription.getUnitVarDeclarations()) {
            String methodName = m.getter();
            String propertyName = m.getName();

            if ( m.isDataSource() ) {

                if (m.setter() != null) { // if writable and DataSource is null create and set a new one
                    Expression nullCheck = new BinaryExpr(new MethodCallExpr(new NameExpr("value"), methodName), new NullLiteralExpr(), BinaryExpr.Operator.EQUALS);
                    Expression createDataSourceExpr = new MethodCallExpr(new NameExpr(DataSource.class.getCanonicalName()), ruleUnitHelper.createDataSourceMethodName(m.getBoxedVarType()));
                    Expression dataSourceSetter = new MethodCallExpr(new NameExpr("value"), m.setter(), new NodeList<>(createDataSourceExpr));
                    methodBlock.addStatement( new IfStmt( nullCheck, new BlockStmt().addStatement( dataSourceSetter ), null ) );
                }

                //  value.$method())
                Expression fieldAccessor =
                        new MethodCallExpr(new NameExpr("value"), methodName);

                // .subscribe( new EntryPointDataProcessor(runtime.getEntryPoint()) )

                String entryPointName = getEntryPointName(ruleUnitDescription, propertyName);
                MethodCallExpr drainInto = new MethodCallExpr(fieldAccessor, "subscribe")
                        .addArgument(new ObjectCreationExpr(null, StaticJavaParser.parseClassOrInterfaceType( EntryPointDataProcessor.class.getName() ), NodeList.nodeList(
                                new MethodCallExpr(
                                        new NameExpr("runtime"), "getEntryPoint",
                                        NodeList.nodeList(new StringLiteralExpr( entryPointName ))))));

                methodBlock.addStatement(drainInto);
            }

            MethodCallExpr setGlobalCall = new MethodCallExpr( new NameExpr("runtime"), "setGlobal" );
            setGlobalCall.addArgument( new StringLiteralExpr( propertyName ) );
            setGlobalCall.addArgument( new MethodCallExpr(new NameExpr("value"), methodName) );
            methodBlock.addStatement(setGlobalCall);
        }

    } catch (Exception e) {
        throw new Error(e);
    }

    return methodDeclaration;
}
 
Example #11
Source File: RegistryMethodVisitor.java    From gauge-java with Apache License 2.0 4 votes vote down vote up
private String getParameterizedStep(Expression expression) {
    if (expression instanceof BinaryExpr) {
        return Util.trimQuotes(((BinaryExpr) expression).getLeft().toString()) + Util.trimQuotes(((BinaryExpr) expression).getRight().toString());
    }
    return Util.trimQuotes(expression.toString());
}
 
Example #12
Source File: TraceVisitor.java    From JCTools with Apache License 2.0 4 votes vote down vote up
@Override
public void visit(BinaryExpr n, Void arg) {
    out.println("BinaryExpr: " + (extended ? n : n.getLeft() + " " + n.getOperator() + " " + n.getRight()));
    super.visit(n, arg);
}