use of com.github.javaparser.ast.NodeList in project javaparser by javaparser.
the class ClassOrInterfaceDeclarationTransformationsTest method addingTypeParameterAsLastWhenThereAreSome.
@Test
public void addingTypeParameterAsLastWhenThereAreSome() throws IOException {
ClassOrInterfaceDeclaration cid = consider("class A<U> {}");
cid.addTypeParameter(new TypeParameter("T", new NodeList<>()));
assertTransformedToString("class A<U, T> {}", cid);
}
use of com.github.javaparser.ast.NodeList in project javaparser by javaparser.
the class ClassOrInterfaceDeclarationTransformationsTest method addingTypeParameterWhenThereAreNone.
// typeParameters
@Test
public void addingTypeParameterWhenThereAreNone() throws IOException {
ClassOrInterfaceDeclaration cid = consider("class A {}");
cid.addTypeParameter(new TypeParameter("T", new NodeList<>()));
assertTransformedToString("class A<T> {}", cid);
}
use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class BlockGenerator method replaceGenericType.
private static void replaceGenericType(int arity, ClassOrInterfaceDeclaration containerClass, ClassOrInterfaceDeclaration innerClass) {
List<TypeParameter> genericTypeParameterList = genericTypeStream(arity, BlockGenerator::createTypeParameter).collect(Collectors.toList());
containerClass.setTypeParameters(NodeList.nodeList(genericTypeParameterList));
MethodDeclaration executeMethod = containerClass.findFirst(MethodDeclaration.class, mc -> "execute".equals(mc.getNameAsString())).orElseThrow(() -> new RuntimeException("Execute method not found"));
List<Parameter> params = genericTypeStream(arity, i -> {
Type t = parseType(i);
return new Parameter(t, argName(i));
}).collect(Collectors.toList());
executeMethod.setParameters(NodeList.nodeList(params));
List<Expression> executParams = genericTypeStream(arity, i -> {
NameExpr nameExpr = new NameExpr("objs");
return new ArrayAccessExpr(nameExpr, new IntegerLiteralExpr(i - 1));
}).collect(Collectors.toList());
innerClass.findFirst(MethodDeclaration.class, mc -> "execute".equals(mc.getNameAsString())).ifPresent(m -> {
BlockStmt blockStmt = new BlockStmt();
NodeList<Expression> expressions = NodeList.nodeList(executParams);
MethodCallExpr methodCallExpr = new MethodCallExpr(new NameExpr("block"), "execute", expressions);
blockStmt.setStatements(NodeList.nodeList(new ExpressionStmt(methodCallExpr)));
m.setBody(blockStmt);
});
}
use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class NoConstructor method generateKieFieldsConstructor.
private void generateKieFieldsConstructor(List<FieldDefinition> keyFields) {
Modifier.Keyword[] modifiers = publicConstructor ? new Modifier.Keyword[] { Modifier.publicModifier().getKeyword() } : new Modifier.Keyword[0];
ConstructorDeclaration constructor = generatedClass.addConstructor(modifiers);
NodeList<Statement> fieldStatements = nodeList();
MethodCallExpr keySuperCall = new MethodCallExpr(null, "super");
fieldStatements.add(new ExpressionStmt(keySuperCall));
for (FieldDefinition fieldDefinition : keyFields) {
String fieldName = fieldDefinition.getFieldName();
addConstructorArgument(constructor, fieldDefinition.getObjectType(), fieldName);
Optional<? extends FieldDefinition> typeDefinition = typeDeclarationFields.stream().filter(td -> td.getFieldName().equals(fieldName)).findAny();
if (typeDefinition.isPresent()) {
fieldStatements.add(fieldAssignment(fieldName));
} else {
keySuperCall.addArgument(fieldName);
}
}
constructor.setBody(new BlockStmt(fieldStatements));
}
use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class ParserUtil method objectCreationExprToValue.
public static Object objectCreationExprToValue(ObjectCreationExpr objectCreationExpr, RuleContext context) {
// Only a few classes/constructors are handled. Otherwise, value becomes null so a link would be UNKNOWN. To be enhanced : DROOLS-6711
ClassOrInterfaceType type = objectCreationExpr.getType();
Class<?> clazz = getClassFromType(context.getTypeResolver(), type);
if (clazz.equals(BigDecimal.class)) {
NodeList<Expression> arguments = objectCreationExpr.getArguments();
Optional<Object> opt = arguments.stream().findFirst().filter(StringLiteralExpr.class::isInstance).map(literalExpr -> literalExpr.asStringLiteralExpr().asString()).map(BigDecimal::new);
return opt.orElse(null);
} else if (clazz.equals(BigInteger.class)) {
NodeList<Expression> arguments = objectCreationExpr.getArguments();
Optional<Object> opt = arguments.stream().findFirst().filter(StringLiteralExpr.class::isInstance).map(literalExpr -> literalExpr.asStringLiteralExpr().asString()).map(BigInteger::new);
return opt.orElse(null);
}
return null;
}
Aggregations