Top Description Fields Constructors Methods
org.springframework.objenesis

public Class SpringObjenesis

extends Object
implements Objenesis
Class Inheritance
All Implemented Interfaces
org.springframework.objenesis.Objenesis
Imports
org.springframework.core.SpringProperties, org.springframework.objenesis.instantiator.ObjectInstantiator, org.springframework.objenesis.strategy.InstantiatorStrategy, .StdInstantiatorStrategy, org.springframework.util.ConcurrentReferenceHashMap

Spring-specific variant of ObjenesisStd / ObjenesisBase, providing a cache based on Class keys instead of class names, and allowing for selective use of the cache.
Author
Juergen Hoeller
Since
4.2
See Also
isWorthTrying(), newInstance(Class, boolean)

Field Summary

Modifier and TypeField and Description
private final ConcurrentReferenceHashMap<Class<?>, ObjectInstantiator<?>>
public static final String
IGNORE_OBJENESIS_PROPERTY_NAME

System property that instructs Spring to ignore Objenesis, not even attempting to use it.

private final InstantiatorStrategy
private volatile Boolean

Constructor Summary

AccessConstructor and Description
public
SpringObjenesis()

Create a new SpringObjenesis instance with the standard instantiator strategy.

public
SpringObjenesis(InstantiatorStrategy
the instantiator strategy to use
strategy
)

Create a new SpringObjenesis instance with the given standard instantiator strategy.

Method Summary

Modifier and TypeMethod and Description
public <T> ObjectInstantiator<T>
getInstantiatorOf(Class<T>
Class to instantiate
clazz
)

Implements org.springframework.objenesis.Objenesis.getInstantiatorOf.

Will pick the best instantiator for the provided class.

public boolean
isWorthTrying()

Return whether this Objenesis instance is worth trying for instance creation, i.e. whether it hasn't been used yet or is known to work.

public <T> T

Returns:

the new instance (never null)
newInstance
(Class<T>
the class to create an instance of
clazz
,
boolean
whether to use the instantiator cache (typically true but can be set to false e.g. for reloadable classes)
useCache
)

Create a new instance of the given class via Objenesis.

public <T> T
newInstance(Class<T>
Class to instantiate
clazz
)

Implements org.springframework.objenesis.Objenesis.newInstance.

Will create a new object without any constructor being called

protected <T> ObjectInstantiator<T>
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

cacheback to summary
private final ConcurrentReferenceHashMap<Class<?>, ObjectInstantiator<?>> cache
IGNORE_OBJENESIS_PROPERTY_NAMEback to summary
public static final String IGNORE_OBJENESIS_PROPERTY_NAME

System property that instructs Spring to ignore Objenesis, not even attempting to use it. Setting this flag to "true" is equivalent to letting Spring find out that Objenesis isn't working at runtime, triggering the fallback code path immediately: Most importantly, this means that all CGLIB AOP proxies will be created through regular instantiation via a default constructor.

strategyback to summary
private final InstantiatorStrategy strategy
worthTryingback to summary
private volatile Boolean worthTrying

Constructor Detail

SpringObjenesisback to summary
public SpringObjenesis()

Create a new SpringObjenesis instance with the standard instantiator strategy.

SpringObjenesisback to summary
public SpringObjenesis(InstantiatorStrategy strategy)

Create a new SpringObjenesis instance with the given standard instantiator strategy.

Parameters
strategy:InstantiatorStrategy

the instantiator strategy to use

Method Detail

getInstantiatorOfback to summary
public <T> ObjectInstantiator<T> getInstantiatorOf(Class<T> clazz)

Implements org.springframework.objenesis.Objenesis.getInstantiatorOf.

Doc from org.springframework.objenesis.Objenesis.getInstantiatorOf.

Will pick the best instantiator for the provided class. If you need to create a lot of instances from the same class, it is way more efficient to create them from the same ObjectInstantiator than calling newInstance(Class).

Parameters
<T>
Type to instantiate
clazz:Class<T>

Class to instantiate

Returns:ObjectInstantiator<T>

Instantiator dedicated to the class

Annotations
@Override
@SuppressWarnings:unchecked
isWorthTryingback to summary
public boolean isWorthTrying()

Return whether this Objenesis instance is worth trying for instance creation, i.e. whether it hasn't been used yet or is known to work.

If the configured Objenesis instantiator strategy has been identified to not work on the current JVM at all or if the "spring.objenesis.ignore" property has been set to "true", this method returns false.

newInstanceback to summary
public <T> T newInstance(Class<T> clazz, boolean useCache)

Create a new instance of the given class via Objenesis.

Parameters
clazz:Class<T>

the class to create an instance of

useCache:boolean

whether to use the instantiator cache (typically true but can be set to false e.g. for reloadable classes)

Returns:T

the new instance (never null)

Exceptions
ObjenesisException:
if instance creation failed
newInstanceback to summary
public <T> T newInstance(Class<T> clazz)

Implements org.springframework.objenesis.Objenesis.newInstance.

Doc from org.springframework.objenesis.Objenesis.newInstance.

Will create a new object without any constructor being called

Parameters
<T>
Type instantiated
clazz:Class<T>

Class to instantiate

Returns:T

New instance of clazz

Annotations
@Override
newInstantiatorOfback to summary
protected <T> ObjectInstantiator<T> newInstantiatorOf(Class<T> clazz)