Skip to content

Commit

Permalink
Fix for drlx-parser rebase (#2216)
Browse files Browse the repository at this point in the history
* Added file

* Updated to drlx-parser after rebasing to 3.10.2
  • Loading branch information
lucamolteni authored and mariofusco committed Feb 1, 2019
1 parent d02a06c commit 6ef0328
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@
import static java.util.stream.Collectors.joining;

import static org.drools.core.util.StringUtils.generateUUID;
import static org.drools.javaparser.ast.Modifier.*;
import static org.drools.javaparser.ast.Modifier.publicModifier;
import static org.drools.javaparser.ast.Modifier.staticModifier;
import static org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType;
import static org.drools.modelcompiler.builder.generator.DrlxParseUtil.toVar;
import static org.drools.modelcompiler.builder.generator.DslMethodNames.GLOBAL_OF_CALL;
Expand Down Expand Up @@ -418,7 +421,7 @@ public RuleSourceResult getRulesSource() {


for(Map.Entry<String, MethodCallExpr> windowReference : windowReferences.entrySet()) {
FieldDeclaration f = rulesClass.addField(WINDOW_REFERENCE_TYPE, windowReference.getKey(), Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL);
FieldDeclaration f = rulesClass.addField(WINDOW_REFERENCE_TYPE, windowReference.getKey(), publicModifier().getKeyword(), staticModifier().getKeyword(), finalModifier().getKeyword());
f.getVariables().get(0).setInitializer(windowReference.getValue());
}

Expand All @@ -427,12 +430,12 @@ public RuleSourceResult getRulesSource() {
}

for(Map.Entry<String, QueryGenerator.QueryDefWithType> queryDef: queryDefWithType.entrySet()) {
FieldDeclaration field = rulesClass.addField(queryDef.getValue().getQueryType(), queryDef.getKey(), Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL);
FieldDeclaration field = rulesClass.addField(queryDef.getValue().getQueryType(), queryDef.getKey(), publicModifier().getKeyword(), staticModifier().getKeyword(), finalModifier().getKeyword());
field.getVariables().get(0).setInitializer(queryDef.getValue().getMethodCallExpr());
}

for(Map.Entry<String, MethodDeclaration> methodName: queryMethods.entrySet()) {
FieldDeclaration field = rulesClass.addField(methodName.getValue().getType(), methodName.getKey(), Modifier.FINAL);
FieldDeclaration field = rulesClass.addField(methodName.getValue().getType(), methodName.getKey(), finalModifier().getKeyword());
field.getVariables().get(0).setInitializer(new MethodCallExpr(null, methodName.getKey()));
}

Expand Down Expand Up @@ -551,7 +554,7 @@ private MethodCallExpr buildRulesField( ClassOrInterfaceDeclaration rulesClass )
MethodCallExpr rulesInit = new MethodCallExpr( null, "Arrays.asList" );
ClassOrInterfaceType rulesType = new ClassOrInterfaceType(null, new SimpleName("List"), new NodeList<Type>(new ClassOrInterfaceType(null, "Rule")));
VariableDeclarator rulesVar = new VariableDeclarator( rulesType, "rulesList", rulesInit );
rulesClass.addMember( new FieldDeclaration( EnumSet.of( Modifier.PUBLIC, Modifier.STATIC), rulesVar ) );
rulesClass.addMember( new FieldDeclaration( NodeList.nodeList( publicModifier(), staticModifier()), rulesVar ) );
return rulesInit;
}

Expand Down Expand Up @@ -592,7 +595,7 @@ private static void addGlobalField(ClassOrInterfaceDeclaration classDeclaration,
declarationOfCall.addArgument(new StringLiteralExpr(packageName));
declarationOfCall.addArgument(new StringLiteralExpr(globalName));

FieldDeclaration field = classDeclaration.addField(varType, toVar(globalName), Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL);
FieldDeclaration field = classDeclaration.addField(varType, toVar(globalName), publicModifier().getKeyword(), staticModifier().getKeyword(), finalModifier().getKeyword());

field.getVariables().get(0).setInitializer(declarationOfCall);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ private boolean rewriteRHS(BlockStmt ruleBlock, BlockStmt rhs) {
bitMaskCreation = new MethodCallExpr( new NameExpr( AllSetButLastBitMask.class.getCanonicalName() ), "get" );
}

VariableDeclarationExpr bitMaskVar = new VariableDeclarationExpr(toClassOrInterfaceType(BitMask.class), "mask_" + updatedVar, Modifier.FINAL);
VariableDeclarationExpr bitMaskVar = new VariableDeclarationExpr(toClassOrInterfaceType(BitMask.class), "mask_" + updatedVar, Modifier.finalModifier());
AssignExpr bitMaskAssign = new AssignExpr(bitMaskVar, bitMaskCreation, AssignExpr.Operator.ASSIGN);
ruleBlock.addStatement(bitMaskAssign);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.drools.modelcompiler.builder.generator;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;

import org.drools.compiler.lang.descr.FunctionDescr;
import org.drools.javaparser.ast.Modifier;
import org.drools.javaparser.ast.NodeList;
import org.drools.javaparser.ast.body.MethodDeclaration;
import org.drools.javaparser.ast.body.Parameter;
import org.drools.javaparser.ast.stmt.BlockStmt;
Expand All @@ -27,7 +27,7 @@ public static MethodDeclaration toFunction(FunctionDescr desc) {
parameters.add(new Parameter(parseType(type), name));
}

EnumSet<Modifier> modifiers = EnumSet.of(Modifier.PUBLIC, Modifier.STATIC);
NodeList<Modifier> modifiers = NodeList.nodeList(Modifier.publicModifier(), Modifier.staticModifier());
MethodDeclaration methodDeclaration = new MethodDeclaration(modifiers, desc.getName(), parseType(desc.getReturnType()), nodeList(parameters));

BlockStmt block = DrlxParseUtil.parseBlock("try {} catch (Exception e) { throw new RuntimeException(e); }");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ private static void processRule(KnowledgeBuilderImpl kbuilder, PackageModel pack

new ModelGeneratorVisitor(context, packageModel).visit(getExtendedLhs(packageDescr, ruleDescr));
final String ruleMethodName = "rule_" + toId(ruleDescr.getName());
MethodDeclaration ruleMethod = new MethodDeclaration(EnumSet.of(Modifier.PUBLIC, Modifier.STATIC), toClassOrInterfaceType( Rule.class ), ruleMethodName);
MethodDeclaration ruleMethod = new MethodDeclaration(NodeList.nodeList(Modifier.publicModifier(), Modifier.staticModifier()), toClassOrInterfaceType( Rule.class ), ruleMethodName);

ruleMethod.setJavadocComment(" Rule name: " + ruleDescr.getName() + " ");

Expand Down Expand Up @@ -394,7 +394,7 @@ private static void addUnitData(RuleContext context, String unitVar, java.lang.r

ClassOrInterfaceType varType = toClassOrInterfaceType(UnitData.class);
varType.setTypeArguments(declType);
VariableDeclarationExpr var_ = new VariableDeclarationExpr(varType, context.getVar(unitVar), Modifier.FINAL);
VariableDeclarationExpr var_ = new VariableDeclarationExpr(varType, context.getVar(unitVar), Modifier.finalModifier());

MethodCallExpr unitDataCall = new MethodCallExpr(null, UNIT_DATA_CALL);

Expand Down Expand Up @@ -432,7 +432,7 @@ private static void addVariable(KnowledgeBuilderImpl kbuilder, BlockStmt ruleBlo

ClassOrInterfaceType varType = toClassOrInterfaceType(Variable.class);
varType.setTypeArguments(declType);
VariableDeclarationExpr var_ = new VariableDeclarationExpr(varType, context.getVar(decl.getBindingId()), Modifier.FINAL);
VariableDeclarationExpr var_ = new VariableDeclarationExpr(varType, context.getVar(decl.getBindingId()), Modifier.finalModifier());

MethodCallExpr declarationOfCall = new MethodCallExpr(null, DECLARATION_OF_CALL);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private static MethodCallExpr registerTypeMetaData( String className ) {
}

private static ClassOrInterfaceDeclaration toClassDeclaration(ModelBuilderImpl builder, TypeDeclarationDescr typeDeclaration, PackageDescr packageDescr, TypeResolver typeResolver) {
EnumSet<Modifier> classModifiers = EnumSet.of(Modifier.PUBLIC);
NodeList<Modifier> classModifiers = NodeList.nodeList(Modifier.publicModifier());
String generatedClassName = typeDeclaration.getTypeName();
ClassOrInterfaceDeclaration generatedClass = new ClassOrInterfaceDeclaration(classModifiers, false, generatedClassName);
generatedClass.addImplementedType( GeneratedFact.class.getName() );
Expand All @@ -153,7 +153,8 @@ private static ClassOrInterfaceDeclaration toClassDeclaration(ModelBuilderImpl b
for (AnnotationDescr ann : typeDeclaration.getAnnotations()) {
if (ann.getName().equals( "serialVersionUID" )) {
LongLiteralExpr valueExpr = new LongLiteralExpr(ann.getValue( "value" ).toString());
generatedClass.addFieldWithInitializer( PrimitiveType.longType(), "serialVersionUID", valueExpr, Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL );
generatedClass.addFieldWithInitializer( PrimitiveType.longType(), "serialVersionUID", valueExpr, Modifier.privateModifier().getKeyword()
, Modifier.staticModifier().getKeyword(), Modifier.finalModifier().getKeyword() );
} else {
processAnnotation( builder, typeResolver, generatedClass, ann, softAnnotations );
}
Expand All @@ -164,7 +165,7 @@ private static ClassOrInterfaceDeclaration toClassDeclaration(ModelBuilderImpl b
generatedClass.setJavadocComment(generatedClassJavadoc);
}

generatedClass.addConstructor(Modifier.PUBLIC); // No-args ctor
generatedClass.addConstructor(Modifier.publicModifier().getKeyword()); // No-args ctor

List<Statement> equalsFieldStatement = new ArrayList<>();
List<Statement> hashCodeFieldStatement = new ArrayList<>();
Expand All @@ -180,7 +181,7 @@ private static ClassOrInterfaceDeclaration toClassDeclaration(ModelBuilderImpl b
NodeList<Statement> ctorFieldStatement = null;

if (createFullArgsConstructor) {
fullArgumentsCtor = generatedClass.addConstructor( Modifier.PUBLIC );
fullArgumentsCtor = generatedClass.addConstructor( Modifier.publicModifier().getKeyword() );
ctorFieldStatement = NodeList.nodeList();

MethodCallExpr superCall = new MethodCallExpr( null, "super" );
Expand All @@ -205,8 +206,8 @@ private static ClassOrInterfaceDeclaration toClassDeclaration(ModelBuilderImpl b
}

FieldDeclaration field = typeFieldDescr.getInitExpr() == null ?
generatedClass.addField( returnType, fieldName, Modifier.PRIVATE ) :
generatedClass.addFieldWithInitializer( returnType, fieldName, parseExpression(typeFieldDescr.getInitExpr()), Modifier.PRIVATE );
generatedClass.addField( returnType, fieldName, Modifier.privateModifier().getKeyword() ) :
generatedClass.addFieldWithInitializer( returnType, fieldName, parseExpression(typeFieldDescr.getInitExpr()), Modifier.privateModifier().getKeyword() );
field.createSetter();
MethodDeclaration getter = field.createGetter();

Expand Down Expand Up @@ -243,7 +244,7 @@ private static ClassOrInterfaceDeclaration toClassDeclaration(ModelBuilderImpl b
}

if (!keyFields.isEmpty() && keyFields.size() != inheritedFields.size() + typeFields.length) {
ConstructorDeclaration keyArgumentsCtor = generatedClass.addConstructor( Modifier.PUBLIC );
ConstructorDeclaration keyArgumentsCtor = generatedClass.addConstructor( Modifier.publicModifier().getKeyword() );
NodeList<Statement> ctorKeyFieldStatement = NodeList.nodeList();
MethodCallExpr keySuperCall = new MethodCallExpr( null, "super" );
ctorKeyFieldStatement.add( new ExpressionStmt(keySuperCall) );
Expand Down Expand Up @@ -363,7 +364,7 @@ private static MethodDeclaration generateEqualsMethod(String generatedClassName,
equalsStatements.add(parseStatement("return true;"));

final Type returnType = JavaParser.parseType(boolean.class.getSimpleName());
final MethodDeclaration equals = new MethodDeclaration(EnumSet.of(Modifier.PUBLIC), returnType, EQUALS);
final MethodDeclaration equals = new MethodDeclaration(NodeList.nodeList(Modifier.publicModifier()), returnType, EQUALS);
equals.addParameter(Object.class, "o");
equals.addAnnotation("Override");
equals.setBody(new BlockStmt(equalsStatements));
Expand Down Expand Up @@ -419,7 +420,7 @@ private static MethodDeclaration generateHashCodeMethod(Collection<Statement> ha
hashCodeStatements.add(parseStatement("return result;"));

final Type returnType = JavaParser.parseType(int.class.getSimpleName());
final MethodDeclaration equals = new MethodDeclaration(EnumSet.of(Modifier.PUBLIC), returnType, HASH_CODE);
final MethodDeclaration equals = new MethodDeclaration(NodeList.nodeList(Modifier.publicModifier()), returnType, HASH_CODE);
equals.addAnnotation("Override");
equals.setBody(new BlockStmt(hashCodeStatements));
return equals;
Expand Down Expand Up @@ -467,7 +468,7 @@ private static MethodDeclaration generateToStringMethod(String generatedClassNam
final Statement toStringStatement = parseStatement(header + body + close);

final Type returnType = JavaParser.parseType(String.class.getSimpleName());
final MethodDeclaration equals = new MethodDeclaration(EnumSet.of(Modifier.PUBLIC), returnType, TO_STRING);
final MethodDeclaration equals = new MethodDeclaration(NodeList.nodeList(Modifier.publicModifier()), returnType, TO_STRING);
equals.addAnnotation("Override");
equals.setBody(new BlockStmt(NodeList.nodeList(toStringStatement)));
return equals;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.drools.compiler.lang.descr.QueryDescr;
import org.drools.javaparser.JavaParser;
import org.drools.javaparser.ast.Modifier;
import org.drools.javaparser.ast.NodeList;
import org.drools.javaparser.ast.body.MethodDeclaration;
import org.drools.javaparser.ast.expr.AssignExpr;
import org.drools.javaparser.ast.expr.ClassExpr;
Expand Down Expand Up @@ -86,7 +87,7 @@ public static void processQuery(KnowledgeBuilderImpl kbuilder, PackageModel pack
new ModelGeneratorVisitor(context, packageModel).visit(queryDescr.getLhs());
final Type queryType = JavaParser.parseType(Query.class.getCanonicalName());

MethodDeclaration queryMethod = new MethodDeclaration(EnumSet.of(Modifier.PRIVATE), queryType, "query_" + toId(queryDescr.getName()));
MethodDeclaration queryMethod = new MethodDeclaration(NodeList.nodeList(Modifier.privateModifier()), queryType, "query_" + toId(queryDescr.getName()));

BlockStmt queryBody = new BlockStmt();
ModelGenerator.createVariables(kbuilder, queryBody, packageModel, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ private Set<String> parseInitBlock( RuleContext context2, AccumulateDescr descr,
for (VariableDeclarator vd : vdExpr.getVariables()) {
final String variableName = vd.getNameAsString();
contextFieldNames.add(variableName);
templateContextClass.addField(vd.getType(), variableName, Modifier.PUBLIC);
templateContextClass.addField(vd.getType(), variableName, Modifier.publicModifier().getKeyword());
createInitializer(variableName, vd.getInitializer()).ifPresent(statement -> {
initMethodBody.addStatement(statement);
statement.findAll(NameExpr.class).stream().map( n -> n.toString()).filter( context2::hasDeclaration ).forEach( externalDeclrs::add );
Expand Down Expand Up @@ -538,7 +538,7 @@ private Set<String> parseInitBlock( RuleContext context2, AccumulateDescr descr,
.map(t -> DrlxParseUtil.classToReferenceType(t.getRawClass()))
.orElseThrow(() -> new RuntimeException("Unknown type: " + initCreationExpression));

templateContextClass.addField(type, variableName, Modifier.PUBLIC);
templateContextClass.addField(type, variableName, Modifier.publicModifier().getKeyword());
final Optional<Statement> initializer = createInitializer(variableName, Optional.of(initCreationExpression));
initializer.ifPresent(initMethodBody::addStatement);
accumulateDeclarations.add(new DeclarationSpec(variableName, DrlxParseUtil.getClassFromContext(context2.getTypeResolver(), type.asString())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.List;

import org.drools.javaparser.ast.Node;
import org.drools.javaparser.ast.NodeList;
import org.drools.javaparser.ast.expr.ArrayAccessExpr;
import org.drools.javaparser.ast.expr.Expression;
import org.drools.javaparser.ast.expr.IntegerLiteralExpr;
Expand Down Expand Up @@ -39,8 +40,10 @@ public void flattenFields() {
@Test
public void flattenMethodCall() {
List<Node> actual = flattenScope(expr("name.startsWith(\"M\")"));
List<Node> expected = asList(new NameExpr("name"), new MethodCallExpr(new NameExpr("name"), "startsWith",
nodeList(new StringLiteralExpr("M"))));
MethodCallExpr methodCallExpr = new MethodCallExpr(new NameExpr("name"), "startsWith",
nodeList(new StringLiteralExpr("M")));
methodCallExpr.setTypeArguments(NodeList.nodeList());
List<Node> expected = asList(new NameExpr("name"), methodCallExpr);
compareArrays(actual, expected);
}

Expand All @@ -50,6 +53,7 @@ public void flattenArrayAccess() {

NameExpr name = new NameExpr("$p");
final MethodCallExpr mc = new MethodCallExpr(name, "getChildrenA", nodeList());
mc.setTypeArguments(NodeList.nodeList());
List<Node> expected = asList(name, mc, new ArrayAccessExpr(mc, new IntegerLiteralExpr(0)));
compareArrays(actual, expected);
}
Expand Down
Empty file added file.txt
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.kie.dmn.core.compiler.execmodelbased;

import java.util.EnumSet;
import java.util.List;

import org.drools.javaparser.JavaParser;
Expand All @@ -41,7 +40,7 @@ public class JavaParserSourceGenerator {
private ClassOrInterfaceDeclaration firstClass;
private CompilationUnit compilationUnit;

public static EnumSet<Modifier> PUBLIC_STATIC_FINAL = EnumSet.of(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL);
public static NodeList<Modifier> PUBLIC_STATIC_FINAL = NodeList.nodeList(Modifier.publicModifier(), Modifier.staticModifier(), Modifier.finalModifier());

public JavaParserSourceGenerator(String className, String namespace, String packageName) {
this.compilationUnit = JavaParser.parse("public class " + className + namespace + "{ }");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,9 +484,9 @@ public static DirectCompilerResult compiledErrorUnaryTest(String msg) {
VariableDeclarator vd = new VariableDeclarator(JavaParser.parseClassOrInterfaceType(UnaryTest.class.getCanonicalName()), constantName);
vd.setInitializer(initializer);
FieldDeclaration fd = new FieldDeclaration();
fd.setModifier(Modifier.PUBLIC, true);
fd.setModifier(Modifier.STATIC, true);
fd.setModifier(Modifier.FINAL, true);
fd.setModifier(Modifier.publicModifier().getKeyword(), true);
fd.setModifier(Modifier.staticModifier().getKeyword(), true);
fd.setModifier(Modifier.finalModifier().getKeyword(), true);
fd.addVariable(vd);

fd.setJavadocComment(" FEEL unary test: - ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import org.drools.javaparser.JavaParser;
import org.drools.javaparser.ast.Modifier;
import org.drools.javaparser.ast.NodeList;
import org.drools.javaparser.ast.body.FieldDeclaration;
import org.drools.javaparser.ast.body.VariableDeclarator;
import org.drools.javaparser.ast.expr.Expression;
Expand Down Expand Up @@ -53,7 +54,7 @@ public class Constants {

public static FieldDeclaration of(Type type, String name, Expression initializer) {
return new FieldDeclaration(
EnumSet.of(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL),
NodeList.nodeList(Modifier.publicModifier(), Modifier.staticModifier(), Modifier.finalModifier()),
new VariableDeclarator(type, name, initializer));
}

Expand Down
Loading

0 comments on commit 6ef0328

Please sign in to comment.