[MAGNOLIA-8528] ByteBuddyI18nizer fails on generic types with extends Created: 25/Aug/22  Updated: 29/Mar/23

Status: Open
Project: Magnolia
Component/s: None
Affects Version/s: 6.2.22
Fix Version/s: None

Type: Bug Priority: Minor
Reporter: Roman Kovařík Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
dependency
is depended upon by MGNLUI-7451 SwitchableField that allows EnumDatas... Open
relation
Template:
Acceptance criteria:
Empty
Task DoD:
[ ]* Doc/release notes changes? Comment present?
[ ]* Downstream builds green?
[ ]* Solution information and context easily available?
[ ]* Tests
[ ]* FixVersion filled and not yet released
[ ]  Architecture Decision Record (ADR)
Bug DoR:
[ ]* Steps to reproduce, expected, and actual results filled
[ ]* Affected version filled
Date of First Response:
Epic Link: FieldsMaintenance
Team: DeveloperX

 Description   

Steps to reproduce

  1. Define a class public class MyTextField<E extends Enum<E>> extends TextFieldDefinition
  2. Use this class in a dialog
  3. Open the dialog

Expected results

The dialog is opened.

Actual results

Caused by: java.lang.IllegalArgumentException: Cannot resolve E from class...

Workaround

Remove the extends:

- public class MyTextField<E extends Enum<E>> extends TextFieldDefinition

+ public class MyTextField<E> extends TextFieldDefinition

Development notes

I believe these extends can be discarded like this:

@@ -149,10 +149,14 @@ public class ByteBuddyI18nizer implements I18nizer {
-            Type[] stubParameterTypes = Arrays.stream(typeParameters).map(param -> {
-                Type[] bounds = param.getBounds();
-                return bounds.length == 0 ? Object.class : bounds[0];
-            }).toArray(Type[]::new);
+            Type[] stubParameterTypes = Arrays.stream(typeParameters).map(param ->
+                            Arrays.stream(param.getBounds())
+                                    .filter(bound -> !(bound instanceof ParameterizedType) ||
+                                            Arrays.stream(((ParameterizedType) bound).getActualTypeArguments())
+                                                    .noneMatch(TypeVariable.class::isInstance))
+                                    .findFirst()
+                                    .orElse(Object.class))
+                    .toArray(Type[]::new);


 Comments   
Comment by Tobias Szczepanski [ 26/Aug/22 ]

Hi everyone,

great, thanks a bunch for this bug issue. The motivation behind this is for us to enable a SwitchableFieldDefinition based on Enums instead of Options. Therefore it would be awesome, if you could also consider to refactor the generic type of info.magnolia.ui.datasource.enumeration.EnumDatasourceDefinition to extend Enum<T>.

Thank you! Best
Tobias

Comment by Roman Kovařík [ 29/Aug/22 ]

Hey tszczepanski,

the is also one liner as a workaround to make this work, see https://git.magnolia-cms.com/users/rkovarik/repos/ui/commits/b19f56f3cd2a02a8372bcdbd70c6a848eda82f40#magnolia-ui-framework/src/main/java/info/magnolia/ui/editor/SwitchableFormView.java  which should work thanks to the erasure of generics at runtime.

(besides implementing a converter for EnumSelectFieldSupport)

Hope that helps 

Roman

Generated at Mon Feb 12 04:33:31 CET 2024 using Jira 9.4.2#940002-sha1:46d1a51de284217efdcb32434eab47a99af2938b.