diff --git a/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/RecordInstanceCreatorTest.java b/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/instantiation/RecordInstanceCreatorTest.java similarity index 93% rename from equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/RecordInstanceCreatorTest.java rename to equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/instantiation/RecordInstanceCreatorTest.java index 8bced9fd4..69a0ea94a 100644 --- a/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/RecordInstanceCreatorTest.java +++ b/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/instantiation/RecordInstanceCreatorTest.java @@ -1,4 +1,4 @@ -package nl.jqno.equalsverifier.internal.reflection.instantiation; +package nl.jqno.equalsverifier.internal.instantiation; import static org.junit.jupiter.api.Assertions.assertEquals; diff --git a/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/RecordFallbackFactoryTest.java b/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factories/RecordFallbackFactoryTest.java similarity index 61% rename from equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/RecordFallbackFactoryTest.java rename to equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factories/RecordFallbackFactoryTest.java index 5c2a62dea..4b003c695 100644 --- a/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/RecordFallbackFactoryTest.java +++ b/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factories/RecordFallbackFactoryTest.java @@ -1,15 +1,14 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage; +package nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories; -import static nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.Factories.values; +import static nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories.Factories.values; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotSame; +import java.util.LinkedHashSet; +import nl.jqno.equalsverifier.internal.instantiation.vintage.FactoryCache; +import nl.jqno.equalsverifier.internal.instantiation.vintage.VintageValueProvider; 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.vintage.prefabvalues.factories.FallbackFactory; -import nl.jqno.equalsverifier.internal.testhelpers.TestValueProviders; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.objenesis.Objenesis; @@ -19,18 +18,16 @@ public class RecordFallbackFactoryTest { private FallbackFactory factory; private VintageValueProvider valueProvider; - private Attributes attributes; + private LinkedHashSet typeStack; @BeforeEach public void setUp() { Objenesis objenesis = new ObjenesisStd(); factory = new FallbackFactory<>(objenesis); - CachedValueProvider cache = new CachedValueProvider(); FactoryCache factoryCache = new FactoryCache(); factoryCache.put(int.class, values(42, 1337, 42)); - valueProvider = - new VintageValueProvider(TestValueProviders.empty(), cache, factoryCache, objenesis); - attributes = Attributes.unlabeled(); + valueProvider = new VintageValueProvider(factoryCache, objenesis); + typeStack = new LinkedHashSet<>(); } @Test @@ -38,7 +35,7 @@ public void redCopyHasTheSameValuesAsRed_whenSutContainsGenericValueThatNeedsToB Tuple tuple = factory.createValues( new TypeTag(GenericRecordContainer.class), valueProvider, - attributes + typeStack ); assertEquals(tuple.getRed(), tuple.getRedCopy()); diff --git a/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/RecordObjectAccessorCopyingTest.java b/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/RecordObjectAccessorCopyingTest.java similarity index 96% rename from equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/RecordObjectAccessorCopyingTest.java rename to equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/RecordObjectAccessorCopyingTest.java index cbcc3c17a..09720656a 100644 --- a/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/RecordObjectAccessorCopyingTest.java +++ b/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/RecordObjectAccessorCopyingTest.java @@ -1,4 +1,4 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage.mutation; +package nl.jqno.equalsverifier.internal.instantiation.vintage.reflection; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotSame; diff --git a/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/RecordObjectAccessorScramblingTest.java b/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/RecordObjectAccessorScramblingTest.java similarity index 67% rename from equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/RecordObjectAccessorScramblingTest.java rename to equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/RecordObjectAccessorScramblingTest.java index 7c9a42732..171b5dcc9 100644 --- a/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/RecordObjectAccessorScramblingTest.java +++ b/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/RecordObjectAccessorScramblingTest.java @@ -1,39 +1,29 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage.mutation; +package nl.jqno.equalsverifier.internal.instantiation.vintage.reflection; -import static nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.Factories.values; +import static nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories.Factories.values; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotSame; import java.lang.reflect.Constructor; -import nl.jqno.equalsverifier.internal.reflection.JavaApiPrefabValues; +import java.util.LinkedHashSet; +import nl.jqno.equalsverifier.internal.instantiation.JavaApiPrefabValues; +import nl.jqno.equalsverifier.internal.instantiation.vintage.FactoryCache; +import nl.jqno.equalsverifier.internal.instantiation.vintage.VintageValueProvider; 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.vintage.FactoryCache; -import nl.jqno.equalsverifier.internal.reflection.vintage.VintageValueProvider; -import nl.jqno.equalsverifier.internal.testhelpers.TestValueProviders; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.objenesis.ObjenesisStd; public class RecordObjectAccessorScramblingTest { - private static final Attributes EMPTY_ATTRIBUTES = Attributes.unlabeled(); - private CachedValueProvider cache; + private static final LinkedHashSet EMPTY_TYPE_STACK = new LinkedHashSet<>(); private FactoryCache factoryCache; private VintageValueProvider valueProvider; @BeforeEach public void setup() throws Exception { - cache = new CachedValueProvider(); factoryCache = JavaApiPrefabValues.build(); - valueProvider = - new VintageValueProvider( - TestValueProviders.empty(), - cache, - factoryCache, - new ObjenesisStd() - ); + valueProvider = new VintageValueProvider(factoryCache, new ObjenesisStd()); } @Test @@ -67,7 +57,7 @@ private RecordObjectAccessor create(T object) { } private ObjectAccessor doScramble(Object object) { - return create(object).scramble(valueProvider, TypeTag.NULL, EMPTY_ATTRIBUTES); + return create(object).scramble(valueProvider, TypeTag.NULL, EMPTY_TYPE_STACK); } record Point(int x, int y) {} diff --git a/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/RecordObjectAccessorTest.java b/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/RecordObjectAccessorTest.java similarity index 81% rename from equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/RecordObjectAccessorTest.java rename to equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/RecordObjectAccessorTest.java index 83a5d6333..6934991b4 100644 --- a/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/RecordObjectAccessorTest.java +++ b/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/RecordObjectAccessorTest.java @@ -1,19 +1,17 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage.mutation; +package nl.jqno.equalsverifier.internal.instantiation.vintage.reflection; import static org.junit.jupiter.api.Assertions.assertSame; 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.instantiation.JavaApiPrefabValues; +import nl.jqno.equalsverifier.internal.instantiation.vintage.VintageValueProvider; 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.vintage.VintageValueProvider; import nl.jqno.equalsverifier.internal.testhelpers.ExpectedException; -import nl.jqno.equalsverifier.internal.testhelpers.TestValueProviders; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.objenesis.Objenesis; @@ -21,7 +19,7 @@ public class RecordObjectAccessorTest { - private static final Attributes EMPTY_ATTRIBUTES = Attributes.unlabeled(); + private static final LinkedHashSet EMPTY_TYPE_STACK = new LinkedHashSet<>(); private Objenesis objenesis; private Object recordInstance; @@ -76,14 +74,9 @@ public void fail_whenConstructorThrowsOnSomethingElse() { .of(OtherThrowingConstructorRecord.class, objenesis) .instantiate(); - VintageValueProvider vp = new VintageValueProvider( - TestValueProviders.empty(), - new CachedValueProvider(), - JavaApiPrefabValues.build(), - objenesis - ); + VintageValueProvider vp = new VintageValueProvider(JavaApiPrefabValues.build(), objenesis); ExpectedException - .when(() -> accessorFor(instance).scramble(vp, TypeTag.NULL, EMPTY_ATTRIBUTES)) + .when(() -> accessorFor(instance).scramble(vp, TypeTag.NULL, EMPTY_TYPE_STACK)) .assertThrows(ReflectionException.class) .assertMessageContains("Record:", "failed to run constructor", "prefab values"); } diff --git a/equalsverifier-17/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/SealedTypesFallbackFactoryTest.java b/equalsverifier-17/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factories/SealedTypesFallbackFactoryTest.java similarity index 73% rename from equalsverifier-17/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/SealedTypesFallbackFactoryTest.java rename to equalsverifier-17/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factories/SealedTypesFallbackFactoryTest.java index afc7eb192..88b60c153 100644 --- a/equalsverifier-17/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/SealedTypesFallbackFactoryTest.java +++ b/equalsverifier-17/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factories/SealedTypesFallbackFactoryTest.java @@ -1,16 +1,15 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage; +package nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories; -import static nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.Factories.values; +import static nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories.Factories.values; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotSame; +import java.util.LinkedHashSet; import java.util.Objects; +import nl.jqno.equalsverifier.internal.instantiation.vintage.FactoryCache; +import nl.jqno.equalsverifier.internal.instantiation.vintage.VintageValueProvider; 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.vintage.prefabvalues.factories.FallbackFactory; -import nl.jqno.equalsverifier.internal.testhelpers.TestValueProviders; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.objenesis.Objenesis; @@ -20,18 +19,16 @@ public class SealedTypesFallbackFactoryTest { private FallbackFactory factory; private VintageValueProvider valueProvider; - private Attributes attributes; + private LinkedHashSet typeStack; @BeforeEach public void setUp() { Objenesis objenesis = new ObjenesisStd(); factory = new FallbackFactory<>(objenesis); - CachedValueProvider cache = new CachedValueProvider(); FactoryCache factoryCache = new FactoryCache(); factoryCache.put(int.class, values(42, 1337, 42)); - valueProvider = - new VintageValueProvider(TestValueProviders.empty(), cache, factoryCache, objenesis); - attributes = Attributes.unlabeled(); + valueProvider = new VintageValueProvider(factoryCache, objenesis); + typeStack = new LinkedHashSet<>(); } @Test @@ -39,7 +36,7 @@ public void redCopyHasTheSameValuesAsRed_whenSutIsAbstractSealedAndPermittedType Tuple tuple = factory.createValues( new TypeTag(SealedParentWithFinalChild.class), valueProvider, - attributes + typeStack ); assertEquals(tuple.getRed(), tuple.getRedCopy()); diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/ConfiguredEqualsVerifier.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/ConfiguredEqualsVerifier.java index 8e9aa2e57..6d3ee7001 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/ConfiguredEqualsVerifier.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/ConfiguredEqualsVerifier.java @@ -9,9 +9,9 @@ import nl.jqno.equalsverifier.api.EqualsVerifierApi; import nl.jqno.equalsverifier.api.MultipleTypeEqualsVerifierApi; import nl.jqno.equalsverifier.api.SingleTypeEqualsVerifierApi; +import nl.jqno.equalsverifier.internal.instantiation.vintage.FactoryCache; +import nl.jqno.equalsverifier.internal.instantiation.vintage.PrefabValuesApi; import nl.jqno.equalsverifier.internal.reflection.PackageScanner; -import nl.jqno.equalsverifier.internal.reflection.vintage.FactoryCache; -import nl.jqno.equalsverifier.internal.reflection.vintage.PrefabValuesApi; import nl.jqno.equalsverifier.internal.util.ListBuilders; import nl.jqno.equalsverifier.internal.util.Validations; import org.objenesis.Objenesis; diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/api/SingleTypeEqualsVerifierApi.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/api/SingleTypeEqualsVerifierApi.java index 60b05d151..e87acdac8 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/api/SingleTypeEqualsVerifierApi.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/api/SingleTypeEqualsVerifierApi.java @@ -7,11 +7,11 @@ import nl.jqno.equalsverifier.Func.Func1; import nl.jqno.equalsverifier.Func.Func2; import nl.jqno.equalsverifier.Warning; -import nl.jqno.equalsverifier.internal.SuppressFBWarnings; import nl.jqno.equalsverifier.internal.checkers.*; import nl.jqno.equalsverifier.internal.exceptions.MessagingException; -import nl.jqno.equalsverifier.internal.reflection.vintage.FactoryCache; -import nl.jqno.equalsverifier.internal.reflection.vintage.PrefabValuesApi; +import nl.jqno.equalsverifier.internal.instantiation.vintage.FactoryCache; +import nl.jqno.equalsverifier.internal.instantiation.vintage.PrefabValuesApi; +import nl.jqno.equalsverifier.internal.reflection.FieldCache; import nl.jqno.equalsverifier.internal.util.*; import nl.jqno.equalsverifier.internal.util.Formatter; import org.objenesis.Objenesis; @@ -32,6 +32,7 @@ public class SingleTypeEqualsVerifierApi implements EqualsVerifierApi { private boolean hasRedefinedSuperclass = false; private Class redefinedSubclass = null; private FactoryCache factoryCache = new FactoryCache(); + private FieldCache fieldCache = new FieldCache(); private CachedHashCodeInitializer cachedHashCodeInitializer = CachedHashCodeInitializer.passthrough(); private Function fieldnameToGetter = null; @@ -39,7 +40,6 @@ public class SingleTypeEqualsVerifierApi implements EqualsVerifierApi { private Set allIncludedFields = new HashSet<>(); private Set nonnullFields = new HashSet<>(); private Set ignoredAnnotationClassNames = new HashSet<>(); - private Set prefabbedFieldNames = new HashSet<>(); private List equalExamples = new ArrayList<>(); private List unequalExamples = new ArrayList<>(); private final Objenesis objenesis; @@ -76,10 +76,6 @@ public SingleTypeEqualsVerifierApi(Class type, Objenesis objenesis) { * equals} method, instead of an {@code instanceof} check. * @param converter A function that converts from field name to getter name. */ - @SuppressFBWarnings( - value = "EI_EXPOSE_REP2", - justification = "FactoryCache is inherently mutable" - ) public SingleTypeEqualsVerifierApi( Class type, EnumSet warningsToSuppress, @@ -90,7 +86,7 @@ public SingleTypeEqualsVerifierApi( ) { this(type, objenesis); this.warningsToSuppress = EnumSet.copyOf(warningsToSuppress); - this.factoryCache = factoryCache; + this.factoryCache = this.factoryCache.merge(factoryCache); this.usingGetClass = usingGetClass; this.fieldnameToGetter = converter; } @@ -148,15 +144,7 @@ public SingleTypeEqualsVerifierApi withPrefabValuesForField( S red, S blue ) { - PrefabValuesApi.addPrefabValuesForField( - factoryCache, - objenesis, - type, - fieldName, - red, - blue - ); - prefabbedFieldNames.add(fieldName); + PrefabValuesApi.addPrefabValuesForField(fieldCache, objenesis, type, fieldName, red, blue); withNonnullFields(fieldName); return this; } @@ -444,7 +432,7 @@ private void performVerification() { Validations.validateClassCanBeVerified(type); Configuration config = buildConfig(); - Context context = new Context<>(config, factoryCache, objenesis); + Context context = new Context<>(config, factoryCache, fieldCache, objenesis); Validations.validateProcessedAnnotations( type, config.getAnnotationCache(), @@ -463,7 +451,7 @@ private Configuration buildConfig() { allExcludedFields, allIncludedFields, nonnullFields, - prefabbedFieldNames, + fieldCache.getFieldNames(), cachedHashCodeInitializer, hasRedefinedSuperclass, redefinedSubclass, 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 30635bb1a..63b3cb93c 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 @@ -4,13 +4,9 @@ import java.lang.reflect.Field; import nl.jqno.equalsverifier.internal.SuppressFBWarnings; -import nl.jqno.equalsverifier.internal.reflection.ClassProbe; -import nl.jqno.equalsverifier.internal.reflection.FieldIterable; -import nl.jqno.equalsverifier.internal.reflection.Tuple; -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.instantiation.SubjectCreator; +import nl.jqno.equalsverifier.internal.instantiation.ValueProvider; +import nl.jqno.equalsverifier.internal.reflection.*; import nl.jqno.equalsverifier.internal.util.*; public class AbstractDelegationChecker implements Checker { @@ -64,7 +60,7 @@ private void checkAbstractEqualsAndHashCode() { private void checkAbstractDelegationInFields() { for (Field field : FieldIterable.of(type)) { TypeTag tag = TypeTag.of(field, typeTag); - Tuple tuple = safelyGetTuple(tag, field.getName()); + Tuple tuple = safelyGetTuple(tag); if (tuple != null) { Object instance = tuple.getRed(); Object copy = tuple.getBlue(); @@ -73,9 +69,9 @@ private void checkAbstractDelegationInFields() { } } - private Tuple safelyGetTuple(TypeTag tag, String fieldName) { + private Tuple safelyGetTuple(TypeTag tag) { try { - return valueProvider.provideOrThrow(tag, Attributes.labeled(fieldName)); + return valueProvider.provideOrThrow(tag); } 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/ExamplesChecker.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/ExamplesChecker.java index 3195e1be8..ba1e91f9f 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/ExamplesChecker.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/ExamplesChecker.java @@ -9,8 +9,8 @@ import java.util.Objects; import nl.jqno.equalsverifier.internal.SuppressFBWarnings; import nl.jqno.equalsverifier.internal.exceptions.AssertionException; +import nl.jqno.equalsverifier.internal.instantiation.SubjectCreator; import nl.jqno.equalsverifier.internal.reflection.FieldIterable; -import nl.jqno.equalsverifier.internal.reflection.instantiation.SubjectCreator; import nl.jqno.equalsverifier.internal.util.*; public class ExamplesChecker implements Checker { diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/FieldsChecker.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/FieldsChecker.java index 263519800..2aab34ba8 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/FieldsChecker.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/FieldsChecker.java @@ -3,11 +3,11 @@ import java.util.function.Predicate; import nl.jqno.equalsverifier.Warning; import nl.jqno.equalsverifier.internal.checkers.fieldchecks.*; +import nl.jqno.equalsverifier.internal.instantiation.SubjectCreator; import nl.jqno.equalsverifier.internal.reflection.FieldProbe; import nl.jqno.equalsverifier.internal.reflection.TypeTag; import nl.jqno.equalsverifier.internal.reflection.annotations.AnnotationCache; import nl.jqno.equalsverifier.internal.reflection.annotations.SupportedAnnotations; -import nl.jqno.equalsverifier.internal.reflection.instantiation.SubjectCreator; import nl.jqno.equalsverifier.internal.util.Configuration; import nl.jqno.equalsverifier.internal.util.Context; diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/HierarchyChecker.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/HierarchyChecker.java index fc7de55b4..1367ef57f 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/HierarchyChecker.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/HierarchyChecker.java @@ -7,10 +7,10 @@ import java.lang.reflect.Modifier; import nl.jqno.equalsverifier.Warning; import nl.jqno.equalsverifier.internal.SuppressFBWarnings; +import nl.jqno.equalsverifier.internal.instantiation.SubjectCreator; import nl.jqno.equalsverifier.internal.reflection.ClassProbe; import nl.jqno.equalsverifier.internal.reflection.Instantiator; import nl.jqno.equalsverifier.internal.reflection.annotations.SupportedAnnotations; -import nl.jqno.equalsverifier.internal.reflection.instantiation.SubjectCreator; import nl.jqno.equalsverifier.internal.util.*; public class HierarchyChecker implements Checker { 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 f9abe6f8c..7e219f02f 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 @@ -4,8 +4,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.instantiation.ValueProvider; import nl.jqno.equalsverifier.internal.util.Configuration; import nl.jqno.equalsverifier.internal.util.Context; import nl.jqno.equalsverifier.internal.util.Formatter; @@ -24,7 +23,7 @@ public MapEntryHashCodeRequirementChecker(Context context) { public void check() { if (Map.Entry.class.isAssignableFrom(config.getType())) { Map.Entry e = valueProvider - .>provideOrThrow(config.getTypeTag(), Attributes.unlabeled()) + .>provideOrThrow(config.getTypeTag()) .getRed(); int expectedHashCode = Objects.hashCode(e.getKey()) ^ Objects.hashCode(e.getValue()); diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/RecordChecker.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/RecordChecker.java index 30cd67409..1ac829845 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/RecordChecker.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/RecordChecker.java @@ -9,9 +9,9 @@ import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; +import nl.jqno.equalsverifier.internal.instantiation.SubjectCreator; import nl.jqno.equalsverifier.internal.reflection.ClassProbe; import nl.jqno.equalsverifier.internal.reflection.FieldIterable; -import nl.jqno.equalsverifier.internal.reflection.instantiation.SubjectCreator; import nl.jqno.equalsverifier.internal.util.Context; import nl.jqno.equalsverifier.internal.util.Formatter; diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/ArrayFieldCheck.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/ArrayFieldCheck.java index 506c37a19..c269b96d8 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/ArrayFieldCheck.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/ArrayFieldCheck.java @@ -3,8 +3,8 @@ import static nl.jqno.equalsverifier.internal.util.Assert.assertEquals; import java.lang.reflect.Array; +import nl.jqno.equalsverifier.internal.instantiation.SubjectCreator; import nl.jqno.equalsverifier.internal.reflection.FieldProbe; -import nl.jqno.equalsverifier.internal.reflection.instantiation.SubjectCreator; import nl.jqno.equalsverifier.internal.util.CachedHashCodeInitializer; import nl.jqno.equalsverifier.internal.util.Formatter; diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/BigDecimalFieldCheck.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/BigDecimalFieldCheck.java index d766321d8..9ec15ae89 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/BigDecimalFieldCheck.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/BigDecimalFieldCheck.java @@ -6,8 +6,8 @@ import java.math.BigDecimal; import java.math.RoundingMode; import nl.jqno.equalsverifier.Warning; +import nl.jqno.equalsverifier.internal.instantiation.SubjectCreator; import nl.jqno.equalsverifier.internal.reflection.FieldProbe; -import nl.jqno.equalsverifier.internal.reflection.instantiation.SubjectCreator; import nl.jqno.equalsverifier.internal.util.CachedHashCodeInitializer; import nl.jqno.equalsverifier.internal.util.Formatter; diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/FloatAndDoubleFieldCheck.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/FloatAndDoubleFieldCheck.java index 64841b5f5..a970b2e3f 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/FloatAndDoubleFieldCheck.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/FloatAndDoubleFieldCheck.java @@ -2,8 +2,8 @@ import static nl.jqno.equalsverifier.internal.util.Assert.assertEquals; +import nl.jqno.equalsverifier.internal.instantiation.SubjectCreator; import nl.jqno.equalsverifier.internal.reflection.FieldProbe; -import nl.jqno.equalsverifier.internal.reflection.instantiation.SubjectCreator; import nl.jqno.equalsverifier.internal.util.Formatter; public class FloatAndDoubleFieldCheck implements FieldCheck { 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 0ef963e88..1ed068764 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 @@ -9,12 +9,11 @@ import java.util.function.Function; import nl.jqno.equalsverifier.Warning; import nl.jqno.equalsverifier.internal.exceptions.EqualsVerifierInternalBugException; +import nl.jqno.equalsverifier.internal.instantiation.SubjectCreator; +import nl.jqno.equalsverifier.internal.instantiation.ValueProvider; import nl.jqno.equalsverifier.internal.reflection.*; import nl.jqno.equalsverifier.internal.reflection.annotations.AnnotationCache; 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; @@ -65,7 +64,7 @@ public void execute(FieldProbe fieldProbe) { ); TypeTag sub = new TypeTag(throwingGetterCreator(getterName)); - Tuple tuple = valueProvider.provideOrThrow(sub, Attributes.labeled(fieldName)); + Tuple tuple = valueProvider.provideOrThrow(sub); T red1 = tuple.getRed(); T red2 = tuple.getRedCopy(); diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/MutableStateFieldCheck.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/MutableStateFieldCheck.java index 13b0b576e..9d71e65ee 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/MutableStateFieldCheck.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/MutableStateFieldCheck.java @@ -3,8 +3,8 @@ import static nl.jqno.equalsverifier.internal.util.Assert.fail; import java.util.function.Predicate; +import nl.jqno.equalsverifier.internal.instantiation.SubjectCreator; import nl.jqno.equalsverifier.internal.reflection.FieldProbe; -import nl.jqno.equalsverifier.internal.reflection.instantiation.SubjectCreator; import nl.jqno.equalsverifier.internal.util.Formatter; public class MutableStateFieldCheck implements FieldCheck { diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/NullPointerExceptionFieldCheck.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/NullPointerExceptionFieldCheck.java index ce83a8c38..7219616f7 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/NullPointerExceptionFieldCheck.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/NullPointerExceptionFieldCheck.java @@ -4,9 +4,9 @@ import java.lang.reflect.Field; import nl.jqno.equalsverifier.internal.SuppressFBWarnings; +import nl.jqno.equalsverifier.internal.instantiation.SubjectCreator; import nl.jqno.equalsverifier.internal.reflection.FieldMutator; import nl.jqno.equalsverifier.internal.reflection.FieldProbe; -import nl.jqno.equalsverifier.internal.reflection.instantiation.SubjectCreator; import nl.jqno.equalsverifier.internal.util.*; @SuppressFBWarnings( 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 e3d3b25b1..9e28dea88 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 @@ -7,13 +7,11 @@ import java.util.EnumSet; import java.util.Set; import nl.jqno.equalsverifier.Warning; -import nl.jqno.equalsverifier.internal.exceptions.NoValueException; +import nl.jqno.equalsverifier.internal.instantiation.SubjectCreator; +import nl.jqno.equalsverifier.internal.instantiation.ValueProvider; import nl.jqno.equalsverifier.internal.reflection.*; import nl.jqno.equalsverifier.internal.reflection.annotations.AnnotationCache; 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; @@ -27,6 +25,7 @@ public class ReflexivityFieldCheck implements FieldCheck { private final Set nonnullFields; private final Set prefabbedFields; private final AnnotationCache annotationCache; + private final FieldCache fieldCache; public ReflexivityFieldCheck(Context context) { this.subjectCreator = context.getSubjectCreator(); @@ -38,6 +37,7 @@ public ReflexivityFieldCheck(Context context) { this.nonnullFields = config.getNonnullFields(); this.prefabbedFields = config.getPrefabbedFields(); this.annotationCache = config.getAnnotationCache(); + this.fieldCache = context.getFieldCache(); } @Override @@ -80,9 +80,9 @@ private void checkValueReflexivity(FieldProbe probe) { Field field = probe.getField(); String fieldName = field.getName(); TypeTag tag = TypeTag.of(field, typeTag); - Tuple tuple = valueProvider - .provide(tag, Attributes.labeled(fieldName)) - .orElseThrow(() -> new NoValueException(tag, fieldName)); + Tuple tuple = prefabbedFields.contains(fieldName) + ? fieldCache.get(fieldName) + : valueProvider.provideOrThrow(tag); Object left = subjectCreator.withFieldSetTo(field, tuple.getRed()); Object right = subjectCreator.withFieldSetTo(field, tuple.getRedCopy()); diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/SignificantFieldCheck.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/SignificantFieldCheck.java index c459965c7..19d530ea8 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/SignificantFieldCheck.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/SignificantFieldCheck.java @@ -7,10 +7,10 @@ import java.util.Set; import java.util.function.Predicate; import nl.jqno.equalsverifier.Warning; +import nl.jqno.equalsverifier.internal.instantiation.SubjectCreator; import nl.jqno.equalsverifier.internal.reflection.FieldProbe; import nl.jqno.equalsverifier.internal.reflection.annotations.AnnotationCache; import nl.jqno.equalsverifier.internal.reflection.annotations.SupportedAnnotations; -import nl.jqno.equalsverifier.internal.reflection.instantiation.SubjectCreator; import nl.jqno.equalsverifier.internal.util.*; public class SignificantFieldCheck implements FieldCheck { 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 aa3823061..0000feb2f 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 @@ -4,11 +4,10 @@ import nl.jqno.equalsverifier.internal.SuppressFBWarnings; import nl.jqno.equalsverifier.internal.exceptions.ReflectionException; +import nl.jqno.equalsverifier.internal.instantiation.SubjectCreator; +import nl.jqno.equalsverifier.internal.instantiation.ValueProvider; import nl.jqno.equalsverifier.internal.reflection.FieldProbe; 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,9 +41,7 @@ public StringFieldCheck( public void execute(FieldProbe fieldProbe) { if (String.class.equals(fieldProbe.getType()) && !fieldProbe.isStatic()) { TypeTag string = new TypeTag(String.class); - String red = valueProvider - .provideOrThrow(string, Attributes.labeled(fieldProbe.getName())) - .getRed(); + String red = valueProvider.provideOrThrow(string).getRed(); final T reference; final T copy; diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/SymmetryFieldCheck.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/SymmetryFieldCheck.java index cad26b78b..1bc2d67ad 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/SymmetryFieldCheck.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/SymmetryFieldCheck.java @@ -3,8 +3,8 @@ import static nl.jqno.equalsverifier.internal.util.Assert.assertTrue; import nl.jqno.equalsverifier.internal.SuppressFBWarnings; +import nl.jqno.equalsverifier.internal.instantiation.SubjectCreator; import nl.jqno.equalsverifier.internal.reflection.FieldProbe; -import nl.jqno.equalsverifier.internal.reflection.instantiation.SubjectCreator; import nl.jqno.equalsverifier.internal.util.Formatter; public class SymmetryFieldCheck implements FieldCheck { diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/TransientFieldsCheck.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/TransientFieldsCheck.java index c149c2ccc..a0f2c9c3b 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/TransientFieldsCheck.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/TransientFieldsCheck.java @@ -3,11 +3,11 @@ import static nl.jqno.equalsverifier.internal.util.Assert.fail; import nl.jqno.equalsverifier.internal.SuppressFBWarnings; +import nl.jqno.equalsverifier.internal.instantiation.SubjectCreator; import nl.jqno.equalsverifier.internal.reflection.FieldProbe; import nl.jqno.equalsverifier.internal.reflection.TypeTag; import nl.jqno.equalsverifier.internal.reflection.annotations.AnnotationCache; import nl.jqno.equalsverifier.internal.reflection.annotations.SupportedAnnotations; -import nl.jqno.equalsverifier.internal.reflection.instantiation.SubjectCreator; import nl.jqno.equalsverifier.internal.util.Formatter; public class TransientFieldsCheck implements FieldCheck { diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/TransitivityFieldCheck.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/TransitivityFieldCheck.java index 243c7b91f..c4b540a96 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/TransitivityFieldCheck.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/TransitivityFieldCheck.java @@ -3,8 +3,8 @@ import static nl.jqno.equalsverifier.internal.util.Assert.fail; import nl.jqno.equalsverifier.internal.SuppressFBWarnings; +import nl.jqno.equalsverifier.internal.instantiation.SubjectCreator; import nl.jqno.equalsverifier.internal.reflection.FieldProbe; -import nl.jqno.equalsverifier.internal.reflection.instantiation.SubjectCreator; import nl.jqno.equalsverifier.internal.util.Formatter; public class TransitivityFieldCheck implements FieldCheck { diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/exceptions/NoValueException.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/exceptions/NoValueException.java index 5a3d5310b..3ccaa06b4 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/exceptions/NoValueException.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/exceptions/NoValueException.java @@ -7,25 +7,14 @@ public class NoValueException extends MessagingException { private final TypeTag tag; - private final String label; public NoValueException(TypeTag tag) { - this(tag, null); - } - - public NoValueException(TypeTag tag, String label) { super(); this.tag = tag; - this.label = label; } @Override public String getDescription() { - return ( - "Could not find a value for " + - tag + - (label == null ? "" : " and label " + label) + - ". Please add prefab values for this type." - ); + return "Could not find a value for " + tag + ". Please add prefab values for this type."; } } diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/instantiation/InstanceCreator.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/InstanceCreator.java similarity index 97% rename from equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/instantiation/InstanceCreator.java rename to equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/InstanceCreator.java index 51b93e953..eb79f3b79 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/instantiation/InstanceCreator.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/InstanceCreator.java @@ -1,4 +1,4 @@ -package nl.jqno.equalsverifier.internal.reflection.instantiation; +package nl.jqno.equalsverifier.internal.instantiation; import java.lang.reflect.Field; import java.util.*; diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/JavaApiPrefabValues.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/JavaApiPrefabValues.java similarity index 98% rename from equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/JavaApiPrefabValues.java rename to equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/JavaApiPrefabValues.java index ab4417d51..65e710c2e 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/JavaApiPrefabValues.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/JavaApiPrefabValues.java @@ -1,8 +1,8 @@ -package nl.jqno.equalsverifier.internal.reflection; +package nl.jqno.equalsverifier.internal.instantiation; +import static nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories.Factories.*; import static nl.jqno.equalsverifier.internal.reflection.Util.classes; import static nl.jqno.equalsverifier.internal.reflection.Util.objects; -import static nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.Factories.*; import static nl.jqno.equalsverifier.internal.util.Rethrow.rethrow; import java.beans.PropertyChangeSupport; @@ -29,9 +29,12 @@ import java.util.function.Supplier; import java.util.regex.Pattern; import nl.jqno.equalsverifier.internal.SuppressFBWarnings; -import nl.jqno.equalsverifier.internal.reflection.vintage.FactoryCache; -import nl.jqno.equalsverifier.internal.reflection.vintage.VintageValueProvider; -import nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.*; +import nl.jqno.equalsverifier.internal.instantiation.vintage.FactoryCache; +import nl.jqno.equalsverifier.internal.instantiation.vintage.VintageValueProvider; +import nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories.*; +import nl.jqno.equalsverifier.internal.reflection.ConditionalInstantiator; +import nl.jqno.equalsverifier.internal.reflection.Tuple; +import nl.jqno.equalsverifier.internal.reflection.TypeTag; /** * Creates instances of classes for use in a {@link VintageValueProvider} object. 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/instantiation/SubjectCreator.java similarity index 94% rename from equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/instantiation/SubjectCreator.java rename to equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/SubjectCreator.java index f5871bdf6..3e5e59b6c 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/instantiation/SubjectCreator.java @@ -1,4 +1,4 @@ -package nl.jqno.equalsverifier.internal.reflection.instantiation; +package nl.jqno.equalsverifier.internal.instantiation; import java.lang.reflect.Field; import java.util.HashMap; @@ -6,7 +6,6 @@ import nl.jqno.equalsverifier.internal.SuppressFBWarnings; 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; @@ -22,6 +21,7 @@ public class SubjectCreator { private final Configuration config; private final ValueProvider valueProvider; private final ClassProbe classProbe; + private final FieldCache fieldCache; private final Objenesis objenesis; private final InstanceCreator instanceCreator; @@ -30,12 +30,14 @@ public class SubjectCreator { * * @param config A configuration object. * @param valueProvider To provide values for the fields of the subject. + * @param fieldCache Prepared values for the fields of the subject. * @param objenesis Needed by InstanceCreator to instantiate non-record classes. */ @SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = "A cache is inherently mutable") public SubjectCreator( Configuration config, ValueProvider valueProvider, + FieldCache fieldCache, Objenesis objenesis ) { this.typeTag = config.getTypeTag(); @@ -43,6 +45,7 @@ public SubjectCreator( this.config = config; this.valueProvider = valueProvider; this.classProbe = new ClassProbe<>(type); + this.fieldCache = fieldCache; this.objenesis = objenesis; this.instanceCreator = new InstanceCreator<>(classProbe, objenesis); } @@ -236,12 +239,14 @@ private FieldIterable nonSuperFields() { } private Tuple valuesFor(Field f) { + String fieldName = f.getName(); + if (fieldCache.contains(fieldName)) { + return fieldCache.get(fieldName); + } try { TypeTag fieldTag = TypeTag.of(f, typeTag); - Tuple tuple = valueProvider.provideOrThrow( - fieldTag, - Attributes.labeled(f.getName()) - ); + Tuple tuple = valueProvider.provideOrThrow(fieldTag); + fieldCache.put(fieldName, tuple); return tuple; } catch (ModuleException e) { throw new ModuleException( diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/ValueProvider.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/ValueProvider.java new file mode 100644 index 000000000..e2482c139 --- /dev/null +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/ValueProvider.java @@ -0,0 +1,41 @@ +package nl.jqno.equalsverifier.internal.instantiation; + +import java.util.Optional; +import nl.jqno.equalsverifier.internal.exceptions.NoValueException; +import nl.jqno.equalsverifier.internal.reflection.Tuple; +import nl.jqno.equalsverifier.internal.reflection.TypeTag; + +/** + * Creator of prefabricated instances of classes. + * + *

These instances are intended to be used to populate a subject, i.e. the class that is + * currently being tested by EqualsVerifier. + * + *

Only creates values ones, and caches them once they've been created. Takes generics into + * account; i.e., {@code List} is different from {@code List}. + */ +public interface ValueProvider { + /** + * Returns a tuple of two different prefabricated values of the specified type. + * + * @param The returned tuple will have this generic type. + * @param tag A description of the desired type, including generic parameters. + * @return A tuple of two different values of the given type, or an empty Optional if none + * could be found. + */ + Optional> provide(TypeTag tag); + + /** + * Returns a tuple of prefabricated values of the specified type, or, if none exists, throws a + * NoValueException. + * + * @param The returned tuple will have this generic type. + * @param tag A description of the desired type, including generic parameters. + * @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) { + return this.provide(tag).orElseThrow(() -> new NoValueException(tag)); + } +} diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/FactoryCache.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/FactoryCache.java new file mode 100644 index 000000000..a07a000cc --- /dev/null +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/FactoryCache.java @@ -0,0 +1,98 @@ +package nl.jqno.equalsverifier.internal.instantiation.vintage; + +import java.util.HashMap; +import java.util.Map; +import nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories.PrefabValueFactory; + +/** Contains a cache of factories, for {@link VintageValueProvider}. */ +public class FactoryCache { + + /** + * We store Strings instead of Classes, so that the cache can be lazy and initializers won't be + * called until the class is actually needed. + */ + private final Map> cache = new HashMap<>(); + + /** + * Adds the given factory to the cache and associates it with the given type. + * + * @param The type of the factory. + * @param type The type to associate with the factory. + * @param factory The factory to associate with the type. + */ + public void put(Class type, PrefabValueFactory factory) { + if (type != null) { + cache.put(type.getName(), factory); + } + } + + /** + * Adds the given factory to the cache and associates it with the given type name. + * + * @param Should match {@code typeName}. + * @param typeName The fully qualified name of the type. + * @param factory The factory to associate with {@code typeName} + */ + public void put(String typeName, PrefabValueFactory factory) { + if (typeName != null) { + cache.put(typeName, factory); + } + } + + /** + * Retrieves the factory from the cache for the given type. + * + *

What happens when there is no factory, is undefined. Always call {@link #contains(Class)} + * first. + * + * @param The returned factory will have this as generic type. + * @param type The type for which a factory is needed. + * @return A factory for the given type, or {@code null} if none is available. + */ + @SuppressWarnings("unchecked") + public PrefabValueFactory get(Class type) { + if (type == null) { + return null; + } + return (PrefabValueFactory) cache.get(type.getName()); + } + + /** + * @param type The type for which a factory is needed. + * @return Whether a factory is available for the given type. + */ + public boolean contains(Class type) { + return cache.containsKey(type.getName()); + } + + /** + * Returns a new {@code FactoryCache} instance containing the factories from {@code this}. + * + * @return a new instance containing factories from {@code this} + */ + public FactoryCache copy() { + FactoryCache result = new FactoryCache(); + addAll(result, this); + return result; + } + + /** + * Returns a new {@code FactoryCache} instance containing the factories from {@code this} and + * from the {@code other} cache. + * + * @param other The other cache + * @return a new instance containing factories from {@code this} and {@code other} + */ + public FactoryCache merge(FactoryCache other) { + FactoryCache result = new FactoryCache(); + addAll(result, this); + addAll(result, other); + return result; + } + + private void addAll(FactoryCache to, FactoryCache from) { + for (Map.Entry> entry : from.cache.entrySet()) { + to.put(entry.getKey(), entry.getValue()); + } + } +} diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/PrefabValuesApi.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/PrefabValuesApi.java similarity index 57% rename from equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/PrefabValuesApi.java rename to equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/PrefabValuesApi.java index 0b1a86954..7d153fe08 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/PrefabValuesApi.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/PrefabValuesApi.java @@ -1,14 +1,14 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage; +package nl.jqno.equalsverifier.internal.instantiation.vintage; -import static nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.Factories.simple; -import static nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.Factories.values; +import static nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories.Factories.simple; +import static nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories.Factories.values; -import java.lang.reflect.Field; import nl.jqno.equalsverifier.Func.Func1; import nl.jqno.equalsverifier.Func.Func2; -import nl.jqno.equalsverifier.internal.reflection.SuperclassIterable; -import nl.jqno.equalsverifier.internal.reflection.vintage.mutation.ObjectAccessor; -import nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.PrefabValueFactory; +import nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories.PrefabValueFactory; +import nl.jqno.equalsverifier.internal.instantiation.vintage.reflection.ObjectAccessor; +import nl.jqno.equalsverifier.internal.reflection.FieldCache; +import nl.jqno.equalsverifier.internal.reflection.Tuple; import nl.jqno.equalsverifier.internal.util.Validations; import org.objenesis.Objenesis; @@ -40,47 +40,27 @@ public static void addPrefabValues( @SuppressWarnings("unchecked") public static void addPrefabValuesForField( - FactoryCache factoryCache, + FieldCache fieldCache, Objenesis objenesis, - Class enclosingType, + Class type, String fieldName, T red, T blue ) { - Field field = findField(enclosingType, fieldName); - Class type = (Class) field.getType(); - - Validations.validateRedAndBluePrefabValues(type, red, blue); - Validations.validateFieldTypeMatches(field, red.getClass()); + Validations.validateRedAndBluePrefabValues((Class) red.getClass(), red, blue); + Validations.validateFieldTypeMatches(type, fieldName, red.getClass()); - if (type.isArray()) { - factoryCache.put(type, fieldName, values(red, blue, red)); + if (red.getClass().isArray()) { + fieldCache.put(fieldName, new Tuple<>(red, blue, red)); } else { try { T redCopy = ObjectAccessor.of(red).copy(objenesis); - factoryCache.put(type, fieldName, values(red, blue, redCopy)); + fieldCache.put(fieldName, new Tuple<>(red, blue, redCopy)); } catch (RuntimeException ignored) { /* specifically, on Java 9+: InacessibleObjectException */ - factoryCache.put(type, fieldName, values(red, blue, red)); - } - } - } - - private static Field findField(Class type, String fieldName) { - for (Class c : SuperclassIterable.ofIncludeSelf(type)) { - try { - return c.getDeclaredField(fieldName); - } catch (NoSuchFieldException ignored) { - // Do nothing + fieldCache.put(fieldName, new Tuple<>(red, blue, red)); } } - throw new IllegalStateException( - "Precondition: class " + - type.getSimpleName() + - " does not contain field " + - fieldName + - "." - ); } public static void addGenericPrefabValues( @@ -107,7 +87,7 @@ private static void addGenericPrefabValueFactory( PrefabValueFactory factory, int arity ) { - Validations.validateGenericPrefabValues(otherType, arity); + Validations.validateGenericPrefabValues(otherType, factory, arity); factoryCache.put(otherType, factory); } } diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/VintageValueProvider.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/VintageValueProvider.java similarity index 63% rename from equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/VintageValueProvider.java rename to equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/VintageValueProvider.java index 0206a2c8f..333b2ef6c 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/VintageValueProvider.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/VintageValueProvider.java @@ -1,22 +1,20 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage; +package nl.jqno.equalsverifier.internal.instantiation.vintage; -import java.util.Arrays; -import java.util.Optional; +import java.util.*; import nl.jqno.equalsverifier.internal.SuppressFBWarnings; import nl.jqno.equalsverifier.internal.exceptions.RecursionException; import nl.jqno.equalsverifier.internal.exceptions.ReflectionException; +import nl.jqno.equalsverifier.internal.instantiation.ValueProvider; +import nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories.FallbackFactory; +import nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories.PrefabValueFactory; 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; -import nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.FallbackFactory; -import nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.PrefabValueFactory; import nl.jqno.equalsverifier.internal.util.PrimitiveMappers; import nl.jqno.equalsverifier.internal.util.Rethrow; import org.objenesis.Objenesis; /** - * Provider of prefabricated instances of classes, using a "vintage" strategy for doing so. + * Creator of prefabricated instances of classes, using a "vintage" strategy for doing so. * * Vintage in this case means that it employs the creation strategy that EqualsVerifier has been * using since its inception. This strategy is quite hacky and messy, and other strategies might @@ -24,34 +22,28 @@ */ public class VintageValueProvider implements ValueProvider { - private final ValueProvider valueProvider; + // I'd like to remove this, but that affects recursion detection it a way I can't yet explain + private final Map> valueCache = new HashMap<>(); + private final FactoryCache factoryCache; private final PrefabValueFactory fallbackFactory; /** * Constructor. * - * @param valueProvider Will be used to look up values before they are created. - * @param cache The values that have already been constructed. * @param factoryCache The factories that can be used to create values. * @param objenesis To instantiate non-record classes. */ @SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = "A cache is inherently mutable.") - public VintageValueProvider( - ValueProvider valueProvider, - CachedValueProvider cache, - FactoryCache factoryCache, - Objenesis objenesis - ) { - this.valueProvider = valueProvider; + public VintageValueProvider(FactoryCache factoryCache, Objenesis objenesis) { this.factoryCache = factoryCache; this.fallbackFactory = new FallbackFactory<>(objenesis); } /** {@inheritDoc} */ @Override - public Optional> provide(TypeTag tag, Attributes attributes) { - return Rethrow.rethrow(() -> Optional.of(giveTuple(tag, attributes))); + public Optional> provide(TypeTag tag) { + return Rethrow.rethrow(() -> Optional.of(giveTuple(tag))); } /** @@ -64,11 +56,7 @@ public Optional> provide(TypeTag tag, Attributes attributes) { * @return The "red" prefabricated value. */ public T giveRed(TypeTag tag) { - return giveRed(tag, Attributes.unlabeled()); - } - - public T giveRed(TypeTag tag, Attributes attributes) { - return this.giveTuple(tag, attributes).getRed(); + return this.giveTuple(tag).getRed(); } /** @@ -81,11 +69,7 @@ public T giveRed(TypeTag tag, Attributes attributes) { * @return The "blue" prefabricated value. */ public T giveBlue(TypeTag tag) { - return giveBlue(tag, Attributes.unlabeled()); - } - - public T giveBlue(TypeTag tag, Attributes attributes) { - return this.giveTuple(tag, attributes).getBlue(); + return this.giveTuple(tag).getBlue(); } /** @@ -98,11 +82,7 @@ public T giveBlue(TypeTag tag, Attributes attributes) { * @return A shallow copy of the "red" prefabricated value. */ public T giveRedCopy(TypeTag tag) { - return giveRedCopy(tag, Attributes.unlabeled()); - } - - public T giveRedCopy(TypeTag tag, Attributes attributes) { - return this.giveTuple(tag, attributes).getRedCopy(); + return this.giveTuple(tag).getRedCopy(); } /** @@ -112,11 +92,11 @@ public T giveRedCopy(TypeTag tag, Attributes attributes) { * @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 attributes Provides metadata needed to provide a value and to keep track of recursion. + * @param typeStack Keeps track of recursion in the type. * @return A value that is different from {@code value}. */ // CHECKSTYLE OFF: CyclomaticComplexity - public T giveOther(TypeTag tag, T value, Attributes attributes) { + public T giveOther(TypeTag tag, T value, LinkedHashSet typeStack) { Class type = tag.getType(); if ( value != null && @@ -126,7 +106,7 @@ public T giveOther(TypeTag tag, T value, Attributes attributes) { throw new ReflectionException("TypeTag does not match value."); } - Tuple tuple = giveTuple(tag, attributes); + Tuple tuple = giveTuple(tag, typeStack); if (tuple.getRed() == null) { return null; } @@ -157,27 +137,44 @@ private boolean arraysAreDeeplyEqual(Object x, Object y) { return Arrays.deepEquals(new Object[] { x }, new Object[] { y }); } - @SuppressWarnings("unchecked") - private Tuple giveTuple(TypeTag tag, Attributes attributes) { - if (attributes.typeStack.contains(tag)) { - throw new RecursionException(attributes.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 typeStack Keeps track of recursion in the type. + */ + public void realizeCacheFor(TypeTag tag, LinkedHashSet typeStack) { + if (!valueCache.containsKey(tag)) { + Tuple tuple = createTuple(tag, typeStack); + valueCache.put(tag, tuple); } + } + + private Tuple giveTuple(TypeTag tag) { + return giveTuple(tag, new LinkedHashSet<>()); + } - Optional> provided = valueProvider.provide(tag, attributes); - if (provided.isPresent()) { - return provided.get(); + @SuppressWarnings("unchecked") + private Tuple giveTuple(TypeTag tag, LinkedHashSet typeStack) { + realizeCacheFor(tag, typeStack); + return (Tuple) valueCache.get(tag); + } + + private Tuple createTuple(TypeTag tag, LinkedHashSet typeStack) { + if (typeStack.contains(tag)) { + throw new RecursionException(typeStack); } Class type = tag.getType(); - 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, attributes); + return factory.createValues(tag, this, typeStack); } - return (Tuple) fallbackFactory.createValues(tag, this, attributes); + @SuppressWarnings("unchecked") + Tuple result = (Tuple) fallbackFactory.createValues(tag, this, typeStack); + return result; } } 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/instantiation/vintage/prefabvalues/factories/AbstractGenericFactory.java similarity index 80% rename from equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/AbstractGenericFactory.java rename to equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factories/AbstractGenericFactory.java index 3346e071f..1534761a2 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/instantiation/vintage/prefabvalues/factories/AbstractGenericFactory.java @@ -1,14 +1,14 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories; +package nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories; 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.SuppressFBWarnings; import nl.jqno.equalsverifier.internal.exceptions.ReflectionException; +import nl.jqno.equalsverifier.internal.instantiation.vintage.VintageValueProvider; import nl.jqno.equalsverifier.internal.reflection.TypeTag; -import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes; -import nl.jqno.equalsverifier.internal.reflection.vintage.VintageValueProvider; /** * Abstract implementation of {@link PrefabValueFactory} that provides helper functions for dealing @@ -18,6 +18,13 @@ 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()) { @@ -30,22 +37,23 @@ protected TypeTag determineAndCacheActualTypeTag( int n, TypeTag tag, VintageValueProvider valueProvider, - Attributes attributes + LinkedHashSet typeStack ) { - return determineAndCacheActualTypeTag(n, tag, valueProvider, attributes, null); + return determineAndCacheActualTypeTag(n, tag, valueProvider, typeStack, null); } protected TypeTag determineAndCacheActualTypeTag( int n, TypeTag tag, VintageValueProvider valueProvider, - Attributes attributes, + LinkedHashSet typeStack, Class bottomType ) { TypeTag result = determineActualTypeTagFor(n, tag); if (bottomType != null && result.getType().equals(Object.class)) { result = new TypeTag(bottomType); } + valueProvider.realizeCacheFor(result, typeStack); 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/instantiation/vintage/prefabvalues/factories/CopyFactory.java similarity index 56% rename from equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/CopyFactory.java rename to equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factories/CopyFactory.java index 5b8f782c1..332c159c2 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/instantiation/vintage/prefabvalues/factories/CopyFactory.java @@ -1,10 +1,10 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories; +package nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories; +import java.util.LinkedHashSet; import java.util.function.Function; +import nl.jqno.equalsverifier.internal.instantiation.vintage.VintageValueProvider; 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.VintageValueProvider; public class CopyFactory extends AbstractGenericFactory { @@ -20,14 +20,15 @@ public CopyFactory(Class source, Function copy) { public Tuple createValues( TypeTag tag, VintageValueProvider valueProvider, - Attributes attributes + LinkedHashSet typeStack ) { - Attributes clone = attributes.cloneAndAdd(tag); + LinkedHashSet clone = cloneWith(typeStack, tag); TypeTag sourceTag = copyGenericTypesInto(source, tag); + valueProvider.realizeCacheFor(sourceTag, clone); - S redSource = valueProvider.giveRed(sourceTag, clone); - S blueSource = valueProvider.giveBlue(sourceTag, clone); - S redCopySource = valueProvider.giveRedCopy(sourceTag, clone); + S redSource = valueProvider.giveRed(sourceTag); + S blueSource = valueProvider.giveBlue(sourceTag); + S redCopySource = valueProvider.giveRedCopy(sourceTag); return Tuple.of(copy.apply(redSource), copy.apply(blueSource), copy.apply(redCopySource)); } 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/instantiation/vintage/prefabvalues/factories/EnumMapFactory.java similarity index 61% rename from equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/EnumMapFactory.java rename to equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factories/EnumMapFactory.java index abbf32cac..8ca507f7f 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/instantiation/vintage/prefabvalues/factories/EnumMapFactory.java @@ -1,12 +1,12 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories; +package nl.jqno.equalsverifier.internal.instantiation.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.instantiation.vintage.VintageValueProvider; 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.VintageValueProvider; @SuppressWarnings({ "unchecked", "rawtypes" }) public final class EnumMapFactory extends AbstractGenericFactory { @@ -21,18 +21,18 @@ public EnumMapFactory(Function factory) { public Tuple createValues( TypeTag tag, VintageValueProvider valueProvider, - Attributes attributes + LinkedHashSet typeStack ) { - Attributes clone = attributes.cloneAndAdd(tag); + LinkedHashSet clone = cloneWith(typeStack, tag); TypeTag keyTag = determineAndCacheActualTypeTag(0, tag, valueProvider, clone, Enum.class); TypeTag valueTag = determineAndCacheActualTypeTag(1, tag, valueProvider, clone, Enum.class); Map red = new HashMap<>(); Map blue = new HashMap<>(); Map redCopy = new HashMap<>(); - red.put(valueProvider.giveRed(keyTag, clone), valueProvider.giveBlue(valueTag, clone)); - blue.put(valueProvider.giveBlue(keyTag, clone), valueProvider.giveBlue(valueTag, clone)); - redCopy.put(valueProvider.giveRed(keyTag, clone), valueProvider.giveBlue(valueTag, clone)); + red.put(valueProvider.giveRed(keyTag), valueProvider.giveBlue(valueTag)); + blue.put(valueProvider.giveBlue(keyTag), valueProvider.giveBlue(valueTag)); + redCopy.put(valueProvider.giveRed(keyTag), valueProvider.giveBlue(valueTag)); return Tuple.of(factory.apply(red), factory.apply(blue), factory.apply(redCopy)); } 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/instantiation/vintage/prefabvalues/factories/EnumSetFactory.java similarity index 67% rename from equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/EnumSetFactory.java rename to equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factories/EnumSetFactory.java index f6dae8634..ad0c9a806 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/instantiation/vintage/prefabvalues/factories/EnumSetFactory.java @@ -1,12 +1,12 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories; +package nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories; import java.util.Collection; import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.function.Function; +import nl.jqno.equalsverifier.internal.instantiation.vintage.VintageValueProvider; 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.VintageValueProvider; /** * Implementation of {@link PrefabValueFactory} that instantiates EnumSets using reflection, while @@ -25,17 +25,17 @@ public EnumSetFactory(Function factory) { public Tuple createValues( TypeTag tag, VintageValueProvider valueProvider, - Attributes attributes + LinkedHashSet typeStack ) { - Attributes clone = attributes.cloneAndAdd(tag); + LinkedHashSet clone = cloneWith(typeStack, tag); TypeTag entryTag = determineAndCacheActualTypeTag(0, tag, valueProvider, clone, Enum.class); Collection red = new HashSet<>(); Collection blue = new HashSet<>(); Collection redCopy = new HashSet<>(); - red.add(valueProvider.giveRed(entryTag, clone)); - blue.add(valueProvider.giveBlue(entryTag, clone)); - redCopy.add(valueProvider.giveRed(entryTag, clone)); + red.add(valueProvider.giveRed(entryTag)); + blue.add(valueProvider.giveBlue(entryTag)); + redCopy.add(valueProvider.giveRed(entryTag)); return new Tuple<>(factory.apply(red), factory.apply(blue), factory.apply(redCopy)); } 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/instantiation/vintage/prefabvalues/factories/ExternalFactory.java similarity index 64% rename from equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/ExternalFactory.java rename to equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factories/ExternalFactory.java index d777d7a64..7ed1166df 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/instantiation/vintage/prefabvalues/factories/ExternalFactory.java @@ -1,20 +1,20 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories; +package nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories; 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.instantiation.vintage.FactoryCache; +import nl.jqno.equalsverifier.internal.instantiation.vintage.VintageValueProvider; +import nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factoryproviders.FactoryProvider; 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.vintage.FactoryCache; -import nl.jqno.equalsverifier.internal.reflection.vintage.VintageValueProvider; -import nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factoryproviders.FactoryProvider; public class ExternalFactory implements PrefabValueFactory { private static final String EXTERNAL_FACTORIES_PACKAGE = - "nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factoryproviders."; + "nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factoryproviders."; private final String factoryName; private FactoryCache factoryCache; @@ -27,7 +27,7 @@ public ExternalFactory(String factoryName) { public Tuple createValues( TypeTag tag, VintageValueProvider valueProvider, - Attributes attributes + LinkedHashSet typeStack ) { 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, attributes); + return factory.createValues(tag, valueProvider, typeStack); } } diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/Factories.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factories/Factories.java similarity index 94% rename from equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/Factories.java rename to equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factories/Factories.java index 42cf962cd..f2240b56f 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/Factories.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factories/Factories.java @@ -1,4 +1,4 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories; +package nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories; import java.util.Collection; import java.util.Map; 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/instantiation/vintage/prefabvalues/factories/FallbackFactory.java similarity index 67% rename from equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/FallbackFactory.java rename to equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factories/FallbackFactory.java index 917f7eb88..73f459a67 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/instantiation/vintage/prefabvalues/factories/FallbackFactory.java @@ -1,13 +1,11 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories; +package nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories; import java.lang.reflect.Array; -import nl.jqno.equalsverifier.internal.reflection.ClassProbe; +import java.util.LinkedHashSet; +import nl.jqno.equalsverifier.internal.instantiation.vintage.VintageValueProvider; +import nl.jqno.equalsverifier.internal.instantiation.vintage.reflection.ClassAccessor; import nl.jqno.equalsverifier.internal.reflection.Tuple; import nl.jqno.equalsverifier.internal.reflection.TypeTag; -import nl.jqno.equalsverifier.internal.reflection.instantiation.InstanceCreator; -import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes; -import nl.jqno.equalsverifier.internal.reflection.vintage.VintageValueProvider; -import nl.jqno.equalsverifier.internal.reflection.vintage.mutation.ClassAccessor; import org.objenesis.Objenesis; /** @@ -28,9 +26,11 @@ public FallbackFactory(Objenesis objenesis) { public Tuple createValues( TypeTag tag, VintageValueProvider valueProvider, - Attributes attributes + LinkedHashSet typeStack ) { - Attributes clone = attributes.cloneAndAdd(tag); + @SuppressWarnings("unchecked") + LinkedHashSet clone = (LinkedHashSet) typeStack.clone(); + clone.add(tag); Class type = tag.getType(); if (type.isEnum()) { @@ -61,18 +61,19 @@ private Tuple giveEnumInstances(TypeTag tag) { private Tuple giveArrayInstances( TypeTag tag, VintageValueProvider valueProvider, - Attributes attributes + LinkedHashSet typeStack ) { Class type = tag.getType(); Class componentType = type.getComponentType(); TypeTag componentTag = new TypeTag(componentType); + valueProvider.realizeCacheFor(componentTag, typeStack); T red = (T) Array.newInstance(componentType, 1); - Array.set(red, 0, valueProvider.giveRed(componentTag, attributes)); + Array.set(red, 0, valueProvider.giveRed(componentTag)); T blue = (T) Array.newInstance(componentType, 1); - Array.set(blue, 0, valueProvider.giveBlue(componentTag, attributes)); + Array.set(blue, 0, valueProvider.giveBlue(componentTag)); T redCopy = (T) Array.newInstance(componentType, 1); - Array.set(redCopy, 0, valueProvider.giveRed(componentTag, attributes)); + Array.set(redCopy, 0, valueProvider.giveRed(componentTag)); return new Tuple<>(red, blue, redCopy); } @@ -80,16 +81,12 @@ private Tuple giveArrayInstances( private Tuple giveInstances( TypeTag tag, VintageValueProvider valueProvider, - Attributes attributes + LinkedHashSet typeStack ) { - ClassAccessor accessor = new ClassAccessor<>(tag.getType(), valueProvider, objenesis); - T red = accessor.getRedObject(tag, attributes); - T blue = accessor.getBlueObject(tag, attributes); - - @SuppressWarnings("unchecked") - Class actualType = (Class) red.getClass(); - T redCopy = new InstanceCreator<>(new ClassProbe(actualType), objenesis).copy(red); - + 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); 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/instantiation/vintage/prefabvalues/factories/MapFactory.java similarity index 70% rename from equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/MapFactory.java rename to equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factories/MapFactory.java index 40030f4de..b50ca8a05 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/instantiation/vintage/prefabvalues/factories/MapFactory.java @@ -1,11 +1,11 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories; +package nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories; +import java.util.LinkedHashSet; import java.util.Map; import java.util.function.Supplier; +import nl.jqno.equalsverifier.internal.instantiation.vintage.VintageValueProvider; 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.VintageValueProvider; /** * Implementation of {@link PrefabValueFactory} that specializes in creating implementations of @@ -24,9 +24,9 @@ public MapFactory(Supplier createEmpty) { public Tuple createValues( TypeTag tag, VintageValueProvider valueProvider, - Attributes attributes + LinkedHashSet typeStack ) { - Attributes clone = attributes.cloneAndAdd(tag); + LinkedHashSet clone = cloneWith(typeStack, tag); TypeTag keyTag = determineAndCacheActualTypeTag(0, tag, valueProvider, clone); TypeTag valueTag = determineAndCacheActualTypeTag(1, tag, valueProvider, clone); @@ -34,16 +34,16 @@ public Tuple createValues( // values. // But don't do it in the Blue map, or they may cancel each other out again. - Object redKey = valueProvider.giveRed(keyTag, clone); - Object blueKey = valueProvider.giveBlue(keyTag, clone); - Object blueValue = valueProvider.giveBlue(valueTag, clone); + Object redKey = valueProvider.giveRed(keyTag); + Object blueKey = valueProvider.giveBlue(keyTag); + Object blueValue = valueProvider.giveBlue(valueTag); T red = createEmpty.get(); red.put(redKey, blueValue); T blue = createEmpty.get(); if (!redKey.equals(blueKey)) { // This happens with single-element enums - blue.put(valueProvider.giveBlue(keyTag, clone), blueValue); + blue.put(blueKey, blueValue); } T redCopy = createEmpty.get(); 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/instantiation/vintage/prefabvalues/factories/PrefabValueFactory.java similarity index 54% rename from equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/PrefabValueFactory.java rename to equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factories/PrefabValueFactory.java index e7c2c3c13..c7f03252a 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/instantiation/vintage/prefabvalues/factories/PrefabValueFactory.java @@ -1,9 +1,9 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories; +package nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories; +import java.util.LinkedHashSet; +import nl.jqno.equalsverifier.internal.instantiation.vintage.VintageValueProvider; 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.VintageValueProvider; /** * Creates instances of generic types for use as prefab value. @@ -17,8 +17,13 @@ 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 attributes Provides metadata needed to provide a value and to keep track of recursion. + * @param typeStack A stack of {@link TypeTag}s that require tag in order to be created. Used + * for recursion detection. * @return A "red" instance of {@code T}. */ - Tuple createValues(TypeTag tag, VintageValueProvider valueProvider, Attributes attributes); + Tuple createValues( + TypeTag tag, + VintageValueProvider valueProvider, + LinkedHashSet typeStack + ); } 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/instantiation/vintage/prefabvalues/factories/SimpleFactory.java similarity index 68% rename from equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/SimpleFactory.java rename to equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factories/SimpleFactory.java index 3012b2cf5..77375f3d6 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/instantiation/vintage/prefabvalues/factories/SimpleFactory.java @@ -1,9 +1,9 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories; +package nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories; +import java.util.LinkedHashSet; +import nl.jqno.equalsverifier.internal.instantiation.vintage.VintageValueProvider; 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.VintageValueProvider; /** * Implementation of {@link PrefabValueFactory} that holds on to two instances that have already @@ -21,7 +21,7 @@ public SimpleFactory(T red, T blue, T redCopy) { public Tuple createValues( TypeTag tag, VintageValueProvider valueProvider, - Attributes attributes + LinkedHashSet typeStack ) { 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/instantiation/vintage/prefabvalues/factories/SimpleGenericFactory.java similarity index 75% rename from equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/SimpleGenericFactory.java rename to equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factories/SimpleGenericFactory.java index 558a8465b..70a8b683d 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/instantiation/vintage/prefabvalues/factories/SimpleGenericFactory.java @@ -1,13 +1,13 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories; +package nl.jqno.equalsverifier.internal.instantiation.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.instantiation.vintage.VintageValueProvider; 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.VintageValueProvider; public class SimpleGenericFactory extends AbstractGenericFactory { @@ -23,9 +23,9 @@ public SimpleGenericFactory(Func factory, Supplier emptyFactory) { public Tuple createValues( TypeTag tag, VintageValueProvider valueProvider, - Attributes attributes + LinkedHashSet typeStack ) { - Attributes clone = attributes.cloneAndAdd(tag); + LinkedHashSet clone = cloneWith(typeStack, tag); List redValues = new ArrayList<>(); List blueValues = new ArrayList<>(); @@ -35,8 +35,8 @@ public Tuple createValues( for (int i = 0; i < n; i++) { TypeTag paramTag = determineAndCacheActualTypeTag(i, tag, valueProvider, clone); - Object redValue = valueProvider.giveRed(paramTag, clone); - Object blueValue = valueProvider.giveBlue(paramTag, clone); + Object redValue = valueProvider.giveRed(paramTag); + Object blueValue = valueProvider.giveBlue(paramTag); if (redValue.equals(blueValue)) { // This happens with single-element enums useEmpty = true; } diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factoryproviders/AwtFactoryProvider.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factoryproviders/AwtFactoryProvider.java similarity index 83% rename from equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factoryproviders/AwtFactoryProvider.java rename to equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factoryproviders/AwtFactoryProvider.java index 9499349f6..ccee73a6b 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factoryproviders/AwtFactoryProvider.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factoryproviders/AwtFactoryProvider.java @@ -1,13 +1,15 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factoryproviders; +package nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factoryproviders; -import static nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.Factories.values; +import static nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories.Factories.values; -import java.awt.*; +import java.awt.Color; +import java.awt.Font; +import java.awt.Image; import java.awt.color.ColorSpace; import java.awt.color.ICC_ColorSpace; import java.awt.color.ICC_Profile; import java.awt.image.BufferedImage; -import nl.jqno.equalsverifier.internal.reflection.vintage.FactoryCache; +import nl.jqno.equalsverifier.internal.instantiation.vintage.FactoryCache; public final class AwtFactoryProvider implements FactoryProvider { diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factoryproviders/FactoryProvider.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factoryproviders/FactoryProvider.java new file mode 100644 index 000000000..685937c3b --- /dev/null +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factoryproviders/FactoryProvider.java @@ -0,0 +1,7 @@ +package nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factoryproviders; + +import nl.jqno.equalsverifier.internal.instantiation.vintage.FactoryCache; + +public interface FactoryProvider { + FactoryCache getFactoryCache(); +} 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/instantiation/vintage/prefabvalues/factoryproviders/GuavaFactoryProvider.java similarity index 83% rename from equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factoryproviders/GuavaFactoryProvider.java rename to equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factoryproviders/GuavaFactoryProvider.java index e9860bd40..ebba614d2 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/instantiation/vintage/prefabvalues/factoryproviders/GuavaFactoryProvider.java @@ -1,17 +1,16 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factoryproviders; +package nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factoryproviders; -import static nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.Factories.*; +import static nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories.Factories.*; import com.google.common.collect.*; import com.google.common.reflect.TypeToken; import java.util.*; import java.util.function.Supplier; +import nl.jqno.equalsverifier.internal.instantiation.vintage.FactoryCache; +import nl.jqno.equalsverifier.internal.instantiation.vintage.VintageValueProvider; +import nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories.*; 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.VintageValueProvider; -import nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.*; public final class GuavaFactoryProvider implements FactoryProvider { @@ -158,24 +157,18 @@ private MultimapFactory(Supplier factory) { public Tuple createValues( TypeTag tag, VintageValueProvider valueProvider, - Attributes attributes + LinkedHashSet typeStack ) { - Attributes clone = attributes.cloneAndAdd(tag); + LinkedHashSet clone = cloneWith(typeStack, tag); TypeTag keyTag = determineAndCacheActualTypeTag(0, tag, valueProvider, clone); TypeTag valueTag = determineAndCacheActualTypeTag(1, tag, valueProvider, clone); T red = factory.get(); T blue = factory.get(); T redCopy = factory.get(); - red.put(valueProvider.giveRed(keyTag, clone), valueProvider.giveBlue(valueTag, clone)); - blue.put( - valueProvider.giveBlue(keyTag, clone), - valueProvider.giveBlue(valueTag, clone) - ); - redCopy.put( - valueProvider.giveRed(keyTag, clone), - valueProvider.giveBlue(valueTag, clone) - ); + red.put(valueProvider.giveRed(keyTag), valueProvider.giveBlue(valueTag)); + blue.put(valueProvider.giveBlue(keyTag), valueProvider.giveBlue(valueTag)); + redCopy.put(valueProvider.giveRed(keyTag), valueProvider.giveBlue(valueTag)); return Tuple.of(red, blue, redCopy); } @@ -194,9 +187,9 @@ private TableFactory(Supplier factory) { public Tuple createValues( TypeTag tag, VintageValueProvider valueProvider, - Attributes attributes + LinkedHashSet typeStack ) { - Attributes clone = attributes.cloneAndAdd(tag); + LinkedHashSet clone = cloneWith(typeStack, tag); TypeTag columnTag = determineAndCacheActualTypeTag(0, tag, valueProvider, clone); TypeTag rowTag = determineAndCacheActualTypeTag(1, tag, valueProvider, clone); TypeTag valueTag = determineAndCacheActualTypeTag(2, tag, valueProvider, clone); @@ -205,19 +198,19 @@ public Tuple createValues( T blue = factory.get(); T redCopy = factory.get(); red.put( - valueProvider.giveRed(columnTag, clone), - valueProvider.giveRed(rowTag, clone), - valueProvider.giveBlue(valueTag, clone) + valueProvider.giveRed(columnTag), + valueProvider.giveRed(rowTag), + valueProvider.giveBlue(valueTag) ); blue.put( - valueProvider.giveBlue(columnTag, clone), - valueProvider.giveBlue(rowTag, clone), - valueProvider.giveBlue(valueTag, clone) + valueProvider.giveBlue(columnTag), + valueProvider.giveBlue(rowTag), + valueProvider.giveBlue(valueTag) ); redCopy.put( - valueProvider.giveRed(columnTag, clone), - valueProvider.giveRed(rowTag, clone), - valueProvider.giveBlue(valueTag, clone) + valueProvider.giveRed(columnTag), + valueProvider.giveRed(rowTag), + valueProvider.giveBlue(valueTag) ); return Tuple.of(red, blue, redCopy); 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/instantiation/vintage/prefabvalues/factoryproviders/JavaFxFactoryProvider.java similarity index 86% rename from equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factoryproviders/JavaFxFactoryProvider.java rename to equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factoryproviders/JavaFxFactoryProvider.java index f601fa8cb..b47636262 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/instantiation/vintage/prefabvalues/factoryproviders/JavaFxFactoryProvider.java @@ -1,22 +1,19 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factoryproviders; +package nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factoryproviders; +import static nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories.Factories.*; import static nl.jqno.equalsverifier.internal.reflection.Util.classForName; import static nl.jqno.equalsverifier.internal.reflection.Util.classes; import static nl.jqno.equalsverifier.internal.reflection.Util.objects; -import static nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.Factories.copy; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; import java.util.function.Function; +import nl.jqno.equalsverifier.internal.instantiation.vintage.FactoryCache; +import nl.jqno.equalsverifier.internal.instantiation.vintage.VintageValueProvider; +import nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories.AbstractGenericFactory; +import nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories.PrefabValueFactory; 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.vintage.FactoryCache; -import nl.jqno.equalsverifier.internal.reflection.vintage.VintageValueProvider; -import nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.AbstractGenericFactory; -import nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.PrefabValueFactory; public final class JavaFxFactoryProvider implements FactoryProvider { @@ -114,22 +111,22 @@ static final class PropertyFactory extends AbstractGenericFactory { public Tuple createValues( TypeTag tag, VintageValueProvider valueProvider, - Attributes attributes + LinkedHashSet typeStack ) { ConditionalInstantiator ci = new ConditionalInstantiator(fullyQualifiedTypeName); TypeTag singleParameterTag = copyGenericTypesInto(parameterRawType, tag); T red = ci.instantiate( classes(parameterRawType), - objects(valueProvider.giveRed(singleParameterTag, attributes)) + objects(valueProvider.giveRed(singleParameterTag)) ); T blue = ci.instantiate( classes(parameterRawType), - objects(valueProvider.giveBlue(singleParameterTag, attributes)) + objects(valueProvider.giveBlue(singleParameterTag)) ); T redCopy = ci.instantiate( classes(parameterRawType), - objects(valueProvider.giveRed(singleParameterTag, attributes)) + objects(valueProvider.giveRed(singleParameterTag)) ); return Tuple.of(red, blue, redCopy); diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factoryproviders/JavaxFactoryProvider.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factoryproviders/JavaxFactoryProvider.java similarity index 70% rename from equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factoryproviders/JavaxFactoryProvider.java rename to equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factoryproviders/JavaxFactoryProvider.java index b968c0bc6..a223b962f 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factoryproviders/JavaxFactoryProvider.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factoryproviders/JavaxFactoryProvider.java @@ -1,10 +1,10 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factoryproviders; +package nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factoryproviders; -import static nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.Factories.values; +import static nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories.Factories.values; import javax.naming.Reference; import javax.swing.tree.DefaultMutableTreeNode; -import nl.jqno.equalsverifier.internal.reflection.vintage.FactoryCache; +import nl.jqno.equalsverifier.internal.instantiation.vintage.FactoryCache; public final class JavaxFactoryProvider implements FactoryProvider { diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factoryproviders/JodaFactoryProvider.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factoryproviders/JodaFactoryProvider.java similarity index 81% rename from equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factoryproviders/JodaFactoryProvider.java rename to equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factoryproviders/JodaFactoryProvider.java index 33eb24584..255320318 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factoryproviders/JodaFactoryProvider.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factoryproviders/JodaFactoryProvider.java @@ -1,8 +1,8 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factoryproviders; +package nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factoryproviders; -import static nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.Factories.values; +import static nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories.Factories.values; -import nl.jqno.equalsverifier.internal.reflection.vintage.FactoryCache; +import nl.jqno.equalsverifier.internal.instantiation.vintage.FactoryCache; import org.joda.time.*; import org.joda.time.chrono.GregorianChronology; import org.joda.time.chrono.ISOChronology; diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factoryproviders/RmiFactoryProvider.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factoryproviders/RmiFactoryProvider.java similarity index 61% rename from equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factoryproviders/RmiFactoryProvider.java rename to equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factoryproviders/RmiFactoryProvider.java index 72e45f836..5f55cac20 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factoryproviders/RmiFactoryProvider.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factoryproviders/RmiFactoryProvider.java @@ -1,10 +1,10 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factoryproviders; +package nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factoryproviders; -import static nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.Factories.values; +import static nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories.Factories.values; import java.rmi.dgc.VMID; import java.rmi.server.UID; -import nl.jqno.equalsverifier.internal.reflection.vintage.FactoryCache; +import nl.jqno.equalsverifier.internal.instantiation.vintage.FactoryCache; public final class RmiFactoryProvider implements FactoryProvider { diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/ClassAccessor.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/ClassAccessor.java new file mode 100644 index 000000000..905184334 --- /dev/null +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/ClassAccessor.java @@ -0,0 +1,108 @@ +package nl.jqno.equalsverifier.internal.instantiation.vintage.reflection; + +import java.util.LinkedHashSet; +import nl.jqno.equalsverifier.internal.instantiation.vintage.VintageValueProvider; +import nl.jqno.equalsverifier.internal.reflection.Instantiator; +import nl.jqno.equalsverifier.internal.reflection.TypeTag; +import org.objenesis.Objenesis; + +/** + * Instantiates and populates objects of a given class. {@link ClassAccessor} can create two + * different instances of T, which are guaranteed not to be equal to each other, and which contain + * no null values. + * + * @param A class. + */ +public class ClassAccessor { + + private final Class type; + private final VintageValueProvider valueProvider; + private final Objenesis objenesis; + + /** Private constructor. Call {@link #of(Class, PrefabValues)} instead. */ + ClassAccessor(Class type, VintageValueProvider valueProvider, Objenesis objenesis) { + this.type = type; + this.valueProvider = valueProvider; + this.objenesis = objenesis; + } + + /** + * Factory method. + * + * @param The class on which {@link ClassAccessor} operates. + * @param type The class on which {@link ClassAccessor} operates. Should be the same as T. + * @param valueProvider Prefabricated values with which to fill instantiated objects. + * @param objenesis To instantiate non-record classes. + * @return A {@link ClassAccessor} for T. + */ + public static ClassAccessor of( + Class type, + VintageValueProvider valueProvider, + Objenesis objenesis + ) { + return new ClassAccessor<>(type, valueProvider, objenesis); + } + + /** + * Returns an instance of T that is not equal to the instance of T returned by {@link + * #getBlueObject(TypeTag, LinkedHashSet)}. + * + * @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 instance of T. + */ + public T getRedObject(TypeTag enclosingType, LinkedHashSet typeStack) { + return getRedAccessor(enclosingType, typeStack).get(); + } + + /** + * Returns an {@link ObjectAccessor} for {@link #getRedObject(TypeTag, LinkedHashSet)}. + * + * @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 #getRedObject}. + */ + public ObjectAccessor getRedAccessor( + TypeTag enclosingType, + LinkedHashSet typeStack + ) { + return buildObjectAccessor().scramble(valueProvider, enclosingType, typeStack); + } + + /** + * Returns an instance of T that is not equal to the instance of T returned by {@link + * #getRedObject(TypeTag, LinkedHashSet)}. + * + * @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 instance of T. + */ + public T getBlueObject(TypeTag enclosingType, LinkedHashSet typeStack) { + return getBlueAccessor(enclosingType, typeStack).get(); + } + + /** + * Returns an {@link ObjectAccessor} for {@link #getBlueObject(TypeTag, LinkedHashSet)}. + * + * @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)}. + */ + public ObjectAccessor getBlueAccessor( + TypeTag enclosingType, + LinkedHashSet typeStack + ) { + return buildObjectAccessor() + .scramble(valueProvider, enclosingType, typeStack) + .scramble(valueProvider, enclosingType, typeStack); + } + + private ObjectAccessor buildObjectAccessor() { + T object = Instantiator.of(type, objenesis).instantiate(); + return ObjectAccessor.of(object); + } +} diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/FieldModifier.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/FieldModifier.java similarity index 89% rename from equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/FieldModifier.java rename to equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/FieldModifier.java index fd5725b16..546236fd3 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/FieldModifier.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/FieldModifier.java @@ -1,13 +1,13 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage.mutation; +package nl.jqno.equalsverifier.internal.instantiation.vintage.reflection; 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.instantiation.vintage.VintageValueProvider; 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.vintage.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 attributes Provides metadata needed to provide a value and to keep track of recursion. + * @param typeStack Keeps track of recursion in the type. * @throws ReflectionException If the operation fails. */ public void changeField( VintageValueProvider valueProvider, TypeTag enclosingType, - Attributes attributes + LinkedHashSet typeStack ) { FieldChanger fm = () -> { TypeTag tag = TypeTag.of(field, enclosingType); - Object newValue = valueProvider.giveOther(tag, field.get(object), attributes); + Object newValue = valueProvider.giveOther(tag, field.get(object), typeStack); field.set(object, newValue); }; change(fm, false); diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/InPlaceObjectAccessor.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/InPlaceObjectAccessor.java similarity index 82% rename from equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/InPlaceObjectAccessor.java rename to equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/InPlaceObjectAccessor.java index 89943838f..68089fdf8 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/InPlaceObjectAccessor.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/InPlaceObjectAccessor.java @@ -1,11 +1,11 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage.mutation; +package nl.jqno.equalsverifier.internal.instantiation.vintage.reflection; import java.lang.reflect.Field; +import java.util.LinkedHashSet; +import nl.jqno.equalsverifier.internal.instantiation.vintage.VintageValueProvider; 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.vintage.VintageValueProvider; import org.objenesis.Objenesis; /** @@ -35,10 +35,10 @@ public T copy(Objenesis objenesis) { public ObjectAccessor scramble( VintageValueProvider valueProvider, TypeTag enclosingType, - Attributes attributes + LinkedHashSet typeStack ) { for (Field field : FieldIterable.of(type())) { - fieldModifierFor(field).changeField(valueProvider, enclosingType, attributes); + fieldModifierFor(field).changeField(valueProvider, enclosingType, typeStack); } return this; } diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/ObjectAccessor.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/ObjectAccessor.java similarity index 89% rename from equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/ObjectAccessor.java rename to equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/ObjectAccessor.java index 7cdd484cb..9fe79aa2c 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/ObjectAccessor.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/ObjectAccessor.java @@ -1,9 +1,9 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage.mutation; +package nl.jqno.equalsverifier.internal.instantiation.vintage.reflection; +import java.util.LinkedHashSet; +import nl.jqno.equalsverifier.internal.instantiation.vintage.VintageValueProvider; 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.vintage.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 attributes Provides metadata needed to provide a value and to keep track of recursion. + * @param typeStack Keeps track of recursion in the type. * @return An accessor to the scrambled object. */ public abstract ObjectAccessor scramble( VintageValueProvider valueProvider, TypeTag enclosingType, - Attributes attributes + LinkedHashSet typeStack ); } diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/RecordObjectAccessor.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/RecordObjectAccessor.java similarity index 86% rename from equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/RecordObjectAccessor.java rename to equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/RecordObjectAccessor.java index 98a80dd49..425d46da6 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/RecordObjectAccessor.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/RecordObjectAccessor.java @@ -1,14 +1,14 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage.mutation; +package nl.jqno.equalsverifier.internal.instantiation.vintage.reflection; 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.instantiation.vintage.VintageValueProvider; 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.vintage.VintageValueProvider; import org.objenesis.Objenesis; /** @@ -40,12 +40,12 @@ public T copy(Objenesis objenesis) { public ObjectAccessor scramble( VintageValueProvider valueProvider, TypeTag enclosingType, - Attributes attributes + LinkedHashSet typeStack ) { return makeAccessor(f -> { Object value = getField(f); TypeTag tag = TypeTag.of(f, enclosingType); - return valueProvider.giveOther(tag, value, attributes); + return valueProvider.giveOther(tag, value, typeStack); }); } diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/FieldCache.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/FieldCache.java new file mode 100644 index 000000000..28856ac6f --- /dev/null +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/FieldCache.java @@ -0,0 +1,60 @@ +package nl.jqno.equalsverifier.internal.reflection; + +import java.util.*; +import nl.jqno.equalsverifier.internal.instantiation.SubjectCreator; + +/** Contains a cache for values connected to specific fields, for {@link SubjectCreator}. */ +public class FieldCache { + + /** + * We store Strings instead of Fields, to make it easier to interact with when we don't + * actually have a reference to a Field. + */ + private final Map> cache = new HashMap<>(); + + /** + * Adds the given factory to the cache and associates it with the given type. + * + * @param The type of the values. + * @param fieldName The name of the field to associate with the values. + * @param tuple The tuple that contains the values. + */ + public void put(String fieldName, Tuple tuple) { + if (fieldName != null) { + cache.put(fieldName, tuple); + } + } + + /** + * Retrieves the values from the cache for the given field. + * + *

What happens when there are no values, is undefined. Always call {@link #contains(String)} + * first. + * + * @param The returned values will have this as generic type. + * @param fieldName The name of the field for which values are needed. + * @return A tuple of values for the given type, or {@code null} if none is available. + */ + @SuppressWarnings("unchecked") + public Tuple get(String fieldName) { + if (fieldName == null) { + return null; + } + return (Tuple) cache.get(fieldName); + } + + /** + * @param fieldName The name of the field for which values are needed. + * @return Whether values are available for the given field. + */ + public boolean contains(String fieldName) { + return cache.containsKey(fieldName); + } + + /** + * @return The fields preset in the cache. + */ + public Set getFieldNames() { + return new HashSet<>(cache.keySet()); + } +} diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/Tuple.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/Tuple.java index 892be3407..c276f3fac 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/Tuple.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/Tuple.java @@ -1,8 +1,5 @@ package nl.jqno.equalsverifier.internal.reflection; -import java.util.function.BiFunction; -import java.util.function.Function; - /** * Container for three values of the same type: a "red" one, a "blue" one, and a shallow copy of the * "red" one. @@ -55,18 +52,6 @@ public T getRedCopy() { return redCopy; } - public Tuple map(Function fn) { - return Tuple.of(fn.apply(red), fn.apply(blue), fn.apply(redCopy)); - } - - public static Tuple combine(Tuple t, Tuple u, BiFunction fn) { - return Tuple.of( - fn.apply(t.getRed(), u.getRed()), - fn.apply(t.getBlue(), u.getBlue()), - fn.apply(t.getRedCopy(), u.getRedCopy()) - ); - } - /** {@inheritDoc} */ @Override public boolean equals(Object obj) { 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 deleted file mode 100644 index e564ecb99..000000000 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/instantiation/CachedValueProvider.java +++ /dev/null @@ -1,75 +0,0 @@ -package nl.jqno.equalsverifier.internal.reflection.instantiation; - -import java.util.*; -import nl.jqno.equalsverifier.internal.reflection.Tuple; -import nl.jqno.equalsverifier.internal.reflection.TypeTag; - -/** - * Provider of instances that have been created before, e.g. by other ValueProviders. - */ -public class CachedValueProvider implements ValueProvider { - - private final Map> cache = new HashMap<>(); - - /** - * Adds a value to the cache. - * - * @param tag The type to assign the value to. - * @param label The label that the value should be linked to. - * @param tuple The value to add to the cache. - */ - public void put(TypeTag tag, String label, Tuple tuple) { - cache.put(Key.of(tag, label), tuple); - } - - /** - * {@inheritDoc}} - */ - @SuppressWarnings("unchecked") - @Override - public Optional> provide(TypeTag tag, Attributes attributes) { - Key first = Key.of(tag, attributes.label); - if (cache.containsKey(first)) { - return Optional.of((Tuple) cache.get(first)); - } - Key second = Key.of(tag, null); - if (cache.containsKey(second)) { - return Optional.of((Tuple) cache.get(second)); - } - return Optional.empty(); - } - - static final class Key { - - final TypeTag tag; - final String label; - - private Key(TypeTag tag, String label) { - this.tag = tag; - this.label = label; - } - - public static Key of(TypeTag tag, String label) { - return new Key(tag, label); - } - - @Override - public boolean equals(Object obj) { - if (!(obj instanceof Key)) { - return false; - } - Key other = (Key) obj; - return Objects.equals(tag, other.tag) && Objects.equals(label, other.label); - } - - @Override - public int hashCode() { - return Objects.hash(tag, label); - } - - @Override - public String toString() { - return "Key: [" + tag + "/" + label + "]"; - } - } -} 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 deleted file mode 100644 index 3409f23cd..000000000 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/instantiation/ChainedValueProvider.java +++ /dev/null @@ -1,56 +0,0 @@ -package nl.jqno.equalsverifier.internal.reflection.instantiation; - -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; -import nl.jqno.equalsverifier.internal.SuppressFBWarnings; -import nl.jqno.equalsverifier.internal.exceptions.EqualsVerifierInternalBugException; -import nl.jqno.equalsverifier.internal.reflection.Tuple; -import nl.jqno.equalsverifier.internal.reflection.TypeTag; - -/** - * Provider of instances of classes, delegating to other ValueProviders in sequence. - */ -public class ChainedValueProvider implements ValueProvider { - - private boolean locked = false; - private final List providers = new ArrayList<>(); - private final CachedValueProvider cache; - - @SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = "A cache is inherently mutable") - public ChainedValueProvider(CachedValueProvider cache) { - this.cache = cache; - providers.add(cache); - } - - /** - * Adds providers to the chain, so they can be delegated to when providing a value. - * - * @param valueProviders ValueProviders to add to the chain. - */ - public void register(ValueProvider... valueProviders) { - if (locked) { - throw new EqualsVerifierInternalBugException( - "Provider is locked; can't add any new ones." - ); - } - for (ValueProvider p : valueProviders) { - providers.add(p); - } - locked = true; - } - - /** {@inheritDoc} */ - @Override - public Optional> provide(TypeTag tag, Attributes attributes) { - Optional> result = providers - .stream() - .map(vp -> vp.provide(tag, attributes)) - .filter(Optional::isPresent) - .findFirst() - .orElse(Optional.empty()); - - 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/ValueProvider.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/instantiation/ValueProvider.java deleted file mode 100644 index 366f6963a..000000000 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/instantiation/ValueProvider.java +++ /dev/null @@ -1,99 +0,0 @@ -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; -import nl.jqno.equalsverifier.internal.reflection.TypeTag; - -/** - * Creator of prefabricated instances of classes. - * - *

These instances are intended to be used to populate a subject, i.e. the class that is - * currently being tested by EqualsVerifier. - * - *

Only creates values ones, and caches them once they've been created. Takes generics into - * account; i.e., {@code List} is different from {@code List}. - */ -public interface ValueProvider { - /** - * Returns a tuple of prefabricated values of the specified type, or, if none exists, returns - * an empty Optional. - * - * @param The returned tuple will have this generic type. - * @param tag A description of the desired type, including generic parameters. - * @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, Attributes attributes); - - /** - * Returns a tuple of prefabricated values of the specified type, or, if none exists, throws a - * NoValueException. - * - * @param The returned tuple will have this generic type. - * @param tag A description of the desired type, including generic parameters. - * @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, 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/vintage/FactoryCache.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/FactoryCache.java deleted file mode 100644 index 61ea3e55b..000000000 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/FactoryCache.java +++ /dev/null @@ -1,180 +0,0 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage; - -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; -import nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.PrefabValueFactory; - -/** Contains a cache of factories, for {@link VintageValueProvider}. */ -public class FactoryCache { - - /** - * We store Strings instead of Classes, so that the cache can be lazy and initializers won't be - * called until the class is actually needed. - */ - private final Map> cache = new HashMap<>(); - - /** - * Adds the given factory to the cache and associates it with the given type. - * - * @param The type of the factory. - * @param type The type to associate with the factory. - * @param factory The factory to associate with the type. - */ - public void put(Class type, PrefabValueFactory factory) { - if (type != null) { - put(Key.of(type.getName()), factory); - } - } - - /** - * Adds the given factory to the cache and associates it with the given type. - * - * @param The type of the factory. - * @param type The type to associate with the factory. - * @param label The label that the factory is linked to, or null if it is not assigned to any label. - * @param factory The factory to associate with the type. - */ - public void put(Class type, String label, PrefabValueFactory factory) { - if (type != null) { - put(Key.of(type.getName(), label), factory); - } - } - - /** - * Adds the given factory to the cache and associates it with the given type name. - * - * @param Should match {@code typeName}. - * @param typeName The fully qualified name of the type. - * @param factory The factory to associate with {@code typeName} - */ - public void put(String typeName, PrefabValueFactory factory) { - if (typeName != null) { - put(Key.of(typeName), factory); - } - } - - private void put(Key key, PrefabValueFactory factory) { - cache.put(key, factory); - } - - /** - * Retrieves the factory from the cache for the given type. - * - *

What happens when there is no factory, is undefined. Always call {@link #contains(Class)} - * first. - * - * @param The returned factory will have this as generic type. - * @param type The type for which a factory is needed. - * @return A factory for the given type, or {@code null} if none is available. - */ - public PrefabValueFactory get(Class type) { - return get(type, null); - } - - /** - * Retrieves the factory from the cache for the given type. - * - *

What happens when there is no factory, is undefined. Always call {@link #contains(Class)} - * first. - * - * @param The returned factory will have this as generic type. - * @param type The type for which a factory is needed. - * @param label Returns only the factory assigned to the given label, or if label is null, - * returns the factory that's not assigned to any label. - * @return A factory for the given type, or {@code null} if none is available. - */ - @SuppressWarnings("unchecked") - public PrefabValueFactory get(Class type, String label) { - if (type == null) { - return null; - } - return (PrefabValueFactory) cache.get(Key.of(type.getName(), label)); - } - - /** - * @param type The type for which a factory is needed. - * @return Whether a factory is available for the given type. - */ - public boolean contains(Class type) { - return cache.containsKey(Key.of(type.getName())); - } - - /** - * @param type The type for which a factory is needed. - * @param label The label that the factory needs to be assigned to. - * @return Whether a factory is available for the given type. - */ - public boolean contains(Class type, String label) { - return cache.containsKey(Key.of(type.getName(), label)); - } - - /** - * Returns a new {@code FactoryCache} instance containing the factories from {@code this}. - * - * @return a new instance containing factories from {@code this} - */ - public FactoryCache copy() { - FactoryCache result = new FactoryCache(); - addAll(result, this); - return result; - } - - /** - * Returns a new {@code FactoryCache} instance containing the factories from {@code this} and - * from the {@code other} cache. - * - * @param other The other cache - * @return a new instance containing factories from {@code this} and {@code other} - */ - public FactoryCache merge(FactoryCache other) { - FactoryCache result = new FactoryCache(); - addAll(result, this); - addAll(result, other); - return result; - } - - private void addAll(FactoryCache to, FactoryCache from) { - for (Map.Entry> entry : from.cache.entrySet()) { - to.put(entry.getKey(), entry.getValue()); - } - } - - static final class Key { - - final String typeName; - final String label; - - private Key(String typeName, String label) { - this.typeName = typeName; - this.label = label; - } - - public static Key of(String typeName) { - return new Key(typeName, null); - } - - public static Key of(String typeName, String label) { - return new Key(typeName, label); - } - - @Override - public boolean equals(Object obj) { - if (!(obj instanceof Key)) { - return false; - } - Key other = (Key) obj; - return Objects.equals(typeName, other.typeName) && Objects.equals(label, other.label); - } - - @Override - public int hashCode() { - return Objects.hash(typeName, label); - } - - @Override - public String toString() { - return "Key: [" + typeName + "/" + label + "]"; - } - } -} diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/ClassAccessor.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/ClassAccessor.java deleted file mode 100644 index bc4757e96..000000000 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/ClassAccessor.java +++ /dev/null @@ -1,68 +0,0 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage.mutation; - -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.vintage.VintageValueProvider; -import org.objenesis.Objenesis; - -/** - * Instantiates and populates objects of a given class. {@link ClassAccessor} can create two - * different instances of T, which are guaranteed not to be equal to each other, and which contain - * no null values. - * - * @param A class. - */ -public class ClassAccessor { - - private final Class type; - private final VintageValueProvider valueProvider; - private final Objenesis objenesis; - - /** - * Constructor. - * - * @param type The class on which {@link ClassAccessor} operates. Should be the same as T. - * @param valueProvider Prefabricated values with which to fill instantiated objects. - * @param objenesis To instantiate non-record classes. - */ - public ClassAccessor(Class type, VintageValueProvider valueProvider, Objenesis objenesis) { - this.type = type; - this.valueProvider = valueProvider; - this.objenesis = objenesis; - } - - /** - * Returns an instance of T that is not equal to the instance of T returned by {@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 attributes Provides metadata needed to provide a value and to keep track of recursion. - * @return An instance of T. - */ - public T getRedObject(TypeTag enclosingType, Attributes attributes) { - return buildObjectAccessor().scramble(valueProvider, enclosingType, attributes).get(); - } - - /** - * Returns an instance of T that is not equal to the instance of T returned by {@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 attributes Provides metadata needed to provide a value and to keep track of recursion. - * @return An instance of T. - */ - public T getBlueObject(TypeTag enclosingType, Attributes attributes) { - return buildObjectAccessor() - .scramble(valueProvider, enclosingType, attributes) - .scramble(valueProvider, enclosingType, attributes) - .get(); - } - - private ObjectAccessor buildObjectAccessor() { - T object = Instantiator.of(type, objenesis).instantiate(); - return ObjectAccessor.of(object); - } -} diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factoryproviders/FactoryProvider.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factoryproviders/FactoryProvider.java deleted file mode 100644 index 9da2eef3b..000000000 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factoryproviders/FactoryProvider.java +++ /dev/null @@ -1,7 +0,0 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factoryproviders; - -import nl.jqno.equalsverifier.internal.reflection.vintage.FactoryCache; - -public interface FactoryProvider { - FactoryCache getFactoryCache(); -} 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 deleted file mode 100644 index 3e971ef63..000000000 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/testhelpers/TestValueProviders.java +++ /dev/null @@ -1,20 +0,0 @@ -package nl.jqno.equalsverifier.internal.testhelpers; - -import java.util.Optional; -import nl.jqno.equalsverifier.internal.reflection.Tuple; -import nl.jqno.equalsverifier.internal.reflection.TypeTag; -import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider; - -public final class TestValueProviders { - - private TestValueProviders() {} - - public static ValueProvider empty() { - return new ValueProvider() { - @Override - public Optional> provide(TypeTag tag, Attributes attributes) { - return Optional.empty(); - } - }; - } -} diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/util/Context.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/util/Context.java index ae240d693..6081356d3 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/util/Context.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/util/Context.java @@ -1,11 +1,13 @@ package nl.jqno.equalsverifier.internal.util; import nl.jqno.equalsverifier.internal.SuppressFBWarnings; +import nl.jqno.equalsverifier.internal.instantiation.JavaApiPrefabValues; +import nl.jqno.equalsverifier.internal.instantiation.SubjectCreator; +import nl.jqno.equalsverifier.internal.instantiation.ValueProvider; +import nl.jqno.equalsverifier.internal.instantiation.vintage.FactoryCache; +import nl.jqno.equalsverifier.internal.instantiation.vintage.VintageValueProvider; import nl.jqno.equalsverifier.internal.reflection.ClassProbe; -import nl.jqno.equalsverifier.internal.reflection.JavaApiPrefabValues; -import nl.jqno.equalsverifier.internal.reflection.instantiation.SubjectCreator; -import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider; -import nl.jqno.equalsverifier.internal.reflection.vintage.FactoryCache; +import nl.jqno.equalsverifier.internal.reflection.FieldCache; import org.objenesis.Objenesis; public final class Context { @@ -13,22 +15,30 @@ public final class Context { private final Class type; private final Configuration configuration; private final ClassProbe classProbe; + private final FieldCache fieldCache; - private final ValueProvider valueProvider; private final SubjectCreator subjectCreator; + private final ValueProvider valueProvider; @SuppressFBWarnings( value = "EI_EXPOSE_REP2", justification = "FieldCache is inherently mutable" ) - public Context(Configuration configuration, FactoryCache factoryCache, Objenesis objenesis) { + public Context( + Configuration configuration, + FactoryCache factoryCache, + FieldCache fieldCache, + Objenesis objenesis + ) { this.type = configuration.getType(); this.configuration = configuration; this.classProbe = new ClassProbe<>(configuration.getType()); + this.fieldCache = fieldCache; FactoryCache cache = JavaApiPrefabValues.build().merge(factoryCache); - this.valueProvider = DefaultValueProviders.create(cache, objenesis); - this.subjectCreator = new SubjectCreator<>(configuration, valueProvider, objenesis); + this.valueProvider = new VintageValueProvider(cache, objenesis); + this.subjectCreator = + new SubjectCreator<>(configuration, valueProvider, fieldCache, objenesis); } public Class getType() { @@ -43,6 +53,11 @@ public ClassProbe getClassProbe() { return classProbe; } + @SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "A cache is inherently mutable") + public FieldCache getFieldCache() { + return fieldCache; + } + @SuppressFBWarnings( value = "EI_EXPOSE_REP", justification = "VintageValueProvider can use a mutable cache." diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/util/DefaultValueProviders.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/util/DefaultValueProviders.java deleted file mode 100644 index 664cd03b6..000000000 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/util/DefaultValueProviders.java +++ /dev/null @@ -1,32 +0,0 @@ -package nl.jqno.equalsverifier.internal.util; - -import nl.jqno.equalsverifier.internal.reflection.instantiation.CachedValueProvider; -import nl.jqno.equalsverifier.internal.reflection.instantiation.ChainedValueProvider; -import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider; -import nl.jqno.equalsverifier.internal.reflection.vintage.FactoryCache; -import nl.jqno.equalsverifier.internal.reflection.vintage.VintageValueProvider; -import org.objenesis.Objenesis; - -public final class DefaultValueProviders { - - private DefaultValueProviders() {} - - public static ValueProvider create(FactoryCache factoryCache, Objenesis objenesis) { - CachedValueProvider cache = new CachedValueProvider(); - - ChainedValueProvider mainChain = new ChainedValueProvider(cache); - ChainedValueProvider vintageChain = new ChainedValueProvider(cache); - - ValueProvider vintage = new VintageValueProvider( - vintageChain, - cache, - factoryCache, - objenesis - ); - - mainChain.register(cache, vintage); - vintageChain.register(cache); - - return mainChain; - } -} diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/util/Validations.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/util/Validations.java index 4d7666227..ece4b6740 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/util/Validations.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/util/Validations.java @@ -3,12 +3,10 @@ import static nl.jqno.equalsverifier.internal.util.ListBuilders.listContainsDuplicates; import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Set; +import java.util.*; import java.util.stream.Collectors; import nl.jqno.equalsverifier.Warning; +import nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories.PrefabValueFactory; import nl.jqno.equalsverifier.internal.reflection.FieldIterable; import nl.jqno.equalsverifier.internal.reflection.annotations.AnnotationCache; import nl.jqno.equalsverifier.internal.reflection.annotations.SupportedAnnotations; @@ -88,24 +86,40 @@ public static void validateRedAndBluePrefabValues(Class type, T red, T bl ); } - public static void validateFieldTypeMatches(Field field, Class realFieldType) { - boolean typeCompatible = field.getType().isAssignableFrom(realFieldType); - boolean wrappingCompatible = realFieldType.equals( - PrimitiveMappers.PRIMITIVE_OBJECT_MAPPER.get(field.getType()) - ); - validate( - !typeCompatible && !wrappingCompatible, - "Prefab values for field " + - field.getName() + - " should be of type " + - field.getType().getSimpleName() + - " but are " + - realFieldType.getSimpleName() + - "." - ); + public static void validateFieldTypeMatches( + Class container, + String fieldName, + Class fieldType + ) { + try { + Field f = container.getDeclaredField(fieldName); + boolean typeCompatible = f.getType().isAssignableFrom(fieldType); + boolean wrappingCompatible = fieldType.equals( + PrimitiveMappers.PRIMITIVE_OBJECT_MAPPER.get(f.getType()) + ); + validate( + !typeCompatible && !wrappingCompatible, + "Prefab values for field " + + fieldName + + " should be of type " + + f.getType().getSimpleName() + + " but are " + + fieldType.getSimpleName() + + "." + ); + } catch (NoSuchFieldException e) { + validate( + false, + "Class " + container.getSimpleName() + " has no field named " + fieldName + "." + ); + } } - public static void validateGenericPrefabValues(Class type, int arity) { + public static void validateGenericPrefabValues( + Class type, + PrefabValueFactory factory, + int arity + ) { validateNotNull(type, "type is null."); int n = type.getTypeParameters().length; diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/extended_contract/GenericTypesTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/extended_contract/GenericTypesTest.java index 43bddc2f6..c3cce62cf 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/extended_contract/GenericTypesTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/extended_contract/GenericTypesTest.java @@ -131,6 +131,19 @@ public void succeed_whenClassHasASelfReferenceGenericParameter_givenPrefabValues .verify(); } + @Test + public void succeed_whenClassContainsAMapWithAnArray() { + EqualsVerifier.forClass(ArrayMapContainer.class).withNonnullFields("map").verify(); + } + + @Test + public void succeed_whenClassContainsAClassThatContainsAMapWithArray() { + EqualsVerifier + .forClass(ArrayMapContainerContainer.class) + .withNonnullFields("mapContainer") + .verify(); + } + static final class GenericContainerWithBuiltin { private final Generic> b; @@ -154,6 +167,30 @@ public int hashCode() { } } + static final class Generic { + + private final T t; + + public Generic(T t) { + this.t = t; + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof Generic)) { + return false; + } + @SuppressWarnings("unchecked") + Generic other = (Generic) obj; + return Objects.equals(t, other.t); + } + + @Override + public int hashCode() { + return Objects.hash(t); + } + } + static final class JavaGenericTypeContainer { private final Optional optional; @@ -205,30 +242,6 @@ public String toString() { } } - static final class Generic { - - private final T t; - - public Generic(T t) { - this.t = t; - } - - @Override - public boolean equals(Object obj) { - if (!(obj instanceof Generic)) { - return false; - } - @SuppressWarnings("unchecked") - Generic other = (Generic) obj; - return Objects.equals(t, other.t); - } - - @Override - public int hashCode() { - return Objects.hash(t); - } - } - static final class ListContainer { private final List list; @@ -848,4 +861,65 @@ public final int hashCode() { return Objects.hash(i, wrapped); } } + + static class ArrayMapContainerContainer { + + private final ArrayMapContainer mapContainer; + + public ArrayMapContainerContainer(final ArrayMapContainer mapContainer) { + this.mapContainer = mapContainer; + } + + @Override + public final boolean equals(final Object o) { + if (!(o instanceof ArrayMapContainerContainer)) { + return false; + } + final ArrayMapContainerContainer that = (ArrayMapContainerContainer) o; + return Objects.equals(mapContainer, that.mapContainer); + } + + @Override + public final int hashCode() { + return Objects.hashCode(mapContainer); + } + } + + static class ArrayMapContainer { + + private final Map map; + + public ArrayMapContainer(final Map map) { + this.map = map; + } + + @Override + public final boolean equals(final Object o) { + if (this == o) { + return true; + } + if (!(o instanceof ArrayMapContainer)) { + return false; + } + final ArrayMapContainer that = (ArrayMapContainer) o; + if (map.size() != that.map.size()) { + return false; + } + for (final Map.Entry entry : map.entrySet()) { + if (!Arrays.equals(entry.getValue(), that.map.get(entry.getKey()))) { + return false; + } + } + return true; + } + + @Override + public final int hashCode() { + int hashCode = 0; + for (final Map.Entry entry : map.entrySet()) { + hashCode += entry.getKey().hashCode() ^ Arrays.hashCode(entry.getValue()); + } + return hashCode; + } + } } diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/operational/RecursionTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/operational/RecursionTest.java index 8724bd3c1..1c01935f6 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/operational/RecursionTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/operational/RecursionTest.java @@ -4,7 +4,9 @@ import static nl.jqno.equalsverifier.internal.testhelpers.Util.defaultHashCode; import com.google.common.collect.ImmutableList; -import java.util.*; +import java.util.Collections; +import java.util.List; +import java.util.Objects; import nl.jqno.equalsverifier.EqualsVerifier; import nl.jqno.equalsverifier.internal.reflection.TypeTag; import nl.jqno.equalsverifier.internal.testhelpers.ExpectedException; diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/operational/WithPrefabValuesForFieldTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/operational/WithPrefabValuesForFieldTest.java index a3330ca3c..39b88a76c 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/operational/WithPrefabValuesForFieldTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/operational/WithPrefabValuesForFieldTest.java @@ -181,7 +181,6 @@ public void succeed_whenClassContainsSomethingThatAllowsSubclassesAndASubclassIs public void succeed_whenClassContainsAGenericInterfaceThatRefersToItself() { DifficultGeneric one = new DifficultGeneric(new ArrayList<>()); DifficultGeneric two = new DifficultGeneric(null); - EqualsVerifier .forClass(DifficultGeneric.class) .withPrefabValuesForField( diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/architecture/ArchitectureTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/architecture/ArchitectureTest.java index 8614a5eb5..014186834 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/architecture/ArchitectureTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/architecture/ArchitectureTest.java @@ -5,9 +5,11 @@ import com.tngtech.archunit.junit.AnalyzeClasses; import com.tngtech.archunit.junit.ArchTest; import com.tngtech.archunit.lang.ArchRule; -import nl.jqno.equalsverifier.internal.reflection.vintage.mutation.ClassAccessor; -import nl.jqno.equalsverifier.internal.reflection.vintage.mutation.FieldModifier; -import nl.jqno.equalsverifier.internal.reflection.vintage.mutation.ObjectAccessor; +import nl.jqno.equalsverifier.internal.instantiation.JavaApiPrefabValues; +import nl.jqno.equalsverifier.internal.instantiation.vintage.reflection.ClassAccessor; +import nl.jqno.equalsverifier.internal.instantiation.vintage.reflection.FieldModifier; +import nl.jqno.equalsverifier.internal.instantiation.vintage.reflection.ObjectAccessor; +import nl.jqno.equalsverifier.testhelpers.FactoryCacheFactory; @AnalyzeClasses(packages = "nl.jqno.equalsverifier") public final class ArchitectureTest { @@ -15,15 +17,19 @@ public final class ArchitectureTest { @ArchTest public static final ArchRule ONLY_VINTAGE_INSTANTIATORS_CAN_USE_VINTAGE_REFLECTION = noClasses() .that() - .resideOutsideOfPackage("nl.jqno.equalsverifier.internal.reflection.vintage..") + .resideOutsideOfPackage("nl.jqno.equalsverifier.internal.instantiation.vintage..") + .and() + .doNotBelongToAnyOf( + JavaApiPrefabValues.class, + // 👇 Test classes + FactoryCacheFactory.class + ) .should() .accessClassesThat() - .resideInAPackage("nl.jqno.equalsverifier.internal.reflection.vintage.mutation..") + .resideInAPackage("nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues..") .orShould() .accessClassesThat() - .resideInAPackage( - "nl.jqno.equalsverifier.internal.reflection.vintage.mutation.prefabvalues.." - ); + .resideInAPackage("nl.jqno.equalsverifier.internal.instantiation.vintage.reflection.."); @ArchTest public static final ArchRule DONT_USE_VINTAGE_REFLECTION_DIRECTLY = noClasses() @@ -68,7 +74,7 @@ public final class ArchitectureTest { ); private static final String FACTORYPROVIDER_PATTERN = - "nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factoryproviders.."; + "nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factoryproviders.."; private static final String TEST_CLASS_PATTERN = ".*Test(\\$.*)?$"; private ArchitectureTest() { diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/exceptions/NoValueExceptionTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/exceptions/NoValueExceptionTest.java index 5eec5c2d8..58259cc49 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/exceptions/NoValueExceptionTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/exceptions/NoValueExceptionTest.java @@ -1,6 +1,5 @@ package nl.jqno.equalsverifier.internal.exceptions; -import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import nl.jqno.equalsverifier.internal.reflection.TypeTag; @@ -9,18 +8,9 @@ public class NoValueExceptionTest { @Test - public void descriptionWithoutLabel() { + public void description() { TypeTag tag = new TypeTag(String.class); - NoValueException e = new NoValueException(tag, null); + NoValueException e = new NoValueException(tag); assertTrue(e.getDescription().contains("String")); - assertFalse(e.getDescription().contains("label")); - } - - @Test - public void descriptionWithLabel() { - TypeTag tag = new TypeTag(String.class); - NoValueException e = new NoValueException(tag, "lbl"); - assertTrue(e.getDescription().contains("String")); - assertTrue(e.getDescription().contains("label lbl")); } } diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/InstanceCreatorTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/InstanceCreatorTest.java similarity index 96% rename from equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/InstanceCreatorTest.java rename to equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/InstanceCreatorTest.java index 4cd563b6f..0ca65bbe0 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/InstanceCreatorTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/InstanceCreatorTest.java @@ -1,4 +1,4 @@ -package nl.jqno.equalsverifier.internal.reflection.instantiation; +package nl.jqno.equalsverifier.internal.instantiation; import static org.junit.jupiter.api.Assertions.assertEquals; 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/instantiation/SubjectCreatorTest.java similarity index 95% rename from equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/SubjectCreatorTest.java rename to equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/SubjectCreatorTest.java index e99595234..46a603f0c 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/instantiation/SubjectCreatorTest.java @@ -1,4 +1,4 @@ -package nl.jqno.equalsverifier.internal.reflection.instantiation; +package nl.jqno.equalsverifier.internal.instantiation; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotSame; @@ -7,6 +7,7 @@ import java.util.Objects; import java.util.Optional; import nl.jqno.equalsverifier.internal.exceptions.NoValueException; +import nl.jqno.equalsverifier.internal.reflection.FieldCache; import nl.jqno.equalsverifier.internal.reflection.Tuple; import nl.jqno.equalsverifier.internal.reflection.TypeTag; import nl.jqno.equalsverifier.internal.testhelpers.ExpectedException; @@ -28,8 +29,14 @@ public class SubjectCreatorTest { SomeClass.class ); private ValueProvider valueProvider = new SubjectCreatorTestValueProvider(); + private FieldCache fieldCache = new FieldCache(); private Objenesis objenesis = new ObjenesisStd(); - private SubjectCreator sut = new SubjectCreator<>(config, valueProvider, objenesis); + private SubjectCreator sut = new SubjectCreator<>( + config, + valueProvider, + fieldCache, + objenesis + ); private Field fieldX; private Field fieldI; @@ -189,7 +196,7 @@ public void copyIntoSubclass() { @Test public void noValueFound() { - sut = new SubjectCreator<>(config, new NoValueProvider(), objenesis); + sut = new SubjectCreator<>(config, new NoValueProvider(), fieldCache, objenesis); ExpectedException .when(() -> sut.plain()) @@ -201,7 +208,7 @@ public void noValueFound() { static class SubjectCreatorTestValueProvider implements ValueProvider { - public Optional> provide(TypeTag tag, Attributes attributes) { + public Optional> provide(TypeTag tag) { if (int.class.equals(tag.getType())) { return Optional.of(Tuple.of(I_RED, I_BLUE, I_RED)); } @@ -215,7 +222,7 @@ public Optional> provide(TypeTag tag, Attributes attributes) { static class NoValueProvider implements ValueProvider { @Override - public Optional> provide(TypeTag tag, Attributes attributes) { + public Optional> provide(TypeTag tag) { return Optional.empty(); } } diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/FactoryCacheTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/FactoryCacheTest.java similarity index 56% rename from equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/FactoryCacheTest.java rename to equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/FactoryCacheTest.java index 614d06fdd..a91e73794 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/FactoryCacheTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/FactoryCacheTest.java @@ -1,16 +1,13 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage; +package nl.jqno.equalsverifier.internal.instantiation.vintage; import static org.junit.jupiter.api.Assertions.*; -import nl.jqno.equalsverifier.EqualsVerifier; -import nl.jqno.equalsverifier.internal.reflection.vintage.FactoryCache.Key; -import nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.PrefabValueFactory; -import nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.SimpleFactory; +import nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories.PrefabValueFactory; +import nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories.SimpleFactory; import org.junit.jupiter.api.Test; public class FactoryCacheTest { - private static final String SOME_LABEL = "label"; private static final Class STRING_CLASS = String.class; private static final PrefabValueFactory STRING_FACTORY = new SimpleFactory<>( "red", @@ -30,14 +27,6 @@ public class FactoryCacheTest { public void putAndGetTuple() { cache.put(STRING_CLASS, STRING_FACTORY); assertEquals(STRING_FACTORY, cache.get(STRING_CLASS)); - assertNull(cache.get(STRING_CLASS, SOME_LABEL)); - } - - @Test - public void putAndGetTupleWithLabel() { - cache.put(STRING_CLASS, SOME_LABEL, STRING_FACTORY); - assertEquals(STRING_FACTORY, cache.get(STRING_CLASS, SOME_LABEL)); - assertNull(cache.get(STRING_CLASS)); } @Test @@ -49,60 +38,29 @@ public void putTwiceAndGetBoth() { assertEquals(STRING_FACTORY, cache.get(STRING_CLASS)); } - @Test - public void putTwiceAndGetBothWithLabel() { - cache.put(STRING_CLASS, SOME_LABEL, STRING_FACTORY); - cache.put(INT_CLASS, SOME_LABEL, INT_FACTORY); - - assertEquals(INT_FACTORY, cache.get(INT_CLASS, SOME_LABEL)); - assertEquals(STRING_FACTORY, cache.get(STRING_CLASS, SOME_LABEL)); - } - @Test public void putNullAndGetNothingBack() { cache.put((Class) null, STRING_FACTORY); assertNull(cache.get(null)); } - @Test - public void putNullAndGetNothingBackWithLabel() { - cache.put((Class) null, SOME_LABEL, STRING_FACTORY); - assertNull(cache.get(null, SOME_LABEL)); - } - @Test public void contains() { cache.put(STRING_CLASS, STRING_FACTORY); assertTrue(cache.contains(STRING_CLASS)); } - @Test - public void containsWithLabel() { - cache.put(STRING_CLASS, SOME_LABEL, STRING_FACTORY); - assertTrue(cache.contains(STRING_CLASS, SOME_LABEL)); - assertFalse(cache.contains(STRING_CLASS)); - } - @Test public void doesntContain() { assertFalse(cache.contains(STRING_CLASS)); } - @Test - public void doesntContainWithLabel() { - cache.put(STRING_CLASS, STRING_FACTORY); - assertFalse(cache.contains(STRING_CLASS, SOME_LABEL)); - } - @Test public void copy() { cache.put(STRING_CLASS, STRING_FACTORY); - FactoryCache copy = cache.copy(); copy.put(INT_CLASS, INT_FACTORY); - assertTrue(copy.contains(STRING_CLASS)); - assertFalse(copy == cache); assertFalse(cache.contains(INT_CLASS)); } @@ -124,9 +82,4 @@ public void merge() { assertFalse(a.contains(INT_CLASS)); assertFalse(b.contains(STRING_CLASS)); } - - @Test - public void key() { - EqualsVerifier.forClass(Key.class).verify(); - } } diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/PrefabValuesApiTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/PrefabValuesApiTest.java similarity index 81% rename from equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/PrefabValuesApiTest.java rename to equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/PrefabValuesApiTest.java index b8c43352c..33d789da6 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/PrefabValuesApiTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/PrefabValuesApiTest.java @@ -1,4 +1,4 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage; +package nl.jqno.equalsverifier.internal.instantiation.vintage; import static nl.jqno.equalsverifier.internal.testhelpers.Util.coverThePrivateConstructor; diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/VintageValueProviderCreatorTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/VintageValueProviderCreatorTest.java similarity index 83% rename from equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/VintageValueProviderCreatorTest.java rename to equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/VintageValueProviderCreatorTest.java index 2a6808a9a..934543a92 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/VintageValueProviderCreatorTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/VintageValueProviderCreatorTest.java @@ -1,14 +1,12 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage; +package nl.jqno.equalsverifier.internal.instantiation.vintage; -import static nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.Factories.values; +import static nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories.Factories.values; import static org.junit.jupiter.api.Assertions.*; import nl.jqno.equalsverifier.internal.exceptions.RecursionException; import nl.jqno.equalsverifier.internal.reflection.TypeTag; -import nl.jqno.equalsverifier.internal.reflection.instantiation.CachedValueProvider; -import nl.jqno.equalsverifier.internal.reflection.vintage.mutation.FactoryCacheFactory; import nl.jqno.equalsverifier.internal.testhelpers.ExpectedException; -import nl.jqno.equalsverifier.internal.testhelpers.TestValueProviders; +import nl.jqno.equalsverifier.testhelpers.FactoryCacheFactory; import nl.jqno.equalsverifier.testhelpers.types.Point; import nl.jqno.equalsverifier.testhelpers.types.RecursiveTypeHelper.*; import nl.jqno.equalsverifier.testhelpers.types.TypeHelper.EmptyEnum; @@ -31,17 +29,14 @@ public class VintageValueProviderCreatorTest { private static final TypeTag TWOSTEP_NODE_ARRAY_A_TAG = new TypeTag(TwoStepNodeArrayA.class); private Objenesis objenesis; - private CachedValueProvider cache; private FactoryCache factoryCache; private VintageValueProvider valueProvider; @BeforeEach public void setup() { objenesis = new ObjenesisStd(); - cache = new CachedValueProvider(); factoryCache = FactoryCacheFactory.withPrimitiveFactories(); - valueProvider = - new VintageValueProvider(TestValueProviders.empty(), cache, factoryCache, objenesis); + valueProvider = new VintageValueProvider(factoryCache, objenesis); } @Test @@ -51,6 +46,15 @@ public void simple() { assertFalse(red.equals(blue)); } + @Test + public void createSecondTimeIsNoOp() { + Point red = valueProvider.giveRed(POINT_TAG); + Point blue = valueProvider.giveBlue(POINT_TAG); + + assertSame(red, valueProvider.giveRed(POINT_TAG)); + assertSame(blue, valueProvider.giveBlue(POINT_TAG)); + } + @Test public void createEnum() { assertNotNull(valueProvider.giveRed(ENUM_TAG)); @@ -72,8 +76,7 @@ public void createEmptyEnum() { @Test public void oneStepRecursiveType() { factoryCache.put(Node.class, values(new Node(), new Node(), new Node())); - valueProvider = - new VintageValueProvider(TestValueProviders.empty(), cache, factoryCache, objenesis); + valueProvider = new VintageValueProvider(factoryCache, objenesis); valueProvider.giveRed(NODE_TAG); } @@ -90,8 +93,7 @@ public void oneStepRecursiveArrayType() { NodeArray.class, values(new NodeArray(), new NodeArray(), new NodeArray()) ); - valueProvider = - new VintageValueProvider(TestValueProviders.empty(), cache, factoryCache, objenesis); + valueProvider = new VintageValueProvider(factoryCache, objenesis); valueProvider.giveRed(NODE_ARRAY_TAG); } @@ -108,8 +110,7 @@ public void addTwoStepRecursiveType() { TwoStepNodeB.class, values(new TwoStepNodeB(), new TwoStepNodeB(), new TwoStepNodeB()) ); - valueProvider = - new VintageValueProvider(TestValueProviders.empty(), cache, factoryCache, objenesis); + valueProvider = new VintageValueProvider(factoryCache, objenesis); valueProvider.giveRed(TWOSTEP_NODE_A_TAG); } @@ -126,8 +127,7 @@ public void twoStepRecursiveArrayType() { TwoStepNodeArrayB.class, values(new TwoStepNodeArrayB(), new TwoStepNodeArrayB(), new TwoStepNodeArrayB()) ); - valueProvider = - new VintageValueProvider(TestValueProviders.empty(), cache, factoryCache, objenesis); + valueProvider = new VintageValueProvider(factoryCache, objenesis); valueProvider.giveRed(TWOSTEP_NODE_ARRAY_A_TAG); } diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/VintageValueProviderTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/VintageValueProviderTest.java similarity index 71% rename from equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/VintageValueProviderTest.java rename to equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/VintageValueProviderTest.java index dab6fa67e..b51060445 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/VintageValueProviderTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/VintageValueProviderTest.java @@ -1,20 +1,16 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage; +package nl.jqno.equalsverifier.internal.instantiation.vintage; -import static nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.Factories.values; +import static nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories.Factories.values; import static nl.jqno.equalsverifier.internal.testhelpers.Util.defaultEquals; import static nl.jqno.equalsverifier.internal.testhelpers.Util.defaultHashCode; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotSame; import static org.junit.jupiter.api.Assertions.fail; -import java.util.ArrayList; -import java.util.List; +import java.util.*; +import nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories.PrefabValueFactory; 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.vintage.prefabvalues.factories.PrefabValueFactory; -import nl.jqno.equalsverifier.internal.testhelpers.TestValueProviders; import nl.jqno.equalsverifier.testhelpers.types.Point; import nl.jqno.equalsverifier.testhelpers.types.ThrowingInitializer; import org.junit.jupiter.api.BeforeEach; @@ -29,21 +25,31 @@ public class VintageValueProviderTest { private static final TypeTag INT_TAG = new TypeTag(int.class); private Objenesis objenesis = new ObjenesisStd(); - private CachedValueProvider cache = new CachedValueProvider(); private FactoryCache factoryCache = new FactoryCache(); private VintageValueProvider vp; @BeforeEach public void setUp() { - factoryCache.put(String.class, values("r", "b", new String("r"))); + factoryCache.put(String.class, new AppendingStringTestFactory()); factoryCache.put(int.class, values(42, 1337, 42)); - vp = new VintageValueProvider(TestValueProviders.empty(), cache, factoryCache, objenesis); + vp = new VintageValueProvider(factoryCache, objenesis); + } + + @Test + public void sanityTestFactoryIncreasesStringLength() { + AppendingStringTestFactory f = new AppendingStringTestFactory(); + assertEquals("r", f.createValues(null, null, null).getRed()); + assertEquals("rr", f.createValues(null, null, null).getRed()); + assertEquals("rrr", f.createValues(null, null, null).getRed()); } @Test public void provide() { - Tuple actual = vp.provideOrThrow(POINT_TAG, Attributes.unlabeled()); - assertEquals(Tuple.of(new Point(42, 42), new Point(1337, 1337), new Point(42, 42)), actual); + Optional> actual = vp.provide(POINT_TAG); + assertEquals( + Tuple.of(new Point(42, 42), new Point(1337, 1337), new Point(42, 42)), + actual.get() + ); } @Test @@ -51,17 +57,36 @@ public void giveRedFromFactory() { assertEquals("r", vp.giveRed(STRING_TAG)); } + @Test + public void giveRedFromCache() { + vp.giveRed(STRING_TAG); + assertEquals("r", vp.giveRed(STRING_TAG)); + } + @Test public void giveBlueFromFactory() { assertEquals("b", vp.giveBlue(STRING_TAG)); } + @Test + public void giveBlueFromCache() { + vp.giveBlue(STRING_TAG); + assertEquals("b", vp.giveBlue(STRING_TAG)); + } + @Test public void giveRedCopyFromFactory() { assertEquals("r", vp.giveRedCopy(STRING_TAG)); assertNotSame(vp.giveRed(STRING_TAG), vp.giveRedCopy(STRING_TAG)); } + @Test + public void giveRedCopyFromCache() { + vp.giveRedCopy(STRING_TAG); + assertEquals("r", vp.giveRedCopy(STRING_TAG)); + assertNotSame(vp.giveRed(STRING_TAG), vp.giveRedCopy(STRING_TAG)); + } + @Test public void giveRedFromFallbackFactory() { Point actual = vp.giveRed(POINT_TAG); @@ -91,7 +116,7 @@ public void fallbackDoesNotAffectStaticFields() { @Test public void stringListIsSeparateFromIntegerList() { factoryCache.put(List.class, new ListTestFactory()); - vp = new VintageValueProvider(TestValueProviders.empty(), cache, factoryCache, objenesis); + vp = new VintageValueProvider(factoryCache, objenesis); List strings = vp.giveRed(new TypeTag(List.class, STRING_TAG)); List ints = vp.giveRed(new TypeTag(List.class, INT_TAG)); @@ -108,7 +133,7 @@ public void addingNullDoesntBreakAnything() { @Test public void addingATypeTwiceOverrulesTheExistingOne() { factoryCache.put(int.class, values(-1, -2, -1)); - vp = new VintageValueProvider(TestValueProviders.empty(), cache, factoryCache, objenesis); + vp = new VintageValueProvider(factoryCache, objenesis); assertEquals(-1, (int) vp.giveRed(INT_TAG)); assertEquals(-2, (int) vp.giveBlue(INT_TAG)); } @@ -117,7 +142,7 @@ public void addingATypeTwiceOverrulesTheExistingOne() { public void addLazyFactoryWorks() { TypeTag lazyTag = new TypeTag(Lazy.class); factoryCache.put(Lazy.class.getName(), values(Lazy.X, Lazy.Y, Lazy.X)); - vp = new VintageValueProvider(TestValueProviders.empty(), cache, factoryCache, objenesis); + vp = new VintageValueProvider(factoryCache, objenesis); assertEquals(Lazy.X, vp.giveRed(lazyTag)); assertEquals(Lazy.Y, vp.giveBlue(lazyTag)); assertEquals(Lazy.X, vp.giveRedCopy(lazyTag)); @@ -133,7 +158,7 @@ public void addLazyFactoryIsLazy() { (t, p, ts) -> Tuple.of(ThrowingInitializer.X, ThrowingInitializer.Y, ThrowingInitializer.X) ); - vp = new VintageValueProvider(TestValueProviders.empty(), cache, factoryCache, objenesis); + vp = new VintageValueProvider(factoryCache, objenesis); // Should throw, because `giveRed` does instantiate objects: try { @@ -167,6 +192,28 @@ public int hashCode() { } } + private static class AppendingStringTestFactory implements PrefabValueFactory { + + private String red; + private String blue; + + public AppendingStringTestFactory() { + red = ""; + blue = ""; + } + + @Override + public Tuple createValues( + TypeTag tag, + VintageValueProvider valueProvider, + LinkedHashSet typeStack + ) { + red += "r"; + blue += "b"; + return new Tuple<>(red, blue, new String(red)); + } + } + @SuppressWarnings("rawtypes") private static final class ListTestFactory implements PrefabValueFactory { @@ -175,7 +222,7 @@ private static final class ListTestFactory implements PrefabValueFactory { public Tuple createValues( TypeTag tag, VintageValueProvider valueProvider, - Attributes attributes + LinkedHashSet typeStack ) { TypeTag subtag = tag.getGenericTypes().get(0); @@ -200,6 +247,7 @@ private static final class StaticContainer { int regularInt = 3; } + @SuppressWarnings("unused") public static class Lazy { public static final Lazy X = new Lazy(1); 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/instantiation/vintage/prefabvalues/factories/AbstractGenericFactoryTest.java similarity index 83% rename from equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/AbstractGenericFactoryTest.java rename to equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factories/AbstractGenericFactoryTest.java index b35ebc104..84d0d7e92 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/instantiation/vintage/prefabvalues/factories/AbstractGenericFactoryTest.java @@ -1,14 +1,14 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories; +package nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories; import static nl.jqno.equalsverifier.internal.reflection.Util.classes; 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.instantiation.vintage.VintageValueProvider; 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.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, - Attributes attributes + LinkedHashSet typeStack ) { return Tuple.of("red", "blue", new String("red")); } diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/FactoriesTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factories/FactoriesTest.java similarity index 75% rename from equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/FactoriesTest.java rename to equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factories/FactoriesTest.java index 98337dd5a..17a113e7d 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/FactoriesTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factories/FactoriesTest.java @@ -1,4 +1,4 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories; +package nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories; import static nl.jqno.equalsverifier.internal.testhelpers.Util.coverThePrivateConstructor; 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/instantiation/vintage/prefabvalues/factories/FallbackFactoryTest.java similarity index 80% rename from equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/FallbackFactoryTest.java rename to equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factories/FallbackFactoryTest.java index 074f279ee..bb5d11a95 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/instantiation/vintage/prefabvalues/factories/FallbackFactoryTest.java @@ -1,21 +1,19 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories; +package nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories; -import static nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.Factories.values; +import static nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories.Factories.values; import static nl.jqno.equalsverifier.internal.testhelpers.Util.defaultEquals; import static nl.jqno.equalsverifier.internal.testhelpers.Util.defaultHashCode; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotSame; +import java.util.LinkedHashSet; import nl.jqno.equalsverifier.internal.exceptions.RecursionException; +import nl.jqno.equalsverifier.internal.instantiation.vintage.FactoryCache; +import nl.jqno.equalsverifier.internal.instantiation.vintage.VintageValueProvider; 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.vintage.FactoryCache; -import nl.jqno.equalsverifier.internal.reflection.vintage.VintageValueProvider; import nl.jqno.equalsverifier.internal.testhelpers.ExpectedException; -import nl.jqno.equalsverifier.internal.testhelpers.TestValueProviders; import nl.jqno.equalsverifier.testhelpers.types.RecursiveTypeHelper.Node; import nl.jqno.equalsverifier.testhelpers.types.RecursiveTypeHelper.NodeArray; import nl.jqno.equalsverifier.testhelpers.types.RecursiveTypeHelper.TwoStepNodeA; @@ -31,18 +29,16 @@ public class FallbackFactoryTest { private FallbackFactory factory; private VintageValueProvider valueProvider; - private Attributes attributes; + private LinkedHashSet typeStack; @BeforeEach public void setUp() { Objenesis objenesis = new ObjenesisStd(); factory = new FallbackFactory<>(objenesis); - CachedValueProvider cache = new CachedValueProvider(); FactoryCache factoryCache = new FactoryCache(); factoryCache.put(int.class, values(42, 1337, 42)); - valueProvider = - new VintageValueProvider(TestValueProviders.empty(), cache, factoryCache, objenesis); - attributes = Attributes.unlabeled(); + valueProvider = new VintageValueProvider(factoryCache, objenesis); + typeStack = new LinkedHashSet<>(); } @Test @@ -62,7 +58,7 @@ public void giveMultiElementEnum() { @Test public void giveArray() { - Tuple tuple = factory.createValues(new TypeTag(int[].class), valueProvider, attributes); + Tuple tuple = factory.createValues(new TypeTag(int[].class), valueProvider, typeStack); assertArrayEquals(new int[] { 42 }, (int[]) tuple.getRed()); assertArrayEquals(new int[] { 1337 }, (int[]) tuple.getBlue()); } @@ -84,7 +80,7 @@ public void redCopyIsNotSameAsRed() { Tuple tuple = factory.createValues( new TypeTag(IntContainer.class), valueProvider, - attributes + typeStack ); assertEquals(tuple.getRed(), tuple.getRedCopy()); @@ -94,7 +90,7 @@ public void redCopyIsNotSameAsRed() { @Test public void dontGiveRecursiveClass() { ExpectedException - .when(() -> factory.createValues(new TypeTag(Node.class), valueProvider, attributes)) + .when(() -> factory.createValues(new TypeTag(Node.class), valueProvider, typeStack)) .assertThrows(RecursionException.class); } @@ -102,7 +98,7 @@ public void dontGiveRecursiveClass() { public void dontGiveTwoStepRecursiveClass() { ExpectedException .when(() -> - factory.createValues(new TypeTag(TwoStepNodeA.class), valueProvider, attributes) + factory.createValues(new TypeTag(TwoStepNodeA.class), valueProvider, typeStack) ) .assertThrows(RecursionException.class) .assertDescriptionContains("TwoStepNodeA", "TwoStepNodeB"); @@ -111,14 +107,13 @@ public void dontGiveTwoStepRecursiveClass() { @Test public void dontGiveRecursiveArray() { ExpectedException - .when(() -> - factory.createValues(new TypeTag(NodeArray.class), valueProvider, attributes) + .when(() -> factory.createValues(new TypeTag(NodeArray.class), valueProvider, typeStack) ) .assertThrows(RecursionException.class); } private void assertCorrectTuple(Class type, T expectedRed, T expectedBlue) { - Tuple tuple = factory.createValues(new TypeTag(type), valueProvider, attributes); + Tuple tuple = factory.createValues(new TypeTag(type), valueProvider, typeStack); assertEquals(expectedRed, tuple.getRed()); assertEquals(expectedBlue, tuple.getBlue()); assertEquals(expectedRed, tuple.getRedCopy()); 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/instantiation/vintage/prefabvalues/factories/MapFactoryTest.java similarity index 77% rename from equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/MapFactoryTest.java rename to equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factories/MapFactoryTest.java index 664462bcf..da1f220e7 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/instantiation/vintage/prefabvalues/factories/MapFactoryTest.java @@ -1,16 +1,14 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories; +package nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories; 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.instantiation.JavaApiPrefabValues; +import nl.jqno.equalsverifier.internal.instantiation.vintage.VintageValueProvider; 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.vintage.VintageValueProvider; -import nl.jqno.equalsverifier.internal.testhelpers.TestValueProviders; import nl.jqno.equalsverifier.testhelpers.types.TypeHelper.OneElementEnum; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -41,7 +39,7 @@ public class MapFactoryTest { private static final MapFactory MAP_FACTORY = new MapFactory<>(HashMap::new); - private final Attributes attributes = Attributes.unlabeled(); + private final LinkedHashSet typeStack = new LinkedHashSet<>(); private VintageValueProvider valueProvider; private String red; private String blue; @@ -51,13 +49,7 @@ public class MapFactoryTest { @BeforeEach public void setUp() { - valueProvider = - new VintageValueProvider( - TestValueProviders.empty(), - new CachedValueProvider(), - JavaApiPrefabValues.build(), - new ObjenesisStd() - ); + valueProvider = new VintageValueProvider(JavaApiPrefabValues.build(), new ObjenesisStd()); red = valueProvider.giveRed(STRING_TYPETAG); blue = valueProvider.giveBlue(STRING_TYPETAG); redObject = valueProvider.giveRed(OBJECT_TYPETAG); @@ -70,7 +62,7 @@ public void createMapsOfStringToString() { Tuple tuple = MAP_FACTORY.createValues( STRINGSTRINGMAP_TYPETAG, valueProvider, - attributes + typeStack ); assertEquals(mapOf(red, blue), tuple.getRed()); assertEquals(mapOf(blue, blue), tuple.getBlue()); @@ -78,14 +70,14 @@ public void createMapsOfStringToString() { @Test public void createMapsOfWildcard() { - Tuple tuple = MAP_FACTORY.createValues(WILDCARDMAP_TYPETAG, valueProvider, attributes); + Tuple tuple = MAP_FACTORY.createValues(WILDCARDMAP_TYPETAG, valueProvider, typeStack); assertEquals(mapOf(redObject, blueObject), tuple.getRed()); assertEquals(mapOf(blueObject, blueObject), tuple.getBlue()); } @Test public void createRawMaps() { - Tuple tuple = MAP_FACTORY.createValues(RAWMAP_TYPETAG, valueProvider, attributes); + Tuple tuple = MAP_FACTORY.createValues(RAWMAP_TYPETAG, valueProvider, typeStack); assertEquals(mapOf(redObject, blueObject), tuple.getRed()); assertEquals(mapOf(blueObject, blueObject), tuple.getBlue()); } @@ -95,7 +87,7 @@ public void createMapOfOneElementEnumKey() { Tuple tuple = MAP_FACTORY.createValues( ONEELEMENTENUMKEYMAP_TYPETAG, valueProvider, - attributes + typeStack ); 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/SimpleFactoryTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factories/SimpleFactoryTest.java similarity index 89% rename from equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/SimpleFactoryTest.java rename to equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factories/SimpleFactoryTest.java index 3adabc58f..7eec1bf30 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/SimpleFactoryTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factories/SimpleFactoryTest.java @@ -1,4 +1,4 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories; +package nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotSame; 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/instantiation/vintage/prefabvalues/factories/SimpleGenericFactoryTest.java similarity index 78% rename from equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/SimpleGenericFactoryTest.java rename to equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factories/SimpleGenericFactoryTest.java index 1065d71e2..7f7ba2b29 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/instantiation/vintage/prefabvalues/factories/SimpleGenericFactoryTest.java @@ -1,15 +1,13 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories; +package nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories; 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.instantiation.JavaApiPrefabValues; +import nl.jqno.equalsverifier.internal.instantiation.vintage.VintageValueProvider; 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.vintage.VintageValueProvider; -import nl.jqno.equalsverifier.internal.testhelpers.TestValueProviders; import nl.jqno.equalsverifier.testhelpers.types.Pair; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -42,7 +40,7 @@ public class SimpleGenericFactoryTest { ); private static final PrefabValueFactory PAIR_FACTORY = Factories.simple(Pair::new, null); - private final Attributes attributes = Attributes.unlabeled(); + private final LinkedHashSet typeStack = new LinkedHashSet<>(); private VintageValueProvider valueProvider; private String redString; private String blueString; @@ -53,13 +51,7 @@ public class SimpleGenericFactoryTest { @BeforeEach public void setUp() { - valueProvider = - new VintageValueProvider( - TestValueProviders.empty(), - new CachedValueProvider(), - JavaApiPrefabValues.build(), - new ObjenesisStd() - ); + valueProvider = new VintageValueProvider(JavaApiPrefabValues.build(), new ObjenesisStd()); redString = valueProvider.giveRed(STRING_TYPETAG); blueString = valueProvider.giveBlue(STRING_TYPETAG); redInt = valueProvider.giveRed(INTEGER_TYPETAG); @@ -73,7 +65,7 @@ public void createOptionalsOfMapOfString() { Tuple tuple = OPTIONAL_FACTORY.createValues( STRINGOPTIONAL_TYPETAG, valueProvider, - attributes + typeStack ); assertEquals(Optional.of(redString), tuple.getRed()); assertEquals(Optional.of(blueString), tuple.getBlue()); @@ -84,7 +76,7 @@ public void createOptionalsOfWildcard() { Tuple tuple = OPTIONAL_FACTORY.createValues( WILDCARDOPTIONAL_TYPETAG, valueProvider, - attributes + typeStack ); assertEquals(Optional.of(redObject), tuple.getRed()); assertEquals(Optional.of(blueObject), tuple.getBlue()); @@ -95,7 +87,7 @@ public void createRawOptionals() { Tuple tuple = OPTIONAL_FACTORY.createValues( RAWOPTIONAL_TYPETAG, valueProvider, - attributes + typeStack ); assertEquals(Optional.of(redObject), tuple.getRed()); assertEquals(Optional.of(blueObject), tuple.getBlue()); @@ -103,7 +95,7 @@ public void createRawOptionals() { @Test public void createSomethingWithMoreThanOneTypeParameter() { - Tuple tuple = PAIR_FACTORY.createValues(PAIR_TYPETAG, valueProvider, attributes); + Tuple tuple = PAIR_FACTORY.createValues(PAIR_TYPETAG, valueProvider, typeStack); assertEquals(new Pair<>(redString, redInt), tuple.getRed()); assertEquals(new Pair<>(blueString, blueInt), tuple.getBlue()); } diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factoryproviders/JavaFxFactoryProviderTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factoryproviders/JavaFxFactoryProviderTest.java similarity index 70% rename from equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factoryproviders/JavaFxFactoryProviderTest.java rename to equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factoryproviders/JavaFxFactoryProviderTest.java index f782fae61..ec5218593 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factoryproviders/JavaFxFactoryProviderTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/prefabvalues/factoryproviders/JavaFxFactoryProviderTest.java @@ -1,19 +1,16 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factoryproviders; +package nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factoryproviders; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import java.util.List; import java.util.Map; -import nl.jqno.equalsverifier.internal.reflection.JavaApiPrefabValues; +import nl.jqno.equalsverifier.internal.instantiation.JavaApiPrefabValues; +import nl.jqno.equalsverifier.internal.instantiation.vintage.VintageValueProvider; +import nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories.PrefabValueFactory; +import nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factoryproviders.JavaFxFactoryProvider.PropertyFactory; 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.vintage.VintageValueProvider; -import nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.PrefabValueFactory; -import nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factoryproviders.JavaFxFactoryProvider.PropertyFactory; -import nl.jqno.equalsverifier.internal.testhelpers.TestValueProviders; import nl.jqno.equalsverifier.testhelpers.types.Point; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -26,13 +23,7 @@ public class JavaFxFactoryProviderTest { @BeforeEach public void setUp() { - valueProvider = - new VintageValueProvider( - TestValueProviders.empty(), - new CachedValueProvider(), - JavaApiPrefabValues.build(), - new ObjenesisStd() - ); + valueProvider = new VintageValueProvider(JavaApiPrefabValues.build(), new ObjenesisStd()); } @Test @@ -49,11 +40,7 @@ public void createInstancesWithCorrectSingleGenericParameter() { GenericContainer.class.getName(), List.class ); - Tuple tuple = factory.createValues( - tag, - valueProvider, - Attributes.unlabeled() - ); + Tuple tuple = factory.createValues(tag, valueProvider, null); assertEquals(valueProvider.giveRed(listTag), tuple.getRed().t); assertEquals(valueProvider.giveBlue(listTag), tuple.getBlue().t); @@ -77,11 +64,7 @@ public void createInstancesWithCorrectMultipleGenericParameter() { GenericMultiContainer.class.getName(), Map.class ); - Tuple tuple = factory.createValues( - tag, - valueProvider, - Attributes.unlabeled() - ); + Tuple tuple = factory.createValues(tag, valueProvider, null); assertEquals(valueProvider.giveRed(mapTag), tuple.getRed().t); assertEquals(valueProvider.giveBlue(mapTag), tuple.getBlue().t); diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/ClassAccessorTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/ClassAccessorTest.java similarity index 65% rename from equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/ClassAccessorTest.java rename to equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/ClassAccessorTest.java index 17cb4b1e0..912287180 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/ClassAccessorTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/ClassAccessorTest.java @@ -1,17 +1,15 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage.mutation; +package nl.jqno.equalsverifier.internal.instantiation.vintage.reflection; -import static nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.Factories.values; +import static nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories.Factories.values; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; -import nl.jqno.equalsverifier.internal.reflection.JavaApiPrefabValues; +import java.util.LinkedHashSet; +import nl.jqno.equalsverifier.internal.instantiation.JavaApiPrefabValues; +import nl.jqno.equalsverifier.internal.instantiation.vintage.FactoryCache; +import nl.jqno.equalsverifier.internal.instantiation.vintage.VintageValueProvider; 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.vintage.FactoryCache; -import nl.jqno.equalsverifier.internal.reflection.vintage.VintageValueProvider; -import nl.jqno.equalsverifier.internal.testhelpers.TestValueProviders; import nl.jqno.equalsverifier.testhelpers.types.PointContainer; import nl.jqno.equalsverifier.testhelpers.types.RecursiveTypeHelper.TwoStepNodeA; import nl.jqno.equalsverifier.testhelpers.types.RecursiveTypeHelper.TwoStepNodeB; @@ -23,23 +21,19 @@ public class ClassAccessorTest { - private Attributes empty; + private LinkedHashSet empty; private Objenesis objenesis; - private CachedValueProvider cache; private FactoryCache factoryCache; private VintageValueProvider valueProvider; private ClassAccessor pointContainerAccessor; @BeforeEach public void setup() { - empty = Attributes.unlabeled(); + empty = new LinkedHashSet<>(); objenesis = new ObjenesisStd(); - cache = new CachedValueProvider(); factoryCache = JavaApiPrefabValues.build(); - valueProvider = - new VintageValueProvider(TestValueProviders.empty(), cache, factoryCache, objenesis); - pointContainerAccessor = - new ClassAccessor<>(PointContainer.class, valueProvider, objenesis); + valueProvider = new VintageValueProvider(factoryCache, objenesis); + pointContainerAccessor = ClassAccessor.of(PointContainer.class, valueProvider, objenesis); } @Test @@ -50,7 +44,7 @@ public void getRedObject() { @Test @SuppressWarnings("rawtypes") public void getRedObjectGeneric() { - ClassAccessor accessor = new ClassAccessor<>( + ClassAccessor accessor = ClassAccessor.of( GenericTypeVariableListContainer.class, valueProvider, objenesis @@ -62,6 +56,16 @@ public void getRedObjectGeneric() { assertEquals(String.class, foo.tList.get(0).getClass()); } + @Test + public void getRedAccessor() { + PointContainer foo = pointContainerAccessor.getRedObject(TypeTag.NULL, empty); + ObjectAccessor objectAccessor = pointContainerAccessor.getRedAccessor( + TypeTag.NULL, + empty + ); + assertEquals(foo, objectAccessor.get()); + } + @Test public void getBlueObject() { assertObjectHasNoNullFields(pointContainerAccessor.getBlueObject(TypeTag.NULL, empty)); @@ -70,7 +74,7 @@ public void getBlueObject() { @Test @SuppressWarnings("rawtypes") public void getBlueObjectGeneric() { - ClassAccessor accessor = new ClassAccessor<>( + ClassAccessor accessor = ClassAccessor.of( GenericTypeVariableListContainer.class, valueProvider, objenesis @@ -82,6 +86,16 @@ public void getBlueObjectGeneric() { assertEquals(String.class, foo.tList.get(0).getClass()); } + @Test + public void getBlueAccessor() { + PointContainer foo = pointContainerAccessor.getBlueObject(TypeTag.NULL, empty); + ObjectAccessor objectAccessor = pointContainerAccessor.getBlueAccessor( + TypeTag.NULL, + empty + ); + assertEquals(foo, objectAccessor.get()); + } + @Test public void redAndBlueNotEqual() { PointContainer red = pointContainerAccessor.getRedObject(TypeTag.NULL, empty); @@ -91,13 +105,15 @@ public void redAndBlueNotEqual() { @Test public void instantiateAllTypes() { - new ClassAccessor<>(AllTypesContainer.class, valueProvider, objenesis) + ClassAccessor + .of(AllTypesContainer.class, valueProvider, objenesis) .getRedObject(TypeTag.NULL, empty); } @Test public void instantiateArrayTypes() { - new ClassAccessor<>(AllArrayTypesContainer.class, valueProvider, objenesis) + ClassAccessor + .of(AllArrayTypesContainer.class, valueProvider, objenesis) .getRedObject(TypeTag.NULL, empty); } @@ -107,27 +123,29 @@ public void instantiateRecursiveTypeUsingPrefabValue() { TwoStepNodeB.class, values(new TwoStepNodeB(), new TwoStepNodeB(), new TwoStepNodeB()) ); - valueProvider = - new VintageValueProvider(TestValueProviders.empty(), cache, factoryCache, objenesis); - new ClassAccessor<>(TwoStepNodeA.class, valueProvider, objenesis) + valueProvider = new VintageValueProvider(factoryCache, objenesis); + ClassAccessor + .of(TwoStepNodeA.class, valueProvider, objenesis) .getRedObject(TypeTag.NULL, empty); } @Test public void instantiateInterfaceField() { - new ClassAccessor<>(InterfaceContainer.class, valueProvider, objenesis) + ClassAccessor + .of(InterfaceContainer.class, valueProvider, objenesis) .getRedObject(TypeTag.NULL, empty); } @Test public void instantiateAbstractClassField() { - new ClassAccessor<>(AbstractClassContainer.class, valueProvider, objenesis) + ClassAccessor + .of(AbstractClassContainer.class, valueProvider, objenesis) .getRedObject(TypeTag.NULL, empty); } @Test public void anInvalidTypeShouldNotThrowAnExceptionUponCreation() { - new ClassAccessor<>(null, valueProvider, objenesis); + ClassAccessor.of(null, valueProvider, objenesis); } private void assertObjectHasNoNullFields(PointContainer foo) { diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/FieldModifierTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/FieldModifierTest.java similarity index 95% rename from equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/FieldModifierTest.java rename to equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/FieldModifierTest.java index 060709d09..7746c1702 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/FieldModifierTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/FieldModifierTest.java @@ -1,4 +1,4 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage.mutation; +package nl.jqno.equalsverifier.internal.instantiation.vintage.reflection; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertSame; diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/InPlaceObjectAccessorCopyingTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/InPlaceObjectAccessorCopyingTest.java similarity index 97% rename from equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/InPlaceObjectAccessorCopyingTest.java rename to equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/InPlaceObjectAccessorCopyingTest.java index 71fa1f1aa..5ec8b577a 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/InPlaceObjectAccessorCopyingTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/InPlaceObjectAccessorCopyingTest.java @@ -1,4 +1,4 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage.mutation; +package nl.jqno.equalsverifier.internal.instantiation.vintage.reflection; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotSame; diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/InPlaceObjectAccessorScramblingTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/InPlaceObjectAccessorScramblingTest.java similarity index 85% rename from equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/InPlaceObjectAccessorScramblingTest.java rename to equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/InPlaceObjectAccessorScramblingTest.java index f6e6aca62..8a826a5d5 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/InPlaceObjectAccessorScramblingTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/InPlaceObjectAccessorScramblingTest.java @@ -1,20 +1,18 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage.mutation; +package nl.jqno.equalsverifier.internal.instantiation.vintage.reflection; -import static nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.Factories.values; +import static nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories.Factories.values; import static org.junit.jupiter.api.Assertions.*; 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.instantiation.JavaApiPrefabValues; +import nl.jqno.equalsverifier.internal.instantiation.vintage.FactoryCache; +import nl.jqno.equalsverifier.internal.instantiation.vintage.VintageValueProvider; 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.vintage.FactoryCache; -import nl.jqno.equalsverifier.internal.reflection.vintage.VintageValueProvider; import nl.jqno.equalsverifier.internal.testhelpers.ExpectedException; -import nl.jqno.equalsverifier.internal.testhelpers.TestValueProviders; import nl.jqno.equalsverifier.testhelpers.types.Point; import nl.jqno.equalsverifier.testhelpers.types.Point3D; import nl.jqno.equalsverifier.testhelpers.types.TypeHelper.StaticFinalContainer; @@ -27,7 +25,7 @@ public class InPlaceObjectAccessorScramblingTest { - private static final Attributes EMPTY_ATTRIBUTES = Attributes.unlabeled(); + private static final LinkedHashSet EMPTY_TYPE_STACK = new LinkedHashSet<>(); private Objenesis objenesis; private VintageValueProvider valueProviderTest; @@ -36,13 +34,7 @@ public void setup() { FactoryCache factoryCache = JavaApiPrefabValues.build(); factoryCache.put(Point.class, values(new Point(1, 2), new Point(2, 3), new Point(1, 2))); objenesis = new ObjenesisStd(); - valueProviderTest = - new VintageValueProvider( - TestValueProviders.empty(), - new CachedValueProvider(), - factoryCache, - objenesis - ); + valueProviderTest = new VintageValueProvider(factoryCache, objenesis); } @Test @@ -162,7 +154,7 @@ private T copy(T object) { } private ObjectAccessor doScramble(Object object) { - return create(object).scramble(valueProviderTest, TypeTag.NULL, EMPTY_ATTRIBUTES); + return create(object).scramble(valueProviderTest, TypeTag.NULL, EMPTY_TYPE_STACK); } static final class StringContainer { diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/InPlaceObjectAccessorTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/InPlaceObjectAccessorTest.java similarity index 91% rename from equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/InPlaceObjectAccessorTest.java rename to equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/InPlaceObjectAccessorTest.java index b44d77d52..77a4a7c52 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/InPlaceObjectAccessorTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/instantiation/vintage/reflection/InPlaceObjectAccessorTest.java @@ -1,4 +1,4 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage.mutation; +package nl.jqno.equalsverifier.internal.instantiation.vintage.reflection; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertTrue; diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/FieldCacheTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/FieldCacheTest.java new file mode 100644 index 000000000..5d09df19a --- /dev/null +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/FieldCacheTest.java @@ -0,0 +1,61 @@ +package nl.jqno.equalsverifier.internal.reflection; + +import static org.junit.jupiter.api.Assertions.*; + +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; +import org.junit.jupiter.api.Test; + +public class FieldCacheTest { + + private String stringField = "string"; + private final Tuple stringValues = new Tuple<>("red", "blue", "red"); + + private String intField = "int"; + private final Tuple intValues = new Tuple<>(1, 2, 1); + + private FieldCache cache = new FieldCache(); + + @Test + public void putAndGetTuple() { + cache.put(stringField, stringValues); + assertEquals(stringValues, cache.get(stringField)); + } + + @Test + public void putTwiceAndGetBoth() { + cache.put(stringField, stringValues); + cache.put(intField, intValues); + + assertEquals(intValues, cache.get(intField)); + assertEquals(stringValues, cache.get(stringField)); + } + + @Test + public void putNullAndGetNothingBack() { + cache.put(null, stringValues); + assertNull(cache.get(null)); + } + + @Test + public void contains() { + cache.put(stringField, stringValues); + assertTrue(cache.contains(stringField)); + } + + @Test + public void doesntContain() { + assertFalse(cache.contains(stringField)); + } + + @Test + public void getFieldNames() { + assertEquals(Collections.emptySet(), cache.getFieldNames()); + + cache.put(stringField, stringValues); + Set expected = new HashSet<>(); + expected.add(stringField); + assertEquals(expected, cache.getFieldNames()); + } +} diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/TupleTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/TupleTest.java index 8a59ccd51..ec04bba0b 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/TupleTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/TupleTest.java @@ -36,16 +36,4 @@ public void redAndRedCopyInvariant() { assertEquals(tuple.getRed(), tuple.getRedCopy()); assertNotSame(tuple.getRed(), tuple.getRedCopy()); } - - @Test - public void map() { - assertEquals(Tuple.of("redx", "bluex", "redx"), tuple.map(s -> s + "x")); - } - - @Test - public void combine() { - Tuple ints = Tuple.of(2, 3, 2); - Tuple actual = Tuple.combine(tuple, ints, (s, n) -> s + n); - assertEquals(Tuple.of("red2", "blue3", "red2"), actual); - } } diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/CachedValueProviderTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/CachedValueProviderTest.java deleted file mode 100644 index db4c5d5c9..000000000 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/CachedValueProviderTest.java +++ /dev/null @@ -1,62 +0,0 @@ -package nl.jqno.equalsverifier.internal.reflection.instantiation; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.util.Optional; -import nl.jqno.equalsverifier.EqualsVerifier; -import nl.jqno.equalsverifier.internal.reflection.Tuple; -import nl.jqno.equalsverifier.internal.reflection.TypeTag; -import nl.jqno.equalsverifier.internal.reflection.instantiation.CachedValueProvider.Key; -import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes; -import org.junit.jupiter.api.Test; - -public class CachedValueProviderTest { - - private static final TypeTag INT = new TypeTag(int.class); - - private CachedValueProvider sut = new CachedValueProvider(); - - @Test - public void aRegisteredValueCanBeFound() { - sut.put(INT, null, Tuple.of(3, 2, 3)); - assertEquals(Tuple.of(3, 2, 3), sut.provide(INT, Attributes.unlabeled()).get()); - } - - @Test - public void aValueRegisteredWithALabelCanBeFoundUnderThatLabel() { - sut.put(INT, "label", Tuple.of(3, 2, 3)); - assertEquals(Tuple.of(3, 2, 3), sut.provide(INT, Attributes.labeled("label")).get()); - } - - @Test - public void aValueRegisteredWithALabelCanNotBeFoundWithoutThatLabel() { - sut.put(INT, "label", Tuple.of(3, 2, 3)); - assertEquals(Optional.empty(), sut.provide(INT, Attributes.unlabeled())); - } - - @Test - public void aQueryWithLabelFallsBackToRegisteredValueWithoutLabel() { - sut.put(INT, null, Tuple.of(3, 2, 3)); - assertEquals(Tuple.of(3, 2, 3), sut.provide(INT, Attributes.labeled("label")).get()); - } - - @Test - public void aQueryWithLabelPrefersRegisteredValueWithThatLabel() { - sut.put(INT, null, Tuple.of(3, 2, 3)); - sut.put(INT, "label", Tuple.of(4, 3, 4)); - assertEquals(Tuple.of(4, 3, 4), sut.provide(INT, Attributes.labeled("label")).get()); - } - - @Test - public void anUnregisteredValueCanNotBeFound() { - assertEquals(Optional.empty(), sut.provide(INT, Attributes.unlabeled())); - } - - @Test - public void key() { - EqualsVerifier - .forClass(Key.class) - .withPrefabValues(TypeTag.class, new TypeTag(String.class), new TypeTag(Integer.class)) - .verify(); - } -} 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 deleted file mode 100644 index 9f092f438..000000000 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/ChainedValueProviderTest.java +++ /dev/null @@ -1,124 +0,0 @@ -package nl.jqno.equalsverifier.internal.reflection.instantiation; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -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<>( - int.class, - 1, - 2, - 1 - ); - private final SingleTypeValueProvider stringProvider = new SingleTypeValueProvider<>( - String.class, - "a", - "b", - "a" - ); - - private final CachedValueProvider cache = new CachedValueProvider(); - private final ChainedValueProvider sut = new ChainedValueProvider(cache); - - @Test - public void returnsValueIfMatch() { - sut.register(intProvider); - assertEquals(1, sut.provideOrThrow(INT, EMPTY_ATTRIBUTES).getRed()); - } - - @Test - public void returnsEmptyIfNoMatch() { - sut.register(stringProvider); - assertEquals(Optional.empty(), sut.provide(INT, EMPTY_ATTRIBUTES)); - } - - @Test - public void throwsExceptionIfNoMatch() { - sut.register(stringProvider); - ExpectedException - .when(() -> sut.provideOrThrow(INT, EMPTY_ATTRIBUTES)) - .assertThrows(NoValueException.class) - .assertDescriptionContains("Could not find a value for int"); - } - - @Test - public void skipsNonMatchingValue() { - sut.register(stringProvider, intProvider); - assertEquals(1, sut.provideOrThrow(INT, EMPTY_ATTRIBUTES).getRed()); - assertEquals(1, stringProvider.called); - assertEquals(1, intProvider.called); - } - - @Test - public void returnsValueFromFirstMatch() { - SingleTypeValueProvider anotherIntProvider = new SingleTypeValueProvider( - int.class, - 1, - 2, - 1 - ); - sut.register(intProvider, anotherIntProvider); - assertEquals(1, sut.provideOrThrow(INT, EMPTY_ATTRIBUTES).getRed()); - assertEquals(1, intProvider.called); - assertEquals(0, anotherIntProvider.called); - } - - @Test - public void updatesTheCache() { - sut.register(intProvider); - sut.provideOrThrow(INT, EMPTY_ATTRIBUTES); - - assertEquals(1, cache.provideOrThrow(INT, EMPTY_ATTRIBUTES).getRed()); - } - - @Test - public void makesDefensiveCopy() { - ValueProvider[] providers = { stringProvider }; - sut.register(providers); - providers[0] = intProvider; - assertEquals(Optional.empty(), sut.provide(INT, EMPTY_ATTRIBUTES)); - } - - @Test - public void locksAfterFirstAssignment() { - sut.register(intProvider); - ExpectedException - .when(() -> sut.register(stringProvider)) - .assertThrows(EqualsVerifierInternalBugException.class) - .assertMessageContains("Provider is locked"); - } - - static class SingleTypeValueProvider implements ValueProvider { - - private final Class type; - private final Tuple values; - private int called = 0; - - public SingleTypeValueProvider(Class type, X red, X blue, X redCopy) { - this.type = type; - this.values = Tuple.of(red, blue, redCopy); - } - - @Override - @SuppressWarnings("unchecked") - public Optional> provide(TypeTag tag, Attributes attributes) { - called++; - if (tag.getType().equals(type)) { - return Optional.of((Tuple) values); - } - return Optional.empty(); - } - } -} diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/util/ValidationsTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/util/ValidationsTest.java index a4fd8efd6..3906f1183 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/util/ValidationsTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/util/ValidationsTest.java @@ -4,8 +4,10 @@ import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -import java.lang.reflect.Field; -import java.util.*; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; import nl.jqno.equalsverifier.internal.testhelpers.ExpectedException; import org.junit.jupiter.api.Test; @@ -23,7 +25,8 @@ public void validateFieldTypeMatches_shouldFailOnWrongType() { ExpectedException .when(() -> Validations.validateFieldTypeMatches( - getField(TestContainer.class, "listField"), + TestContainer.class, + "listField", HashSet.class ) ) @@ -34,7 +37,8 @@ public void validateFieldTypeMatches_shouldFailOnWrongType() { ExpectedException .when(() -> Validations.validateFieldTypeMatches( - getField(TestContainer.class, "objectField"), + TestContainer.class, + "objectField", int.class ) ) @@ -45,7 +49,8 @@ public void validateFieldTypeMatches_shouldFailOnWrongType() { ExpectedException .when(() -> Validations.validateFieldTypeMatches( - getField(TestContainer.class, "charsField"), + TestContainer.class, + "charsField", Character.class ) ) @@ -62,7 +67,8 @@ public void validateFieldTypeMatches_shouldAllowSubTypes() { assertDoesNotThrow( () -> Validations.validateFieldTypeMatches( - getField(TestContainer.class, "listField"), + TestContainer.class, + "listField", ArrayList.class ), "Should allow ArrayList as a List" @@ -71,7 +77,8 @@ public void validateFieldTypeMatches_shouldAllowSubTypes() { assertDoesNotThrow( () -> Validations.validateFieldTypeMatches( - getField(TestContainer.class, "objectField"), + TestContainer.class, + "objectField", Integer.class ), "Should allow Integer as an Object" @@ -80,7 +87,8 @@ public void validateFieldTypeMatches_shouldAllowSubTypes() { assertDoesNotThrow( () -> Validations.validateFieldTypeMatches( - getField(TestContainer.class, "charsField"), + TestContainer.class, + "charsField", String.class ), "Should allow String as a CharSequence" @@ -95,7 +103,8 @@ public void validateFieldTypeMatches_shouldFailOnSuperTypes() { ExpectedException .when(() -> Validations.validateFieldTypeMatches( - getField(TestContainer.class, "listField"), + TestContainer.class, + "listField", Collection.class ) ) @@ -106,7 +115,8 @@ public void validateFieldTypeMatches_shouldFailOnSuperTypes() { ExpectedException .when(() -> Validations.validateFieldTypeMatches( - getField(TestContainer.class, "charsField"), + TestContainer.class, + "charsField", Object.class ) ) @@ -116,14 +126,6 @@ public void validateFieldTypeMatches_shouldFailOnSuperTypes() { ); } - private Field getField(Class type, String fieldName) { - try { - return type.getDeclaredField(fieldName); - } catch (NoSuchFieldException e) { - throw new AssertionError("Class " + type + " has no field " + fieldName); - } - } - @SuppressWarnings("unused") private static final class TestContainer { diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/FactoryCacheFactory.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/testhelpers/FactoryCacheFactory.java similarity index 75% rename from equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/FactoryCacheFactory.java rename to equalsverifier-core/src/test/java/nl/jqno/equalsverifier/testhelpers/FactoryCacheFactory.java index 9a07e1245..b8f31e641 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/mutation/FactoryCacheFactory.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/testhelpers/FactoryCacheFactory.java @@ -1,8 +1,8 @@ -package nl.jqno.equalsverifier.internal.reflection.vintage.mutation; +package nl.jqno.equalsverifier.testhelpers; -import static nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.Factories.values; +import static nl.jqno.equalsverifier.internal.instantiation.vintage.prefabvalues.factories.Factories.values; -import nl.jqno.equalsverifier.internal.reflection.vintage.FactoryCache; +import nl.jqno.equalsverifier.internal.instantiation.vintage.FactoryCache; public final class FactoryCacheFactory { diff --git a/equalsverifier-release-verify/src/test/java/nl/jqno/equalsverifier/verify_release/jar/helper/JarAsserter.java b/equalsverifier-release-verify/src/test/java/nl/jqno/equalsverifier/verify_release/jar/helper/JarAsserter.java index be86ac31b..3c21c6a96 100644 --- a/equalsverifier-release-verify/src/test/java/nl/jqno/equalsverifier/verify_release/jar/helper/JarAsserter.java +++ b/equalsverifier-release-verify/src/test/java/nl/jqno/equalsverifier/verify_release/jar/helper/JarAsserter.java @@ -23,7 +23,7 @@ public JarAsserter(JarReader reader) { public void assertPresenceOfCoreClasses() { assertPresenceOf( EV + "/EqualsVerifier.class", - EV + "/internal/reflection/vintage/mutation/ClassAccessor.class", + EV + "/internal/reflection/ClassProbe.class", EV + "/internal/checkers/HierarchyChecker.class" ); }