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 1eb552ecd1d [incubator-kie#7306] fix(drools): declared enums with more 
than 127 literals fail on first use (#7307)
1eb552ecd1d is described below

commit 1eb552ecd1db8543a499c9918712111ab78f9dc5
Author: Daniel Clark <[email protected]>
AuthorDate: Thu Nov 26 02:04:09 2026 +0100

    [incubator-kie#7306] fix(drools): declared enums with more than 127 
literals fail on first use (#7307)
    
    AsmUtil.pushInt emitted BIPUSH for every value above 5. Its operand is a
    signed byte that ASM truncates silently, so the generated enum <clinit>
    built a negative or short $VALUES array. Use SIPUSH, then LDC, beyond the
    byte range, as javac does.
    
    Co-authored-by: Claude <[email protected]>
---
 .../src/main/java/org/drools/mvel/asm/AsmUtil.java |  9 ++-
 .../test/java/org/drools/mvel/asm/AsmUtilTest.java | 69 ++++++++++++++++++++++
 .../org/drools/mvel/integrationtests/EnumTest.java | 38 ++++++++++++
 3 files changed, 115 insertions(+), 1 deletion(-)

diff --git a/drools-mvel/src/main/java/org/drools/mvel/asm/AsmUtil.java 
b/drools-mvel/src/main/java/org/drools/mvel/asm/AsmUtil.java
index cf7332a1429..9ab90020fc4 100644
--- a/drools-mvel/src/main/java/org/drools/mvel/asm/AsmUtil.java
+++ b/drools-mvel/src/main/java/org/drools/mvel/asm/AsmUtil.java
@@ -686,7 +686,14 @@ public final class AsmUtil {
                 break;
             case 5 : mv.visitInsn( Opcodes.ICONST_5 );
                 break;
-            default : mv.visitIntInsn( Opcodes.BIPUSH, j );
+            default :
+                if ( j >= Byte.MIN_VALUE && j <= Byte.MAX_VALUE ) {
+                    mv.visitIntInsn( Opcodes.BIPUSH, j );
+                } else if ( j >= Short.MIN_VALUE && j <= Short.MAX_VALUE ) {
+                    mv.visitIntInsn( Opcodes.SIPUSH, j );
+                } else {
+                    mv.visitLdcInsn( j );
+                }
         }
     }
 }
diff --git a/drools-mvel/src/test/java/org/drools/mvel/asm/AsmUtilTest.java 
b/drools-mvel/src/test/java/org/drools/mvel/asm/AsmUtilTest.java
new file mode 100644
index 00000000000..05408a6ed97
--- /dev/null
+++ b/drools-mvel/src/test/java/org/drools/mvel/asm/AsmUtilTest.java
@@ -0,0 +1,69 @@
+/*
+ * 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.mvel.asm;
+
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+import org.mvel2.asm.ClassWriter;
+import org.mvel2.asm.MethodVisitor;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mvel2.asm.Opcodes.ACC_PUBLIC;
+import static org.mvel2.asm.Opcodes.ACC_STATIC;
+import static org.mvel2.asm.Opcodes.ACC_SUPER;
+import static org.mvel2.asm.Opcodes.IRETURN;
+
+public class AsmUtilTest {
+
+    private static final String PROBE_CLASS_NAME = 
"org.drools.mvel.asm.PushIntProbe";
+
+    @ParameterizedTest(name = "value={0}")
+    @ValueSource(ints = { Integer.MIN_VALUE, Short.MIN_VALUE - 1, 
Short.MIN_VALUE, Byte.MIN_VALUE - 1, Byte.MIN_VALUE, -1,
+                          0, 5, 6, Byte.MAX_VALUE, Byte.MAX_VALUE + 1, 
Short.MAX_VALUE, Short.MAX_VALUE + 1, Integer.MAX_VALUE })
+    public void pushIntPushesTheGivenValue(int value) throws Exception {
+        ClassWriter cw = 
ClassGenerator.createClassWriter(getClass().getClassLoader(),
+                                                          ACC_PUBLIC | 
ACC_SUPER,
+                                                          
PROBE_CLASS_NAME.replace('.', '/'),
+                                                          null,
+                                                          "java/lang/Object",
+                                                          null);
+        MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "value", 
"()I", null, null);
+        mv.visitCode();
+        AsmUtil.pushInt(mv, value);
+        mv.visitInsn(IRETURN);
+        mv.visitMaxs(0, 0);
+        mv.visitEnd();
+        cw.visitEnd();
+
+        Class<?> probe = new 
ProbeClassLoader(getClass().getClassLoader()).define(PROBE_CLASS_NAME, 
cw.toByteArray());
+
+        assertThat(probe.getMethod("value").invoke(null)).isEqualTo(value);
+    }
+
+    private static class ProbeClassLoader extends ClassLoader {
+
+        ProbeClassLoader(ClassLoader parent) {
+            super(parent);
+        }
+
+        Class<?> define(String name, byte[] bytes) {
+            return defineClass(name, bytes, 0, bytes.length);
+        }
+    }
+}
diff --git 
a/drools-test-coverage/test-compiler-integration/src/test/java/org/drools/mvel/integrationtests/EnumTest.java
 
b/drools-test-coverage/test-compiler-integration/src/test/java/org/drools/mvel/integrationtests/EnumTest.java
index 8b1ff660118..a4f573ffbd9 100644
--- 
a/drools-test-coverage/test-compiler-integration/src/test/java/org/drools/mvel/integrationtests/EnumTest.java
+++ 
b/drools-test-coverage/test-compiler-integration/src/test/java/org/drools/mvel/integrationtests/EnumTest.java
@@ -18,8 +18,11 @@
  */
 package org.drools.mvel.integrationtests;
 
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
 import java.util.stream.Stream;
 
 import org.drools.kiesession.rulebase.InternalKnowledgeBase;
@@ -30,6 +33,7 @@ import org.drools.testcoverage.common.util.KieUtil;
 import org.drools.testcoverage.common.util.TestParametersUtil2;
 import org.junit.jupiter.api.Assumptions;
 import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
 import org.junit.jupiter.params.provider.MethodSource;
 import org.kie.api.KieBase;
 import org.kie.api.builder.KieBuilder;
@@ -82,6 +86,40 @@ public class EnumTest {
         ksession.dispose();
     }
 
+    public static Stream<Arguments> parametersWithLiteralCounts() {
+        return parameters().flatMap(config -> Stream.of(Arguments.of(config, 
128), Arguments.of(config, 300)));
+    }
+
+    @ParameterizedTest(name = "KieBase type={0}, literals={1}")
+    @MethodSource("parametersWithLiteralCounts")
+    public void 
testEnumWithMoreLiteralsThanFitInASignedByte(KieBaseTestConfiguration 
kieBaseTestConfiguration, int literalCount) {
+        final String literals = IntStream.range(0, literalCount).mapToObj(i -> 
"V" + i).collect(Collectors.joining(", "));
+        final String lastLiteral = "V" + (literalCount - 1);
+        final String drl =
+                "package org.test;\n" +
+                "global java.util.List list;\n" +
+                "declare enum Big\n" +
+                "    " + literals + ";\n" +
+                "end\n" +
+                "rule R when\n" +
+                "then\n" +
+                "    java.util.List<Big> all = java.util.Arrays.asList( 
Big.values() );\n" +
+                "    list.add( all.size() );\n" +
+                "    list.add( Big." + lastLiteral + ".ordinal() );\n" +
+                "    list.add( all.get( " + (literalCount - 1) + " ).name() 
);\n" +
+                "end\n";
+
+        KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", 
kieBaseTestConfiguration, drl);
+        KieSession ksession = kbase.newKieSession();
+        final List<Object> list = new ArrayList<>();
+        ksession.setGlobal("list", list);
+
+        ksession.fireAllRules();
+
+        assertThat(list).containsExactly(literalCount, literalCount - 1, 
lastLiteral);
+        ksession.dispose();
+    }
+
     @ParameterizedTest(name = "KieBase type={0}")
     @MethodSource("parameters")
     public void testIndexedEnumValuesInConsequence(KieBaseTestConfiguration 
kieBaseTestConfiguration) {


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to