Skip to content

Commit

Permalink
Replaces ClassProbe constructor with a factory method
Browse files Browse the repository at this point in the history
  • Loading branch information
jqno committed Dec 18, 2024
1 parent 5cc40e8 commit 8f3f651
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class RecordInstanceCreatorTest {

@Test
public void instanceCreator() throws NoSuchFieldException {
ClassProbe<SomeRecord> probe = new ClassProbe<>(SomeRecord.class);
ClassProbe<SomeRecord> probe = ClassProbe.of(SomeRecord.class);
InstanceCreator<SomeRecord> sut = new InstanceCreator<>(probe, new ObjenesisStd());

Field x = SomeRecord.class.getDeclaredField("x");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class ClassProbeRecordTest {

@Test
public void isRecord() {
var probe = new ClassProbe<>(SimpleRecord.class);
var probe = ClassProbe.of(SimpleRecord.class);
assertTrue(probe.isRecord());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ public class ClassProbeSealedTest {

@Test
public void isNotSealed() {
var probe = new ClassProbe<>(SealedChild.class);
var probe = ClassProbe.of(SealedChild.class);
assertFalse(probe.isSealed());
}

@Test
public void isSealed() {
var probe = new ClassProbe<>(SealedParent.class);
var probe = ClassProbe.of(SealedParent.class);
assertTrue(probe.isSealed());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private void checkValueReflexivity(FieldProbe probe) {
if (probe.isStatic()) {
return;
}
ClassProbe<?> fieldTypeProbe = new ClassProbe<>(fieldType);
ClassProbe<?> fieldTypeProbe = ClassProbe.of(fieldType);
if (!fieldTypeProbe.declaresEquals()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public SubjectCreator(
this.type = typeTag.getType();
this.config = config;
this.valueProvider = valueProvider;
this.classProbe = new ClassProbe<>(type);
this.classProbe = ClassProbe.of(type);
this.fieldCache = fieldCache;
this.objenesis = objenesis;
this.instanceCreator = new InstanceCreator<>(classProbe, objenesis);
Expand Down Expand Up @@ -173,8 +173,7 @@ public T copy(T original) {
* @return An instance of the givenoriginal's superclass, but otherwise a copy of the original.
*/
public Object copyIntoSuperclass(T original) {
InstanceCreator<? super T> superCreator =
new InstanceCreator<>(new ClassProbe<>(type.getSuperclass()), objenesis);
InstanceCreator<? super T> superCreator = new InstanceCreator<>(ClassProbe.of(type.getSuperclass()), objenesis);
return superCreator.copy(original);
}

Expand All @@ -189,7 +188,7 @@ public Object copyIntoSuperclass(T original) {
* @return An instance of the given subType, but otherwise a copy of the given original.
*/
public <S extends T> S copyIntoSubclass(T original, Class<S> subType) {
InstanceCreator<S> subCreator = new InstanceCreator<>(new ClassProbe<>(subType), objenesis);
InstanceCreator<S> subCreator = new InstanceCreator<>(ClassProbe.of(subType), objenesis);
return subCreator.copy(original);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,26 @@
/**
* Provides read-only reflective access to a class.
*/
public class ClassProbe<T> {
public final class ClassProbe<T> {

private final Class<T> type;

public ClassProbe(Class<T> type) {
/** Private constructor. Call {@link #of(Class)} instead. */
private ClassProbe(Class<T> type) {
this.type = type;
}

/**
* Factory method.
*
* @param <T> The class on which {@link ClassProbe} operates.
* @param type The class on which {@link ClassProbe} operates.
* @return A {@link ClassProbe} for T.
*/
public static <T> ClassProbe<T> of(Class<T> type) {
return new ClassProbe<>(type);
}

/** @return The class on which {@link ClassProbe} operates. */
public Class<T> getType() {
return type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public Context(
Objenesis objenesis) {
this.type = configuration.getType();
this.configuration = configuration;
this.classProbe = new ClassProbe<>(configuration.getType());
this.classProbe = ClassProbe.of(configuration.getType());
this.fieldCache = fieldCache;

FactoryCache cache = JavaApiPrefabValues.build().merge(factoryCache);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class InstanceCreatorTest {

@Test
public void instantiate() throws NoSuchFieldException {
ClassProbe<SomeClass> probe = new ClassProbe<>(SomeClass.class);
ClassProbe<SomeClass> probe = ClassProbe.of(SomeClass.class);
Objenesis objenesis = new ObjenesisStd();
InstanceCreator<SomeClass> sut = new InstanceCreator<>(probe, objenesis);

Expand All @@ -34,7 +34,7 @@ public void instantiate() throws NoSuchFieldException {

@Test
public void copy() throws NoSuchFieldException {
ClassProbe<SomeSubClass> probe = new ClassProbe<>(SomeSubClass.class);
ClassProbe<SomeSubClass> probe = ClassProbe.of(SomeSubClass.class);
Objenesis objenesis = new ObjenesisStd();
InstanceCreator<SomeSubClass> sut = new InstanceCreator<>(probe, objenesis);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public class ClassProbeTest {

@BeforeEach
public void setup() {
pointProbe = new ClassProbe<>(PointContainer.class);
abstractProbe = new ClassProbe<>(AbstractEqualsAndHashCode.class);
pointProbe = ClassProbe.of(PointContainer.class);
abstractProbe = ClassProbe.of(AbstractEqualsAndHashCode.class);
}

@Test
Expand Down Expand Up @@ -47,7 +47,7 @@ public void declaresEquals() {

@Test
public void doesNotDeclareEquals() {
ClassProbe<?> accessor = new ClassProbe<>(Empty.class);
ClassProbe<?> accessor = ClassProbe.of(Empty.class);
assertFalse(accessor.declaresEquals());
}

Expand All @@ -59,37 +59,37 @@ public void declaresHashCode() {

@Test
public void doesNotDeclareHashCode() {
ClassProbe<?> accessor = new ClassProbe<>(Empty.class);
ClassProbe<?> accessor = ClassProbe.of(Empty.class);
assertFalse(accessor.declaresHashCode());
}

@Test
public void hasMethod() {
ClassProbe<?> accessor = new ClassProbe<>(MethodContainer.class);
ClassProbe<?> accessor = ClassProbe.of(MethodContainer.class);
assertTrue(accessor.hasMethod("m"));
}

@Test
public void hasProtectedMethod() {
ClassProbe<?> accessor = new ClassProbe<>(MethodContainer.class);
ClassProbe<?> accessor = ClassProbe.of(MethodContainer.class);
assertTrue(accessor.hasMethod("m_protected"));
}

@Test
public void hasMethodInSuper() {
ClassProbe<?> accessor = new ClassProbe<>(ChildOfMethodContainer.class);
ClassProbe<?> accessor = ClassProbe.of(ChildOfMethodContainer.class);
assertTrue(accessor.hasMethod("m"));
}

@Test
public void hasProtectedMethodInSuper() {
ClassProbe<?> accessor = new ClassProbe<>(ChildOfMethodContainer.class);
ClassProbe<?> accessor = ClassProbe.of(ChildOfMethodContainer.class);
assertTrue(accessor.hasMethod("m_protected"));
}

@Test
public void doesNotHaveMethod() {
ClassProbe<?> accessor = new ClassProbe<>(MethodContainer.class);
ClassProbe<?> accessor = ClassProbe.of(MethodContainer.class);
assertFalse(accessor.hasMethod("doesNotExist"));
}

Expand All @@ -115,7 +115,7 @@ public void hashCodeIsAbstract() {

@Test
public void equalsIsInheritedFromObject() {
ClassProbe<NoFieldsSubWithFields> accessor = new ClassProbe<>(NoFieldsSubWithFields.class);
ClassProbe<NoFieldsSubWithFields> accessor = ClassProbe.of(NoFieldsSubWithFields.class);
assertTrue(accessor.isEqualsInheritedFromObject());
}

Expand All @@ -132,7 +132,7 @@ public void getSuperAccessorForPojo() {

@Test
public void getSuperAccessorInHierarchy() {
ClassProbe<ColorPoint3D> accessor = new ClassProbe<>(ColorPoint3D.class);
ClassProbe<ColorPoint3D> accessor = ClassProbe.of(ColorPoint3D.class);
ClassProbe<? super ColorPoint3D> superAccessor = accessor.getSuperProbe();
assertEquals(Point3D.class, superAccessor.getType());
}
Expand Down

0 comments on commit 8f3f651

Please sign in to comment.