Scott Murphy Heiberg created GROOVY-12793:
---------------------------------------------

             Summary: 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: 5.0.8, 4.0.33, 6.0.0
            Reporter: Scott Murphy Heiberg


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