Copilot commented on code in PR #2911:
URL: https://github.com/apache/groovy/pull/2911#discussion_r3968795274


##########
src/main/java/org/codehaus/groovy/vmplugin/v8/Java8.java:
##########
@@ -257,7 +276,7 @@ private GenericsType[] configureTypeParameters(final 
TypeVariable<?>[] tp, final
             ClassNode[] bounds = configureTypes(tp[i].getBounds());
             gt[i] = configureTypeVariableDefinition(t, bounds);
             gt[i].setGenericDeclaration(declaration);
-            for (Annotation annotation : tp[i].getAnnotations()) {
+            for (Annotation annotation : TYPE_ANNOTATIONS ? 
tp[i].getAnnotations() : NO_ANNOTATIONS) {

Review Comment:
   This introduces a behavioral change: when `TYPE_ANNOTATIONS` is false, it 
will skip processing `TypeVariable#getAnnotations()`. Those annotations are not 
obtained via `AnnotatedType` / `getAnnotated*` accessors, so they may still be 
available even on runtimes where you need to skip `AnnotatedType` usage. If the 
intent is only to avoid missing `getAnnotated*` APIs, consider always iterating 
`tp[i].getAnnotations()` here (or add a targeted guard only if there is a known 
runtime where `TypeVariable#getAnnotations()` itself is broken).



##########
src/main/java/org/codehaus/groovy/vmplugin/v8/Java8.java:
##########
@@ -248,6 +248,25 @@ private GenericsType[] configureTypeArguments(final Type[] 
ta) {
         return gts;
     }
 
+    /**
+     * Whether the reflection API exposes type-use annotations ({@code 
AnnotatedType},
+     * Java 8). Runtimes built on the JDK class library without it (Android's 
ART)
+     * lack the {@code getAnnotated*} accessors; type annotations on 
precompiled
+     * classes are then not seen, which is the only possible outcome there
+     * (GROOVY-12389).
+     */
+    private static final boolean TYPE_ANNOTATIONS = typeAnnotationsAvailable();
+    private static final Annotation[] NO_ANNOTATIONS = new Annotation[0];
+
+    static boolean typeAnnotationsAvailable() {
+        try {
+            Class.class.getMethod("getAnnotatedSuperclass");
+            return true;
+        } catch (Throwable t) {

Review Comment:
   Catching `Throwable` is overly broad and will also swallow serious problems 
(e.g., `OutOfMemoryError`, `ThreadDeath`). This check should catch the narrow 
set of failures expected from reflective lookup (e.g., `NoSuchMethodException`, 
`SecurityException`, and possibly `LinkageError` if you want to cover 
incomplete class library implementations) and let other fatal errors propagate.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to