Class TypeManagerImpl

  • All Implemented Interfaces:
    java.io.Serializable, TypeManager

    public class TypeManagerImpl
    extends java.lang.Object
    implements TypeManager, java.io.Serializable
    Implementation of registry of java type support. Provides information applicable to all datastores for how a field of a class is treated; whether it is by default persistent, whether it is by default embedded, whether it is in the DFG, and if it has a wrapper for SCO operations. Also stores whether the type can be converted to/from a String (for datastores that don't provide storage natively). Uses the plugin mechanism extension-point "org.datanucleus.java_type".
    See Also:
    Serialized Form
    • Field Detail

      • SCO_WRAPPER_CONSTRUCTOR_ARG_TYPES

        private static final java.lang.Class[] SCO_WRAPPER_CONSTRUCTOR_ARG_TYPES
      • javaTypes

        protected java.util.Map<java.lang.String,​TypeManagerImpl.JavaType> javaTypes
        Map of java types, keyed by the class name.
      • containerHandlersByClass

        protected java.util.Map<java.lang.Class,​ContainerHandler> containerHandlersByClass
        Map of ContainerHandlers, keyed by the container type class name.
      • classesWithoutContainerHandler

        protected java.util.Set<java.lang.Class> classesWithoutContainerHandler
        Cache of classes that don't need a ContainerHandler.
      • typeConverterByName

        protected java.util.Map<java.lang.String,​TypeConverter> typeConverterByName
        Map of TypeConverter keyed by their symbolic name.
      • autoApplyConvertersByType

        protected java.util.Map<java.lang.String,​TypeConverter> autoApplyConvertersByType
        Map of TypeConverter keyed by type name that we should default to for this type (user-defined).
      • typeConverterMap

        protected java.util.Map<java.lang.Class,​java.util.Map<java.lang.Class,​TypeConverter>> typeConverterMap
        Map of (Map of TypeConverter keyed by the datastore type), keyed by the member type.
      • typeConverterDatastoreTypeByConverter

        protected java.util.Map<TypeConverter,​java.lang.Class> typeConverterDatastoreTypeByConverter
        Cache of TypeConverter datastore type, keyed by the converter.
      • typeConverterMemberTypeByConverter

        protected java.util.Map<TypeConverter,​java.lang.Class> typeConverterMemberTypeByConverter
        Cache of TypeConverter member type, keyed by the converter.
      • ALPHABETICAL_ORDER

        private static java.util.Comparator<java.lang.Class> ALPHABETICAL_ORDER
      • ALPHABETICAL_ORDER_STRING

        private static java.util.Comparator<java.lang.String> ALPHABETICAL_ORDER_STRING
    • Constructor Detail

      • TypeManagerImpl

        public TypeManagerImpl​(NucleusContext nucCtx)
        Constructor, loading support for type mappings using the plugin mechanism.
        Parameters:
        nucCtx - NucleusContext
    • Method Detail

      • getSupportedSecondClassTypes

        public java.util.Set<java.lang.String> getSupportedSecondClassTypes()
        Description copied from interface: TypeManager
        Accessor for the supported second-class Types. This may or may not be a complete list, just that it provides the principal ones.
        Specified by:
        getSupportedSecondClassTypes in interface TypeManager
        Returns:
        Set of supported types (fully qualified class names).
      • isSupportedSecondClassType

        public boolean isSupportedSecondClassType​(java.lang.String className)
        Description copied from interface: TypeManager
        Accessor for whether a class is supported as being second class.
        Specified by:
        isSupportedSecondClassType in interface TypeManager
        Parameters:
        className - The class name
        Returns:
        Whether the class is supported (to some degree)
      • filterOutSupportedSecondClassNames

        public java.lang.String[] filterOutSupportedSecondClassNames​(java.lang.String[] inputClassNames)
        Description copied from interface: TypeManager
        Convenience method to filter out any supported classes from a list.
        Specified by:
        filterOutSupportedSecondClassNames in interface TypeManager
        Parameters:
        inputClassNames - Names of the classes
        Returns:
        Names of the classes (omitting supported types)
      • isDefaultPersistent

        public boolean isDefaultPersistent​(java.lang.Class c)
        Description copied from interface: TypeManager
        Accessor for whether the type is by default persistent. TODO Support use of apiAdapter.isMemberDefaultPersistent to get strict JDO/JPA behaviour.
        Specified by:
        isDefaultPersistent in interface TypeManager
        Parameters:
        c - The type
        Returns:
        Whether persistent
      • isDefaultFetchGroup

        public boolean isDefaultFetchGroup​(java.lang.Class c)
        Description copied from interface: TypeManager
        Accessor for whether the type is by default in the DFG.
        Specified by:
        isDefaultFetchGroup in interface TypeManager
        Parameters:
        c - The type
        Returns:
        Whether in the DFG
      • isDefaultFetchGroupForCollection

        public boolean isDefaultFetchGroupForCollection​(java.lang.Class c,
                                                        java.lang.Class genericType)
        Description copied from interface: TypeManager
        Accessor for whether the generic collection type is by default in the DFG.
        Specified by:
        isDefaultFetchGroupForCollection in interface TypeManager
        Parameters:
        c - The type
        genericType - The element generic type
        Returns:
        Whether in the DFG
      • isDefaultEmbeddedType

        public boolean isDefaultEmbeddedType​(java.lang.Class c)
        Description copied from interface: TypeManager
        Accessor for whether the type is by default embedded.
        Specified by:
        isDefaultEmbeddedType in interface TypeManager
        Parameters:
        c - The type
        Returns:
        Whether embedded
      • isSecondClassMutableType

        public boolean isSecondClassMutableType​(java.lang.String className)
        Description copied from interface: TypeManager
        Accessor for whether the type is SCO mutable.
        Specified by:
        isSecondClassMutableType in interface TypeManager
        Parameters:
        className - The type
        Returns:
        Whether SCO mutable
      • getWrapperTypeForType

        public java.lang.Class<? extends SCO> getWrapperTypeForType​(java.lang.String className)
        Description copied from interface: TypeManager
        Accessor for the SCO wrapper for the type
        Specified by:
        getWrapperTypeForType in interface TypeManager
        Parameters:
        className - The type
        Returns:
        SCO wrapper type
      • getWrappedTypeBackedForType

        public java.lang.Class<? extends SCO> getWrappedTypeBackedForType​(java.lang.String className)
        Description copied from interface: TypeManager
        Accessor for the backing-store Second Class Wrapper class for the supplied class. A type will have a SCO wrapper if it is SCO supported and is mutable. If there is no backed wrapper provided returns the simple wrapper.
        Specified by:
        getWrappedTypeBackedForType in interface TypeManager
        Parameters:
        className - The class name
        Returns:
        The second class wrapper
      • isSecondClassWrapper

        public boolean isSecondClassWrapper​(java.lang.String className)
        Description copied from interface: TypeManager
        Accessor for whether the type is a SCO wrapper itself.
        Specified by:
        isSecondClassWrapper in interface TypeManager
        Parameters:
        className - The type
        Returns:
        Whether is SCO wrapper
      • createSCOWrapper

        public <T> SCO<T> createSCOWrapper​(DNStateManager ownerSM,
                                           AbstractMemberMetaData mmd,
                                           java.lang.Class<T> instantiatedType)
        Description copied from interface: TypeManager
        Method to create a new SCO wrapper for member type. Will find a wrapper suitable for the instantiated type (if provided), otherwise suitable for the member metadata type.
        Specified by:
        createSCOWrapper in interface TypeManager
        Type Parameters:
        T - Type of the object that we are creating a wrapper for
        Parameters:
        ownerSM - StateManager for the owning object
        mmd - The MetaData for the related member.
        instantiatedType - Type to instantiate the member as (if known), otherwise falls back to the type from metadata
        Returns:
        The wrapper object of the required type
      • getBackedWrapperTypeForType

        private java.lang.Class<? extends SCO> getBackedWrapperTypeForType​(java.lang.Class declaredType,
                                                                           java.lang.Class instantiatedType,
                                                                           java.lang.String typeName)
        Convenience method to return the backed wrapper type for the field definition. Wrapper is null if no backed wrapper is defined for the type.
        Parameters:
        declaredType - Declared type of the field
        instantiatedType - Instantiated type of the field
        typeName - Type name to try first
        Returns:
        The wrapper type
      • getSimpleWrapperTypeForType

        private java.lang.Class<? extends SCO> getSimpleWrapperTypeForType​(java.lang.Class declaredType,
                                                                           java.lang.Class instantiatedType,
                                                                           java.lang.String typeName)
        Convenience method to return the simple wrapper type for the field definition. Wrapper is null if no simple wrapper is defined for the type.
        Parameters:
        declaredType - Declared type of the field
        instantiatedType - Instantiated type of the field
        typeName - Type name to try first
        Returns:
        The wrapper type
      • getTypeForSecondClassWrapper

        public java.lang.Class getTypeForSecondClassWrapper​(java.lang.String className)
        Description copied from interface: TypeManager
        Accessor for a java type that the supplied class is a SCO wrapper for. If the supplied class is not a SCO wrapper for anything then returns null.
        Specified by:
        getTypeForSecondClassWrapper in interface TypeManager
        Parameters:
        className - Name of the class
        Returns:
        The java class that this is a wrapper for (or null)
      • getContainerAdapter

        public ContainerAdapter getContainerAdapter​(java.lang.Object container)
        Description copied from interface: TypeManager
        Convenience method to obtain the ContainerAdapter using the container object instance
        Specified by:
        getContainerAdapter in interface TypeManager
        Parameters:
        container - The container instance
        Returns:
        The ContainerAdapter for the respective container or null if it's not a supported container
      • getContainerHandler

        public <H extends ContainerHandler> H getContainerHandler​(java.lang.Class containerClass)
        Description copied from interface: TypeManager
        Obtains the registered ContainerHandler for the given containerClass. ContainerHandler are specified via the plugin mechanism using the container-handler attribute of the java-type element.
        Specified by:
        getContainerHandler in interface TypeManager
        Type Parameters:
        H - Handler type
        Parameters:
        containerClass - The class of the container.
        Returns:
        The respective ContainerHandler if registered or null if no ContainerHandler is found for the containerClass.
      • getTypeConverterForName

        public TypeConverter getTypeConverterForName​(java.lang.String converterName)
        Description copied from interface: TypeManager
        Accessor for the type converter with the provided name. This is used when the user has specified metadata for a field to use a particular named converter.
        Specified by:
        getTypeConverterForName in interface TypeManager
        Parameters:
        converterName - Name of the converter
        Returns:
        The converter
      • registerConverter

        public void registerConverter​(java.lang.String name,
                                      TypeConverter converter,
                                      java.lang.Class memberType,
                                      java.lang.Class dbType,
                                      boolean autoApply,
                                      java.lang.String autoApplyType)
        Description copied from interface: TypeManager
        Register a TypeConverter with the TypeManager process for specific attribute/db types. TypeConverters are registered either from the contents of "plugin.xml" (i.e the builtin types) where the name is of the form "dn.*", or from user-registered metadata (e.g JPA Annotations) where the name is the class name of the converter or a user supplied name.
        Specified by:
        registerConverter in interface TypeManager
        Parameters:
        name - The name to register the converter under
        converter - The converter
        memberType - Type of the java member
        dbType - Type of the database column
        autoApply - Whether this should be used as an auto-apply converter
        autoApplyType - Java type to auto apply this for
      • getAutoApplyTypeConverterForType

        public TypeConverter getAutoApplyTypeConverterForType​(java.lang.Class memberType)
        Description copied from interface: TypeManager
        Method to return a TypeConverter that should be applied by default for the specified java (member) type. Will return null if the java type has no autoApply type defined for it (the default).
        Specified by:
        getAutoApplyTypeConverterForType in interface TypeManager
        Parameters:
        memberType - The java (member) type
        Returns:
        The converter to use by default
      • setDefaultTypeConverterForType

        public void setDefaultTypeConverterForType​(java.lang.Class memberType,
                                                   java.lang.String converterName)
        Description copied from interface: TypeManager
        Method providing the ability for a datastore plugin to override the default converter type for the specified java type.
        Specified by:
        setDefaultTypeConverterForType in interface TypeManager
        Parameters:
        memberType - Member type
        converterName - The converter to use by default. This is assumed to exist.
      • getDefaultTypeConverterForType

        public TypeConverter getDefaultTypeConverterForType​(java.lang.Class memberType)
        Description copied from interface: TypeManager
        Accessor for the default type converter for the provided Java type.
        Specified by:
        getDefaultTypeConverterForType in interface TypeManager
        Parameters:
        memberType - Java type for the member
        Returns:
        The default converter (if any)
      • getTypeConverterForType

        public TypeConverter getTypeConverterForType​(java.lang.Class memberType,
                                                     java.lang.Class datastoreType)
        Description copied from interface: TypeManager
        Accessor for the type converter for the provided Java type and its datastore type.
        Specified by:
        getTypeConverterForType in interface TypeManager
        Parameters:
        memberType - Java type for the member
        datastoreType - Java type for the datastore
        Returns:
        The converter (if any)
      • getTypeConvertersForType

        public java.util.Collection<TypeConverter> getTypeConvertersForType​(java.lang.Class memberType)
        Description copied from interface: TypeManager
        Accessor for the available type converters for the provided Java type.
        Specified by:
        getTypeConvertersForType in interface TypeManager
        Parameters:
        memberType - The java type
        Returns:
        The available Type Converters
      • getDatastoreTypeForTypeConverter

        public java.lang.Class getDatastoreTypeForTypeConverter​(TypeConverter conv,
                                                                java.lang.Class memberType)
        Method to return the datastore type for the specified TypeConverter.
        Specified by:
        getDatastoreTypeForTypeConverter in interface TypeManager
        Parameters:
        conv - The converter
        memberType - The member type
        Returns:
        The datastore type
      • getMemberTypeForTypeConverter

        public java.lang.Class getMemberTypeForTypeConverter​(TypeConverter conv,
                                                             java.lang.Class datastoreType)
        Method to return the member type for the specified TypeConverter.
        Specified by:
        getMemberTypeForTypeConverter in interface TypeManager
        Parameters:
        conv - The converter
        datastoreType - The datastore type for this converter
        Returns:
        The member type
      • findJavaTypeForClass

        protected TypeManagerImpl.JavaType findJavaTypeForClass​(java.lang.Class cls)
        Convenience method to return the JavaType for the specified class. If this class has a defined JavaType then returns it. If not then tries to find a superclass that is castable to the specified type.
        Parameters:
        cls - The class required
        Returns:
        The JavaType
      • findJavaTypeForCollectionClass

        protected TypeManagerImpl.JavaType findJavaTypeForCollectionClass​(java.lang.Class cls,
                                                                          java.lang.Class genericType)
        Convenience method to return the JavaType for the specified class. If this class has a defined JavaType then returns it. If not then tries to find a superclass that is castable to the specified type.
        Parameters:
        cls - The class required
        genericType - Any generic type specified for the element
        Returns:
        The JavaType
      • loadJavaTypes

        private void loadJavaTypes​(PluginManager mgr)
        Method to load the java types that we support out of the box. This includes all built-in types, as well as all types registered via the plugin mechanism.
        Parameters:
        mgr - the PluginManager
      • addJavaType

        private void addJavaType​(java.lang.Class cls,
                                 java.lang.Class genericType,
                                 boolean embedded,
                                 boolean dfg,
                                 java.lang.Class wrapperType,
                                 java.lang.Class wrapperTypeBacked,
                                 java.lang.Class containerHandlerType,
                                 java.lang.String typeConverterName)
      • loadTypeConverters

        private void loadTypeConverters​(PluginManager mgr)
        Method to load the java type that are currently registered in the PluginManager.
        Parameters:
        mgr - the PluginManager