[ 
https://issues.apache.org/jira/browse/GROOVY-12793?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18148251#comment-18148251
 ] 

ASF GitHub Bot commented on GROOVY-12793:
-----------------------------------------

codeconsole opened a new pull request, #3316:
URL: https://github.com/apache/groovy/pull/3316

   https://issues.apache.org/jira/browse/GROOVY-12793
   
   A statically compiled call to a method declared with an array of a bounded 
type variable was written with `Object[]` in its descriptor and failed at run 
time:
   
   ```groovy
   @groovy.transform.CompileStatic
   void test() {
       Class<?> type = String
       Deprecated[] annotations = type.getAnnotationsByType(Deprecated)
   }
   ```
   
   ```
   java.lang.NoSuchMethodError: 'java.lang.Object[] 
java.lang.Class.getAnnotationsByType(java.lang.Class)'
   ```
   
   The JVM descriptor of an array is built from its innermost component, which 
for `A[]` is the type-variable placeholder. Only the array node was redirected 
to the erasure, so the placeholder kept `Object`:
   
   - `Java8#makeClassNode`, used for `ClassHelper`'s predefined types such as 
`java.lang.Class` and for every class when asmResolving is off, never 
redirected the component, for return and parameter types of any dimension.
   - `TypeSignatureParser#applyErasure`, used for classes read from their class 
file, did it for one dimension only, so a `T[][]` from a jar was written as 
`Object[][]`.
   
   Both now redirect every component level of an array of a type variable to 
the matching level of its erasure. An array of any other type is left alone.
   
   `Groovy12793` calls `Class#getAnnotationsByType` and 
`getDeclaredAnnotationsByType`, and methods of a Java support class that return 
and take `T[]` and `T[][]` with `T extends Number`. It runs with asmResolving 
on and off (`Groovy12793_AsmResolveOff`); 7 of its 10 tests fail without the 
fix.
   
   The bug is in 4.0.x, 5.0.x and 6.0.0. Please backport to `GROOVY_6_0_X`, 
where the change applies cleanly. `GROOVY_5_0_X` conflicts in both files and 
would need the change ported by hand.
   




> SC: a call to a method returning or taking an array of a bounded type 
> variable, such as Class#getAnnotationsByType, fails with NoSuchMethodError
> ------------------------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-12793
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12793
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static compilation
>    Affects Versions: 4.0.33, 5.0.8, 6.0.0
>            Reporter: Scott Murphy Heiberg
>            Priority: Major
>
> A statically compiled call to a JDK method declared with an array of a 
> bounded type variable is written with Object[] in the method descriptor, and 
> fails at run time:
> {code:groovy}
> @groovy.transform.CompileStatic
> void test() {
>     Class<?> type = String
>     Deprecated[] annotations = type.getAnnotationsByType(Deprecated)
> }
> test()
> {code}
> {noformat}
> java.lang.NoSuchMethodError: 'java.lang.Object[] 
> java.lang.Class.getAnnotationsByType(java.lang.Class)'
> {noformat}
> {{Class#getAnnotationsByType}} is declared {{<A extends Annotation> A[] 
> getAnnotationsByType(Class<A>)}}, whose descriptor returns 
> {{[Ljava/lang/annotation/Annotation;}}. The same happens for 
> {{getDeclaredAnnotationsByType}}, and for any method of a precompiled class 
> that returns or takes {{T[]}} or {{T[][]}} with {{T extends X}}, such as 
> {{static <T extends Number> T[] array(Class<T>)}}. Reproduced with 4.0.33, 
> 5.1.3, 6.0.0, 6.0.1-SNAPSHOT and 7.0.0-SNAPSHOT. Methods of a class compiled 
> in the same unit are fine.
> Cause: the JVM descriptor of an array is built from its innermost component 
> type ({{TypeUtil.getDescriptionByType}} walks {{getComponentType()}}). For 
> {{A[]}} that component is the type-variable placeholder. Only the array node 
> is redirected to the erasure; the placeholder keeps {{Object}} as its 
> redirect.
> * {{Java8#makeClassNode}}, the reflection path (the predefined types of 
> {{ClassHelper}} such as {{java.lang.Class}}, and every class when 
> asmResolving is off), never redirects the component, for return and parameter 
> types of any dimension.
> * {{TypeSignatureParser#applyErasure}}, the class-file path, redirects the 
> component for one dimension only, so {{T[][]}} from a jar is written as 
> {{Object[][]}}.
> Fix: redirect every component level of an array of a type variable to the 
> matching level of its erasure, in both places.
> Workaround: call the method from dynamic code ({{@CompileDynamic}}) or from 
> Java, or use {{getAnnotation}} on the annotation and its container.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to