Class RenameHandler


  • public final class RenameHandler
    extends java.lang.Object
    A general purpose utility for registering renames.

    This handles type and enum constant renames. For example, use as follows:

      RenameHandler.INSTANCE.renamedType("org.joda.OldName", NewName.class);
      RenameHandler.INSTANCE.renamedEnum("CORRECT", Status.VALID);
      RenameHandler.INSTANCE.renamedEnum("INCORRECT", Status.INVALID);
     
    The recommended usage is to edit the static singleton before using other classes. Editing a static is acceptable because renames are driven by bytecode which is static. For additional security, an application should lock the rename handler instance once any types and enums have been registered using lock().

    This class is thread-safe with concurrent caches.

    Since:
    1.6
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.concurrent.ConcurrentHashMap<java.lang.Class<?>,​java.util.Map<java.lang.String,​java.lang.Enum<?>>> enumRenames
      The enum renames.
      static RenameHandler INSTANCE
      A mutable global instance.
      private boolean locked
      The lock flag.
      private java.util.concurrent.ConcurrentHashMap<java.lang.String,​java.lang.Class<?>> typeRenames
      The type renames.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private RenameHandler()
      Restricted constructor.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void checkNotLocked()  
      static RenameHandler create()
      Creates an instance.
      java.util.Map<java.lang.String,​java.lang.Enum<?>> getEnumRenames​(java.lang.Class<?> type)
      Gets the map of renamed for an enum type.
      java.util.Set<java.lang.Class<?>> getEnumTypesWithRenames()
      Gets the set of enum types that have renames.
      java.util.Map<java.lang.String,​java.lang.Class<?>> getTypeRenames()
      Gets the map of renamed types.
      private java.lang.Class<?> loadPrimitiveType​(java.lang.String fullName, java.lang.ClassNotFoundException ex)  
      (package private) java.lang.Class<?> loadType​(java.lang.String fullName)
      Loads a type avoiding nulls
      void lock()
      Locks this instance of the rename handler.
      <T extends java.lang.Enum<T>>
      T
      lookupEnum​(java.lang.Class<T> type, java.lang.String name)
      Lookup an enum from a name, handling renames.
      java.lang.Class<?> lookupType​(java.lang.String name)
      Lookup a type from a name, handling renames.
      void renamedEnum​(java.lang.String oldName, java.lang.Enum<?> currentValue)
      Register the fact that an enum constant was renamed.
      void renamedType​(java.lang.String oldName, java.lang.Class<?> currentValue)
      Register the fact that a type was renamed.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • INSTANCE

        public static final RenameHandler INSTANCE
        A mutable global instance. This is a singleton instance which is mutated.
      • locked

        private volatile boolean locked
        The lock flag.
      • typeRenames

        private final java.util.concurrent.ConcurrentHashMap<java.lang.String,​java.lang.Class<?>> typeRenames
        The type renames.
      • enumRenames

        private final java.util.concurrent.ConcurrentHashMap<java.lang.Class<?>,​java.util.Map<java.lang.String,​java.lang.Enum<?>>> enumRenames
        The enum renames.
    • Constructor Detail

      • RenameHandler

        private RenameHandler()
        Restricted constructor.
    • Method Detail

      • create

        public static RenameHandler create()
        Creates an instance.

        This is not normally used as the preferred option is to edit the singleton.

        Returns:
        a new instance, not null
      • renamedType

        public void renamedType​(java.lang.String oldName,
                                java.lang.Class<?> currentValue)
        Register the fact that a type was renamed.

        This handles the use case where a class is renamed.

        Parameters:
        oldName - the old name of the type including the package name, not null
        currentValue - the current type, not null
      • getTypeRenames

        public java.util.Map<java.lang.String,​java.lang.Class<?>> getTypeRenames()
        Gets the map of renamed types.

        An empty map is returned if there are no renames.

        Returns:
        a copy of the set of enum types with renames, not null
      • lookupType

        public java.lang.Class<?> lookupType​(java.lang.String name)
                                      throws java.lang.ClassNotFoundException
        Lookup a type from a name, handling renames.
        Parameters:
        name - the name of the type to lookup, not null
        Returns:
        the type, not null
        Throws:
        java.lang.ClassNotFoundException - if the name is not a valid type
      • loadType

        java.lang.Class<?> loadType​(java.lang.String fullName)
                             throws java.lang.ClassNotFoundException
        Loads a type avoiding nulls
        Parameters:
        fullName - the full class name
        Returns:
        the loaded class
        Throws:
        java.lang.ClassNotFoundException - if the class is not found
      • loadPrimitiveType

        private java.lang.Class<?> loadPrimitiveType​(java.lang.String fullName,
                                                     java.lang.ClassNotFoundException ex)
                                              throws java.lang.ClassNotFoundException
        Throws:
        java.lang.ClassNotFoundException
      • renamedEnum

        public void renamedEnum​(java.lang.String oldName,
                                java.lang.Enum<?> currentValue)
        Register the fact that an enum constant was renamed.

        This handles the use case where an enum constant is renamed, but the enum class remains the same.

        Parameters:
        oldName - the old name of the enum constant, not null
        currentValue - the current enum constant, not null
      • getEnumTypesWithRenames

        public java.util.Set<java.lang.Class<?>> getEnumTypesWithRenames()
        Gets the set of enum types that have renames.

        An empty set is returned if there are no renames.

        Returns:
        a copy of the set of enum types with renames, not null
      • getEnumRenames

        public java.util.Map<java.lang.String,​java.lang.Enum<?>> getEnumRenames​(java.lang.Class<?> type)
        Gets the map of renamed for an enum type.

        An empty map is returned if there are no renames.

        Parameters:
        type - the enum type, not null
        Returns:
        a copy of the set of enum renames, not null
      • lookupEnum

        public <T extends java.lang.Enum<T>> T lookupEnum​(java.lang.Class<T> type,
                                                          java.lang.String name)
        Lookup an enum from a name, handling renames.
        Type Parameters:
        T - the type of the desired enum
        Parameters:
        type - the enum type, not null
        name - the name of the enum to lookup, not null
        Returns:
        the enum value, not null
        Throws:
        java.lang.IllegalArgumentException - if the name is not a valid enum constant
      • lock

        public void lock()
        Locks this instance of the rename handler.

        For additional security, an application should lock the rename handler once any types and enums have been registered.

      • checkNotLocked

        private void checkNotLocked()
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object