From 22280c5f554bcdf245e94254ce5acba55ad5cd98 Mon Sep 17 00:00:00 2001 From: Riley Park Date: Mon, 21 Jun 2021 10:08:30 -0700 Subject: [PATCH] Better checking for method return TYPE_USE annotations --- ...03-Allow-use-of-TYPE_USE-annotations.patch | 34 +++++++++++++++---- patches/api/0007-Adventure.patch | 11 +----- ...rivate-methods-for-nullability-annot.patch | 4 +-- .../0259-Better-AnnotationTest-printout.patch | 4 +-- 4 files changed, 33 insertions(+), 20 deletions(-) diff --git a/patches/api/0003-Allow-use-of-TYPE_USE-annotations.patch b/patches/api/0003-Allow-use-of-TYPE_USE-annotations.patch index 154124ad2d..0a7c04dc50 100644 --- a/patches/api/0003-Allow-use-of-TYPE_USE-annotations.patch +++ b/patches/api/0003-Allow-use-of-TYPE_USE-annotations.patch @@ -5,15 +5,28 @@ Subject: [PATCH] Allow use of TYPE_USE annotations diff --git a/src/test/java/org/bukkit/AnnotationTest.java b/src/test/java/org/bukkit/AnnotationTest.java -index 0c7377247ad9251c9e498039511e7220370aba2d..2ef6b56642f792d1a648e76e904e61bf7a662f8c 100644 +index 0c7377247ad9251c9e498039511e7220370aba2d..8d14d81c078d29781591e0ce96ea80338565e42b 100644 --- a/src/test/java/org/bukkit/AnnotationTest.java +++ b/src/test/java/org/bukkit/AnnotationTest.java -@@ -66,15 +66,26 @@ public class AnnotationTest { - continue; +@@ -67,14 +67,40 @@ public class AnnotationTest { } -- if (mustBeAnnotated(Type.getReturnType(method.desc)) && !isWellAnnotated(method.invisibleAnnotations)) { -+ if (mustBeAnnotated(Type.getReturnType(method.desc)) && !isWellAnnotated(method.invisibleAnnotations) && !isWellAnnotated(method.visibleTypeAnnotations) && !isWellAnnotated(method.invisibleTypeAnnotations)) { // Paper - also check (in)visible type annotations + if (mustBeAnnotated(Type.getReturnType(method.desc)) && !isWellAnnotated(method.invisibleAnnotations)) { ++ // Paper start ++ boolean warn = true; ++ if (isWellAnnotated(method.visibleTypeAnnotations)) { ++ warn = false; ++ } else if (method.invisibleTypeAnnotations != null) { ++ dance: for (final org.objectweb.asm.tree.TypeAnnotationNode invisibleTypeAnnotation : method.invisibleTypeAnnotations) { ++ final org.objectweb.asm.TypeReference ref = new org.objectweb.asm.TypeReference(invisibleTypeAnnotation.typeRef); ++ if (ref.getSort() == org.objectweb.asm.TypeReference.METHOD_RETURN && java.util.Arrays.binarySearch(ACCEPTED_ANNOTATIONS, invisibleTypeAnnotation.desc) >= 0) { ++ warn = false; ++ break dance; // cha cha real smooth ++ } ++ } ++ } ++ if (warn) ++ // Paper end warn(errors, clazz, method, "return value"); } @@ -23,7 +36,7 @@ index 0c7377247ad9251c9e498039511e7220370aba2d..2ef6b56642f792d1a648e76e904e61bf + dancing: // Paper for (int i = 0; i < paramTypes.length; i++) { if (mustBeAnnotated(paramTypes[i]) && !isWellAnnotated(method.invisibleParameterAnnotations == null ? null : method.invisibleParameterAnnotations[i])) { -+ // Paper start - wheeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee ++ // Paper start + if (method.invisibleTypeAnnotations != null) { + for (final org.objectweb.asm.tree.TypeAnnotationNode invisibleTypeAnnotation : method.invisibleTypeAnnotations) { + final org.objectweb.asm.TypeReference ref = new org.objectweb.asm.TypeReference(invisibleTypeAnnotation.typeRef); @@ -36,3 +49,12 @@ index 0c7377247ad9251c9e498039511e7220370aba2d..2ef6b56642f792d1a648e76e904e61bf ParameterNode paramNode = parameters == null ? null : parameters.get(i); String paramName = paramNode == null ? null : paramNode.name; +@@ -174,7 +200,7 @@ public class AnnotationTest { + return true; + } + +- private static boolean isWellAnnotated(@Nullable List annotations) { ++ private static boolean isWellAnnotated(@Nullable List annotations) { // Paper + if (annotations == null) { + return false; + } diff --git a/patches/api/0007-Adventure.patch b/patches/api/0007-Adventure.patch index 7fcfad68c6..395cf6ddf0 100644 --- a/patches/api/0007-Adventure.patch +++ b/patches/api/0007-Adventure.patch @@ -3706,7 +3706,7 @@ index da01d2926cc8a2485a3349ac1ebb32cad20e287c..f0af10a5b9ad048be197ed5ec6c8ed26 /** diff --git a/src/test/java/org/bukkit/AnnotationTest.java b/src/test/java/org/bukkit/AnnotationTest.java -index 4aee3dd321453009fda10db9c4fbea0ffed6ed69..002677ac589aa88b643c52a883a72a2bdc3696dd 100644 +index 8d14d81c078d29781591e0ce96ea80338565e42b..f82f085ab9069abce10c2339446ef0d0bec8888c 100644 --- a/src/test/java/org/bukkit/AnnotationTest.java +++ b/src/test/java/org/bukkit/AnnotationTest.java @@ -26,6 +26,12 @@ import org.objectweb.asm.tree.ParameterNode; @@ -3722,12 +3722,3 @@ index 4aee3dd321453009fda10db9c4fbea0ffed6ed69..002677ac589aa88b643c52a883a72a2b "Lorg/jetbrains/annotations/Nullable;", "Lorg/jetbrains/annotations/NotNull;", "Lorg/jetbrains/annotations/Contract;", -@@ -185,7 +191,7 @@ public class AnnotationTest { - return true; - } - -- private static boolean isWellAnnotated(@Nullable List annotations) { -+ private static boolean isWellAnnotated(@Nullable List annotations) { // Paper - allow children of AnnotationNode - if (annotations == null) { - return false; - } diff --git a/patches/api/0177-Ignore-package-private-methods-for-nullability-annot.patch b/patches/api/0177-Ignore-package-private-methods-for-nullability-annot.patch index 9649b22683..2b621fed5e 100644 --- a/patches/api/0177-Ignore-package-private-methods-for-nullability-annot.patch +++ b/patches/api/0177-Ignore-package-private-methods-for-nullability-annot.patch @@ -6,10 +6,10 @@ Subject: [PATCH] Ignore package-private methods for nullability annotations This isn't API diff --git a/src/test/java/org/bukkit/AnnotationTest.java b/src/test/java/org/bukkit/AnnotationTest.java -index dfd4174c8dca51600c2203e3f4c933507fb827e7..1c6a41644257cd35ced235af6cc60efc925ebc18 100644 +index 15ae70ae69b27712d68270866d7940ff618c9fbe..911654d36f6ac708eb38b6df1f7f535a15c05fb8 100644 --- a/src/test/java/org/bukkit/AnnotationTest.java +++ b/src/test/java/org/bukkit/AnnotationTest.java -@@ -179,7 +179,7 @@ public class AnnotationTest { +@@ -194,7 +194,7 @@ public class AnnotationTest { private static boolean isMethodIncluded(@NotNull ClassNode clazz, @NotNull MethodNode method, @NotNull Map allClasses) { // Exclude private, synthetic and deprecated methods diff --git a/patches/api/0259-Better-AnnotationTest-printout.patch b/patches/api/0259-Better-AnnotationTest-printout.patch index a5a0384234..d8884cfe79 100644 --- a/patches/api/0259-Better-AnnotationTest-printout.patch +++ b/patches/api/0259-Better-AnnotationTest-printout.patch @@ -5,10 +5,10 @@ Subject: [PATCH] Better AnnotationTest printout diff --git a/src/test/java/org/bukkit/AnnotationTest.java b/src/test/java/org/bukkit/AnnotationTest.java -index 1c6a41644257cd35ced235af6cc60efc925ebc18..b94d87832a271a76a5c8c0c9bf403c35348b31ed 100644 +index 911654d36f6ac708eb38b6df1f7f535a15c05fb8..32af614abd761e39a412422abe2fcb5272a6374c 100644 --- a/src/test/java/org/bukkit/AnnotationTest.java +++ b/src/test/java/org/bukkit/AnnotationTest.java -@@ -118,13 +118,18 @@ public class AnnotationTest { +@@ -133,13 +133,18 @@ public class AnnotationTest { Collections.sort(errors);