Skip to content

Commit

Permalink
Fix @Require annotations so that features implied by absent feature…
Browse files Browse the repository at this point in the history
…s 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
  • Loading branch information
chaoren authored and Google Java Core Libraries committed Oct 5, 2024
1 parent 5da71a7 commit 81be061
Show file tree
Hide file tree
Showing 4 changed files with 494 additions and 398 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@

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

import static com.google.common.collect.testing.Helpers.copyToSet;
import static java.util.Collections.disjoint;
import static java.util.Collections.unmodifiableList;

import com.google.common.annotations.GwtIncompatible;
import com.google.common.collect.testing.Helpers;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
Expand All @@ -39,9 +41,9 @@
* @author George van den Driessche
*/
@GwtIncompatible
public class FeatureUtil {
public final class FeatureUtil {
/** A cache of annotated objects (typically a Class or Method) to its set of annotations. */
private static Map<AnnotatedElement, List<Annotation>> annotationCache = new HashMap<>();
private static final Map<AnnotatedElement, List<Annotation>> annotationCache = new HashMap<>();

private static final Map<Class<?>, TesterRequirements> classTesterRequirementsCache =
new HashMap<>();
Expand Down Expand Up @@ -181,16 +183,14 @@ private static TesterRequirements buildTesterRequirements(Annotation testerAnnot
Feature<?>[] presentFeatures;
Feature<?>[] absentFeatures;
try {
presentFeatures = (Feature[]) annotationClass.getMethod("value").invoke(testerAnnotation);
absentFeatures = (Feature[]) annotationClass.getMethod("absent").invoke(testerAnnotation);
presentFeatures = (Feature<?>[]) annotationClass.getMethod("value").invoke(testerAnnotation);
absentFeatures = (Feature<?>[]) annotationClass.getMethod("absent").invoke(testerAnnotation);
} catch (Exception e) {
throw new IllegalArgumentException("Error extracting features from tester annotation.", e);
}
Set<Feature<?>> allPresentFeatures =
addImpliedFeatures(Helpers.<Feature<?>>copyToSet(presentFeatures));
Set<Feature<?>> allAbsentFeatures =
addImpliedFeatures(Helpers.<Feature<?>>copyToSet(absentFeatures));
if (!Collections.disjoint(allPresentFeatures, allAbsentFeatures)) {
Set<Feature<?>> allPresentFeatures = addImpliedFeatures(copyToSet(presentFeatures));
Set<Feature<?>> allAbsentFeatures = copyToSet(absentFeatures);
if (!disjoint(allPresentFeatures, allAbsentFeatures)) {
throw new ConflictingRequirementsException(
"Annotation explicitly or "
+ "implicitly requires one or more features to be both present "
Expand Down Expand Up @@ -239,7 +239,7 @@ public static Iterable<Annotation> getTesterAnnotations(AnnotatedElement classOr
annotations.add(a);
}
}
annotations = Collections.unmodifiableList(annotations);
annotations = unmodifiableList(annotations);
annotationCache.put(classOrMethod, annotations);
}
return annotations;
Expand Down Expand Up @@ -279,7 +279,7 @@ private static void checkConflict(
Set<Feature<?>> newFeatures,
Object source)
throws ConflictingRequirementsException {
if (!Collections.disjoint(newFeatures, earlierFeatures)) {
if (!disjoint(newFeatures, earlierFeatures)) {
throw new ConflictingRequirementsException(
String.format(
Locale.ROOT,
Expand All @@ -292,10 +292,17 @@ private static void checkConflict(
}
}

/** Construct a new {@link java.util.Set} that is the intersection of the given sets. */
/**
* Construct a new {@link java.util.Set} that is the intersection of the given sets.
*
* @deprecated Use {@link com.google.common.collect.Sets#intersection(Set, Set)} instead.
*/
@Deprecated
public static <T> Set<T> intersection(Set<? extends T> set1, Set<? extends T> set2) {
Set<T> result = Helpers.<T>copyToSet(set1);
Set<T> result = copyToSet(set1);
result.retainAll(set2);
return result;
}

private FeatureUtil() {}
}
Loading

0 comments on commit 81be061

Please sign in to comment.