Fix problems with concurrency.

This commit is contained in:
Jesse Brault 2026-01-04 18:06:28 -06:00
parent c797190f5a
commit b2e8e0c5b9
2 changed files with 16 additions and 18 deletions

View File

@ -4,7 +4,7 @@ plugins {
}
group = 'com.jessebrault.di'
version = '0.1.0'
version = '0.2.0-SNAPSHOT'
repositories {
mavenCentral()

View File

@ -5,6 +5,7 @@ import org.jetbrains.annotations.Nullable;
import java.lang.reflect.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import java.util.stream.Collectors;
@ -104,20 +105,15 @@ public abstract class AbstractInjectingObjectFactory implements ObjectFactory {
}
private final Map<Class<?>, Constructor<?>[]> cachedAllConstructors = new HashMap<>();
private final Collection<CachedInjectConstructor<?>> cachedInjectConstructors = new ArrayList<>();
private final Collection<CachedNonInjectConstructor<?>> cachedNonInjectConstructors = new ArrayList<>();
private final Map<Class<?>, Collection<Method>> cachedSetters = new HashMap<>();
private final Map<Method, Parameter> cachedSetterParameters = new HashMap<>();
private final Map<Class<?>, Constructor<?>[]> cachedAllConstructors = new ConcurrentHashMap<>();
private final Map<Class<?>, Constructor<?>> cachedInjectConstructors = new ConcurrentHashMap<>();
private final Map<Class<?>, Map<Class<?>[], Constructor<?>>> cachedNonInjectConstructors = new ConcurrentHashMap<>();
private final Map<Class<?>, Collection<Method>> cachedSetters = new ConcurrentHashMap<>();
private final Map<Method, Parameter> cachedSetterParameters = new ConcurrentHashMap<>();
@SuppressWarnings("unchecked")
private <T> @Nullable Constructor<T> findCachedInjectConstructor(Class<T> clazz) {
for (final CachedInjectConstructor<?> cachedConstructor : this.cachedInjectConstructors) {
if (clazz.equals(cachedConstructor.clazz())) {
return (Constructor<T>) cachedConstructor.constructor();
}
}
return null;
return (Constructor<T>) this.cachedInjectConstructors.get(clazz);
}
/**
@ -153,15 +149,14 @@ public abstract class AbstractInjectingObjectFactory implements ObjectFactory {
}
protected final void putCachedInjectConstructor(CachedInjectConstructor<?> cached) {
this.cachedInjectConstructors.add(cached);
this.cachedInjectConstructors.put(cached.clazz(), cached.constructor());
}
@SuppressWarnings("unchecked")
private <T> @Nullable Constructor<T> findCachedNonInjectConstructor(Class<T> clazz, Class<?>[] paramTypes) {
for (final CachedNonInjectConstructor<?> cachedConstructor : this.cachedNonInjectConstructors) {
if (clazz.equals(cachedConstructor.clazz()) && Arrays.equals(cachedConstructor.paramTypes(), paramTypes)) {
return (Constructor<T>) cachedConstructor.constructor();
}
final Map<Class<?>[], Constructor<?>> constructors = this.cachedNonInjectConstructors.get(clazz);
if (constructors != null) {
return (Constructor<T>) constructors.get(paramTypes);
}
return null;
}
@ -209,7 +204,10 @@ public abstract class AbstractInjectingObjectFactory implements ObjectFactory {
}
protected final void putCachedNonInjectConstructor(CachedNonInjectConstructor<?> cached) {
this.cachedNonInjectConstructors.add(cached);
final Map<Class<?>[], Constructor<?>> constructors = this.cachedNonInjectConstructors.computeIfAbsent(
cached.clazz(), clazz -> new HashMap<>()
);
constructors.put(cached.paramTypes(), cached.constructor());
}
/**