From ad71723902fd1ec2be5897531d19db59d2953556 Mon Sep 17 00:00:00 2001 From: Anton Keks Date: Sun, 17 May 2020 16:52:21 +0300 Subject: [PATCH] unfortunately, HashMap.computeIfAbsent() doesn't store values properly in a recursive scenario --- src/net/azib/ipscan/di/Injector.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/net/azib/ipscan/di/Injector.java b/src/net/azib/ipscan/di/Injector.java index 435c0a59..802b7662 100644 --- a/src/net/azib/ipscan/di/Injector.java +++ b/src/net/azib/ipscan/di/Injector.java @@ -24,7 +24,14 @@ public class Injector { } T require(Key key) { - return (T) instances.computeIfAbsent(key, k -> createInstance(key.type)); + T value = (T) instances.get(key); + if (value == null) { + value = createInstance(key.type); + instances.put(key, value); + } + return value; + // unfortunately, HashMap.computeIfAbsent() doesn't put values properly in a recursive scenario + // return (T) instances.computeIfAbsent(key, k -> createInstance(k.type)); } public T require(Class type) { @@ -67,8 +74,7 @@ public class Injector { } private boolean isCollection(Type type) { - return type instanceof ParameterizedType && - Collection.class.isAssignableFrom(toClass(type)); + return type instanceof ParameterizedType && Collection.class.isAssignableFrom(toClass(type)); } private Class getParamClass(ParameterizedType type) {