This is an automated email from the ASF dual-hosted git repository.
tkobayas pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-kie.git
The following commit(s) were added to refs/heads/main by this push:
new e81bdbe6cfe [incubator-kie#7262] Executable model: honor capitalized
DRL type annotations (#7270)
e81bdbe6cfe is described below
commit e81bdbe6cfef0646c51e9f5f797f8e2b3a288bd2
Author: eatmoreduck 多吃鸭 <[email protected]>
AuthorDate: Tue Nov 10 09:03:51 2026 +0800
[incubator-kie#7262] Executable model: honor capitalized DRL type
annotations (#7270)
* Executable model: honor capitalized DRL type annotations
A declare block redeclaring an existing Java class silently ignored the
capitalized (Java-name) spelling of the built-in type annotations: the
executable model kept the Java class's @Role/@Expires values instead of
the DRL ones, while the classic compiler honored both spellings (#7252).
The generated model registers type annotations under the name as written
in the DRL, and TypeDeclarationUtil.wireMetaTypeAnnotations matched that
key against a lowercase-only switch: '@Role(FACT)' matched no case, was
classified as a custom annotation, and reached the TypeDeclaration as
raw ClassDefinition metadata instead of configuring it.
wireMetaTypeAnnotations now canonicalizes the key before matching: an
unqualified name maps to its built-in case-insensitively (Role -> role,
PropertyReactive -> propertyReactive), and a qualified name only maps
when it is a built-in's own FQN — any other qualified annotation keeps
its name and still lands in custom metadata. Custom annotations keep
their original spelling in ClassDefinition metadata.
AlignAnnotationTest gains capitalized @Role(FACT)/@Expires(5s) override
cases, red on main for the executable-model run types.
* Executable model: resolve built-in annotation identity at codegen time
Restructure per review: the consumer-side case-insensitive mapping in
TypeDeclarationUtil misclassifies a custom annotation whose simple name
collides with a built-in (e.g. an imported com.acme.ClassReactive), which
was correctly kept as custom metadata before.
AnnotationNormalizer already resolves each annotation's identity before
codegen, so AnnotationDescr.getFullyQualifiedName() tells the built-ins
apart from same-named customs. POJOGenerator now registers a resolved
built-in under its canonical name (role, expires, ...) and keeps any
other annotation under the name as written, leaving custom metadata keys
unchanged. TypeDeclarationUtil reverts to its previous exact-name match.
Adds: capitalized @ClassReactive on a plain class (built-in semantics,
red on main), and an imported custom @ClassReactive whose simple name
collides — property reactivity must be unchanged and the annotation kept
as custom metadata (red with the previous consumer-side approach).
---
.../generator/declaredtype/POJOGenerator.java | 31 +++++++++-
.../codegen/execmodel/AlignAnnotationTest.java | 67 ++++++++++++++++++++++
.../codegen/execmodel/domain/ClassReactive.java | 37 ++++++++++++
3 files changed, 134 insertions(+), 1 deletion(-)
diff --git
a/drools-model/drools-model-codegen/src/main/java/org/drools/model/codegen/execmodel/generator/declaredtype/POJOGenerator.java
b/drools-model/drools-model-codegen/src/main/java/org/drools/model/codegen/execmodel/generator/declaredtype/POJOGenerator.java
index ca7ade7f46a..c39d59fb121 100644
---
a/drools-model/drools-model-codegen/src/main/java/org/drools/model/codegen/execmodel/generator/declaredtype/POJOGenerator.java
+++
b/drools-model/drools-model-codegen/src/main/java/org/drools/model/codegen/execmodel/generator/declaredtype/POJOGenerator.java
@@ -70,6 +70,20 @@ public class POJOGenerator implements CompilationPhase {
private static final List<String> exprAnnotations =
Arrays.asList("duration", "timestamp");
+ /**
+ * Maps a built-in type annotation's FQN to the canonical name the
generated model registers it
+ * under. Mirrors the known-annotation names in
+ * {@code org.drools.modelcompiler.util.TypeDeclarationUtil}.
+ */
+ private static final Map<String, String> BUILTIN_TYPE_ANNOTATIONS_BY_FQN =
Map.of(
+ "org.kie.api.definition.type.Role", "role",
+ "org.kie.api.definition.type.Duration", "duration",
+ "org.kie.api.definition.type.Timestamp", "timestamp",
+ "org.kie.api.definition.type.Expires", "expires",
+ "org.kie.api.definition.type.PropertyReactive", "propertyReactive",
+ "org.kie.api.definition.type.ClassReactive", "classReactive",
+ "org.kie.api.definition.type.PropertyChangeSupport",
"propertyChangeSupport");
+
public POJOGenerator(BuildResultCollector builder,
InternalKnowledgePackage pkg, PackageDescr packageDescr, PackageModel
packageModel,
Collection<? extends PackageDescr> allPackages,
PackageRegistryManager pkgRegistryManager) {
this.builder = builder;
@@ -217,7 +231,7 @@ public class POJOGenerator implements CompilationPhase {
for (AnnotationDescr ann : annotations) {
typeMetaDataCall = new MethodCallExpr(typeMetaDataCall,
ADD_ANNOTATION_CALL);
- typeMetaDataCall.addArgument(toStringLiteral(ann.getName()));
+
typeMetaDataCall.addArgument(toStringLiteral(typeMetadataAnnotationName(ann)));
for (Map.Entry<String, Object> entry :
ann.getValueMap().entrySet()) {
MethodCallExpr annotationValueCall =
createDslTopLevelMethod(ANNOTATION_VALUE_CALL);
annotationValueCall.addArgument(toStringLiteral(entry.getKey()));
@@ -239,6 +253,21 @@ public class POJOGenerator implements CompilationPhase {
return typeMetaDataCall;
}
+ /**
+ * The name a type annotation is registered under in the generated model.
{@code AnnotationNormalizer}
+ * resolved the annotation's identity before codegen, so when it resolved
to a built-in the canonical
+ * (lowercase) name is emitted and {@code TypeDeclarationUtil} recognizes
it no matter which spelling
+ * the DRL used ({@code @Role} or {@code @role}). Anything else keeps the
name as written, so a custom
+ * annotation whose simple name collides with a built-in (e.g. an imported
{@code com.acme.ClassReactive})
+ * stays custom metadata.
+ */
+ private static String typeMetadataAnnotationName(AnnotationDescr ann) {
+ String canonical = ann.getFullyQualifiedName() == null
+ ? null
+ :
BUILTIN_TYPE_ANNOTATIONS_BY_FQN.get(ann.getFullyQualifiedName());
+ return canonical != null ? canonical : ann.getName();
+ }
+
private static void addAnnotationValueIfNotNull(MethodCallExpr
typeMetaDataCall, Object value) {
if (value != null) {
MethodCallExpr annotationValueCall =
createDslTopLevelMethod(ANNOTATION_VALUE_CALL);
diff --git
a/drools-model/drools-model-codegen/src/test/java/org/drools/model/codegen/execmodel/AlignAnnotationTest.java
b/drools-model/drools-model-codegen/src/test/java/org/drools/model/codegen/execmodel/AlignAnnotationTest.java
index 6da83c930a8..47b29b38c3d 100644
---
a/drools-model/drools-model-codegen/src/test/java/org/drools/model/codegen/execmodel/AlignAnnotationTest.java
+++
b/drools-model/drools-model-codegen/src/test/java/org/drools/model/codegen/execmodel/AlignAnnotationTest.java
@@ -34,6 +34,7 @@ import org.kie.api.runtime.KieSession;
import org.kie.internal.builder.conf.PropertySpecificOption;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assumptions.assumeTrue;
/**
* Reproduces the annotation-precedence discrepancy between classic and
executable
@@ -64,6 +65,16 @@ public class AlignAnnotationTest extends BaseModelTest {
"end\n" +
"rule R when AnnotatedEvent() then end\n";
+ // #7252: capitalized (Java-name) annotations must override the Java class
+ // annotations exactly like the lowercase forms do.
+ private static final String ANNOTATED_EVENT_CAPITAL_OVERRIDE_DRL =
+ "import " + AnnotatedEvent.class.getCanonicalName() + ";\n" +
+ "declare AnnotatedEvent\n" +
+ " @Role(FACT)\n" +
+ " @Expires(5s)\n" +
+ "end\n" +
+ "rule R when AnnotatedEvent() then end\n";
+
private TypeDeclaration getTypeDeclaration(RUN_TYPE runType, String drl,
Class<?> clazz) {
return getTypeDeclaration(runType, null, drl, clazz);
}
@@ -217,6 +228,22 @@ public class AlignAnnotationTest extends BaseModelTest {
assertThat(typeDecl.getExpirationOffset()).isEqualTo(5_000L);
}
+ // --- #7252: capitalized DRL annotations must override Java class
annotations ---
+
+ @ParameterizedTest
+ @MethodSource("parameters")
+ public void testCapitalizedRoleExplicitOverride(RUN_TYPE runType) {
+ TypeDeclaration typeDecl = getTypeDeclaration(runType,
ANNOTATED_EVENT_CAPITAL_OVERRIDE_DRL, AnnotatedEvent.class);
+
assertThat(typeDecl.getRole()).as("@Role(FACT)").isEqualTo(Role.Type.FACT);
+ }
+
+ @ParameterizedTest
+ @MethodSource("parameters")
+ public void testCapitalizedExpiresExplicitOverride(RUN_TYPE runType) {
+ TypeDeclaration typeDecl = getTypeDeclaration(runType,
ANNOTATED_EVENT_CAPITAL_OVERRIDE_DRL, AnnotatedEvent.class);
+
assertThat(typeDecl.getExpirationOffset()).as("@Expires(5s)").isEqualTo(5_000L);
+ }
+
@ParameterizedTest
@MethodSource("parameters")
public void testPropertyReactiveExplicitOverride(RUN_TYPE runType) {
@@ -224,4 +251,44 @@ public class AlignAnnotationTest extends BaseModelTest {
// DRL @propertyReactive overrides Java @ClassReactive
assertThat(typeDecl.isPropertyReactive()).isTrue();
}
+
+ // --- #7252: capitalized ClassReactive on a plain class means the
built-in, not a custom annotation ---
+
+ @ParameterizedTest
+ @MethodSource("parameters")
+ public void testCapitalizedClassReactiveOnPlainClass(RUN_TYPE runType) {
+ String drl =
+ "import " + Person.class.getCanonicalName() + ";\n" +
+ "declare Person\n" +
+ " @ClassReactive\n" +
+ "end\n" +
+ "rule R when Person() then end\n";
+
+ TypeDeclaration typeDecl = getTypeDeclaration(runType, drl,
Person.class);
+
assertThat(typeDecl.isPropertyReactive()).as("@ClassReactive").isFalse();
+ }
+
+ // --- #7270: an imported custom annotation whose simple name collides
with a built-in stays custom ---
+
+ @ParameterizedTest
+ @MethodSource("parameters")
+ public void
testImportedAnnotationWithCollidingSimpleNameStaysCustom(RUN_TYPE runType) {
+ String drl =
+ "import " +
org.drools.model.codegen.execmodel.domain.ClassReactive.class.getCanonicalName()
+ ";\n" +
+ "import " + Person.class.getCanonicalName() + ";\n" +
+ "declare Person\n" +
+ " @ClassReactive(\"custom-marker\")\n" +
+ "end\n" +
+ "rule R when Person() then end\n";
+
+ TypeDeclaration typeDecl = getTypeDeclaration(runType, drl,
Person.class);
+ // The custom annotation must not act as the built-in @classReactive:
property reactivity
+ // keeps its default in both models...
+ assertThat(typeDecl.isPropertyReactive()).as("custom @ClassReactive
must not switch off property reactivity").isTrue();
+ // ...and PATTERN_DSL keeps it as custom metadata under its own name.
(STANDARD_FROM_DRL does
+ // not populate a ClassDefinition metadata map for custom annotations
on an existing class.)
+ assumeTrue(runType == RUN_TYPE.PATTERN_DSL);
+ assertThat(typeDecl.getTypeClassDef().getMetaData()).as("custom
annotation is kept as custom metadata")
+
.containsKey(org.drools.model.codegen.execmodel.domain.ClassReactive.class.getSimpleName());
+ }
}
diff --git
a/drools-model/drools-model-codegen/src/test/java/org/drools/model/codegen/execmodel/domain/ClassReactive.java
b/drools-model/drools-model-codegen/src/test/java/org/drools/model/codegen/execmodel/domain/ClassReactive.java
new file mode 100644
index 00000000000..08a9f20ed47
--- /dev/null
+++
b/drools-model/drools-model-codegen/src/test/java/org/drools/model/codegen/execmodel/domain/ClassReactive.java
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.drools.model.codegen.execmodel.domain;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * A custom annotation whose simple name collides with the built-in
+ * {@code org.kie.api.definition.type.ClassReactive}. Imported in a declare
block, it must stay
+ * custom metadata and not switch off property reactivity the way the built-in
does
+ * (see incubator-kie#7270).
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.TYPE)
+public @interface ClassReactive {
+
+ String value() default "";
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]