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