public final class SimpleClassNameCache extends Object
A SimpleClassNameCache
is a cache for indexing simple class names.
If 2 classes are discovered to have the same simple class name then a useful exception is thrown telling the user to choose which class they want and use the full class name.
This class will scan the class path to resolve the simple class names and cache
the results. To allow your class to be included in the resolver you need to
annotate it with JsonSimpleClassName
or call addSimpleClassNames(boolean, Class[])
In addition to classes annotated with JsonSimpleClassName, implementations
of Predicate
, Function
and BinaryOperator
are included.
All classes within java.lang and java.util are also included.
By default the full class name will be serialised into json. To override this
call setUseFullNameForSerialisation(boolean)
.
Only a small set of packages on the class path are scanned.
You can add extra packages to the scanner. See ReflectionUtil
for details.
ReflectionUtil
Modifier and Type | Field and Description |
---|---|
static boolean |
DEFAULT_USE_FULL_NAME_FOR_SERIALISATION |
Modifier and Type | Method and Description |
---|---|
static void |
addSimpleClassNames(boolean includeSubtypes,
Class... classes)
Allows you to add additional classes to the cache that you wish to be
accessed via the simple class name.
|
static String |
getClassName(String id)
Gets the class name for a given id.
|
static String |
getClassName(String id,
com.fasterxml.jackson.databind.JavaType baseType)
Gets the class name for a given id.
|
static String |
getSimpleClassName(Class<?> clazz)
Gets a simple class name for the given class.
|
static String |
getSimpleClassNameOrNull(Class<?> clazz) |
static void |
initialise() |
static boolean |
isUseFullNameForSerialisation() |
static void |
reset()
Resets the caches.
|
static void |
setUseFullNameForSerialisation(boolean useFullNameForSerialisation) |
public static final boolean DEFAULT_USE_FULL_NAME_FOR_SERIALISATION
public static void initialise()
public static boolean isUseFullNameForSerialisation()
public static void setUseFullNameForSerialisation(boolean useFullNameForSerialisation)
useFullNameForSerialisation
- if true then the full class name is used for serialisation.public static void addSimpleClassNames(boolean includeSubtypes, Class... classes)
includeSubtypes
- if true then all implementations are included.classes
- the classes to be included.public static String getSimpleClassName(Class<?> clazz)
clazz
- the class to get the simple class name for.public static String getClassName(String id)
id
- the simple class name or full class name.IllegalArgumentException
- if there are multiple classes with the same id.public static String getClassName(String id, com.fasterxml.jackson.databind.JavaType baseType)
id
- the simple class name or full class name.baseType
- the base type of the ID - used to help resolve conflicting IDsIllegalArgumentException
- if there are multiple classes with the same id.public static void reset()
Copyright © 2021. All rights reserved.