Details
-
Type:
Bug
-
Status:
Closed
-
Priority:
Minor
-
Resolution: Fixed
-
Affects Version/s: 1.5.6
-
Fix Version/s: 1.6-rc-1, 1.5.8, 1.7-beta-1
-
Component/s: None
-
Labels:None
-
Number of attachments :
Description
This one
enum MyEnum { ONE ([1,2]), TWO ([3,4]); private final List myList MyEnum (List myList) { this.myList=myList } } MyEnum.ONE
leads to "groovy.lang.GroovyRuntimeException: Could not find matching constructor for: MyEnum(java.lang.String, java.lang.Integer, java.lang.Integer, java.lang.Integer)"
Adding "as List" to the List declaration works:
enum MyEnum { ONE ([1,2] as List), TWO ([3,4] as List); private final List myList MyEnum (List myList) { this.myList=myList } } MyEnum.ONE
Why isn't "[x,y,z]" interpreted as a List in construction of enum instances?
Bug or a feature? ![]()
-----------
blackdrag: "looks like a bug for me.. please fill a report in JIRA"
Just adding some details related to the issue that I looked into.
When an enum constructor is invoked with a list as "TWO([1, 2])" (enum definition below), the AST
that is being generated seems to be wrong (The problem might be in the CST itself from which AST gets created, but I couldn't understand the CST down/right details well enough to be sure).
In the AST, the initialValueExpression that is created for the enum value field node in this case is having multiple, individual org.codehaus.groovy.ast.expr.ConstantExpression (one for each list member) instead of being one org.codehaus.groovy.ast.expr.ListExpression that wraps ConstantExpression(s) inside.
As a result, what is happening is that the instead of passing [1, 2] as one argument of type list, it is passing 2 argumements - each of type int.
The code demonstrating it is below:
When explicit cast "as List" is used, then enum's initialValueExpression correctly gets a CastExpression that wraps a ListExpression that wraps multiple ConstantExpression(s). Without the cast, ConstantExpression(s) do not get wrapped in ListExpression, which is causing the problem.