fix deps checker tool handling of primitive arrays in annotations
PiperOrigin-RevId: 191948995
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 de5bdac..64d2c7f 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 @@
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 33460f6..3d2e171 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 @@
"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 307f2d8..392096e 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.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 @@
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 @@
@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 @@
/** 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 d0b883a..b5c41ad 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 @@
/** 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 @@
/** 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 @@
/** 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
+ }
}