Search in sources :

Example 51 with NodeList

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);
}
Also used : TypeParameter(com.github.javaparser.ast.type.TypeParameter) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) NodeList(com.github.javaparser.ast.NodeList) Test(org.junit.Test) AbstractLexicalPreservingTest(com.github.javaparser.printer.lexicalpreservation.AbstractLexicalPreservingTest)

Example 52 with NodeList

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);
}
Also used : TypeParameter(com.github.javaparser.ast.type.TypeParameter) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) NodeList(com.github.javaparser.ast.NodeList) Test(org.junit.Test) AbstractLexicalPreservingTest(com.github.javaparser.printer.lexicalpreservation.AbstractLexicalPreservingTest)

Example 53 with NodeList

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);
    });
}
Also used : IntStream(java.util.stream.IntStream) ArrayAccessExpr(com.github.javaparser.ast.expr.ArrayAccessExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) Parameter(com.github.javaparser.ast.body.Parameter) Type(com.github.javaparser.ast.type.Type) Expression(com.github.javaparser.ast.expr.Expression) CompilationUnit(com.github.javaparser.ast.CompilationUnit) Path(java.nio.file.Path) IntFunction(java.util.function.IntFunction) NodeList(com.github.javaparser.ast.NodeList) Files(java.nio.file.Files) TypeParameter(com.github.javaparser.ast.type.TypeParameter) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) StandardOpenOption(java.nio.file.StandardOpenOption) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) IOException(java.io.IOException) NameExpr(com.github.javaparser.ast.expr.NameExpr) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) Collectors(java.util.stream.Collectors) List(java.util.List) StaticJavaParser(com.github.javaparser.StaticJavaParser) Stream(java.util.stream.Stream) Paths(java.nio.file.Paths) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) TypeParameter(com.github.javaparser.ast.type.TypeParameter) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) ArrayAccessExpr(com.github.javaparser.ast.expr.ArrayAccessExpr) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NameExpr(com.github.javaparser.ast.expr.NameExpr) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) Type(com.github.javaparser.ast.type.Type) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) Expression(com.github.javaparser.ast.expr.Expression) Parameter(com.github.javaparser.ast.body.Parameter) TypeParameter(com.github.javaparser.ast.type.TypeParameter) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 54 with NodeList

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));
}
Also used : NodeList(com.github.javaparser.ast.NodeList) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) FieldDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.FieldDefinition) StaticJavaParser.parseStatement(com.github.javaparser.StaticJavaParser.parseStatement) GeneratedClassDeclaration.replaceFieldName(org.drools.modelcompiler.builder.generator.declaredtype.generator.GeneratedClassDeclaration.replaceFieldName) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) Collection(java.util.Collection) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) NodeList.nodeList(com.github.javaparser.ast.NodeList.nodeList) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) Statement(com.github.javaparser.ast.stmt.Statement) Modifier(com.github.javaparser.ast.Modifier) List(java.util.List) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) Type(com.github.javaparser.ast.type.Type) Optional(java.util.Optional) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) StaticJavaParser.parseStatement(com.github.javaparser.StaticJavaParser.parseStatement) Statement(com.github.javaparser.ast.stmt.Statement) FieldDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.FieldDefinition) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) Modifier(com.github.javaparser.ast.Modifier) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 55 with NodeList

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;
}
Also used : NodeList(com.github.javaparser.ast.NodeList) SimpleName(com.github.javaparser.ast.expr.SimpleName) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) DrlxParseUtil.getClassFromType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.getClassFromType) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) BigDecimal(java.math.BigDecimal) RuleContext(org.drools.modelcompiler.builder.generator.RuleContext) BigDecimalLiteralExpr(org.drools.mvel.parser.ast.expr.BigDecimalLiteralExpr) LiteralExpr(com.github.javaparser.ast.expr.LiteralExpr) BigIntegerLiteralExpr(org.drools.mvel.parser.ast.expr.BigIntegerLiteralExpr) CharLiteralExpr(com.github.javaparser.ast.expr.CharLiteralExpr) DoubleLiteralExpr(com.github.javaparser.ast.expr.DoubleLiteralExpr) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Optional(java.util.Optional) BigInteger(java.math.BigInteger) Expression(com.github.javaparser.ast.expr.Expression) LongLiteralExpr(com.github.javaparser.ast.expr.LongLiteralExpr) IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) Optional(java.util.Optional) Expression(com.github.javaparser.ast.expr.Expression) NodeList(com.github.javaparser.ast.NodeList) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) BigInteger(java.math.BigInteger) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) BigDecimal(java.math.BigDecimal)

Aggregations

NodeList (com.github.javaparser.ast.NodeList)83 Expression (com.github.javaparser.ast.expr.Expression)48 NameExpr (com.github.javaparser.ast.expr.NameExpr)40 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)37 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)36 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)29 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)25 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)24 List (java.util.List)22 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)18 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)18 CompilationUnit (com.github.javaparser.ast.CompilationUnit)17 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)17 ArrayList (java.util.ArrayList)17 Collectors (java.util.stream.Collectors)17 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)16 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)16 Parameter (com.github.javaparser.ast.body.Parameter)15 Test (org.junit.Test)15 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)14