Skip to content

Commit

Permalink
Small performance improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
jqno committed Sep 30, 2024
1 parent dfb7165 commit c2acb22
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
import net.bytebuddy.dynamic.scaffold.TypeValidation;
import org.objenesis.Objenesis;
import org.objenesis.instantiator.ObjectInstantiator;

/**
* Instantiates objects of a given class.
Expand All @@ -34,11 +35,13 @@ public final class Instantiator<T> {

private final Class<T> type;
private final Objenesis objenesis;
private final ObjectInstantiator<T> objenesisInstantiator;

/** Private constructor. Call {@link #of(Class)} to instantiate. */
private Instantiator(Class<T> type, Objenesis objenesis) {
this.type = type;
this.objenesis = objenesis;
this.objenesisInstantiator = objenesis.getInstantiatorOf(type);
}

/**
Expand Down Expand Up @@ -69,7 +72,7 @@ public static <T> Instantiator<T> of(Class<T> type, Objenesis objenesis) {
* @return An object of type T.
*/
public T instantiate() {
return objenesis.newInstance(type);
return objenesisInstantiator.newInstance();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class InstanceCreator<T> {

private final Class<T> type;
private final ClassProbe<T> probe;
private final Objenesis objenesis;
private final Instantiator<T> instantiator;

/**
* Constructor.
Expand All @@ -25,7 +25,7 @@ public class InstanceCreator<T> {
public InstanceCreator(ClassProbe<T> probe, Objenesis objenesis) {
this.type = probe.getType();
this.probe = probe;
this.objenesis = objenesis;
this.instantiator = Instantiator.of(type, objenesis);
}

/**
Expand Down Expand Up @@ -64,7 +64,7 @@ private T createRecordInstance(Map<Field, Object> values) {
}

private T createClassInstance(Map<Field, Object> values) {
T instance = Instantiator.of(type, objenesis).instantiate();
T instance = instantiator.instantiate();
traverseFields(
values,
(f, v) -> new FieldMutator(FieldProbe.of(f)).setNewValue(instance, v)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class SubjectCreator<T> {
private final ClassProbe<T> classProbe;
private final FieldCache fieldCache;
private final Objenesis objenesis;
private final InstanceCreator<T> instanceCreator;

/**
* Constructor.
Expand All @@ -46,6 +47,7 @@ public SubjectCreator(
this.classProbe = new ClassProbe<>(type);
this.fieldCache = fieldCache;
this.objenesis = objenesis;
this.instanceCreator = new InstanceCreator<>(classProbe, objenesis);
}

/**
Expand Down Expand Up @@ -165,7 +167,6 @@ public T withAllFieldsShallowlyChanged() {
* @return A copy of the given original.
*/
public T copy(T original) {
InstanceCreator<T> instanceCreator = new InstanceCreator<>(classProbe, objenesis);
return Rethrow.rethrow(() -> instanceCreator.copy(original));
}

Expand Down Expand Up @@ -202,7 +203,6 @@ public <S extends T> S copyIntoSubclass(T original, Class<S> subType) {

private T createInstance(Map<Field, Object> givens) {
Map<Field, Object> values = determineValues(givens);
InstanceCreator<T> instanceCreator = new InstanceCreator<>(classProbe, objenesis);
return Rethrow.rethrow(() -> instanceCreator.instantiate(values));
}

Expand Down

0 comments on commit c2acb22

Please sign in to comment.