Skip to content

Commit 81be061

Browse files
chaorenGoogle Java Core Libraries
authored and
Google Java Core Libraries
committed
Fix @Require annotations so that features implied by absent features are not also required to be absent.
Fixes #7401 RELNOTES=`collect.testing.features`: Fixed `@Require` annotations so that features implied by absent features are not also required to be absent. PiperOrigin-RevId: 682731934
1 parent 5da71a7 commit 81be061

File tree

4 files changed

+494
-398
lines changed

4 files changed

+494
-398
lines changed

android/guava-testlib/src/com/google/common/collect/testing/features/FeatureUtil.java

+22-15
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@
1616

1717
package com.google.common.collect.testing.features;
1818

19+
import static com.google.common.collect.testing.Helpers.copyToSet;
20+
import static java.util.Collections.disjoint;
21+
import static java.util.Collections.unmodifiableList;
22+
1923
import com.google.common.annotations.GwtIncompatible;
20-
import com.google.common.collect.testing.Helpers;
2124
import com.google.errorprone.annotations.CanIgnoreReturnValue;
2225
import java.lang.annotation.Annotation;
2326
import java.lang.reflect.AnnotatedElement;
2427
import java.lang.reflect.Method;
2528
import java.util.ArrayDeque;
2629
import java.util.ArrayList;
27-
import java.util.Collections;
2830
import java.util.HashMap;
2931
import java.util.LinkedHashSet;
3032
import java.util.List;
@@ -39,9 +41,9 @@
3941
* @author George van den Driessche
4042
*/
4143
@GwtIncompatible
42-
public class FeatureUtil {
44+
public final class FeatureUtil {
4345
/** A cache of annotated objects (typically a Class or Method) to its set of annotations. */
44-
private static Map<AnnotatedElement, List<Annotation>> annotationCache = new HashMap<>();
46+
private static final Map<AnnotatedElement, List<Annotation>> annotationCache = new HashMap<>();
4547

4648
private static final Map<Class<?>, TesterRequirements> classTesterRequirementsCache =
4749
new HashMap<>();
@@ -181,16 +183,14 @@ private static TesterRequirements buildTesterRequirements(Annotation testerAnnot
181183
Feature<?>[] presentFeatures;
182184
Feature<?>[] absentFeatures;
183185
try {
184-
presentFeatures = (Feature[]) annotationClass.getMethod("value").invoke(testerAnnotation);
185-
absentFeatures = (Feature[]) annotationClass.getMethod("absent").invoke(testerAnnotation);
186+
presentFeatures = (Feature<?>[]) annotationClass.getMethod("value").invoke(testerAnnotation);
187+
absentFeatures = (Feature<?>[]) annotationClass.getMethod("absent").invoke(testerAnnotation);
186188
} catch (Exception e) {
187189
throw new IllegalArgumentException("Error extracting features from tester annotation.", e);
188190
}
189-
Set<Feature<?>> allPresentFeatures =
190-
addImpliedFeatures(Helpers.<Feature<?>>copyToSet(presentFeatures));
191-
Set<Feature<?>> allAbsentFeatures =
192-
addImpliedFeatures(Helpers.<Feature<?>>copyToSet(absentFeatures));
193-
if (!Collections.disjoint(allPresentFeatures, allAbsentFeatures)) {
191+
Set<Feature<?>> allPresentFeatures = addImpliedFeatures(copyToSet(presentFeatures));
192+
Set<Feature<?>> allAbsentFeatures = copyToSet(absentFeatures);
193+
if (!disjoint(allPresentFeatures, allAbsentFeatures)) {
194194
throw new ConflictingRequirementsException(
195195
"Annotation explicitly or "
196196
+ "implicitly requires one or more features to be both present "
@@ -239,7 +239,7 @@ public static Iterable<Annotation> getTesterAnnotations(AnnotatedElement classOr
239239
annotations.add(a);
240240
}
241241
}
242-
annotations = Collections.unmodifiableList(annotations);
242+
annotations = unmodifiableList(annotations);
243243
annotationCache.put(classOrMethod, annotations);
244244
}
245245
return annotations;
@@ -279,7 +279,7 @@ private static void checkConflict(
279279
Set<Feature<?>> newFeatures,
280280
Object source)
281281
throws ConflictingRequirementsException {
282-
if (!Collections.disjoint(newFeatures, earlierFeatures)) {
282+
if (!disjoint(newFeatures, earlierFeatures)) {
283283
throw new ConflictingRequirementsException(
284284
String.format(
285285
Locale.ROOT,
@@ -292,10 +292,17 @@ private static void checkConflict(
292292
}
293293
}
294294

295-
/** Construct a new {@link java.util.Set} that is the intersection of the given sets. */
295+
/**
296+
* Construct a new {@link java.util.Set} that is the intersection of the given sets.
297+
*
298+
* @deprecated Use {@link com.google.common.collect.Sets#intersection(Set, Set)} instead.
299+
*/
300+
@Deprecated
296301
public static <T> Set<T> intersection(Set<? extends T> set1, Set<? extends T> set2) {
297-
Set<T> result = Helpers.<T>copyToSet(set1);
302+
Set<T> result = copyToSet(set1);
298303
result.retainAll(set2);
299304
return result;
300305
}
306+
307+
private FeatureUtil() {}
301308
}

0 commit comments

Comments
 (0)