Class ClassNameResolver


  • public class ClassNameResolver
    extends java.lang.Object
    Serves as a bidirectional mapping between classes and their names. Supports aliases and package search paths. Also comes pre-configured with shortened aliases for most common types (primitives, numbers, lists, etc) and search paths for common java packages like lang, util, awt, swing, beans.
    • Constructor Detail

      • ClassNameResolver

        public ClassNameResolver()
    • Method Detail

      • addDefaults

        public void addDefaults()
      • addAlias

        public void addAlias​(java.lang.Class clazz,
                             java.lang.String alias)
        Adds a new alias for this class. This alias takes precedence over a search path. Each alias can only be used once, but an class can have more than one alias. The class will resolve to the first alias (name) added for it.
      • addSearchPath

        @Deprecated
        public void addSearchPath​(java.lang.String path)
        Deprecated.
        This functionality is deprecated. You can continue to add search paths for the backwards compatibility of de-serializing, but new serialized code will not contain the shortened versions of the classnames.
        Adds a new search path. For example, if you add the search path of "javax.swing" then the class "javax.swing.JLabel" will map bidirectionally to the string "JLabel"
      • getName

        public java.lang.String getName​(java.lang.Class clazz)
        Returns the inverse of classForName(String). That is, the appropriate string for which a call to classForName with the return value of this method would return clazz. Makes use of any configured aliases and search paths, and appropriately encodes array types.
      • classForName

        public final java.lang.Class classForName​(java.lang.String name)
                                           throws java.lang.ClassNotFoundException
        Attempts to find the Class for the given name, in the context of any aliases and search paths that have been previously configured

        Logic is as follows:

        1. Look through the aliases for the given name. These can be configured manually (see addAlias(Class, String)), and will also be added automatically to act as a cache of any of the following steps find the class.
        2. If the name starts with a "[", get call classForName recursively on name.substring(1) to find the array component type, and then find the array class using Array.newInstance(Class, int)
        3. Try Class.forName(name)
        4. Look through each search path (see addSearchPath(String), looking for Class.forName(searchPath + "." + name)
        Throws:
        java.lang.ClassNotFoundException
      • classForNameImpl

        protected java.lang.Class classForNameImpl​(java.lang.String name)
                                            throws java.lang.ClassNotFoundException
        Throws:
        java.lang.ClassNotFoundException