Rikkola opened a new issue, #7181:
URL: https://github.com/apache/incubator-kie/issues/7181

   ### Describe the bug
   
   A possible bug that needs to be investigated. Came up while working on 
another area.
   
   ### Summary
   When a Java class carries `@Role(Role.Type.EVENT) `and a DRL file contains a 
`declare` block for that same type (even an empty one), the `@Role` annotation 
on the class is silently discarded. The engine treats the type as `FACT` 
instead of `EVENT`.
   
   ### Root cause
   `TypeDeclarationFactory.processTypeDeclaration() `constructs the 
`TypeDeclaration` using the string constructor, which defaults `role = 
Role.Type.FACT`. The Phase 1 call to `TypeDeclaration.processTypeAnnotations()` 
(added in the `@PropertyChangeSupport` fix) handles `@Expires`, property 
reactivity, and `@PropertyChangeSupport` — but it never reads `@Role` from the 
Java class. Phase 2 (`processAnnotations` from the DRL descriptor) only sets 
the role if the DRL `declare` block explicitly carries `@role(...).` An empty 
`declare` block leaves the role at the `FACT` default.
   
   Contrast: the private `TypeDeclaration(Class<?>)` constructor (used by 
`createTypeDeclarationForBean`) does read `@Role` from the class — but that 
path is not invoked from `processTypeDeclaration`.
   
   ### Steps to reproduce
   Java class:
   
   ```
   @Role(value = Role.Type.EVENT)
   public static class MyEvent { }
   ```
   
   DRL with an empty `declare` block:
   
   ```
   import com.example.MyEvent;
   declare MyEvent
   end
   rule "window rule"
   when
       MyEvent() over window:time (1d)
   then
   end
   ```
   
   Build in STREAM mode (e.g. `KieBaseTestConfiguration.STREAM_IDENTITY`).
   
   **Expected**: builds without errors — `@Role(EVENT)` on the class must 
survive the empty redeclaration.
   
   **Actual**:
   
   ```
   A Sliding Window can only be assigned to types declared with @role( event ).
   The type '[ClassObjectType class=com.example.MyEvent]' in 'window rule' is 
not declared as an Event.
   ```
   
   Note: the bug does not occur when there is no `declare` block at all. The 
presence of any `declare` block — even an empty one — is enough to trigger it.
   
   ### Failing test
   Add to `CepJavaTypeTest` in `drools-test-coverage/test-compiler-integration`:
   
   ```
   @ParameterizedTest(name = "KieBase type={0}")
   @MethodSource("parameters")
   public void 
testJavaRoleEventNotOverriddenByEmptyDrlDeclare(KieBaseTestConfiguration 
kieBaseTestConfiguration) {
       // @Role(EVENT) on the Java class must be preserved even when a DRL 
'declare' block
       // re-declares the type without any @role annotation.
       final String drl =
               "import " + Event.class.getCanonicalName() + ";\n"
               + "declare Event\n"   // no @role annotation — Java class 
annotation must win
               + "end\n"
               + "rule \"window rule\"\n"
               + "when\n"
               + "    Event() over window:time (1d)\n"
               + "then\n"
               + "end\n";
   
       final KieBuilder kieBuilder = 
KieUtil.getKieBuilderFromDrls(kieBaseTestConfiguration, false, drl);
       assertThat(kieBuilder.getResults().getMessages())
               .as("@Role(EVENT) declared on the Java class must survive an 
empty DRL declare block; "
                       + "window:time is only valid for EVENT types and must 
compile without errors")
               .isEmpty();
   }
   ```
   
   Observed failure (1 of 2 parameterised variants fails — the STREAM 
configuration):
   ```
   [@Role(EVENT) declared on the Java class must survive an empty DRL declare 
block;
    window:time is only valid for EVENT types and must compile without errors]
   Expecting empty but was: [Message [id=1, kieBase=KieBaseModelName, 
level=ERROR, path=rules1.drl, line=6, column=0
      text=A Sliding Window can only be assigned to types declared with @role( 
event ).
           The type '[ClassObjectType class=...CepJavaTypeTest$Event]'
           in 'window rule' is not declared as an Event.]]
   ```
   
   ### Fix direction
   In `TypeDeclarationFactory.processTypeDeclaration()`, Phase 1 should also 
read `@Role` from the existing class (e.g. calling `setRole` when the class 
carries it, or extending `TypeDeclaration.processTypeAnnotations()` to cover 
it). Phase 2 in `processAnnotations` should then guard with `(type.getSetMask() 
& ROLE_BIT) == 0` before overwriting, mirroring the guard already applied to 
`@PropertyChangeSupport`.
   
   ### Related
   This is a sibling gap to the `@PropertyChangeSupport `class-level annotation 
bug fixed in the same file. Both share the same structural root cause: 
class-level annotations that Phase 1 does not propagate to the 
`Typ`eDeclaration, leaving them silently lost when a DRL `declare` block is 
present.
   
   ### Expected behavior
   
   _No response_
   
   ### Actual behavior
   
   _No response_
   
   ### How to Reproduce?
   
   _No response_
   
   ### Output of `uname -a` or `ver`
   
   _No response_
   
   ### Output of `java -version`
   
   _No response_
   
   ### GraalVM version (if different from Java)
   
   _No response_
   
   ### Kogito version or git rev (or at least Quarkus version if you are using 
Kogito via Quarkus platform BOM)
   
   _No response_
   
   ### Build tool (ie. output of `mvnw --version` or `gradlew --version`)
   
   _No response_
   
   ### Additional information
   
   _No response_


-- 
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]

Reply via email to