From b362d72c95a88fc1b297c76d4740cca91a4704d8 Mon Sep 17 00:00:00 2001 From: Jan Ouwens Date: Fri, 22 Nov 2024 10:29:07 +0100 Subject: [PATCH] Introduces Attributes for ValueProviders --- .../RecordObjectAccessorScramblingTest.java | 6 +- .../vintage/RecordObjectAccessorTest.java | 6 +- .../checkers/AbstractDelegationChecker.java | 3 +- .../MapEntryHashCodeRequirementChecker.java | 3 +- .../fieldchecks/JpaLazyGetterFieldCheck.java | 3 +- .../fieldchecks/ReflexivityFieldCheck.java | 3 +- .../fieldchecks/StringFieldCheck.java | 3 +- .../instantiation/CachedValueProvider.java | 4 +- .../instantiation/ChainedValueProvider.java | 6 +- .../instantiation/SubjectCreator.java | 6 +- .../instantiation/ValueProvider.java | 67 +++++++++++++++++-- .../instantiation/VintageValueProvider.java | 67 ++++++++----------- .../reflection/vintage/ClassAccessor.java | 44 ++++++------ .../reflection/vintage/FieldModifier.java | 8 +-- .../vintage/InPlaceObjectAccessor.java | 6 +- .../reflection/vintage/ObjectAccessor.java | 6 +- .../vintage/RecordObjectAccessor.java | 6 +- .../factories/AbstractGenericFactory.java | 17 ++--- .../prefabvalues/factories/CopyFactory.java | 6 +- .../factories/EnumMapFactory.java | 6 +- .../factories/EnumSetFactory.java | 6 +- .../factories/ExternalFactory.java | 6 +- .../factories/FallbackFactory.java | 29 ++++---- .../prefabvalues/factories/MapFactory.java | 6 +- .../factories/PrefabValueFactory.java | 11 +-- .../prefabvalues/factories/SimpleFactory.java | 4 +- .../factories/SimpleGenericFactory.java | 6 +- .../GuavaFactoryProvider.java | 9 +-- .../JavaFxFactoryProvider.java | 3 +- .../testhelpers/TestValueProviders.java | 4 +- .../ChainedValueProviderTest.java | 22 +++--- .../instantiation/SubjectCreatorTest.java | 4 +- .../VintageValueProviderTest.java | 8 +-- .../reflection/vintage/ClassAccessorTest.java | 6 +- .../InPlaceObjectAccessorScramblingTest.java | 6 +- .../factories/AbstractGenericFactoryTest.java | 4 +- .../factories/FallbackFactoryTest.java | 17 ++--- .../factories/MapFactoryTest.java | 12 ++-- .../factories/SimpleGenericFactoryTest.java | 12 ++-- 39 files changed, 241 insertions(+), 210 deletions(-) diff --git a/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/RecordObjectAccessorScramblingTest.java b/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/RecordObjectAccessorScramblingTest.java index 898826026..a39f867db 100644 --- a/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/RecordObjectAccessorScramblingTest.java +++ b/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/RecordObjectAccessorScramblingTest.java @@ -5,10 +5,10 @@ import static org.junit.jupiter.api.Assertions.assertNotSame; import java.lang.reflect.Constructor; -import java.util.LinkedHashSet; import nl.jqno.equalsverifier.internal.reflection.JavaApiPrefabValues; import nl.jqno.equalsverifier.internal.reflection.TypeTag; import nl.jqno.equalsverifier.internal.reflection.instantiation.CachedValueProvider; +import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes; import nl.jqno.equalsverifier.internal.reflection.instantiation.VintageValueProvider; import nl.jqno.equalsverifier.internal.testhelpers.TestValueProviders; import org.junit.jupiter.api.BeforeEach; @@ -17,7 +17,7 @@ public class RecordObjectAccessorScramblingTest { - private static final LinkedHashSet EMPTY_TYPE_STACK = new LinkedHashSet<>(); + private static final Attributes EMPTY_ATTRIBUTES = Attributes.unlabeled(); private CachedValueProvider cache; private FactoryCache factoryCache; private VintageValueProvider valueProvider; @@ -66,7 +66,7 @@ private RecordObjectAccessor create(T object) { } private ObjectAccessor doScramble(Object object) { - return create(object).scramble(valueProvider, TypeTag.NULL, EMPTY_TYPE_STACK); + return create(object).scramble(valueProvider, TypeTag.NULL, EMPTY_ATTRIBUTES); } record Point(int x, int y) {} diff --git a/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/RecordObjectAccessorTest.java b/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/RecordObjectAccessorTest.java index b083634f7..8975d574f 100644 --- a/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/RecordObjectAccessorTest.java +++ b/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/RecordObjectAccessorTest.java @@ -4,13 +4,13 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import java.lang.reflect.Constructor; -import java.util.LinkedHashSet; import java.util.Objects; import nl.jqno.equalsverifier.internal.exceptions.ReflectionException; import nl.jqno.equalsverifier.internal.reflection.Instantiator; import nl.jqno.equalsverifier.internal.reflection.JavaApiPrefabValues; import nl.jqno.equalsverifier.internal.reflection.TypeTag; import nl.jqno.equalsverifier.internal.reflection.instantiation.CachedValueProvider; +import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes; import nl.jqno.equalsverifier.internal.reflection.instantiation.VintageValueProvider; import nl.jqno.equalsverifier.internal.testhelpers.ExpectedException; import nl.jqno.equalsverifier.internal.testhelpers.TestValueProviders; @@ -21,7 +21,7 @@ public class RecordObjectAccessorTest { - private static final LinkedHashSet EMPTY_TYPE_STACK = new LinkedHashSet<>(); + private static final Attributes EMPTY_ATTRIBUTES = Attributes.unlabeled(); private Objenesis objenesis; private Object recordInstance; @@ -83,7 +83,7 @@ public void fail_whenConstructorThrowsOnSomethingElse() { objenesis ); ExpectedException - .when(() -> accessorFor(instance).scramble(vp, TypeTag.NULL, EMPTY_TYPE_STACK)) + .when(() -> accessorFor(instance).scramble(vp, TypeTag.NULL, EMPTY_ATTRIBUTES)) .assertThrows(ReflectionException.class) .assertMessageContains("Record:", "failed to run constructor", "prefab values"); } diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/AbstractDelegationChecker.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/AbstractDelegationChecker.java index 9cd851599..4ae690cc6 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/AbstractDelegationChecker.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/AbstractDelegationChecker.java @@ -10,6 +10,7 @@ import nl.jqno.equalsverifier.internal.reflection.TypeTag; import nl.jqno.equalsverifier.internal.reflection.instantiation.SubjectCreator; import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider; +import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes; import nl.jqno.equalsverifier.internal.util.*; public class AbstractDelegationChecker implements Checker { @@ -74,7 +75,7 @@ private void checkAbstractDelegationInFields() { private Tuple safelyGetTuple(TypeTag tag, String fieldName) { try { - return valueProvider.provideOrThrow(tag, fieldName); + return valueProvider.provideOrThrow(tag, Attributes.labeled(fieldName)); } catch (Exception ignored) { // If it fails for some reason, any reason, just return null so we can skip the test. return null; diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/MapEntryHashCodeRequirementChecker.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/MapEntryHashCodeRequirementChecker.java index 7438783b4..f9abe6f8c 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/MapEntryHashCodeRequirementChecker.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/MapEntryHashCodeRequirementChecker.java @@ -5,6 +5,7 @@ import java.util.Map; import java.util.Objects; import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider; +import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes; import nl.jqno.equalsverifier.internal.util.Configuration; import nl.jqno.equalsverifier.internal.util.Context; import nl.jqno.equalsverifier.internal.util.Formatter; @@ -23,7 +24,7 @@ public MapEntryHashCodeRequirementChecker(Context context) { public void check() { if (Map.Entry.class.isAssignableFrom(config.getType())) { Map.Entry e = valueProvider - .>provideOrThrow(config.getTypeTag(), null) + .>provideOrThrow(config.getTypeTag(), Attributes.unlabeled()) .getRed(); int expectedHashCode = Objects.hashCode(e.getKey()) ^ Objects.hashCode(e.getValue()); diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/JpaLazyGetterFieldCheck.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/JpaLazyGetterFieldCheck.java index c2119280c..0ef963e88 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/JpaLazyGetterFieldCheck.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/JpaLazyGetterFieldCheck.java @@ -14,6 +14,7 @@ import nl.jqno.equalsverifier.internal.reflection.annotations.SupportedAnnotations; import nl.jqno.equalsverifier.internal.reflection.instantiation.SubjectCreator; import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider; +import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes; import nl.jqno.equalsverifier.internal.util.Configuration; import nl.jqno.equalsverifier.internal.util.Context; import nl.jqno.equalsverifier.internal.util.Formatter; @@ -64,7 +65,7 @@ public void execute(FieldProbe fieldProbe) { ); TypeTag sub = new TypeTag(throwingGetterCreator(getterName)); - Tuple tuple = valueProvider.provideOrThrow(sub, fieldName); + Tuple tuple = valueProvider.provideOrThrow(sub, Attributes.labeled(fieldName)); T red1 = tuple.getRed(); T red2 = tuple.getRedCopy(); diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/ReflexivityFieldCheck.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/ReflexivityFieldCheck.java index fd2d98f06..e3d3b25b1 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/ReflexivityFieldCheck.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/ReflexivityFieldCheck.java @@ -13,6 +13,7 @@ import nl.jqno.equalsverifier.internal.reflection.annotations.SupportedAnnotations; import nl.jqno.equalsverifier.internal.reflection.instantiation.SubjectCreator; import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider; +import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes; import nl.jqno.equalsverifier.internal.util.Configuration; import nl.jqno.equalsverifier.internal.util.Context; import nl.jqno.equalsverifier.internal.util.Formatter; @@ -80,7 +81,7 @@ private void checkValueReflexivity(FieldProbe probe) { String fieldName = field.getName(); TypeTag tag = TypeTag.of(field, typeTag); Tuple tuple = valueProvider - .provide(tag, fieldName) + .provide(tag, Attributes.labeled(fieldName)) .orElseThrow(() -> new NoValueException(tag, fieldName)); Object left = subjectCreator.withFieldSetTo(field, tuple.getRed()); diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/StringFieldCheck.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/StringFieldCheck.java index 6c67acc42..a90be9261 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/StringFieldCheck.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/StringFieldCheck.java @@ -8,6 +8,7 @@ import nl.jqno.equalsverifier.internal.reflection.TypeTag; import nl.jqno.equalsverifier.internal.reflection.instantiation.SubjectCreator; import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider; +import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes; import nl.jqno.equalsverifier.internal.util.CachedHashCodeInitializer; import nl.jqno.equalsverifier.internal.util.Formatter; @@ -42,7 +43,7 @@ public void execute(FieldProbe fieldProbe) { if (String.class.equals(fieldProbe.getType()) && !fieldProbe.isStatic()) { TypeTag string = new TypeTag(String.class); String red = valueProvider - .provideOrThrow(string, fieldProbe.getName()) + .provideOrThrow(string, Attributes.labeled(fieldProbe.getName())) .getRed(); final T reference; diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/instantiation/CachedValueProvider.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/instantiation/CachedValueProvider.java index a7a84a60a..6aba203fb 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/instantiation/CachedValueProvider.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/instantiation/CachedValueProvider.java @@ -39,8 +39,8 @@ public boolean contains(TypeTag tag, String label) { */ @SuppressWarnings("unchecked") @Override - public Optional> provide(TypeTag tag, String label) { - Key key = Key.of(tag, label); + public Optional> provide(TypeTag tag, Attributes attributes) { + Key key = Key.of(tag, attributes.label); if (cache.containsKey(key)) { return Optional.of((Tuple) cache.get(key)); } diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/instantiation/ChainedValueProvider.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/instantiation/ChainedValueProvider.java index 0cb14a26d..599444795 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/instantiation/ChainedValueProvider.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/instantiation/ChainedValueProvider.java @@ -42,15 +42,15 @@ public void register(ValueProvider... valueProviders) { /** {@inheritDoc} */ @Override - public Optional> provide(TypeTag tag, String label) { + public Optional> provide(TypeTag tag, Attributes attributes) { Optional> result = providers .stream() - .map(vp -> vp.provide(tag, label)) + .map(vp -> vp.provide(tag, attributes)) .filter(Optional::isPresent) .findFirst() .orElse(Optional.empty()); - result.ifPresent(r -> cache.put(tag, label, r)); + result.ifPresent(r -> cache.put(tag, attributes.label, r)); return result; } } diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/instantiation/SubjectCreator.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/instantiation/SubjectCreator.java index 0e4a8f70a..56fd956fb 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/instantiation/SubjectCreator.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/instantiation/SubjectCreator.java @@ -6,6 +6,7 @@ import java.util.Map; import nl.jqno.equalsverifier.internal.exceptions.ModuleException; import nl.jqno.equalsverifier.internal.reflection.*; +import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes; import nl.jqno.equalsverifier.internal.util.Configuration; import nl.jqno.equalsverifier.internal.util.Rethrow; import org.objenesis.Objenesis; @@ -237,7 +238,10 @@ private FieldIterable nonSuperFields() { private Tuple valuesFor(Field f) { try { TypeTag fieldTag = TypeTag.of(f, typeTag); - Tuple tuple = valueProvider.provideOrThrow(fieldTag, f.getName()); + Tuple tuple = valueProvider.provideOrThrow( + fieldTag, + Attributes.labeled(f.getName()) + ); return tuple; } catch (ModuleException e) { throw new ModuleException( diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/instantiation/ValueProvider.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/instantiation/ValueProvider.java index cc37b9873..366f6963a 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/instantiation/ValueProvider.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/instantiation/ValueProvider.java @@ -1,5 +1,6 @@ package nl.jqno.equalsverifier.internal.reflection.instantiation; +import java.util.LinkedHashSet; import java.util.Optional; import nl.jqno.equalsverifier.internal.exceptions.NoValueException; import nl.jqno.equalsverifier.internal.reflection.Tuple; @@ -21,12 +22,11 @@ public interface ValueProvider { * * @param The returned tuple will have this generic type. * @param tag A description of the desired type, including generic parameters. - * @param label Returns only the value assigned to the given label, or if label is null, - * returns the value that's not assigned to any label. + * @param attributes Provides metadata needed to provide a value. * @return A tuple of two different values of the given type, or an empty Optional if none * could be found. */ - Optional> provide(TypeTag tag, String label); + Optional> provide(TypeTag tag, Attributes attributes); /** * Returns a tuple of prefabricated values of the specified type, or, if none exists, throws a @@ -34,13 +34,66 @@ public interface ValueProvider { * * @param The returned tuple will have this generic type. * @param tag A description of the desired type, including generic parameters. - * @param label Returns only the value assigned to the given label, or if label is null, - * returns the value that's not assigned to any label. + * @param attributes Provides metadata needed to provide a value. * @return A tuple of two different values of the given type, or an empty Optional if none * could be found. * @throws NoValueException if no value could be found for the given tag. */ - default Tuple provideOrThrow(TypeTag tag, String label) { - return this.provide(tag, label).orElseThrow(() -> new NoValueException(tag)); + default Tuple provideOrThrow(TypeTag tag, Attributes attributes) { + return this.provide(tag, attributes).orElseThrow(() -> new NoValueException(tag)); + } + + /** + * Container for metadata needed to provide values. + */ + public static final class Attributes { + + /** + * Values can be assigned to a label; if one is specified, ValueProvider returns the value + * assigned to it (or falls back to the value assigned to a null label). If label is null, + * it immediately returns the value assigned to the null label. + */ + public final String label; + /** + * Keeps track of recursion. + */ + public final LinkedHashSet typeStack; + + /** Private constructor. Use the factories instead. */ + private Attributes(String label, LinkedHashSet typeStack) { + this.label = label; + this.typeStack = typeStack; + } + + /** + * Don't use a label when providing a value. + * + * @return An Attributes object with no label. + */ + public static Attributes unlabeled() { + return new Attributes(null, new LinkedHashSet<>()); + } + + /** + * Use a label when providing a value. + * + * @param label The label to use. + * @return An Attributes object with the given label. + */ + public static Attributes labeled(String label) { + return new Attributes(label, new LinkedHashSet<>()); + } + + /** + * Clones the internal typeStack and adds a type to it. + * + * @param tag A type to add to the recursion checker. + * @return A new Attributes object with a type added to its typeStack. + */ + public Attributes cloneAndAdd(TypeTag tag) { + LinkedHashSet clone = new LinkedHashSet<>(typeStack); + clone.add(tag); + return new Attributes(label, clone); + } } } diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/instantiation/VintageValueProvider.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/instantiation/VintageValueProvider.java index 0c2b73783..2c1294903 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/instantiation/VintageValueProvider.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/instantiation/VintageValueProvider.java @@ -50,8 +50,8 @@ public VintageValueProvider( /** {@inheritDoc} */ @Override - public Optional> provide(TypeTag tag, String label) { - return Rethrow.rethrow(() -> Optional.of(giveTuple(tag, label))); + public Optional> provide(TypeTag tag, Attributes attributes) { + return Rethrow.rethrow(() -> Optional.of(giveTuple(tag, attributes))); } /** @@ -64,7 +64,7 @@ public Optional> provide(TypeTag tag, String label) { * @return The "red" prefabricated value. */ public T giveRed(TypeTag tag) { - return this.giveTuple(tag, null).getRed(); + return this.giveTuple(tag).getRed(); } /** @@ -77,7 +77,7 @@ public T giveRed(TypeTag tag) { * @return The "blue" prefabricated value. */ public T giveBlue(TypeTag tag) { - return this.giveTuple(tag, null).getBlue(); + return this.giveTuple(tag).getBlue(); } /** @@ -90,7 +90,7 @@ public T giveBlue(TypeTag tag) { * @return A shallow copy of the "red" prefabricated value. */ public T giveRedCopy(TypeTag tag) { - return this.giveTuple(tag, null).getRedCopy(); + return this.giveTuple(tag).getRedCopy(); } /** @@ -100,11 +100,11 @@ public T giveRedCopy(TypeTag tag) { * @param The type of the value. * @param tag A description of the desired type, including generic parameters. * @param value A value that is different from the value that will be returned. - * @param typeStack Keeps track of recursion in the type. + * @param attributes Provides metadata needed to provide a value and to keep track of recursion. * @return A value that is different from {@code value}. */ // CHECKSTYLE OFF: CyclomaticComplexity - public T giveOther(TypeTag tag, T value, LinkedHashSet typeStack) { + public T giveOther(TypeTag tag, T value, Attributes attributes) { Class type = tag.getType(); if ( value != null && @@ -114,7 +114,7 @@ public T giveOther(TypeTag tag, T value, LinkedHashSet typeStack) { throw new ReflectionException("TypeTag does not match value."); } - Tuple tuple = giveTuple(tag, null, typeStack); + Tuple tuple = giveTuple(tag, attributes); if (tuple.getRed() == null) { return null; } @@ -151,60 +151,47 @@ private boolean arraysAreDeeplyEqual(Object x, Object y) { * * @param The desired type. * @param tag A description of the desired type, including generic parameters. - * @param typeStack Keeps track of recursion in the type. + * @param attributes Keeps track of recursion in the type. */ - public void realizeCacheFor(TypeTag tag, LinkedHashSet typeStack) { - realizeCacheFor(tag, null, typeStack); - } - - /** - * Makes sure that values for the specified type are present in the cache, but doesn't return - * them. - * - * @param The desired type. - * @param tag A description of the desired type, including generic parameters. - * @param label Adds the value assigned to the given label, if a label is given. - * @param typeStack Keeps track of recursion in the type. - */ - public void realizeCacheFor(TypeTag tag, String label, LinkedHashSet typeStack) { - if (!cache.contains(tag, label)) { - Tuple tuple = createTuple(tag, label, typeStack); - cache.put(tag, label, tuple); + public void realizeCacheFor(TypeTag tag, Attributes attributes) { + if (!cache.contains(tag, attributes.label)) { + Tuple tuple = createTuple(tag, attributes); + cache.put(tag, attributes.label, tuple); } } - private Tuple giveTuple(TypeTag tag, String label) { - return giveTuple(tag, label, new LinkedHashSet<>()); + private Tuple giveTuple(TypeTag tag) { + return giveTuple(tag, Attributes.unlabeled()); } @SuppressWarnings("unchecked") - private Tuple giveTuple(TypeTag tag, String label, LinkedHashSet typeStack) { - realizeCacheFor(tag, label, typeStack); - return (Tuple) cache.provide(tag, label).get(); + private Tuple giveTuple(TypeTag tag, Attributes attributes) { + realizeCacheFor(tag, attributes); + return (Tuple) cache.provide(tag, attributes).get(); } - private Tuple createTuple(TypeTag tag, String label, LinkedHashSet typeStack) { - if (typeStack.contains(tag)) { - throw new RecursionException(typeStack); + private Tuple createTuple(TypeTag tag, Attributes attributes) { + if (attributes.typeStack.contains(tag)) { + throw new RecursionException(attributes.typeStack); } - Optional> provided = valueProvider.provide(tag, null); + Optional> provided = valueProvider.provide(tag, attributes); if (provided.isPresent()) { return provided.get(); } Class type = tag.getType(); - if (label != null && factoryCache.contains(type, label)) { - PrefabValueFactory factory = factoryCache.get(type, label); - return factory.createValues(tag, this, typeStack); + if (attributes.label != null && factoryCache.contains(type, attributes.label)) { + PrefabValueFactory factory = factoryCache.get(type, attributes.label); + return factory.createValues(tag, this, attributes); } if (factoryCache.contains(type)) { PrefabValueFactory factory = factoryCache.get(type); - return factory.createValues(tag, this, typeStack); + return factory.createValues(tag, this, attributes); } @SuppressWarnings("unchecked") - Tuple result = (Tuple) fallbackFactory.createValues(tag, this, typeStack); + Tuple result = (Tuple) fallbackFactory.createValues(tag, this, attributes); return result; } } diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/ClassAccessor.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/ClassAccessor.java index e19fde95d..45a49d695 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/ClassAccessor.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/ClassAccessor.java @@ -1,8 +1,8 @@ package nl.jqno.equalsverifier.internal.reflection.vintage; -import java.util.LinkedHashSet; import nl.jqno.equalsverifier.internal.reflection.Instantiator; import nl.jqno.equalsverifier.internal.reflection.TypeTag; +import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes; import nl.jqno.equalsverifier.internal.reflection.instantiation.VintageValueProvider; import org.objenesis.Objenesis; @@ -45,60 +45,54 @@ public static ClassAccessor of( /** * Returns an instance of T that is not equal to the instance of T returned by {@link - * #getBlueObject(TypeTag, LinkedHashSet)}. + * #getBlueObject(TypeTag, Attributes)}. * * @param enclosingType Describes the type that contains this object as a field, to determine * any generic parameters it may contain. - * @param typeStack Keeps track of recursion in the type. + * @param attributes Provides metadata needed to provide a value and to keep track of recursion. * @return An instance of T. */ - public T getRedObject(TypeTag enclosingType, LinkedHashSet typeStack) { - return getRedAccessor(enclosingType, typeStack).get(); + public T getRedObject(TypeTag enclosingType, Attributes attributes) { + return getRedAccessor(enclosingType, attributes).get(); } /** - * Returns an {@link ObjectAccessor} for {@link #getRedObject(TypeTag, LinkedHashSet)}. + * Returns an {@link ObjectAccessor} for {@link #getRedObject(TypeTag, Attributes)}. * * @param enclosingType Describes the type that contains this object as a field, to determine * any generic parameters it may contain. - * @param typeStack Keeps track of recursion in the type. + * @param attributes Provides metadata needed to provide a value and to keep track of recursion. * @return An {@link ObjectAccessor} for {@link #getRedObject}. */ - public ObjectAccessor getRedAccessor( - TypeTag enclosingType, - LinkedHashSet typeStack - ) { - return buildObjectAccessor().scramble(valueProvider, enclosingType, typeStack); + public ObjectAccessor getRedAccessor(TypeTag enclosingType, Attributes attributes) { + return buildObjectAccessor().scramble(valueProvider, enclosingType, attributes); } /** * Returns an instance of T that is not equal to the instance of T returned by {@link - * #getRedObject(TypeTag, LinkedHashSet)}. + * #getRedObject(TypeTag, Attributes)}. * * @param enclosingType Describes the type that contains this object as a field, to determine * any generic parameters it may contain. - * @param typeStack Keeps track of recursion in the type. + * @param attributes Provides metadata needed to provide a value and to keep track of recursion. * @return An instance of T. */ - public T getBlueObject(TypeTag enclosingType, LinkedHashSet typeStack) { - return getBlueAccessor(enclosingType, typeStack).get(); + public T getBlueObject(TypeTag enclosingType, Attributes attributes) { + return getBlueAccessor(enclosingType, attributes).get(); } /** - * Returns an {@link ObjectAccessor} for {@link #getBlueObject(TypeTag, LinkedHashSet)}. + * Returns an {@link ObjectAccessor} for {@link #getBlueObject(TypeTag, Attributes)}. * * @param enclosingType Describes the type that contains this object as a field, to determine * any generic parameters it may contain. - * @param typeStack Keeps track of recursion in the type. - * @return An {@link ObjectAccessor} for {@link #getBlueObject(TypeTag, LinkedHashSet)}. + * @param attributes Provides metadata needed to provide a value and to keep track of recursion. + * @return An {@link ObjectAccessor} for {@link #getBlueObject(TypeTag, Attributes)}. */ - public ObjectAccessor getBlueAccessor( - TypeTag enclosingType, - LinkedHashSet typeStack - ) { + public ObjectAccessor getBlueAccessor(TypeTag enclosingType, Attributes attributes) { return buildObjectAccessor() - .scramble(valueProvider, enclosingType, typeStack) - .scramble(valueProvider, enclosingType, typeStack); + .scramble(valueProvider, enclosingType, attributes) + .scramble(valueProvider, enclosingType, attributes); } private ObjectAccessor buildObjectAccessor() { diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/FieldModifier.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/FieldModifier.java index 0b91e5f87..995e9bbbf 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/FieldModifier.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/FieldModifier.java @@ -3,10 +3,10 @@ import static nl.jqno.equalsverifier.internal.util.Rethrow.rethrow; import java.lang.reflect.Field; -import java.util.LinkedHashSet; import nl.jqno.equalsverifier.internal.exceptions.ReflectionException; import nl.jqno.equalsverifier.internal.reflection.FieldProbe; import nl.jqno.equalsverifier.internal.reflection.TypeTag; +import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes; import nl.jqno.equalsverifier.internal.reflection.instantiation.VintageValueProvider; public final class FieldModifier { @@ -53,17 +53,17 @@ public void copyTo(Object to) { * will be taken from it. * @param enclosingType A tag for the type that contains the field. Needed to determine a * generic type, if it has one.. - * @param typeStack Keeps track of recursion in the type. + * @param attributes Provides metadata needed to provide a value and to keep track of recursion. * @throws ReflectionException If the operation fails. */ public void changeField( VintageValueProvider valueProvider, TypeTag enclosingType, - LinkedHashSet typeStack + Attributes attributes ) { FieldChanger fm = () -> { TypeTag tag = TypeTag.of(field, enclosingType); - Object newValue = valueProvider.giveOther(tag, field.get(object), typeStack); + Object newValue = valueProvider.giveOther(tag, field.get(object), attributes); field.set(object, newValue); }; change(fm, false); diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/InPlaceObjectAccessor.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/InPlaceObjectAccessor.java index c9cf1c573..f48130627 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/InPlaceObjectAccessor.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/InPlaceObjectAccessor.java @@ -1,10 +1,10 @@ package nl.jqno.equalsverifier.internal.reflection.vintage; import java.lang.reflect.Field; -import java.util.LinkedHashSet; import nl.jqno.equalsverifier.internal.reflection.FieldIterable; import nl.jqno.equalsverifier.internal.reflection.Instantiator; import nl.jqno.equalsverifier.internal.reflection.TypeTag; +import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes; import nl.jqno.equalsverifier.internal.reflection.instantiation.VintageValueProvider; import org.objenesis.Objenesis; @@ -35,10 +35,10 @@ public T copy(Objenesis objenesis) { public ObjectAccessor scramble( VintageValueProvider valueProvider, TypeTag enclosingType, - LinkedHashSet typeStack + Attributes attributes ) { for (Field field : FieldIterable.of(type())) { - fieldModifierFor(field).changeField(valueProvider, enclosingType, typeStack); + fieldModifierFor(field).changeField(valueProvider, enclosingType, attributes); } return this; } diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/ObjectAccessor.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/ObjectAccessor.java index 480e30224..329980e52 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/ObjectAccessor.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/ObjectAccessor.java @@ -1,8 +1,8 @@ package nl.jqno.equalsverifier.internal.reflection.vintage; -import java.util.LinkedHashSet; import nl.jqno.equalsverifier.internal.reflection.RecordsHelper; import nl.jqno.equalsverifier.internal.reflection.TypeTag; +import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes; import nl.jqno.equalsverifier.internal.reflection.instantiation.VintageValueProvider; import org.objenesis.Objenesis; @@ -95,12 +95,12 @@ public Class type() { * @param valueProvider Prefabricated values to take values from. * @param enclosingType Describes the type that contains this object as a field, to determine * any generic parameters it may contain. - * @param typeStack Keeps track of recursion in the type. + * @param attributes Provides metadata needed to provide a value and to keep track of recursion. * @return An accessor to the scrambled object. */ public abstract ObjectAccessor scramble( VintageValueProvider valueProvider, TypeTag enclosingType, - LinkedHashSet typeStack + Attributes attributes ); } diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/RecordObjectAccessor.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/RecordObjectAccessor.java index c1f63ea44..8c45295aa 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/RecordObjectAccessor.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/RecordObjectAccessor.java @@ -1,13 +1,13 @@ package nl.jqno.equalsverifier.internal.reflection.vintage; import java.lang.reflect.Field; -import java.util.LinkedHashSet; import java.util.List; import java.util.function.Function; import java.util.stream.Collectors; import nl.jqno.equalsverifier.internal.reflection.FieldProbe; import nl.jqno.equalsverifier.internal.reflection.RecordProbe; import nl.jqno.equalsverifier.internal.reflection.TypeTag; +import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes; import nl.jqno.equalsverifier.internal.reflection.instantiation.VintageValueProvider; import org.objenesis.Objenesis; @@ -40,12 +40,12 @@ public T copy(Objenesis objenesis) { public ObjectAccessor scramble( VintageValueProvider valueProvider, TypeTag enclosingType, - LinkedHashSet typeStack + Attributes attributes ) { return makeAccessor(f -> { Object value = getField(f); TypeTag tag = TypeTag.of(f, enclosingType); - return valueProvider.giveOther(tag, value, typeStack); + return valueProvider.giveOther(tag, value, attributes); }); } diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/AbstractGenericFactory.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/AbstractGenericFactory.java index 0058867ad..06e4571e1 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/AbstractGenericFactory.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/AbstractGenericFactory.java @@ -4,10 +4,10 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; -import java.util.LinkedHashSet; import java.util.List; import nl.jqno.equalsverifier.internal.exceptions.ReflectionException; import nl.jqno.equalsverifier.internal.reflection.TypeTag; +import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes; import nl.jqno.equalsverifier.internal.reflection.instantiation.VintageValueProvider; /** @@ -18,13 +18,6 @@ public abstract class AbstractGenericFactory implements PrefabValueFactory public static final TypeTag OBJECT_TYPE_TAG = new TypeTag(Object.class); - protected LinkedHashSet cloneWith(LinkedHashSet typeStack, TypeTag tag) { - @SuppressWarnings("unchecked") - LinkedHashSet clone = (LinkedHashSet) typeStack.clone(); - clone.add(tag); - return clone; - } - protected TypeTag copyGenericTypesInto(Class type, TypeTag source) { List genericTypes = new ArrayList<>(); for (TypeTag tag : source.getGenericTypes()) { @@ -37,23 +30,23 @@ protected TypeTag determineAndCacheActualTypeTag( int n, TypeTag tag, VintageValueProvider valueProvider, - LinkedHashSet typeStack + Attributes attributes ) { - return determineAndCacheActualTypeTag(n, tag, valueProvider, typeStack, null); + return determineAndCacheActualTypeTag(n, tag, valueProvider, attributes, null); } protected TypeTag determineAndCacheActualTypeTag( int n, TypeTag tag, VintageValueProvider valueProvider, - LinkedHashSet typeStack, + Attributes attributes, Class bottomType ) { TypeTag result = determineActualTypeTagFor(n, tag); if (bottomType != null && result.getType().equals(Object.class)) { result = new TypeTag(bottomType); } - valueProvider.realizeCacheFor(result, typeStack); + valueProvider.realizeCacheFor(result, attributes); return result; } diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/CopyFactory.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/CopyFactory.java index 1b1222861..9b1af52e3 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/CopyFactory.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/CopyFactory.java @@ -1,9 +1,9 @@ package nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories; -import java.util.LinkedHashSet; import java.util.function.Function; import nl.jqno.equalsverifier.internal.reflection.Tuple; import nl.jqno.equalsverifier.internal.reflection.TypeTag; +import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes; import nl.jqno.equalsverifier.internal.reflection.instantiation.VintageValueProvider; public class CopyFactory extends AbstractGenericFactory { @@ -20,9 +20,9 @@ public CopyFactory(Class source, Function copy) { public Tuple createValues( TypeTag tag, VintageValueProvider valueProvider, - LinkedHashSet typeStack + Attributes attributes ) { - LinkedHashSet clone = cloneWith(typeStack, tag); + Attributes clone = attributes.cloneAndAdd(tag); TypeTag sourceTag = copyGenericTypesInto(source, tag); valueProvider.realizeCacheFor(sourceTag, clone); diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/EnumMapFactory.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/EnumMapFactory.java index 5b1424694..836bc01e2 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/EnumMapFactory.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/EnumMapFactory.java @@ -1,11 +1,11 @@ package nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories; import java.util.HashMap; -import java.util.LinkedHashSet; import java.util.Map; import java.util.function.Function; import nl.jqno.equalsverifier.internal.reflection.Tuple; import nl.jqno.equalsverifier.internal.reflection.TypeTag; +import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes; import nl.jqno.equalsverifier.internal.reflection.instantiation.VintageValueProvider; @SuppressWarnings({ "unchecked", "rawtypes" }) @@ -21,9 +21,9 @@ public EnumMapFactory(Function factory) { public Tuple createValues( TypeTag tag, VintageValueProvider valueProvider, - LinkedHashSet typeStack + Attributes attributes ) { - LinkedHashSet clone = cloneWith(typeStack, tag); + Attributes clone = attributes.cloneAndAdd(tag); TypeTag keyTag = determineAndCacheActualTypeTag(0, tag, valueProvider, clone, Enum.class); TypeTag valueTag = determineAndCacheActualTypeTag(1, tag, valueProvider, clone, Enum.class); diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/EnumSetFactory.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/EnumSetFactory.java index 61a59e447..23d3bdfcc 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/EnumSetFactory.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/EnumSetFactory.java @@ -2,10 +2,10 @@ import java.util.Collection; import java.util.HashSet; -import java.util.LinkedHashSet; import java.util.function.Function; import nl.jqno.equalsverifier.internal.reflection.Tuple; import nl.jqno.equalsverifier.internal.reflection.TypeTag; +import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes; import nl.jqno.equalsverifier.internal.reflection.instantiation.VintageValueProvider; /** @@ -25,9 +25,9 @@ public EnumSetFactory(Function factory) { public Tuple createValues( TypeTag tag, VintageValueProvider valueProvider, - LinkedHashSet typeStack + Attributes attributes ) { - LinkedHashSet clone = cloneWith(typeStack, tag); + Attributes clone = attributes.cloneAndAdd(tag); TypeTag entryTag = determineAndCacheActualTypeTag(0, tag, valueProvider, clone, Enum.class); Collection red = new HashSet<>(); diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/ExternalFactory.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/ExternalFactory.java index d458ee0a7..da625da89 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/ExternalFactory.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/ExternalFactory.java @@ -3,10 +3,10 @@ import static nl.jqno.equalsverifier.internal.reflection.Util.classes; import static nl.jqno.equalsverifier.internal.reflection.Util.objects; -import java.util.LinkedHashSet; import nl.jqno.equalsverifier.internal.reflection.ConditionalInstantiator; import nl.jqno.equalsverifier.internal.reflection.Tuple; import nl.jqno.equalsverifier.internal.reflection.TypeTag; +import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes; import nl.jqno.equalsverifier.internal.reflection.instantiation.VintageValueProvider; import nl.jqno.equalsverifier.internal.reflection.vintage.FactoryCache; import nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factoryproviders.FactoryProvider; @@ -27,7 +27,7 @@ public ExternalFactory(String factoryName) { public Tuple createValues( TypeTag tag, VintageValueProvider valueProvider, - LinkedHashSet typeStack + Attributes attributes ) { if (factoryCache == null) { ConditionalInstantiator ci = new ConditionalInstantiator(factoryName); @@ -36,6 +36,6 @@ public Tuple createValues( } PrefabValueFactory factory = factoryCache.get(tag.getType()); - return factory.createValues(tag, valueProvider, typeStack); + return factory.createValues(tag, valueProvider, attributes); } } diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/FallbackFactory.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/FallbackFactory.java index eb69ea6b3..91b9aeac7 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/FallbackFactory.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/FallbackFactory.java @@ -2,11 +2,8 @@ import java.lang.reflect.Array; import java.lang.reflect.Field; -import java.util.LinkedHashSet; -import nl.jqno.equalsverifier.internal.reflection.FieldIterable; -import nl.jqno.equalsverifier.internal.reflection.FieldProbe; -import nl.jqno.equalsverifier.internal.reflection.Tuple; -import nl.jqno.equalsverifier.internal.reflection.TypeTag; +import nl.jqno.equalsverifier.internal.reflection.*; +import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes; import nl.jqno.equalsverifier.internal.reflection.instantiation.VintageValueProvider; import nl.jqno.equalsverifier.internal.reflection.vintage.ClassAccessor; import org.objenesis.Objenesis; @@ -29,11 +26,9 @@ public FallbackFactory(Objenesis objenesis) { public Tuple createValues( TypeTag tag, VintageValueProvider valueProvider, - LinkedHashSet typeStack + Attributes attributes ) { - @SuppressWarnings("unchecked") - LinkedHashSet clone = (LinkedHashSet) typeStack.clone(); - clone.add(tag); + Attributes clone = attributes.cloneAndAdd(tag); Class type = tag.getType(); if (type.isEnum()) { @@ -65,12 +60,12 @@ private Tuple giveEnumInstances(TypeTag tag) { private Tuple giveArrayInstances( TypeTag tag, VintageValueProvider valueProvider, - LinkedHashSet typeStack + Attributes attributes ) { Class type = tag.getType(); Class componentType = type.getComponentType(); TypeTag componentTag = new TypeTag(componentType); - valueProvider.realizeCacheFor(componentTag, typeStack); + valueProvider.realizeCacheFor(componentTag, attributes); T red = (T) Array.newInstance(componentType, 1); Array.set(red, 0, valueProvider.giveRed(componentTag)); @@ -85,14 +80,14 @@ private Tuple giveArrayInstances( private void traverseFields( TypeTag tag, VintageValueProvider valueProvider, - LinkedHashSet typeStack + Attributes attributes ) { Class type = tag.getType(); for (Field field : FieldIterable.of(type)) { FieldProbe probe = FieldProbe.of(field); boolean isStaticAndFinal = probe.isStatic() && probe.isFinal(); if (!isStaticAndFinal) { - valueProvider.realizeCacheFor(TypeTag.of(field, tag), typeStack); + valueProvider.realizeCacheFor(TypeTag.of(field, tag), attributes); } } } @@ -100,12 +95,12 @@ private void traverseFields( private Tuple giveInstances( TypeTag tag, VintageValueProvider valueProvider, - LinkedHashSet typeStack + Attributes attributes ) { ClassAccessor accessor = ClassAccessor.of(tag.getType(), valueProvider, objenesis); - T red = accessor.getRedObject(tag, typeStack); - T blue = accessor.getBlueObject(tag, typeStack); - T redCopy = accessor.getRedObject(tag, typeStack); + T red = accessor.getRedObject(tag, attributes); + T blue = accessor.getBlueObject(tag, attributes); + T redCopy = accessor.getRedObject(tag, attributes); return new Tuple<>(red, blue, redCopy); } } diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/MapFactory.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/MapFactory.java index f835cfe90..92752ac21 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/MapFactory.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/MapFactory.java @@ -1,10 +1,10 @@ package nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories; -import java.util.LinkedHashSet; import java.util.Map; import java.util.function.Supplier; import nl.jqno.equalsverifier.internal.reflection.Tuple; import nl.jqno.equalsverifier.internal.reflection.TypeTag; +import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes; import nl.jqno.equalsverifier.internal.reflection.instantiation.VintageValueProvider; /** @@ -24,9 +24,9 @@ public MapFactory(Supplier createEmpty) { public Tuple createValues( TypeTag tag, VintageValueProvider valueProvider, - LinkedHashSet typeStack + Attributes attributes ) { - LinkedHashSet clone = cloneWith(typeStack, tag); + Attributes clone = attributes.cloneAndAdd(tag); TypeTag keyTag = determineAndCacheActualTypeTag(0, tag, valueProvider, clone); TypeTag valueTag = determineAndCacheActualTypeTag(1, tag, valueProvider, clone); diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/PrefabValueFactory.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/PrefabValueFactory.java index 4616c2660..172bebbe9 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/PrefabValueFactory.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/PrefabValueFactory.java @@ -1,8 +1,8 @@ package nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories; -import java.util.LinkedHashSet; import nl.jqno.equalsverifier.internal.reflection.Tuple; import nl.jqno.equalsverifier.internal.reflection.TypeTag; +import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes; import nl.jqno.equalsverifier.internal.reflection.instantiation.VintageValueProvider; /** @@ -17,13 +17,8 @@ public interface PrefabValueFactory { * * @param tag The typetag of the type for which to create values. * @param valueProvider Repository for querying instances of generic types of the type tag. - * @param typeStack A stack of {@link TypeTag}s that require tag in order to be created. Used - * for recursion detection. + * @param attributes Provides metadata needed to provide a value and to keep track of recursion. * @return A "red" instance of {@code T}. */ - Tuple createValues( - TypeTag tag, - VintageValueProvider valueProvider, - LinkedHashSet typeStack - ); + Tuple createValues(TypeTag tag, VintageValueProvider valueProvider, Attributes attributes); } diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/SimpleFactory.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/SimpleFactory.java index 44a7ae041..8e54df590 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/SimpleFactory.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/SimpleFactory.java @@ -1,8 +1,8 @@ package nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories; -import java.util.LinkedHashSet; import nl.jqno.equalsverifier.internal.reflection.Tuple; import nl.jqno.equalsverifier.internal.reflection.TypeTag; +import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes; import nl.jqno.equalsverifier.internal.reflection.instantiation.VintageValueProvider; /** @@ -21,7 +21,7 @@ public SimpleFactory(T red, T blue, T redCopy) { public Tuple createValues( TypeTag tag, VintageValueProvider valueProvider, - LinkedHashSet typeStack + Attributes attributes ) { return tuple; } diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/SimpleGenericFactory.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/SimpleGenericFactory.java index 748f64f7f..785e2e5cb 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/SimpleGenericFactory.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/SimpleGenericFactory.java @@ -1,12 +1,12 @@ package nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories; import java.util.ArrayList; -import java.util.LinkedHashSet; import java.util.List; import java.util.function.Supplier; import nl.jqno.equalsverifier.Func; import nl.jqno.equalsverifier.internal.reflection.Tuple; import nl.jqno.equalsverifier.internal.reflection.TypeTag; +import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes; import nl.jqno.equalsverifier.internal.reflection.instantiation.VintageValueProvider; public class SimpleGenericFactory extends AbstractGenericFactory { @@ -23,9 +23,9 @@ public SimpleGenericFactory(Func factory, Supplier emptyFactory) { public Tuple createValues( TypeTag tag, VintageValueProvider valueProvider, - LinkedHashSet typeStack + Attributes attributes ) { - LinkedHashSet clone = cloneWith(typeStack, tag); + Attributes clone = attributes.cloneAndAdd(tag); List redValues = new ArrayList<>(); List blueValues = new ArrayList<>(); diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factoryproviders/GuavaFactoryProvider.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factoryproviders/GuavaFactoryProvider.java index cf571ca2f..317e55259 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factoryproviders/GuavaFactoryProvider.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factoryproviders/GuavaFactoryProvider.java @@ -8,6 +8,7 @@ import java.util.function.Supplier; import nl.jqno.equalsverifier.internal.reflection.Tuple; import nl.jqno.equalsverifier.internal.reflection.TypeTag; +import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes; import nl.jqno.equalsverifier.internal.reflection.instantiation.VintageValueProvider; import nl.jqno.equalsverifier.internal.reflection.vintage.FactoryCache; import nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.AbstractGenericFactory; @@ -160,9 +161,9 @@ private MultimapFactory(Supplier factory) { public Tuple createValues( TypeTag tag, VintageValueProvider valueProvider, - LinkedHashSet typeStack + Attributes attributes ) { - LinkedHashSet clone = cloneWith(typeStack, tag); + Attributes clone = attributes.cloneAndAdd(tag); TypeTag keyTag = determineAndCacheActualTypeTag(0, tag, valueProvider, clone); TypeTag valueTag = determineAndCacheActualTypeTag(1, tag, valueProvider, clone); @@ -190,9 +191,9 @@ private TableFactory(Supplier factory) { public Tuple createValues( TypeTag tag, VintageValueProvider valueProvider, - LinkedHashSet typeStack + Attributes attributes ) { - LinkedHashSet clone = cloneWith(typeStack, tag); + Attributes clone = attributes.cloneAndAdd(tag); TypeTag columnTag = determineAndCacheActualTypeTag(0, tag, valueProvider, clone); TypeTag rowTag = determineAndCacheActualTypeTag(1, tag, valueProvider, clone); TypeTag valueTag = determineAndCacheActualTypeTag(2, tag, valueProvider, clone); diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factoryproviders/JavaFxFactoryProvider.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factoryproviders/JavaFxFactoryProvider.java index 9d6496d4c..1693bc08b 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factoryproviders/JavaFxFactoryProvider.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factoryproviders/JavaFxFactoryProvider.java @@ -8,6 +8,7 @@ import java.util.*; import java.util.function.Function; import nl.jqno.equalsverifier.internal.reflection.*; +import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes; import nl.jqno.equalsverifier.internal.reflection.instantiation.VintageValueProvider; import nl.jqno.equalsverifier.internal.reflection.vintage.FactoryCache; import nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.AbstractGenericFactory; @@ -109,7 +110,7 @@ static final class PropertyFactory extends AbstractGenericFactory { public Tuple createValues( TypeTag tag, VintageValueProvider valueProvider, - LinkedHashSet typeStack + Attributes attributes ) { ConditionalInstantiator ci = new ConditionalInstantiator(fullyQualifiedTypeName); TypeTag singleParameterTag = copyGenericTypesInto(parameterRawType, tag); diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/testhelpers/TestValueProviders.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/testhelpers/TestValueProviders.java index 7c0a56187..915790688 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/testhelpers/TestValueProviders.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/testhelpers/TestValueProviders.java @@ -20,7 +20,7 @@ private TestValueProviders() {} public static ValueProvider empty() { return new ValueProvider() { @Override - public Optional> provide(TypeTag tag, String label) { + public Optional> provide(TypeTag tag, Attributes attributes) { return Optional.empty(); } }; @@ -30,7 +30,7 @@ public static ValueProvider simple() { return new ValueProvider() { @SuppressWarnings("unchecked") @Override - public Optional> provide(TypeTag tag, String label) { + public Optional> provide(TypeTag tag, Attributes attributes) { if (tag.getType().equals(int.class)) { return Optional.of((Tuple) INTS); } diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/ChainedValueProviderTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/ChainedValueProviderTest.java index c4c092904..9f092f438 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/ChainedValueProviderTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/ChainedValueProviderTest.java @@ -2,16 +2,18 @@ import static org.junit.jupiter.api.Assertions.assertEquals; -import java.util.*; +import java.util.Optional; import nl.jqno.equalsverifier.internal.exceptions.EqualsVerifierInternalBugException; import nl.jqno.equalsverifier.internal.exceptions.NoValueException; import nl.jqno.equalsverifier.internal.reflection.Tuple; import nl.jqno.equalsverifier.internal.reflection.TypeTag; +import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes; import nl.jqno.equalsverifier.internal.testhelpers.ExpectedException; import org.junit.jupiter.api.Test; public class ChainedValueProviderTest { + private static final Attributes EMPTY_ATTRIBUTES = Attributes.unlabeled(); private static final TypeTag INT = new TypeTag(int.class); private final SingleTypeValueProvider intProvider = new SingleTypeValueProvider<>( @@ -33,20 +35,20 @@ public class ChainedValueProviderTest { @Test public void returnsValueIfMatch() { sut.register(intProvider); - assertEquals(1, sut.provideOrThrow(INT, null).getRed()); + assertEquals(1, sut.provideOrThrow(INT, EMPTY_ATTRIBUTES).getRed()); } @Test public void returnsEmptyIfNoMatch() { sut.register(stringProvider); - assertEquals(Optional.empty(), sut.provide(INT, null)); + assertEquals(Optional.empty(), sut.provide(INT, EMPTY_ATTRIBUTES)); } @Test public void throwsExceptionIfNoMatch() { sut.register(stringProvider); ExpectedException - .when(() -> sut.provideOrThrow(INT, null)) + .when(() -> sut.provideOrThrow(INT, EMPTY_ATTRIBUTES)) .assertThrows(NoValueException.class) .assertDescriptionContains("Could not find a value for int"); } @@ -54,7 +56,7 @@ public void throwsExceptionIfNoMatch() { @Test public void skipsNonMatchingValue() { sut.register(stringProvider, intProvider); - assertEquals(1, sut.provideOrThrow(INT, null).getRed()); + assertEquals(1, sut.provideOrThrow(INT, EMPTY_ATTRIBUTES).getRed()); assertEquals(1, stringProvider.called); assertEquals(1, intProvider.called); } @@ -68,7 +70,7 @@ public void returnsValueFromFirstMatch() { 1 ); sut.register(intProvider, anotherIntProvider); - assertEquals(1, sut.provideOrThrow(INT, null).getRed()); + assertEquals(1, sut.provideOrThrow(INT, EMPTY_ATTRIBUTES).getRed()); assertEquals(1, intProvider.called); assertEquals(0, anotherIntProvider.called); } @@ -76,9 +78,9 @@ public void returnsValueFromFirstMatch() { @Test public void updatesTheCache() { sut.register(intProvider); - sut.provideOrThrow(INT, null); + sut.provideOrThrow(INT, EMPTY_ATTRIBUTES); - assertEquals(1, cache.provideOrThrow(INT, null).getRed()); + assertEquals(1, cache.provideOrThrow(INT, EMPTY_ATTRIBUTES).getRed()); } @Test @@ -86,7 +88,7 @@ public void makesDefensiveCopy() { ValueProvider[] providers = { stringProvider }; sut.register(providers); providers[0] = intProvider; - assertEquals(Optional.empty(), sut.provide(INT, null)); + assertEquals(Optional.empty(), sut.provide(INT, EMPTY_ATTRIBUTES)); } @Test @@ -111,7 +113,7 @@ public SingleTypeValueProvider(Class type, X red, X blue, X redCopy) { @Override @SuppressWarnings("unchecked") - public Optional> provide(TypeTag tag, String label) { + public Optional> provide(TypeTag tag, Attributes attributes) { called++; if (tag.getType().equals(type)) { return Optional.of((Tuple) values); diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/SubjectCreatorTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/SubjectCreatorTest.java index ee51ae639..e99595234 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/SubjectCreatorTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/SubjectCreatorTest.java @@ -201,7 +201,7 @@ public void noValueFound() { static class SubjectCreatorTestValueProvider implements ValueProvider { - public Optional> provide(TypeTag tag, String label) { + public Optional> provide(TypeTag tag, Attributes attributes) { if (int.class.equals(tag.getType())) { return Optional.of(Tuple.of(I_RED, I_BLUE, I_RED)); } @@ -215,7 +215,7 @@ public Optional> provide(TypeTag tag, String label) { static class NoValueProvider implements ValueProvider { @Override - public Optional> provide(TypeTag tag, String label) { + public Optional> provide(TypeTag tag, Attributes attributes) { return Optional.empty(); } } diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/VintageValueProviderTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/VintageValueProviderTest.java index 092b1beee..57d4f42f6 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/VintageValueProviderTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/VintageValueProviderTest.java @@ -8,10 +8,10 @@ import static org.junit.jupiter.api.Assertions.fail; import java.util.ArrayList; -import java.util.LinkedHashSet; import java.util.List; import nl.jqno.equalsverifier.internal.reflection.Tuple; import nl.jqno.equalsverifier.internal.reflection.TypeTag; +import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes; import nl.jqno.equalsverifier.internal.reflection.vintage.FactoryCache; import nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.PrefabValueFactory; import nl.jqno.equalsverifier.internal.testhelpers.TestValueProviders; @@ -50,7 +50,7 @@ public void sanityTestFactoryIncreasesStringLength() { @Test public void provide() { - Tuple actual = vp.provideOrThrow(POINT_TAG, null); + Tuple actual = vp.provideOrThrow(POINT_TAG, Attributes.unlabeled()); assertEquals(Tuple.of(new Point(42, 42), new Point(1337, 1337), new Point(42, 42)), actual); } @@ -208,7 +208,7 @@ public AppendingStringTestFactory() { public Tuple createValues( TypeTag tag, VintageValueProvider valueProvider, - LinkedHashSet typeStack + Attributes attributes ) { red += "r"; blue += "b"; @@ -224,7 +224,7 @@ private static final class ListTestFactory implements PrefabValueFactory { public Tuple createValues( TypeTag tag, VintageValueProvider valueProvider, - LinkedHashSet typeStack + Attributes attributes ) { TypeTag subtag = tag.getGenericTypes().get(0); diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/ClassAccessorTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/ClassAccessorTest.java index 13e56d293..ab5c63656 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/ClassAccessorTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/ClassAccessorTest.java @@ -5,10 +5,10 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; -import java.util.LinkedHashSet; import nl.jqno.equalsverifier.internal.reflection.JavaApiPrefabValues; import nl.jqno.equalsverifier.internal.reflection.TypeTag; import nl.jqno.equalsverifier.internal.reflection.instantiation.CachedValueProvider; +import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes; import nl.jqno.equalsverifier.internal.reflection.instantiation.VintageValueProvider; import nl.jqno.equalsverifier.internal.testhelpers.TestValueProviders; import nl.jqno.equalsverifier.testhelpers.types.PointContainer; @@ -22,7 +22,7 @@ public class ClassAccessorTest { - private LinkedHashSet empty; + private Attributes empty; private Objenesis objenesis; private CachedValueProvider cache; private FactoryCache factoryCache; @@ -31,7 +31,7 @@ public class ClassAccessorTest { @BeforeEach public void setup() { - empty = new LinkedHashSet<>(); + empty = Attributes.unlabeled(); objenesis = new ObjenesisStd(); cache = new CachedValueProvider(); factoryCache = JavaApiPrefabValues.build(); diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/InPlaceObjectAccessorScramblingTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/InPlaceObjectAccessorScramblingTest.java index e59ab0728..0f997ed47 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/InPlaceObjectAccessorScramblingTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/InPlaceObjectAccessorScramblingTest.java @@ -5,12 +5,12 @@ import java.text.AttributedString; import java.util.ArrayList; -import java.util.LinkedHashSet; import java.util.List; import nl.jqno.equalsverifier.internal.exceptions.ModuleException; import nl.jqno.equalsverifier.internal.reflection.JavaApiPrefabValues; import nl.jqno.equalsverifier.internal.reflection.TypeTag; import nl.jqno.equalsverifier.internal.reflection.instantiation.CachedValueProvider; +import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes; import nl.jqno.equalsverifier.internal.reflection.instantiation.VintageValueProvider; import nl.jqno.equalsverifier.internal.testhelpers.ExpectedException; import nl.jqno.equalsverifier.internal.testhelpers.TestValueProviders; @@ -26,7 +26,7 @@ public class InPlaceObjectAccessorScramblingTest { - private static final LinkedHashSet EMPTY_TYPE_STACK = new LinkedHashSet<>(); + private static final Attributes EMPTY_ATTRIBUTES = Attributes.unlabeled(); private Objenesis objenesis; private VintageValueProvider valueProviderTest; @@ -161,7 +161,7 @@ private T copy(T object) { } private ObjectAccessor doScramble(Object object) { - return create(object).scramble(valueProviderTest, TypeTag.NULL, EMPTY_TYPE_STACK); + return create(object).scramble(valueProviderTest, TypeTag.NULL, EMPTY_ATTRIBUTES); } static final class StringContainer { diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/AbstractGenericFactoryTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/AbstractGenericFactoryTest.java index e0a2263fd..f480f042a 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/AbstractGenericFactoryTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/AbstractGenericFactoryTest.java @@ -4,10 +4,10 @@ import static nl.jqno.equalsverifier.internal.reflection.Util.objects; import static org.junit.jupiter.api.Assertions.assertThrows; -import java.util.LinkedHashSet; import nl.jqno.equalsverifier.internal.exceptions.ReflectionException; import nl.jqno.equalsverifier.internal.reflection.Tuple; import nl.jqno.equalsverifier.internal.reflection.TypeTag; +import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes; import nl.jqno.equalsverifier.internal.reflection.instantiation.VintageValueProvider; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -26,7 +26,7 @@ public void setUp() { public Tuple createValues( TypeTag tag, VintageValueProvider valueProvider, - LinkedHashSet typeStack + Attributes attributes ) { return Tuple.of("red", "blue", new String("red")); } diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/FallbackFactoryTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/FallbackFactoryTest.java index 606e7fbbd..79b553380 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/FallbackFactoryTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/FallbackFactoryTest.java @@ -6,11 +6,11 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; -import java.util.LinkedHashSet; import nl.jqno.equalsverifier.internal.exceptions.RecursionException; import nl.jqno.equalsverifier.internal.reflection.Tuple; import nl.jqno.equalsverifier.internal.reflection.TypeTag; import nl.jqno.equalsverifier.internal.reflection.instantiation.CachedValueProvider; +import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes; import nl.jqno.equalsverifier.internal.reflection.instantiation.VintageValueProvider; import nl.jqno.equalsverifier.internal.reflection.vintage.FactoryCache; import nl.jqno.equalsverifier.internal.testhelpers.ExpectedException; @@ -30,7 +30,7 @@ public class FallbackFactoryTest { private FallbackFactory factory; private VintageValueProvider valueProvider; - private LinkedHashSet typeStack; + private Attributes attributes; @BeforeEach public void setUp() { @@ -41,7 +41,7 @@ public void setUp() { factoryCache.put(int.class, values(42, 1337, 42)); valueProvider = new VintageValueProvider(TestValueProviders.empty(), cache, factoryCache, objenesis); - typeStack = new LinkedHashSet<>(); + attributes = Attributes.unlabeled(); } @Test @@ -61,7 +61,7 @@ public void giveMultiElementEnum() { @Test public void giveArray() { - Tuple tuple = factory.createValues(new TypeTag(int[].class), valueProvider, typeStack); + Tuple tuple = factory.createValues(new TypeTag(int[].class), valueProvider, attributes); assertArrayEquals(new int[] { 42 }, (int[]) tuple.getRed()); assertArrayEquals(new int[] { 1337 }, (int[]) tuple.getBlue()); } @@ -81,7 +81,7 @@ public void giveClassWithFields() { @Test public void dontGiveRecursiveClass() { ExpectedException - .when(() -> factory.createValues(new TypeTag(Node.class), valueProvider, typeStack)) + .when(() -> factory.createValues(new TypeTag(Node.class), valueProvider, attributes)) .assertThrows(RecursionException.class); } @@ -89,7 +89,7 @@ public void dontGiveRecursiveClass() { public void dontGiveTwoStepRecursiveClass() { ExpectedException .when(() -> - factory.createValues(new TypeTag(TwoStepNodeA.class), valueProvider, typeStack) + factory.createValues(new TypeTag(TwoStepNodeA.class), valueProvider, attributes) ) .assertThrows(RecursionException.class) .assertDescriptionContains("TwoStepNodeA", "TwoStepNodeB"); @@ -98,13 +98,14 @@ public void dontGiveTwoStepRecursiveClass() { @Test public void dontGiveRecursiveArray() { ExpectedException - .when(() -> factory.createValues(new TypeTag(NodeArray.class), valueProvider, typeStack) + .when(() -> + factory.createValues(new TypeTag(NodeArray.class), valueProvider, attributes) ) .assertThrows(RecursionException.class); } private void assertCorrectTuple(Class type, T expectedRed, T expectedBlue) { - Tuple tuple = factory.createValues(new TypeTag(type), valueProvider, typeStack); + Tuple tuple = factory.createValues(new TypeTag(type), valueProvider, attributes); assertEquals(expectedRed, tuple.getRed()); assertEquals(expectedBlue, tuple.getBlue()); } diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/MapFactoryTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/MapFactoryTest.java index eea125d5e..677a949bb 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/MapFactoryTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/MapFactoryTest.java @@ -3,12 +3,12 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.HashMap; -import java.util.LinkedHashSet; import java.util.Map; import nl.jqno.equalsverifier.internal.reflection.JavaApiPrefabValues; import nl.jqno.equalsverifier.internal.reflection.Tuple; import nl.jqno.equalsverifier.internal.reflection.TypeTag; import nl.jqno.equalsverifier.internal.reflection.instantiation.CachedValueProvider; +import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes; import nl.jqno.equalsverifier.internal.reflection.instantiation.VintageValueProvider; import nl.jqno.equalsverifier.internal.testhelpers.TestValueProviders; import nl.jqno.equalsverifier.testhelpers.types.TypeHelper.OneElementEnum; @@ -41,7 +41,7 @@ public class MapFactoryTest { private static final MapFactory MAP_FACTORY = new MapFactory<>(HashMap::new); - private final LinkedHashSet typeStack = new LinkedHashSet<>(); + private final Attributes attributes = Attributes.unlabeled(); private VintageValueProvider valueProvider; private String red; private String blue; @@ -70,7 +70,7 @@ public void createMapsOfStringToString() { Tuple tuple = MAP_FACTORY.createValues( STRINGSTRINGMAP_TYPETAG, valueProvider, - typeStack + attributes ); assertEquals(mapOf(red, blue), tuple.getRed()); assertEquals(mapOf(blue, blue), tuple.getBlue()); @@ -78,14 +78,14 @@ public void createMapsOfStringToString() { @Test public void createMapsOfWildcard() { - Tuple tuple = MAP_FACTORY.createValues(WILDCARDMAP_TYPETAG, valueProvider, typeStack); + Tuple tuple = MAP_FACTORY.createValues(WILDCARDMAP_TYPETAG, valueProvider, attributes); assertEquals(mapOf(redObject, blueObject), tuple.getRed()); assertEquals(mapOf(blueObject, blueObject), tuple.getBlue()); } @Test public void createRawMaps() { - Tuple tuple = MAP_FACTORY.createValues(RAWMAP_TYPETAG, valueProvider, typeStack); + Tuple tuple = MAP_FACTORY.createValues(RAWMAP_TYPETAG, valueProvider, attributes); assertEquals(mapOf(redObject, blueObject), tuple.getRed()); assertEquals(mapOf(blueObject, blueObject), tuple.getBlue()); } @@ -95,7 +95,7 @@ public void createMapOfOneElementEnumKey() { Tuple tuple = MAP_FACTORY.createValues( ONEELEMENTENUMKEYMAP_TYPETAG, valueProvider, - typeStack + attributes ); assertEquals(mapOf(redEnum, blueObject), tuple.getRed()); assertEquals(new HashMap<>(), tuple.getBlue()); diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/SimpleGenericFactoryTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/SimpleGenericFactoryTest.java index cfa9a3137..fe9af463e 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/SimpleGenericFactoryTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/SimpleGenericFactoryTest.java @@ -2,12 +2,12 @@ import static org.junit.jupiter.api.Assertions.assertEquals; -import java.util.LinkedHashSet; import java.util.Optional; import nl.jqno.equalsverifier.internal.reflection.JavaApiPrefabValues; import nl.jqno.equalsverifier.internal.reflection.Tuple; import nl.jqno.equalsverifier.internal.reflection.TypeTag; import nl.jqno.equalsverifier.internal.reflection.instantiation.CachedValueProvider; +import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes; import nl.jqno.equalsverifier.internal.reflection.instantiation.VintageValueProvider; import nl.jqno.equalsverifier.internal.testhelpers.TestValueProviders; import nl.jqno.equalsverifier.testhelpers.types.Pair; @@ -42,7 +42,7 @@ public class SimpleGenericFactoryTest { ); private static final PrefabValueFactory PAIR_FACTORY = Factories.simple(Pair::new, null); - private final LinkedHashSet typeStack = new LinkedHashSet<>(); + private final Attributes attributes = Attributes.unlabeled(); private VintageValueProvider valueProvider; private String redString; private String blueString; @@ -73,7 +73,7 @@ public void createOptionalsOfMapOfString() { Tuple tuple = OPTIONAL_FACTORY.createValues( STRINGOPTIONAL_TYPETAG, valueProvider, - typeStack + attributes ); assertEquals(Optional.of(redString), tuple.getRed()); assertEquals(Optional.of(blueString), tuple.getBlue()); @@ -84,7 +84,7 @@ public void createOptionalsOfWildcard() { Tuple tuple = OPTIONAL_FACTORY.createValues( WILDCARDOPTIONAL_TYPETAG, valueProvider, - typeStack + attributes ); assertEquals(Optional.of(redObject), tuple.getRed()); assertEquals(Optional.of(blueObject), tuple.getBlue()); @@ -95,7 +95,7 @@ public void createRawOptionals() { Tuple tuple = OPTIONAL_FACTORY.createValues( RAWOPTIONAL_TYPETAG, valueProvider, - typeStack + attributes ); assertEquals(Optional.of(redObject), tuple.getRed()); assertEquals(Optional.of(blueObject), tuple.getBlue()); @@ -103,7 +103,7 @@ public void createRawOptionals() { @Test public void createSomethingWithMoreThanOneTypeParameter() { - Tuple tuple = PAIR_FACTORY.createValues(PAIR_TYPETAG, valueProvider, typeStack); + Tuple tuple = PAIR_FACTORY.createValues(PAIR_TYPETAG, valueProvider, attributes); assertEquals(new Pair<>(redString, redInt), tuple.getRed()); assertEquals(new Pair<>(blueString, blueInt), tuple.getBlue()); }