Search in sources :

Example 11 with NodeList

use of com.github.javaparser.ast.NodeList in project cloud-sea-towerman by huadahuang1983.

the class JavaFileMergerJaxp method mergerFile.

public String mergerFile(CompilationUnit newCompilationUnit, CompilationUnit existingCompilationUnit) {
    StringBuilder sb = new StringBuilder(newCompilationUnit.getPackageDeclaration().get().toString());
    newCompilationUnit.removePackageDeclaration();
    // 合并imports
    NodeList<ImportDeclaration> imports = newCompilationUnit.getImports();
    imports.addAll(existingCompilationUnit.getImports());
    Set<ImportDeclaration> importSet = new HashSet<ImportDeclaration>();
    importSet.addAll(imports);
    NodeList<ImportDeclaration> newImports = new NodeList<>();
    newImports.addAll(importSet);
    newCompilationUnit.setImports(newImports);
    for (ImportDeclaration i : newCompilationUnit.getImports()) {
        sb.append(i.toString());
    }
    newLine(sb);
    NodeList<TypeDeclaration<?>> types = newCompilationUnit.getTypes();
    NodeList<TypeDeclaration<?>> oldTypes = existingCompilationUnit.getTypes();
    for (int i = 0; i < types.size(); i++) {
        // 截取Class
        String classNameInfo = types.get(i).toString().substring(0, types.get(i).toString().indexOf("{") + 1);
        sb.append(classNameInfo);
        newLine(sb);
        newLine(sb);
        // 合并fields
        List<FieldDeclaration> fields = types.get(i).getFields();
        List<FieldDeclaration> oldFields = oldTypes.get(i).getFields();
        List<FieldDeclaration> newFields = new ArrayList<>();
        Set<FieldDeclaration> fieldDeclarations = new LinkedHashSet<>();
        fieldDeclarations.addAll(fields);
        fieldDeclarations.addAll(oldFields);
        newFields.addAll(fieldDeclarations);
        for (FieldDeclaration f : newFields) {
            sb.append("\t" + f.toString());
            newLine(sb);
            newLine(sb);
        }
        // 合并methods
        List<MethodDeclaration> methods = types.get(i).getMethods();
        List<MethodDeclaration> existingMethods = oldTypes.get(i).getMethods();
        Set<MethodDeclaration> methodDeclarations = new LinkedHashSet<>();
        methodDeclarations.addAll(methods);
        methodDeclarations.addAll(existingMethods);
        for (MethodDeclaration f : methodDeclarations) {
            String res = f.toString().replaceAll("\r\n", "\r\n\t");
            sb.append("\t" + res);
            newLine(sb);
            newLine(sb);
        }
        // 判断是否有内部类
        types.get(i).getChildNodes();
        for (Node n : types.get(i).getChildNodes()) {
            if (n.toString().contains("static class")) {
                sb.append(n.toString());
            }
        }
    }
    return sb.append(System.getProperty("line.separator") + "}").toString();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) NodeList(com.github.javaparser.ast.NodeList) Node(com.github.javaparser.ast.Node) ArrayList(java.util.ArrayList) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) ImportDeclaration(com.github.javaparser.ast.ImportDeclaration) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 12 with NodeList

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

the class Expressions method genContextType.

public static Expression genContextType(Map<String, Expression> fields) {
    final ClassOrInterfaceType sie = parseClassOrInterfaceType(java.util.AbstractMap.SimpleImmutableEntry.class.getCanonicalName());
    sie.setTypeArguments(parseClassOrInterfaceType(String.class.getCanonicalName()), parseClassOrInterfaceType(org.kie.dmn.feel.lang.Type.class.getCanonicalName()));
    List<Expression> entryParams = fields.entrySet().stream().map(e -> new ObjectCreationExpr(null, sie, new NodeList<>(stringLiteral(e.getKey()), e.getValue()))).collect(Collectors.toList());
    MethodCallExpr mOf = new MethodCallExpr(new NameExpr(java.util.stream.Stream.class.getCanonicalName()), "of");
    entryParams.forEach(mOf::addArgument);
    MethodCallExpr mCollect = new MethodCallExpr(mOf, "collect");
    mCollect.addArgument(new MethodCallExpr(new NameExpr(java.util.stream.Collectors.class.getCanonicalName()), "toMap").addArgument(new MethodReferenceExpr(new NameExpr(java.util.Map.Entry.class.getCanonicalName()), new NodeList<>(), "getKey")).addArgument(new MethodReferenceExpr(new NameExpr(java.util.Map.Entry.class.getCanonicalName()), new NodeList<>(), "getValue")));
    return new ObjectCreationExpr(null, MapBackedTypeT, new NodeList<>(stringLiteral("[anonymous]"), mCollect));
}
Also used : StaticJavaParser.parseClassOrInterfaceType(com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType) ClassExpr(com.github.javaparser.ast.expr.ClassExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) MapBackedType(org.kie.dmn.feel.lang.impl.MapBackedType) GenFnType(org.kie.dmn.feel.lang.types.GenFnType) UnaryTestNode(org.kie.dmn.feel.lang.ast.UnaryTestNode) Parameter(com.github.javaparser.ast.body.Parameter) QuantifiedExpressionNode(org.kie.dmn.feel.lang.ast.QuantifiedExpressionNode) CastExpr(com.github.javaparser.ast.expr.CastExpr) Type(com.github.javaparser.ast.type.Type) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) NamedParameter(org.kie.dmn.feel.lang.impl.NamedParameter) Map(java.util.Map) Expression(com.github.javaparser.ast.expr.Expression) NodeList(com.github.javaparser.ast.NodeList) UnknownType(com.github.javaparser.ast.type.UnknownType) BuiltInTypeT(org.kie.dmn.feel.codegen.feel11.Constants.BuiltInTypeT) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) BigDecimalT(org.kie.dmn.feel.codegen.feel11.Constants.BigDecimalT) RangeNode(org.kie.dmn.feel.lang.ast.RangeNode) StaticJavaParser.parseExpression(com.github.javaparser.StaticJavaParser.parseExpression) NameExpr(com.github.javaparser.ast.expr.NameExpr) Collectors(java.util.stream.Collectors) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) MethodReferenceExpr(com.github.javaparser.ast.expr.MethodReferenceExpr) List(java.util.List) InfixOpNode(org.kie.dmn.feel.lang.ast.InfixOpNode) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) GenListType(org.kie.dmn.feel.lang.types.GenListType) BaseFEELFunction(org.kie.dmn.feel.runtime.functions.BaseFEELFunction) EvalHelper(org.kie.dmn.feel.util.EvalHelper) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Collectors(java.util.stream.Collectors) NodeList(com.github.javaparser.ast.NodeList) NameExpr(com.github.javaparser.ast.expr.NameExpr) StaticJavaParser.parseClassOrInterfaceType(com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) MethodReferenceExpr(com.github.javaparser.ast.expr.MethodReferenceExpr) Expression(com.github.javaparser.ast.expr.Expression) StaticJavaParser.parseExpression(com.github.javaparser.StaticJavaParser.parseExpression) Map(java.util.Map) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 13 with NodeList

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

the class FunctionDefs method asMethodCall.

public static Expression asMethodCall(String className, String methodSignature, List<String> params) {
    // creating a simple algorithm to find the method in java
    // without using any external libraries in this initial implementation
    // might need to explicitly use a classloader here
    String[] mp = FunctionDefNode.parseMethod(methodSignature);
    try {
        String methodName = mp[0];
        String[] paramTypeNames = FunctionDefNode.parseParams(mp[1]);
        ArrayList<Expression> paramExprs = new ArrayList<>();
        if (paramTypeNames.length == params.size()) {
            for (int i = 0; i < params.size(); i++) {
                String paramName = params.get(i);
                String paramTypeName = paramTypeNames[i];
                Type paramTypeCanonicalName = parseType(FunctionDefNode.getType(paramTypeName, null).getCanonicalName());
                Expression param = new CastExpr(paramTypeCanonicalName, new MethodCallExpr(null, "coerceTo", new NodeList<>(new ClassExpr(paramTypeCanonicalName), new MethodCallExpr(new NameExpr("feelExprCtx"), "getValue", new NodeList<>(new StringLiteralExpr(paramName))))));
                paramExprs.add(param);
            }
            return new MethodCallExpr(new NameExpr(className), methodName, new NodeList<>(paramExprs));
        } else {
            throw new FEELCompilationError(Msg.createMessage(Msg.ERROR_RESOLVING_EXTERNAL_FUNCTION_AS_DEFINED_BY, methodSignature));
        }
    } catch (Exception e) {
        throw new IllegalArgumentException(e);
    }
}
Also used : NodeList(com.github.javaparser.ast.NodeList) ArrayList(java.util.ArrayList) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) Type(com.github.javaparser.ast.type.Type) StaticJavaParser.parseType(com.github.javaparser.StaticJavaParser.parseType) Expression(com.github.javaparser.ast.expr.Expression) CastExpr(com.github.javaparser.ast.expr.CastExpr) FEELCompilationError(org.kie.dmn.feel.codegen.feel11.FEELCompilationError) ClassExpr(com.github.javaparser.ast.expr.ClassExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 14 with NodeList

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

the class KiePMMLDefineFunctionFactory method getDefineFunctionVariableDeclaration.

static BlockStmt getDefineFunctionVariableDeclaration(final DefineFunction defineFunction) {
    final MethodDeclaration methodDeclaration = DEFINE_FUNCTION_TEMPLATE.getMethodsByName(GETKIEPMMLDEFINEFUNCTION).get(0).clone();
    final BlockStmt defineFunctionBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(defineFunctionBody, DEFINE_FUNCTION).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, DEFINE_FUNCTION, defineFunctionBody)));
    variableDeclarator.setName(defineFunction.getName());
    final BlockStmt toReturn = new BlockStmt();
    int counter = 0;
    final NodeList<Expression> parameterFieldArguments = new NodeList<>();
    for (ParameterField parameterField : defineFunction.getParameterFields()) {
        String nestedVariableName = String.format(VARIABLE_NAME_TEMPLATE, defineFunction.getName(), counter);
        parameterFieldArguments.add(new NameExpr(nestedVariableName));
        BlockStmt toAdd = getParameterFieldVariableDeclaration(nestedVariableName, parameterField);
        toAdd.getStatements().forEach(toReturn::addStatement);
        counter++;
    }
    String kiePMMLExpression = String.format("%s_Expression", defineFunction.getName());
    BlockStmt toAdd = getKiePMMLExpressionBlockStmt(kiePMMLExpression, defineFunction.getExpression());
    toAdd.getStatements().forEach(toReturn::addStatement);
    final ObjectCreationExpr objectCreationExpr = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, DEFINE_FUNCTION, toReturn))).asObjectCreationExpr();
    final StringLiteralExpr nameExpr = new StringLiteralExpr(defineFunction.getName());
    objectCreationExpr.getArguments().set(0, nameExpr);
    final Expression dataTypeExpression = getExpressionForDataType(defineFunction.getDataType());
    final Expression opTypeExpression = getExpressionForOpType(defineFunction.getOpType());
    objectCreationExpr.getArguments().set(2, dataTypeExpression);
    objectCreationExpr.getArguments().set(3, opTypeExpression);
    objectCreationExpr.getArguments().get(4).asMethodCallExpr().setArguments(parameterFieldArguments);
    objectCreationExpr.getArguments().set(5, new NameExpr(kiePMMLExpression));
    defineFunctionBody.getStatements().forEach(toReturn::addStatement);
    return toReturn;
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) KiePMMLExpressionFactory.getKiePMMLExpressionBlockStmt(org.kie.pmml.compiler.commons.codegenfactories.KiePMMLExpressionFactory.getKiePMMLExpressionBlockStmt) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NodeList(com.github.javaparser.ast.NodeList) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) CommonCodegenUtils.getVariableDeclarator(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) Expression(com.github.javaparser.ast.expr.Expression) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) ParameterField(org.dmg.pmml.ParameterField)

Example 15 with NodeList

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

the class KiePMMLLocalTransformationsFactory method addDerivedField.

static NodeList<Expression> addDerivedField(final BlockStmt body, final List<DerivedField> derivedFields) {
    NodeList<Expression> arguments = new NodeList<>();
    int counter = 0;
    for (DerivedField derivedField : derivedFields) {
        String nestedVariableName = String.format("localTransformationsDerivedField_%s", counter);
        arguments.add(new NameExpr(nestedVariableName));
        BlockStmt toAdd = getDerivedFieldVariableDeclaration(nestedVariableName, derivedField);
        toAdd.getStatements().forEach(body::addStatement);
        counter++;
    }
    return getArraysAsListInvocation(arguments);
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) NodeList(com.github.javaparser.ast.NodeList) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NameExpr(com.github.javaparser.ast.expr.NameExpr) DerivedField(org.dmg.pmml.DerivedField)

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