From bbc3fb9834597547df4d96525787380577e8faff Mon Sep 17 00:00:00 2001 From: kmb Date: Fri, 6 Apr 2018 15:25:09 -0700 Subject: fix deps checker tool handling of primitive arrays in annotations PiperOrigin-RevId: 191948995 --- .../build/importdeps/DepsCheckerClassVisitor.java | 10 ++++++--- .../build/importdeps/AbstractClassCacheTest.java | 3 ++- .../devtools/build/importdeps/testdata/Client.java | 15 ++++++++----- .../importdeps/testdata/LibraryAnnotations.java | 26 +++++++++++++++++----- 4 files changed, 40 insertions(+), 14 deletions(-) diff --git a/src/java_tools/import_deps_checker/java/com/google/devtools/build/importdeps/DepsCheckerClassVisitor.java b/src/java_tools/import_deps_checker/java/com/google/devtools/build/importdeps/DepsCheckerClassVisitor.java index de5bdac7c0..64d2c7ff85 100644 --- a/src/java_tools/import_deps_checker/java/com/google/devtools/build/importdeps/DepsCheckerClassVisitor.java +++ b/src/java_tools/import_deps_checker/java/com/google/devtools/build/importdeps/DepsCheckerClassVisitor.java @@ -223,11 +223,15 @@ public class DepsCheckerClassVisitor extends ClassVisitor { checkType(((Type) value)); // Class literals. return; } - if (PRIMITIVE_TYPES.contains(value.getClass())) { - checkType(Type.getType(value.getClass())); + Class clazz = value.getClass(); + if (PRIMITIVE_TYPES.contains(clazz)) { + checkType(Type.getType(clazz)); return; } - throw new UnsupportedOperationException("Unhandled value " + value); + if (clazz.isArray() && clazz.getComponentType().isPrimitive()) { + return; // nothing to check for primitive arrays + } + throw new UnsupportedOperationException("Unhandled value " + value + " of type " + clazz); } @Override diff --git a/src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/AbstractClassCacheTest.java b/src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/AbstractClassCacheTest.java index 33460f6614..3d2e171262 100644 --- a/src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/AbstractClassCacheTest.java +++ b/src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/AbstractClassCacheTest.java @@ -64,7 +64,8 @@ public abstract class AbstractClassCacheTest { "ConstructorAnnotation", "ParameterAnnotation", "TypeAnnotation", - "AnnotationAnnotation") + "AnnotationAnnotation", + "AnnotationFlag") .map(name -> "LibraryAnnotations$" + name) .collect(ImmutableList.toImmutableList())) .build() diff --git a/src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/testdata/Client.java b/src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/testdata/Client.java index 307f2d8395..392096e5ec 100644 --- a/src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/testdata/Client.java +++ b/src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/testdata/Client.java @@ -14,6 +14,7 @@ package com.google.devtools.build.importdeps.testdata; import com.google.devtools.build.importdeps.testdata.LibraryAnnotations.AnnotationAnnotation; +import com.google.devtools.build.importdeps.testdata.LibraryAnnotations.AnnotationFlag; import com.google.devtools.build.importdeps.testdata.LibraryAnnotations.ClassAnnotation; import com.google.devtools.build.importdeps.testdata.LibraryAnnotations.ConstructorAnnotation; import com.google.devtools.build.importdeps.testdata.LibraryAnnotations.FieldAnnotation; @@ -27,7 +28,10 @@ import java.lang.annotation.Target; import java.util.Objects; /** Client class that uses several libraries. */ -@ClassAnnotation +@ClassAnnotation( + friends = { Library.class, LibraryException.class }, + nested = @SuppressWarnings({"unused", "unchecked"}) +) public class Client<@TypeAnnotation T> extends Library implements LibraryInterface { @SuppressWarnings("unused") @@ -35,7 +39,7 @@ public class Client<@TypeAnnotation T> extends Library implements LibraryInterfa private Library.Class1 field; @SuppressWarnings("unused") - @FieldAnnotation + @FieldAnnotation({ 1, 2, 3 }) private LibraryAnnotations annotations; public static final Class1 I = Class1.I; @@ -43,8 +47,9 @@ public class Client<@TypeAnnotation T> extends Library implements LibraryInterfa @ConstructorAnnotation public Client() {} - @MethodAnnotation - public void method(@ParameterAnnotation int p, Library.Class2 p2) throws LibraryException { + @MethodAnnotation(name = "method") + public void method(@ParameterAnnotation(position = 0) int p, Library.Class2 p2) + throws LibraryException { Objects.nonNull(p2); // javac9 silently uses Objects. Class3 c3 = new Class3(); Class4 c4 = c3.field; @@ -71,7 +76,7 @@ public class Client<@TypeAnnotation T> extends Library implements LibraryInterfa /** An inner annotation. */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) - @AnnotationAnnotation + @AnnotationAnnotation(AnnotationFlag.Y) public @interface NestedAnnotation {} public enum EnumTest { diff --git a/src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/testdata/LibraryAnnotations.java b/src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/testdata/LibraryAnnotations.java index d0b883ae57..b5c41addbc 100644 --- a/src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/testdata/LibraryAnnotations.java +++ b/src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/testdata/LibraryAnnotations.java @@ -23,17 +23,24 @@ public class LibraryAnnotations { /** A library annotation for testing. */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) - public @interface ClassAnnotation {} + public @interface ClassAnnotation { + Class[] friends() default {}; + SuppressWarnings nested() default @SuppressWarnings("raw"); + } /** A library annotation for testing. */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) - public @interface MethodAnnotation {} + public @interface MethodAnnotation { + String name(); + } /** A library annotation for testing. */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) - public @interface FieldAnnotation {} + public @interface FieldAnnotation { + int[] value() default {}; + } /** A library annotation for testing. */ @Retention(RetentionPolicy.RUNTIME) @@ -43,7 +50,9 @@ public class LibraryAnnotations { /** A library annotation for testing. */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.PARAMETER) - public @interface ParameterAnnotation {} + public @interface ParameterAnnotation { + byte position() default -1; + } /** A library annotation for testing. */ @Retention(RetentionPolicy.RUNTIME) @@ -53,5 +62,12 @@ public class LibraryAnnotations { /** A library annotation for testing. */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) - public @interface AnnotationAnnotation {} + public @interface AnnotationAnnotation { + AnnotationFlag value(); + } + + /** An enum used in annotations for testing */ + public enum AnnotationFlag { + N, Y + } } -- cgit v1.2.3