This is an automated email from the ASF dual-hosted git repository.
tzimanyi pushed a commit to branch 8.40.x
in repository https://gitbox.apache.org/repos/asf/incubator-kie-drools.git
The following commit(s) were added to refs/heads/8.40.x by this push:
new 2a217ea158 [kie-issues#1077-backport-8.40.x] Backport of issue 1077
(#5843)
2a217ea158 is described below
commit 2a217ea15826454aeb4e182125e787162074e0f1
Author: Tibor Zimányi <[email protected]>
AuthorDate: Thu Apr 11 15:40:29 2024 +0200
[kie-issues#1077-backport-8.40.x] Backport of issue 1077 (#5843)
---
.../model/codegen/execmodel/GenericsTest.java | 69 +++++++++++++++++-----
.../src/main/java/org/drools/util/ClassUtils.java | 10 ++--
2 files changed, 60 insertions(+), 19 deletions(-)
diff --git
a/drools-model/drools-model-codegen/src/test/java/org/drools/model/codegen/execmodel/GenericsTest.java
b/drools-model/drools-model-codegen/src/test/java/org/drools/model/codegen/execmodel/GenericsTest.java
index 6eb87af4a5..93187d3e40 100644
---
a/drools-model/drools-model-codegen/src/test/java/org/drools/model/codegen/execmodel/GenericsTest.java
+++
b/drools-model/drools-model-codegen/src/test/java/org/drools/model/codegen/execmodel/GenericsTest.java
@@ -38,20 +38,20 @@ public class GenericsTest extends BaseModelTest {
// accumulate inline code supports generics
String str =
"import " + Person.class.getCanonicalName() + ";\n" +
- "import " + Address.class.getCanonicalName() + ";\n" +
- "import " + List.class.getCanonicalName() + ";\n" +
- "import " + ArrayList.class.getCanonicalName() + ";\n" +
- "global List results;\n" +
- "dialect \"mvel\"\n" +
- "rule R when\n" +
- " $l : List() from accumulate (Person($addrList :
addresses),\n" +
- " init( List<String> cityList = new ArrayList(); ),\n"
+
- " action( for(Address addr: $addrList){String city =
addr.getCity(); cityList.add(city);} ),\n" +
- " result( cityList )\n" +
- " )\n" +
- "then\n" +
- " results.add($l);\n" +
- "end";
+ "import " + Address.class.getCanonicalName() + ";\n" +
+ "import " + List.class.getCanonicalName() + ";\n" +
+ "import " + ArrayList.class.getCanonicalName() + ";\n"
+
+ "global List results;\n" +
+ "dialect \"mvel\"\n" +
+ "rule R when\n" +
+ " $l : List() from accumulate (Person($addrList :
addresses),\n" +
+ " init( List<String> cityList = new
ArrayList(); ),\n" +
+ " action( for(Address addr: $addrList){String
city = addr.getCity(); cityList.add(city);} ),\n" +
+ " result( cityList )\n" +
+ " )\n" +
+ "then\n" +
+ " results.add($l);\n" +
+ "end";
KieSession ksession = getKieSession(str);
List<List<String>> results = new ArrayList<>();
@@ -74,4 +74,45 @@ public class GenericsTest extends BaseModelTest {
ksession.fireAllRules();
assertThat(results.get(0).size()).isEqualTo(4);
}
+
+ public static class ClassWithGenericField<P extends Address> {
+
+ private P extendedAddress;
+
+ public ClassWithGenericField(final P extendedAddress) {
+ this.extendedAddress = extendedAddress;
+ }
+
+ public P getExtendedAddress() {
+ return extendedAddress;
+ }
+
+ public void setExtendedAddress(final P extendedAddress) {
+ this.extendedAddress = extendedAddress;
+ }
+ }
+
+ @Test
+ public void testClassWithGenericField() {
+ // KIE-1077
+ String str =
+ "import " + ClassWithGenericField.class.getCanonicalName() +
";\n " +
+ "import " + List.class.getCanonicalName() + ";\n " +
+ "global List results;\n " +
+ "rule R when\n " +
+ " ClassWithGenericField($addressStreet:
extendedAddress.street) \n " +
+ "then\n " +
+ " results.add($addressStreet);\n " +
+ "end";
+
+ KieSession ksession = getKieSession(str);
+ List<String> results = new ArrayList<>();
+ ksession.setGlobal("results", results);
+
+ final Address address = new Address("someStreet", 1, "Levice");
+ final ClassWithGenericField<Address> classWithGenericField = new
ClassWithGenericField<>(address);
+
+ ksession.insert(classWithGenericField);
+ assertThat(ksession.fireAllRules()).isEqualTo(1);
+ }
}
\ No newline at end of file
diff --git a/drools-util/src/main/java/org/drools/util/ClassUtils.java
b/drools-util/src/main/java/org/drools/util/ClassUtils.java
index 30bd1edc75..90677f2d9f 100644
--- a/drools-util/src/main/java/org/drools/util/ClassUtils.java
+++ b/drools-util/src/main/java/org/drools/util/ClassUtils.java
@@ -269,8 +269,7 @@ public final class ClassUtils {
/**
* Populates the import style pattern map from give comma delimited string
*/
- public static void addImportStylePatterns(Map<String, Object> patterns,
- String str) {
+ public static void addImportStylePatterns(Map<String, Object> patterns,
String str) {
if ( str == null || "".equals( str.trim() ) ) {
return;
}
@@ -595,11 +594,12 @@ public final class ClassUtils {
if (type instanceof Class<?>) {
return ( Class ) type;
}
- if (type instanceof ParameterizedType ) {
+ if (type instanceof ParameterizedType) {
return toRawClass( (( ParameterizedType ) type).getRawType() );
}
- if (type instanceof TypeVariable ) {
- return Object.class;
+ if (type instanceof TypeVariable) {
+ final TypeVariable typeVar = (TypeVariable) type;
+ return typeVar.getBounds().length == 1 ?
toRawClass(typeVar.getBounds()[0]) : Object.class;
}
throw new UnsupportedOperationException( "Unknown type " + type );
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]