use of org.drools.modelcompiler.builder.generator.declaredtype.api.FieldDefinition in project drools by kiegroup.
the class GeneratedClassDeclaration method generateFullClass.
private ClassOrInterfaceDeclaration generateFullClass(String generatedClassName, Collection<FieldDefinition> inheritedFields) {
boolean hasSuper = typeDefinition.getSuperTypeName().isPresent();
generatedHashcode = new GeneratedHashcode(hasSuper);
generatedToString = new GeneratedToString(generatedClassName);
generatedEqualsMethod = new GeneratedEqualsMethod(generatedClassName, hasSuper);
List<? extends FieldDefinition> typeFields = typeDefinition.getFields();
GeneratedConstructor fullArgumentConstructor = GeneratedConstructor.factory(generatedClass, typeFields);
typeDefinition.getMethods().forEach(this::addMethod);
for (FieldDefinition tf : typeFields) {
processTypeField(tf);
}
List<FieldDefinition> keyFields = typeDefinition.getKeyFields();
fullArgumentConstructor.generateConstructor(inheritedFields, keyFields);
if (!keyFields.isEmpty()) {
generatedClass.addMember(generatedEqualsMethod.method());
generatedClass.addMember(generatedHashcode.method());
}
generatedClass.addMember(generatedToString.method());
return generatedClass;
}
use of org.drools.modelcompiler.builder.generator.declaredtype.api.FieldDefinition in project drools by kiegroup.
the class NoConstructor method generateConstructor.
@Override
public void generateConstructor(Collection<FieldDefinition> inheritedFields, List<FieldDefinition> keyFields) {
NodeList<Modifier> modifiers = publicConstructor ? new NodeList<>(Modifier.publicModifier()) : new NodeList<Modifier>();
ConstructorDeclaration constructor = new ConstructorDeclaration(modifiers, generatedClass.getNameAsString());
NodeList<Statement> fieldAssignStatement = nodeList();
MethodCallExpr superCall = new MethodCallExpr(null, "super");
for (FieldDefinition typeField : inheritedFields) {
if (typeField.isStatic()) {
continue;
}
String fieldName = typeField.getFieldName();
addConstructorArgument(constructor, typeField.getObjectType(), fieldName);
superCall.addArgument(fieldName);
if (typeField.isKeyField()) {
keyFields.add(typeField);
}
}
if (shouldCallSuper) {
fieldAssignStatement.add(new ExpressionStmt(superCall));
}
int nonStaticFields = 0;
for (FieldDefinition fieldDefinition : typeDeclarationFields) {
if (fieldDefinition.isStatic() || fieldDefinition.isOverride()) {
continue;
}
nonStaticFields++;
String fieldName = fieldDefinition.getFieldName();
Type returnType = toClassOrInterfaceType(fieldDefinition.getObjectType());
addConstructorArgument(constructor, returnType, fieldName);
fieldAssignStatement.add(fieldAssignment(fieldName));
}
constructor.setBody(new BlockStmt(fieldAssignStatement));
if (constructor.getParameters().isNonEmpty()) {
generatedClass.addMember(constructor);
}
if (!keyFields.isEmpty() && keyFields.size() != inheritedFields.size() + nonStaticFields) {
generateKieFieldsConstructor(keyFields);
}
}
use of org.drools.modelcompiler.builder.generator.declaredtype.api.FieldDefinition 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));
}
Aggregations