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;
}
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;
}
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");
}
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));
}
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));
}
Aggregations