Search in sources :

Example 41 with ObjectCreationExpr

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;
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) ArrayList(java.util.ArrayList)

Example 42 with ObjectCreationExpr

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()));
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr)

Example 43 with ObjectCreationExpr

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()));
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 44 with ObjectCreationExpr

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();
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) NodeList(com.github.javaparser.ast.NodeList) List(java.util.List) StaticJavaParser(com.github.javaparser.StaticJavaParser) JavaParserUtils(org.kie.pmml.compiler.commons.utils.JavaParserUtils) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Expression(com.github.javaparser.ast.expr.Expression) CompilationUnit(com.github.javaparser.ast.CompilationUnit) NameExpr(com.github.javaparser.ast.expr.NameExpr) Collectors(java.util.stream.Collectors) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Expression(com.github.javaparser.ast.expr.Expression) NameExpr(com.github.javaparser.ast.expr.NameExpr) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 45 with ObjectCreationExpr

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());
    });
}
Also used : RESULT_FEATURE(org.kie.pmml.api.enums.RESULT_FEATURE) Arrays(java.util.Arrays) Spliterators(java.util.Spliterators) Random(java.util.Random) OP_TYPE(org.kie.pmml.api.enums.OP_TYPE) FIELD_USAGE_TYPE(org.kie.pmml.api.enums.FIELD_USAGE_TYPE) GET_CREATED_MININGFIELDS(org.kie.pmml.compiler.commons.codegenfactories.KiePMMLModelFactoryUtils.GET_CREATED_MININGFIELDS) KiePMMLUtil(org.kie.pmml.compiler.commons.utils.KiePMMLUtil) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) GET_CREATED_LOCAL_TRANSFORMATIONS(org.kie.pmml.compiler.commons.codegenfactories.KiePMMLModelFactoryUtils.GET_CREATED_LOCAL_TRANSFORMATIONS) GET_CREATED_TRANSFORMATION_DICTIONARY(org.kie.pmml.compiler.commons.codegenfactories.KiePMMLModelFactoryUtils.GET_CREATED_TRANSFORMATION_DICTIONARY) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) GET_CREATED_OUTPUTFIELDS(org.kie.pmml.compiler.commons.codegenfactories.KiePMMLModelFactoryUtils.GET_CREATED_OUTPUTFIELDS) GET_CREATED_KIEPMMLOUTPUTFIELDS(org.kie.pmml.compiler.commons.codegenfactories.KiePMMLModelFactoryUtils.GET_CREATED_KIEPMMLOUTPUTFIELDS) Expression(com.github.javaparser.ast.expr.Expression) Assert.fail(org.junit.Assert.fail) CompilationUnit(com.github.javaparser.ast.CompilationUnit) Interval(org.kie.pmml.api.models.Interval) ModelUtils(org.kie.pmml.compiler.api.utils.ModelUtils) NodeList(com.github.javaparser.ast.NodeList) PMML(org.dmg.pmml.PMML) CommonCodegenUtils(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils) KiePMMLTargetValue(org.kie.pmml.commons.model.KiePMMLTargetValue) GET_CREATED_KIEPMMLTARGETS(org.kie.pmml.compiler.commons.codegenfactories.KiePMMLModelFactoryUtils.GET_CREATED_KIEPMMLTARGETS) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) TreeModel(org.dmg.pmml.tree.TreeModel) Collectors(java.util.stream.Collectors) ExplicitConstructorInvocationStmt(com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt) FileUtils.getFileContent(org.kie.test.util.filesystem.FileUtils.getFileContent) List(java.util.List) Stream(java.util.stream.Stream) CommonCompilationDTO(org.kie.pmml.compiler.api.dto.CommonCompilationDTO) Optional(java.util.Optional) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) Spliterator(java.util.Spliterator) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) IntStream(java.util.stream.IntStream) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) BeforeClass(org.junit.BeforeClass) OutputField(org.kie.pmml.api.models.OutputField) JavaParserUtils.getFromFileName(org.kie.pmml.compiler.commons.utils.JavaParserUtils.getFromFileName) TargetField(org.kie.pmml.api.models.TargetField) MiningField(org.kie.pmml.api.models.MiningField) CAST_INTEGER(org.kie.pmml.api.enums.CAST_INTEGER) StreamSupport(java.util.stream.StreamSupport) GET_CREATED_KIEPMMLMININGFIELDS(org.kie.pmml.compiler.commons.codegenfactories.KiePMMLModelFactoryUtils.GET_CREATED_KIEPMMLMININGFIELDS) MISSING_VALUE_TREATMENT_METHOD(org.kie.pmml.api.enums.MISSING_VALUE_TREATMENT_METHOD) Before(org.junit.Before) INVALID_VALUE_TREATMENT_METHOD(org.kie.pmml.api.enums.INVALID_VALUE_TREATMENT_METHOD) CompilationDTO(org.kie.pmml.compiler.api.dto.CompilationDTO) JavaParserUtils(org.kie.pmml.compiler.commons.utils.JavaParserUtils) PACKAGE_NAME(org.kie.pmml.commons.Constants.PACKAGE_NAME) CommonCodegenUtils.getChainedMethodCallExprFrom(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getChainedMethodCallExprFrom) Assert.assertNotNull(org.junit.Assert.assertNotNull) PMMLModelTestUtils.getRandomOpType(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomOpType) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) NameExpr(com.github.javaparser.ast.expr.NameExpr) PMMLModelTestUtils.getRandomDataField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomDataField) Statement(com.github.javaparser.ast.stmt.Statement) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE) PMMLModelTestUtils.getRandomOutputField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomOutputField) PMMLModelTestUtils.getRandomTarget(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomTarget) KiePMMLTarget(org.kie.pmml.commons.model.KiePMMLTarget) FileUtils.getFileInputStream(org.kie.test.util.filesystem.FileUtils.getFileInputStream) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) CodegenTestUtils.commonValidateCompilationWithImports(org.kie.pmml.compiler.commons.testutils.CodegenTestUtils.commonValidateCompilationWithImports) PMMLModelTestUtils.getRandomCastInteger(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomCastInteger) PMMLModelTestUtils.getRandomMiningField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomMiningField) HasClassLoaderMock(org.kie.pmml.compiler.commons.mocks.HasClassLoaderMock) GET_MODEL(org.kie.pmml.commons.Constants.GET_MODEL) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) TargetValue(org.kie.pmml.api.models.TargetValue) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Random(java.util.Random) Expression(com.github.javaparser.ast.expr.Expression) Interval(org.kie.pmml.api.models.Interval) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Test(org.junit.Test)

Aggregations

ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)83 Expression (com.github.javaparser.ast.expr.Expression)59 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)44 NameExpr (com.github.javaparser.ast.expr.NameExpr)41 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)38 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)31 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)30 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)30 NodeList (com.github.javaparser.ast.NodeList)24 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)24 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)21 CommonCodegenUtils.getVariableDeclarator (org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator)19 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)14 List (java.util.List)13 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)12 Type (com.github.javaparser.ast.type.Type)12 Test (org.junit.Test)12 StaticJavaParser.parseClassOrInterfaceType (com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType)11 DrlxParseUtil.toClassOrInterfaceType (org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType)11 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)10