Copilot commented on code in PR #6414:
URL:
https://github.com/apache/incubator-kie-drools/pull/6414#discussion_r2265782808
##########
drools-model/drools-model-codegen/src/main/java/org/drools/model/codegen/execmodel/generator/declaredtype/POJOGenerator.java:
##########
@@ -153,7 +153,38 @@ private void createPOJO(TypeDeclarationDescr typeDescr) {
MARKER_INTERFACES)
.toClassDeclaration();
packageModel.addGeneratedPOJO(generatedClass);
- addTypeMetadata(typeDescr.getTypeName());
+
+ // Add type metadata including custom annotations
+ String fullTypeName = pkg.getName() + "." + typeDescr.getTypeName();
+ MethodCallExpr typeMetaDataCall = registerTypeMetaData(fullTypeName);
+
+ // Add custom metadata from non-defined annotations
+ Map<String, Object> classMetaData =
descrDeclaredTypeDefinition.getClassMetaData();
+ for (Map.Entry<String, Object> entry : classMetaData.entrySet()) {
+ typeMetaDataCall = new MethodCallExpr(typeMetaDataCall,
ADD_ANNOTATION_CALL);
+ typeMetaDataCall.addArgument(toStringLiteral(entry.getKey()));
+ addAnnotationValueIfNotNull(typeMetaDataCall, entry.getValue());
+ }
+
+ // Add field metadata
+ for (DescrFieldDefinition field :
descrDeclaredTypeDefinition.getFields()) {
+ Map<String, Object> fieldMetaData = field.getFieldMetaData();
+ if (!fieldMetaData.isEmpty()) {
+ // First, add the field to the TypeMetaData
+ typeMetaDataCall = new MethodCallExpr(typeMetaDataCall,
"withField");
+
typeMetaDataCall.addArgument(toStringLiteral(field.getFieldName()));
+
+ // Then add each annotation as field metadata
+ for (Map.Entry<String, Object> entry :
fieldMetaData.entrySet()) {
+ typeMetaDataCall = new MethodCallExpr(typeMetaDataCall,
"withFieldAnnotation");
+
typeMetaDataCall.addArgument(toStringLiteral(field.getFieldName()));
+
typeMetaDataCall.addArgument(toStringLiteral(entry.getKey()));
+ addAnnotationValueIfNotNull(typeMetaDataCall,
entry.getValue());
+ }
+ }
+ }
+
+ packageModel.addTypeMetaDataExpressions(typeMetaDataCall);
}
private void addTypeMetadata(String typeName) {
Review Comment:
The method `addTypeMetadata` appears to be unused after the refactoring. The
new implementation directly calls `registerTypeMetaData` and processes metadata
inline. This method should be removed to avoid dead code.
##########
drools-model/drools-model-codegen/src/main/java/org/drools/model/codegen/execmodel/generator/declaredtype/DescrTypeDefinition.java:
##########
@@ -243,15 +258,33 @@ private List<DescrFieldDefinition> processFields() {
private ProcessedTypeField processTypeField(int position, TypeFieldDescr
typeFieldDescr) {
DescrFieldDefinition typeField = new
DescrFieldDefinition(typeFieldDescr);
- List<DescrAnnotationDefinition> parsedAnnotations =
typeFieldDescr.getAnnotations().stream()
- .map(this::createAnnotationDefinition)
- .filter(Optional::isPresent)
- .map(Optional::get)
- .collect(toList());
+ // Create a map of successfully parsed annotations, keyed by original
annotation name
+ Map<String, DescrAnnotationDefinition> parsedAnnotations = new
HashMap<>();
+ for (AnnotationDescr ann : typeFieldDescr.getAnnotations()) {
+ Optional<DescrAnnotationDefinition> parsed =
createAnnotationDefinition(ann);
+ if (parsed.isPresent()) {
+ parsedAnnotations.put(ann.getName(), parsed.get());
+ }
+ }
- parsedAnnotations.stream().filter(a -> !a.isPosition()).forEach(a ->
processDefinitions(typeField, a));
+ parsedAnnotations.values().stream().filter(a ->
!a.isPosition()).forEach(a -> processDefinitions(typeField, a));
- int currentFieldPosition = setFieldPosition(position, typeField,
parsedAnnotations);
+ // Add built-in annotations and non-defined custom annotations as
field metadata
+ for (AnnotationDescr ann : typeFieldDescr.getAnnotations()) {
+ DescrAnnotationDefinition parsed =
parsedAnnotations.get(ann.getName());
+ if (parsed != null) {
+ // This annotation was successfully parsed - check if it's
built-in
+ if (isBuiltInAnnotation(parsed)) {
+ Object value = ann.getSingleValue();
+ typeField.addFieldMetaData(ann.getName(), value);
+ }
+ } else {
+ // This annotation failed to parse - it's a non-defined custom
annotation
+ typeField.addFieldMetaData(ann.getName(),
ann.getSingleValue());
Review Comment:
Non-defined custom annotations are being added to field metadata regardless
of whether they failed to parse due to unknown annotation class or unknown
annotation properties. This could result in storing annotations that have
parsing errors. The logic should only store metadata for
UnkownAnnotationClassException, not for other parsing failures.
```suggestion
// Only add metadata for non-defined custom annotations if
the failure was due to UnknownAnnotationClassException
try {
// Try to parse again to see if it throws
UnknownAnnotationClassException
createAnnotationDefinition(ann);
} catch (UnknownAnnotationClassException e) {
typeField.addFieldMetaData(ann.getName(),
ann.getSingleValue());
} catch (Exception e) {
// Do not add metadata for other parsing failures
}
```
##########
drools-model/drools-model-codegen/src/main/java/org/drools/model/codegen/execmodel/generator/declaredtype/DescrTypeDefinition.java:
##########
@@ -109,11 +113,22 @@ private void processClassAnnotations() {
}
try {
annotations.add(DescrAnnotationDefinition.fromDescr(typeResolver, ann));
- } catch (UnkownAnnotationClassException | UnknownKeysInAnnotation
e) {
- // Do not do anything
+ } catch (UnkownAnnotationClassException e) {
+ // Store non-defined custom annotations as metadata
+ // class level built-in annotations are not added here. Rather
added in TypeDeclarationUtil at the later phase
Review Comment:
[nitpick] The comment mentions that 'class level built-in annotations are
not added here' but doesn't explain why or where they are added instead. This
could be clearer by referencing the specific location (TypeDeclarationUtil)
mentioned in the broader comment.
```suggestion
// Class level built-in annotations are not added here; they
are added in TypeDeclarationUtil at a later phase.
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]