Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Duplicate
-
2.5.16, 3.0.10, 4.0.1
-
None
-
None
Description
Based on the discussion in GROOVY-10567 I tried creating a extension method by explicitly annotating the other parameters with @NamedParam. However, the method was not picked up as an extension method.
As an example:
import groovy.transform.* @NamedVariant static javaBlockingTest(File self, @NamedParam String packageName = 'com.example', @NamedParam String className = "BlockingTest") { }
will generate as
@groovy.transform.Generated public static java.lang.Object javaBlockingTest(@groovy.transform.NamedParams(value = [@groovy.transform.NamedParam(value = 'packageName', type = java.lang.String), @groovy.transform.NamedParam(value = 'className', type = java.lang.String)]) java.util.Map namedArgs, java.io.File self) { if ( namedArgs == null) { throw new java.lang.IllegalArgumentException('Named parameter map cannot be null') } for (java.lang.String namedArgKey : namedArgs.keySet()) { assert ['self', 'packageName', 'className'].contains( namedArgKey ) : 'Unrecognized namedArgKey: ' + namedArgKey } return this.javaBlockingTest(self, namedArgs.containsKey('packageName') ? namedArgs.packageName : 'com.example', namedArgs.containsKey('className') ? namedArgs.className : 'BlockingTest') }
Important to note is that the namedArgs parameter was moved before the self parameter, basically shifting the extended type from File to Map.
If we handcraft
public static java.lang.Object javaBlockingTest(java.io.File self, @groovy.transform.NamedParams(value = [@groovy.transform.NamedParam(value = 'packageName', type = java.lang.String), @groovy.transform.NamedParam(value = 'className', type = java.lang.String)]) java.util.Map namedArgs) { }
it works as expected.
IMHO we would need a flag @NamedVariant(extensionMethod = true), which changes the generated code by preserving the first parameter.
Attachments
Attachments
Issue Links
- duplicates
-
GROOVY-8879 @NamedVariant on extension method
- Open