|
|
@ -18,6 +18,7 @@ package com.fr.third.org.objenesis; |
|
|
|
import com.fr.third.org.objenesis.instantiator.ObjectInstantiator; |
|
|
|
import com.fr.third.org.objenesis.instantiator.ObjectInstantiator; |
|
|
|
import com.fr.third.org.objenesis.strategy.InstantiatorStrategy; |
|
|
|
import com.fr.third.org.objenesis.strategy.InstantiatorStrategy; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.Map; |
|
|
|
import java.util.concurrent.ConcurrentHashMap; |
|
|
|
import java.util.concurrent.ConcurrentHashMap; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -28,11 +29,15 @@ import java.util.concurrent.ConcurrentHashMap; |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class ObjenesisBase implements Objenesis { |
|
|
|
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; |
|
|
|
protected final InstantiatorStrategy strategy; |
|
|
|
|
|
|
|
|
|
|
|
/** Strategy cache. Key = Class, Value = InstantiatorStrategy */ |
|
|
|
/** |
|
|
|
protected ConcurrentHashMap<String, ObjectInstantiator<?>> cache; |
|
|
|
* Strategy cache. Key = Class, Value = InstantiatorStrategy |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
protected Map<Class, ObjectInstantiator<?>> cache; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Constructor allowing to pick a strategy and using 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 |
|
|
|
* @param useCache If {@link ObjectInstantiator}s should be cached |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public ObjenesisBase(InstantiatorStrategy strategy, boolean useCache) { |
|
|
|
public ObjenesisBase(InstantiatorStrategy strategy, boolean useCache) { |
|
|
|
if(strategy == null) { |
|
|
|
if (strategy == null) { |
|
|
|
throw new IllegalArgumentException("A strategy can't be null"); |
|
|
|
throw new IllegalArgumentException("A strategy can't be null"); |
|
|
|
} |
|
|
|
} |
|
|
|
this.strategy = strategy; |
|
|
|
this.strategy = strategy; |
|
|
|
this.cache = useCache ? new ConcurrentHashMap<String, ObjectInstantiator<?>>() : null; |
|
|
|
this.cache = useCache ? new ConcurrentHashMap<Class, ObjectInstantiator<?>>() : null; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public String toString() { |
|
|
|
public String toString() { |
|
|
|
return getClass().getName() + " using " + strategy.getClass().getName() |
|
|
|
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") |
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
public <T> ObjectInstantiator<T> getInstantiatorOf(Class<T> clazz) { |
|
|
|
public <T> ObjectInstantiator<T> getInstantiatorOf(Class<T> clazz) { |
|
|
|
if(clazz.isPrimitive()) { |
|
|
|
if (clazz.isPrimitive()) { |
|
|
|
throw new IllegalArgumentException("Primitive types can't be instantiated in Java"); |
|
|
|
throw new IllegalArgumentException("Primitive types can't be instantiated in Java"); |
|
|
|
} |
|
|
|
} |
|
|
|
if(cache == null) { |
|
|
|
if (cache == null) { |
|
|
|
return strategy.newInstantiatorOf(clazz); |
|
|
|
return strategy.newInstantiatorOf(clazz); |
|
|
|
} |
|
|
|
} |
|
|
|
ObjectInstantiator<?> instantiator = cache.get(clazz.getName()); |
|
|
|
ObjectInstantiator<?> instantiator = cache.get(clazz); |
|
|
|
if(instantiator == null) { |
|
|
|
if (instantiator == null) { |
|
|
|
ObjectInstantiator<?> newInstantiator = strategy.newInstantiatorOf(clazz); |
|
|
|
ObjectInstantiator<?> newInstantiator = strategy.newInstantiatorOf(clazz); |
|
|
|
instantiator = cache.putIfAbsent(clazz.getName(), newInstantiator); |
|
|
|
instantiator = putIfAbsent(clazz, newInstantiator); |
|
|
|
if(instantiator == null) { |
|
|
|
if (instantiator == null) { |
|
|
|
instantiator = newInstantiator; |
|
|
|
instantiator = newInstantiator; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return (ObjectInstantiator<T>) instantiator; |
|
|
|
return (ObjectInstantiator<T>) instantiator; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private ObjectInstantiator<?> putIfAbsent(Class key, ObjectInstantiator<?> newInstantiator) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ObjectInstantiator<?> instantiator = cache.get(key); |
|
|
|
|
|
|
|
if (instantiator == null) { |
|
|
|
|
|
|
|
instantiator = cache.put(key, newInstantiator); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return instantiator; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |