From 52b3945886e196f5f8278f8fb1cf26c807118bf3 Mon Sep 17 00:00:00 2001 From: "andrew.asa" Date: Thu, 5 Sep 2019 18:13:28 +0800 Subject: [PATCH] =?UTF-8?q?REPORT-18730=20objenesis=E5=86=85=E9=83=A8?= =?UTF-8?q?=E4=BC=9A=E6=A0=B9=E6=8D=AE=E7=B1=BB=E5=90=8D=E7=BC=93=E5=AD=98?= =?UTF-8?q?class=EF=BC=8C=E6=8F=92=E4=BB=B6=E7=A6=81=E7=94=A8=E5=92=8C?= =?UTF-8?q?=E5=8D=B8=E8=BD=BD=E5=90=8E=E4=BC=9A=E4=BA=A7=E7=94=9F=E5=90=8C?= =?UTF-8?q?=E5=90=8Dclass=EF=BC=8C=E5=AF=BC=E8=87=B4swift=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=E5=AF=B9=E8=B1=A1=E4=B8=8D=E5=8C=B9=E9=85=8D?= =?UTF-8?q?=EF=BC=8C=E6=8D=A2=E4=B8=80=E4=B8=8Bkey?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fr/third/org/objenesis/ObjenesisBase.java | 40 +++++++++++++------ 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/fine-kryo/src/com/fr/third/org/objenesis/ObjenesisBase.java b/fine-kryo/src/com/fr/third/org/objenesis/ObjenesisBase.java index 2d4b2209f..2d08272c3 100644 --- a/fine-kryo/src/com/fr/third/org/objenesis/ObjenesisBase.java +++ b/fine-kryo/src/com/fr/third/org/objenesis/ObjenesisBase.java @@ -18,6 +18,7 @@ package com.fr.third.org.objenesis; import com.fr.third.org.objenesis.instantiator.ObjectInstantiator; import com.fr.third.org.objenesis.strategy.InstantiatorStrategy; +import java.util.Map; import java.util.concurrent.ConcurrentHashMap; /** @@ -28,11 +29,15 @@ import java.util.concurrent.ConcurrentHashMap; */ public class ObjenesisBase implements Objenesis { - /** Strategy used by this Objenesi implementation to create classes */ + /** + * Strategy used by this Objenesi implementation to create classes + */ protected final InstantiatorStrategy strategy; - /** Strategy cache. Key = Class, Value = InstantiatorStrategy */ - protected ConcurrentHashMap> cache; + /** + * Strategy cache. Key = Class, Value = InstantiatorStrategy + */ + protected Map> cache; /** * Constructor allowing to pick a strategy and using cache @@ -50,17 +55,17 @@ public class ObjenesisBase implements Objenesis { * @param useCache If {@link ObjectInstantiator}s should be cached */ public ObjenesisBase(InstantiatorStrategy strategy, boolean useCache) { - if(strategy == null) { + if (strategy == null) { throw new IllegalArgumentException("A strategy can't be null"); } this.strategy = strategy; - this.cache = useCache ? new ConcurrentHashMap>() : null; + this.cache = useCache ? new ConcurrentHashMap>() : null; } @Override public String toString() { return getClass().getName() + " using " + strategy.getClass().getName() - + (cache == null ? " without" : " with") + " caching"; + + (cache == null ? " without" : " with") + " caching"; } /** @@ -83,20 +88,29 @@ public class ObjenesisBase implements Objenesis { */ @SuppressWarnings("unchecked") public ObjectInstantiator getInstantiatorOf(Class clazz) { - if(clazz.isPrimitive()) { + if (clazz.isPrimitive()) { throw new IllegalArgumentException("Primitive types can't be instantiated in Java"); } - if(cache == null) { + if (cache == null) { return strategy.newInstantiatorOf(clazz); } - ObjectInstantiator instantiator = cache.get(clazz.getName()); - if(instantiator == null) { + ObjectInstantiator instantiator = cache.get(clazz); + if (instantiator == null) { ObjectInstantiator newInstantiator = strategy.newInstantiatorOf(clazz); - instantiator = cache.putIfAbsent(clazz.getName(), newInstantiator); - if(instantiator == null) { + instantiator = putIfAbsent(clazz, newInstantiator); + if (instantiator == null) { instantiator = newInstantiator; } } return (ObjectInstantiator) instantiator; } -} + + private ObjectInstantiator putIfAbsent(Class key, ObjectInstantiator newInstantiator) { + + ObjectInstantiator instantiator = cache.get(key); + if (instantiator == null) { + instantiator = cache.put(key, newInstantiator); + } + return instantiator; + } +} \ No newline at end of file