use of com.github.javaparser.ast.expr.ObjectCreationExpr in project checker-framework by typetools.
the class JointJavacJavaParserVisitor method visitNewClass.
@Override
public Void visitNewClass(NewClassTree javacTree, Node javaParserNode) {
ObjectCreationExpr node = castNode(ObjectCreationExpr.class, javaParserNode, javacTree);
processNewClass(javacTree, node);
// MyInnerClass(this). So, we only traverse the enclosing expression if present in both.
if (javacTree.getEnclosingExpression() != null && node.getScope().isPresent()) {
javacTree.getEnclosingExpression().accept(this, node.getScope().get());
}
javacTree.getIdentifier().accept(this, node.getType());
if (javacTree.getTypeArguments().isEmpty()) {
assert !node.getTypeArguments().isPresent();
} else {
assert node.getTypeArguments().isPresent();
visitLists(javacTree.getTypeArguments(), node.getTypeArguments().get());
}
// Remove synthetic javac argument. When using Java 11, an expression like this.new
// MyInnerClass() would store "this" as the enclosing expression. In Java 8, this would be
// stored as new MyInnerClass(this). So, for the argument lists to match, we may have to
// remove the first argument.
List<? extends ExpressionTree> javacArgs = new ArrayList<>(javacTree.getArguments());
if (javacArgs.size() > node.getArguments().size()) {
javacArgs.remove(0);
}
visitLists(javacArgs, node.getArguments());
assert (javacTree.getClassBody() != null) == node.getAnonymousClassBody().isPresent();
if (javacTree.getClassBody() != null) {
visitAnonymousClassBody(javacTree.getClassBody(), node.getAnonymousClassBody().get());
}
return null;
}
use of com.github.javaparser.ast.expr.ObjectCreationExpr in project checker-framework by typetools.
the class DoubleJavaParserVisitor method visit.
@Override
public void visit(final ObjectCreationExpr node1, final Node other) {
ObjectCreationExpr node2 = (ObjectCreationExpr) other;
defaultAction(node1, node2);
node1.getAnonymousClassBody().ifPresent(l -> visitLists(l, node2.getAnonymousClassBody().get()));
visitLists(node1.getArguments(), node2.getArguments());
node1.getScope().ifPresent(l -> l.accept(this, node2.getScope().get()));
node1.getType().accept(this, node2.getType());
node1.getTypeArguments().ifPresent(l -> visitLists(l, node2.getTypeArguments().get()));
}
use of com.github.javaparser.ast.expr.ObjectCreationExpr in project javaparser by javaparser.
the class Issue235 method issue235.
@Test
public void issue235() {
CompilationUnit cu = parseSample("Issue235");
ClassOrInterfaceDeclaration cls = Navigator.demandClassOrInterface(cu, "Foo");
TypeSolver typeSolver = new ReflectionTypeSolver();
JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
MethodDeclaration m = Navigator.demandMethod(cls, this.method);
ExpressionStmt stmt = (ExpressionStmt) m.getBody().get().getStatements().get(0);
ObjectCreationExpr expression = (ObjectCreationExpr) stmt.getExpression();
Assert.assertNotNull(javaParserFacade.convertToUsage(expression.getType()));
}
use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.
the class PMMLRuleMappersFactory method getPMMLRuleMappersSource.
public static String getPMMLRuleMappersSource(final String packageName, final List<String> generatedRuleMappers) {
CompilationUnit cloneCU = JavaParserUtils.getKiePMMLModelCompilationUnit(KIE_PMML_RULE_MAPPERS_CLASS_NAME, packageName, KIE_PMML_RULE_MAPPERS_TEMPLATE_JAVA, KIE_PMML_RULE_MAPPERS_CLASS_NAME);
ClassOrInterfaceDeclaration typeDeclaration = (ClassOrInterfaceDeclaration) cloneCU.getTypes().get(0);
FieldDeclaration ruleNameField = typeDeclaration.getFieldByName("pmmlRuleMappers").orElseThrow(() -> new RuntimeException("The template " + KIE_PMML_RULE_MAPPERS_TEMPLATE_JAVA + " has been modified."));
final List<Expression> nodeList = generatedRuleMappers.stream().map(generatedRuleMapper -> {
ObjectCreationExpr objectCreationExpr = new ObjectCreationExpr();
objectCreationExpr.setType(generatedRuleMapper);
return objectCreationExpr;
}).collect(Collectors.toList());
NodeList<Expression> expressions = NodeList.nodeList(nodeList);
MethodCallExpr methodCallExpr = new MethodCallExpr(new NameExpr("Arrays"), "asList", expressions);
ruleNameField.getVariables().get(0).setInitializer(methodCallExpr);
return cloneCU.toString();
}
use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.
the class KiePMMLModelFactoryUtilsTest method createIntervalsExpression.
@Test
public void createIntervalsExpression() {
List<Interval> intervals = IntStream.range(0, 3).mapToObj(i -> {
int leftMargin = new Random().nextInt(40);
int rightMargin = leftMargin + 13;
return new Interval(leftMargin, rightMargin);
}).collect(Collectors.toList());
Expression retrieved = KiePMMLModelFactoryUtils.createIntervalsExpression(intervals);
assertNotNull(retrieved);
assertTrue(retrieved instanceof MethodCallExpr);
MethodCallExpr mtdExp = (MethodCallExpr) retrieved;
String expected = "java.util.Arrays";
assertEquals(expected, mtdExp.getScope().get().asNameExpr().toString());
expected = "asList";
assertEquals(expected, mtdExp.getName().asString());
NodeList<Expression> arguments = mtdExp.getArguments();
assertEquals(intervals.size(), arguments.size());
arguments.forEach(argument -> {
assertTrue(argument instanceof ObjectCreationExpr);
ObjectCreationExpr objCrt = (ObjectCreationExpr) argument;
assertEquals(Interval.class.getCanonicalName(), objCrt.getType().asString());
Optional<Interval> intervalOpt = intervals.stream().filter(interval -> String.valueOf(interval.getLeftMargin()).equals(objCrt.getArgument(0).asNameExpr().toString()) && String.valueOf(interval.getRightMargin()).equals(objCrt.getArgument(1).asNameExpr().toString())).findFirst();
assertTrue(intervalOpt.isPresent());
});
}
Aggregations