Search in sources :

Example 11 with ExplicitConstructorInvocationStmt

use of com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt in project checker-framework by typetools.

the class JointJavacJavaParserVisitor method visitExpressionStatement.

@Override
public Void visitExpressionStatement(ExpressionStatementTree javacTree, Node javaParserNode) {
    if (javaParserNode instanceof ExpressionStmt) {
        ExpressionStmt node = (ExpressionStmt) javaParserNode;
        processExpressionStatemen(javacTree, node);
        javacTree.getExpression().accept(this, node.getExpression());
    } else if (javaParserNode instanceof ExplicitConstructorInvocationStmt) {
        // In this case the javac expression will be a MethodTree. Since JavaParser doesn't
        // surround explicit constructor invocations in an expression statement, we match
        // javaParserNode to the javac expression rather than the javac expression statement.
        javacTree.getExpression().accept(this, javaParserNode);
    } else {
        throwUnexpectedNodeType(javacTree, javaParserNode);
    }
    return null;
}
Also used : ExplicitConstructorInvocationStmt(com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt)

Example 12 with ExplicitConstructorInvocationStmt

use of com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt in project checker-framework by typetools.

the class JointJavacJavaParserVisitor method visitMethodInvocation.

@Override
public Void visitMethodInvocation(MethodInvocationTree javacTree, Node javaParserNode) {
    if (javaParserNode instanceof MethodCallExpr) {
        MethodCallExpr node = (MethodCallExpr) javaParserNode;
        processMethodInvocation(javacTree, node);
        // JavaParser the type arguments will have the none Optional value.
        assert javacTree.getTypeArguments().isEmpty() != node.getTypeArguments().isPresent();
        if (!javacTree.getTypeArguments().isEmpty()) {
            visitLists(javacTree.getTypeArguments(), node.getTypeArguments().get());
        }
        // In JavaParser, the method name itself and receiver are stored as fields of the
        // invocation itself, but in javac they might be combined into one MemberSelectTree.
        // That member select may also be a single IdentifierTree if no receiver was written.
        // This requires one layer of unnesting.
        ExpressionTree methodSelect = javacTree.getMethodSelect();
        if (methodSelect.getKind() == Tree.Kind.IDENTIFIER) {
            methodSelect.accept(this, node.getName());
        } else if (methodSelect.getKind() == Tree.Kind.MEMBER_SELECT) {
            MemberSelectTree selection = (MemberSelectTree) methodSelect;
            assert node.getScope().isPresent();
            selection.getExpression().accept(this, node.getScope().get());
        } else {
            throw new BugInCF("Unexpected method selection type: %s", methodSelect);
        }
        visitLists(javacTree.getArguments(), node.getArguments());
    } else if (javaParserNode instanceof ExplicitConstructorInvocationStmt) {
        ExplicitConstructorInvocationStmt node = (ExplicitConstructorInvocationStmt) javaParserNode;
        processMethodInvocation(javacTree, node);
        assert javacTree.getTypeArguments().isEmpty() != node.getTypeArguments().isPresent();
        if (!javacTree.getTypeArguments().isEmpty()) {
            visitLists(javacTree.getTypeArguments(), node.getTypeArguments().get());
        }
        visitLists(javacTree.getArguments(), node.getArguments());
    } else {
        throwUnexpectedNodeType(javacTree, javaParserNode);
    }
    return null;
}
Also used : MemberSelectTree(com.sun.source.tree.MemberSelectTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) ExpressionTree(com.sun.source.tree.ExpressionTree) ExplicitConstructorInvocationStmt(com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt) BugInCF(org.checkerframework.javacutil.BugInCF) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 13 with ExplicitConstructorInvocationStmt

use of com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt in project drools by kiegroup.

the class CommonCodegenUtilsTest method setExplicitConstructorInvocationStmtArgumentNoParameter.

@Test(expected = KiePMMLException.class)
public void setExplicitConstructorInvocationStmtArgumentNoParameter() {
    final String parameterName = "PARAMETER_NAME";
    final ExplicitConstructorInvocationStmt explicitConstructorInvocationStmt = new ExplicitConstructorInvocationStmt();
    explicitConstructorInvocationStmt.setArguments(NodeList.nodeList(new NameExpr("NOT_PARAMETER")));
    CommonCodegenUtils.setExplicitConstructorInvocationStmtArgument(explicitConstructorInvocationStmt, parameterName, "VALUE");
}
Also used : NameExpr(com.github.javaparser.ast.expr.NameExpr) ExplicitConstructorInvocationStmt(com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt) Test(org.junit.Test)

Example 14 with ExplicitConstructorInvocationStmt

use of com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt in project drools by kiegroup.

the class KiePMMLSegmentFactory method setConstructor.

static void setConstructor(final String segmentName, final String generatedClassName, final ConstructorDeclaration constructorDeclaration, final String kiePMMLModelClass, final boolean isInterpreted, final double weight) {
    setConstructorSuperNameInvocation(generatedClassName, constructorDeclaration, segmentName);
    final BlockStmt body = constructorDeclaration.getBody();
    final ExplicitConstructorInvocationStmt superStatement = CommonCodegenUtils.getExplicitConstructorInvocationStmt(body).orElseThrow(() -> new KiePMMLException(String.format(MISSING_CONSTRUCTOR_IN_BODY, body)));
    final Expression instantiationExpression = getInstantiationExpression(kiePMMLModelClass, isInterpreted);
    String modelInstantiationString = instantiationExpression.toString();
    CommonCodegenUtils.setExplicitConstructorInvocationStmtArgument(superStatement, "model", modelInstantiationString);
    CommonCodegenUtils.setAssignExpressionValue(body, "weight", new DoubleLiteralExpr(weight));
    CommonCodegenUtils.setAssignExpressionValue(body, "id", new StringLiteralExpr(segmentName));
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) KiePMMLFactoryFactory.getInstantiationExpression(org.kie.pmml.compiler.commons.factories.KiePMMLFactoryFactory.getInstantiationExpression) DoubleLiteralExpr(com.github.javaparser.ast.expr.DoubleLiteralExpr) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) ExplicitConstructorInvocationStmt(com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt)

Example 15 with ExplicitConstructorInvocationStmt

use of com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt in project drools by kiegroup.

the class KiePMMLCharacteristicsFactory method setCharacteristicsVariableDeclaration.

static void setCharacteristicsVariableDeclaration(final String characteristicsClassName, final Characteristics characteristics, final List<Field<?>> fields, final ClassOrInterfaceDeclaration characteristicsTemplate) {
    int counter = 0;
    NodeList<Expression> arguments = new NodeList<>();
    for (Characteristic characteristic : characteristics.getCharacteristics()) {
        String characteristicVariableName = String.format(VARIABLE_NAME_TEMPLATE, characteristicsClassName, counter);
        addGetCharacteristicMethod(characteristicVariableName, characteristic, fields, characteristicsTemplate);
        MethodCallExpr toAdd = new MethodCallExpr();
        toAdd.setScope(new NameExpr(characteristicsClassName));
        toAdd.setName("get" + characteristicVariableName);
        arguments.add(toAdd);
        counter++;
    }
    final ConstructorDeclaration constructorDeclaration = characteristicsTemplate.getDefaultConstructor().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_DEFAULT_CONSTRUCTOR, characteristicsTemplate.getName())));
    constructorDeclaration.setName(characteristicsClassName);
    final BlockStmt body = constructorDeclaration.getBody();
    final ExplicitConstructorInvocationStmt superStatement = CommonCodegenUtils.getExplicitConstructorInvocationStmt(body).orElseThrow(() -> new KiePMMLException(String.format(MISSING_CONSTRUCTOR_IN_BODY, body)));
    superStatement.setArgument(0, new StringLiteralExpr(characteristicsClassName));
    superStatement.setArgument(2, getArraysAsListInvocationMethodCall(arguments));
}
Also used : NodeList(com.github.javaparser.ast.NodeList) Characteristic(org.dmg.pmml.scorecard.Characteristic) KiePMMLCharacteristic(org.kie.pmml.models.scorecard.model.KiePMMLCharacteristic) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) ExplicitConstructorInvocationStmt(com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt) Expression(com.github.javaparser.ast.expr.Expression) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Aggregations

ExplicitConstructorInvocationStmt (com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt)18 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)10 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)9 NameExpr (com.github.javaparser.ast.expr.NameExpr)6 Test (org.junit.Test)5 ConstructorDeclaration (com.github.javaparser.ast.body.ConstructorDeclaration)4 KiePMMLInternalException (org.kie.pmml.api.exceptions.KiePMMLInternalException)4 Expression (com.github.javaparser.ast.expr.Expression)3 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)3 StaticJavaParser.parseClassOrInterfaceType (com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType)2 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)2 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)2 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)2 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)2 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)2 CompilationUnit (com.github.javaparser.ast.CompilationUnit)1 NodeList (com.github.javaparser.ast.NodeList)1 AssignExpr (com.github.javaparser.ast.expr.AssignExpr)1 DoubleLiteralExpr (com.github.javaparser.ast.expr.DoubleLiteralExpr)1 MethodReferenceExpr (com.github.javaparser.ast.expr.MethodReferenceExpr)1