Skip to content

Commit

Permalink
Ensure class values are tested in the Everything annotation.
Browse files Browse the repository at this point in the history
This is a test-only change that corrects an omission in test coverage. Class values in annotations do in fact work, but this particular test was not demonstrating that.

RELNOTES=n/a
PiperOrigin-RevId: 527072293
  • Loading branch information
eamonnmcmanus authored and Google Java Core Libraries committed Apr 25, 2023
1 parent 3c967d7 commit a68931c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public static Visibility effectiveVisibilityOfElement(Element element) {
Visibility effectiveVisibility = PUBLIC;
Element currentElement = element;
while (currentElement != null) {
// NOTE: We don't use Guava's Comparators.min() because that requires Guava 30, which would
// NOTE - because that requires Guava 30, which would
// make this library unusable in annotation processors using Bazel < 5.0.
effectiveVisibility = Ordering.natural().min(effectiveVisibility, ofElement(currentElement));
currentElement = currentElement.getEnclosingElement();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ public void testEmpty() {

StringValues anAnnotation();

Class<? extends CharSequence> aClass();

byte[] bytes();

short[] shorts();
Expand All @@ -199,6 +201,8 @@ public void testEmpty() {
RetentionPolicy[] enums();

StringValues[] annotations();

Class<? extends CharSequence>[] classes();
}

@AutoAnnotation
Expand All @@ -214,6 +218,7 @@ static Everything newEverything(
String aString,
RetentionPolicy anEnum,
StringValues anAnnotation,
Class<? extends CharSequence> aClass,
byte[] bytes,
short[] shorts,
int[] ints,
Expand All @@ -224,7 +229,8 @@ static Everything newEverything(
boolean[] booleans,
String[] strings,
RetentionPolicy[] enums,
StringValues[] annotations) {
StringValues[] annotations,
Class<? extends CharSequence>... classes) {
return new AutoAnnotation_AutoAnnotationTest_newEverything(
aByte,
aShort,
Expand All @@ -237,6 +243,7 @@ static Everything newEverything(
aString,
anEnum,
anAnnotation,
aClass,
bytes,
shorts,
ints,
Expand All @@ -247,7 +254,8 @@ static Everything newEverything(
booleans,
strings,
enums,
annotations);
annotations,
classes);
}

@AutoAnnotation
Expand All @@ -263,6 +271,7 @@ static Everything newEverythingCollections(
String aString,
RetentionPolicy anEnum,
StringValues anAnnotation,
Class<? extends CharSequence> aClass,
Collection<Byte> bytes,
List<Short> shorts,
ArrayList<Integer> ints,
Expand All @@ -273,7 +282,8 @@ static Everything newEverythingCollections(
ImmutableCollection<Boolean> booleans,
ImmutableList<String> strings,
ImmutableSet<RetentionPolicy> enums,
Set<StringValues> annotations) {
Set<StringValues> annotations,
List<Class<? extends CharSequence>> classes) {
return new AutoAnnotation_AutoAnnotationTest_newEverythingCollections(
aByte,
aShort,
Expand All @@ -286,6 +296,7 @@ static Everything newEverythingCollections(
aString,
anEnum,
anAnnotation,
aClass,
bytes,
shorts,
ints,
Expand All @@ -296,7 +307,8 @@ static Everything newEverythingCollections(
booleans,
strings,
enums,
annotations);
annotations,
classes);
}

@Everything(
Expand All @@ -311,6 +323,7 @@ static Everything newEverythingCollections(
aString = "maybe\nmaybe not\n",
anEnum = RetentionPolicy.RUNTIME,
anAnnotation = @StringValues("whatever"),
aClass = String.class,
bytes = {5, 6},
shorts = {},
ints = {7},
Expand All @@ -321,7 +334,8 @@ static Everything newEverythingCollections(
booleans = {false, true, false},
strings = {"ver", "vers", "vert", "verre", "vair"},
enums = {RetentionPolicy.CLASS, RetentionPolicy.RUNTIME},
annotations = {@StringValues({}), @StringValues({"foo", "bar"})})
annotations = {@StringValues({}), @StringValues({"foo", "bar"})},
classes = {String.class, StringBuilder.class})
private static class AnnotatedWithEverything {}

// Get an instance of @Everything via reflection on the class AnnotatedWithEverything,
Expand All @@ -342,6 +356,7 @@ private static class AnnotatedWithEverything {}
"maybe\nmaybe not\n",
RetentionPolicy.RUNTIME,
newStringValues(new String[] {"whatever"}),
String.class,
new byte[] {5, 6},
new short[] {},
new int[] {7},
Expand All @@ -354,7 +369,9 @@ private static class AnnotatedWithEverything {}
new RetentionPolicy[] {RetentionPolicy.CLASS, RetentionPolicy.RUNTIME},
new StringValues[] {
newStringValues(new String[] {}), newStringValues(new String[] {"foo", "bar"}),
});
},
String.class,
StringBuilder.class);
private static final Everything EVERYTHING_FROM_AUTO_COLLECTIONS =
newEverythingCollections(
(byte) 1,
Expand All @@ -368,6 +385,7 @@ private static class AnnotatedWithEverything {}
"maybe\nmaybe not\n",
RetentionPolicy.RUNTIME,
newStringValues(new String[] {"whatever"}),
String.class,
Arrays.asList((byte) 5, (byte) 6),
Collections.<Short>emptyList(),
new ArrayList<Integer>(Collections.singleton(7)),
Expand All @@ -380,7 +398,10 @@ private static class AnnotatedWithEverything {}
ImmutableList.of("ver", "vers", "vert", "verre", "vair"),
ImmutableSet.of(RetentionPolicy.CLASS, RetentionPolicy.RUNTIME),
ImmutableSet.of(
newStringValues(new String[] {}), newStringValues(new String[] {"foo", "bar"})));
newStringValues(new String[] {}), newStringValues(new String[] {"foo", "bar"})),
ImmutableList.of(String.class.asSubclass(CharSequence.class), StringBuilder.class));
// .asSubclass because of pre-Java8, where otherwise we get a compilation error because
// the inferred type is <Class<? extends CharSequence & Serializable>>.
private static final Everything EVERYTHING_ELSE_FROM_AUTO =
newEverything(
(byte) 0,
Expand All @@ -394,6 +415,7 @@ private static class AnnotatedWithEverything {}
"",
RetentionPolicy.SOURCE,
newStringValues(new String[] {""}),
String.class,
new byte[0],
new short[0],
new int[0],
Expand All @@ -418,6 +440,7 @@ private static class AnnotatedWithEverything {}
"",
RetentionPolicy.SOURCE,
newStringValues(new String[] {""}),
String.class,
ImmutableList.<Byte>of(),
Collections.<Short>emptyList(),
new ArrayList<Integer>(),
Expand All @@ -428,7 +451,8 @@ private static class AnnotatedWithEverything {}
ImmutableSet.<Boolean>of(),
ImmutableList.<String>of(),
ImmutableSet.<RetentionPolicy>of(),
Collections.<StringValues>emptySet());
Collections.<StringValues>emptySet(),
Collections.<Class<? extends CharSequence>>emptyList());

@Test
public void testEqualsAndHashCode() {
Expand Down Expand Up @@ -515,6 +539,7 @@ public void testToString() {
+ "aByte=1, aShort=2, anInt=3, aLong=-4, aFloat=NaN, aDouble=NaN, aChar='#', "
+ "aBoolean=true, aString=\"maybe\\nmaybe not\\n\", anEnum=RUNTIME, "
+ "anAnnotation=@com.google.auto.value.annotations.StringValues([\"whatever\"]), "
+ "aClass=class java.lang.String, "
+ "bytes=[5, 6], shorts=[], ints=[7], longs=[8, 9], floats=[10.0, 11.0], "
+ "doubles=[-Infinity, -12.0, Infinity], "
+ "chars=['?', '!', '\\n'], "
Expand All @@ -524,7 +549,8 @@ public void testToString() {
+ "annotations=["
+ "@com.google.auto.value.annotations.StringValues([]), "
+ "@com.google.auto.value.annotations.StringValues([\"foo\", \"bar\"])"
+ "]"
+ "], "
+ "classes=[class java.lang.String, class java.lang.StringBuilder]"
+ ")";
assertThat(EVERYTHING_FROM_AUTO.toString()).isEqualTo(expected);
assertThat(EVERYTHING_FROM_AUTO_COLLECTIONS.toString()).isEqualTo(expected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public TypeMirror visitArray(ArrayType t, Void p) {
// ImmutableSet<? extends T> target = ImmutableSet.copyOf(actualParameter);
// We will infer E=<? extends T> and rewrite the formal parameter type to
// <? extends T>[], which we must simplify to T[].
// TODO: what if getExtendsBound() returns null?
// TODO - returns null?
comp = MoreTypes.asWildcard(comp).getExtendsBound();
}
return typeUtils.getArrayType(comp);
Expand Down

0 comments on commit a68931c

Please sign in to comment.