Class ImportsTracker


  • public class ImportsTracker
    extends java.lang.Object
    Organizer for imports to a source file. This is a state-based organizer, which allows tentative imports to be overridden up until the point where the map from fully-qualified class names to unqualified names is constructed by calling getNameMap(), then further non-conflicting imports are allowed until the final import list is constructed by calling freeze(String).
    Author:
    Dennis M. Sosnoski
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private boolean m_frozen
      Further imports blocked flag.
      private java.util.TreeSet m_importedTypes
      Set of imported classes.
      private java.util.Map m_localTypeName
      Set of unqualified type full names.
      private java.util.Map m_nameMap
      Map from class names in imports set to names used (null until getNameMap() called).
      private java.lang.String m_packageName
      Package name for classes in source.
      private java.util.Map m_unqualifiedNameType
      Map from simple names of unqualified types to full names.
    • Constructor Summary

      Constructors 
      Constructor Description
      ImportsTracker​(java.lang.String pkgname)
      Constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected boolean addImport​(java.lang.String fqname, boolean force)
      Add import for class.
      void addLocalType​(java.lang.String name, java.lang.String fqname)
      Add local definition name to those visible in class.
      java.util.List freeze​(java.lang.String cname)
      Freeze imports and return a list of imports.
      java.lang.String getName​(java.lang.String type)
      Get the name to be used for a type.
      java.util.Map getNameMap()
      Get map from imported fully-qualified class names to short names.
      boolean isQualified​(java.lang.String fqname)
      Check if type needs qualified references.
      • Methods inherited from class java.lang.Object

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

      • m_packageName

        private final java.lang.String m_packageName
        Package name for classes in source.
      • m_importedTypes

        private final java.util.TreeSet m_importedTypes
        Set of imported classes.
      • m_unqualifiedNameType

        private final java.util.Map m_unqualifiedNameType
        Map from simple names of unqualified types to full names.
      • m_localTypeName

        private final java.util.Map m_localTypeName
        Set of unqualified type full names.
      • m_nameMap

        private java.util.Map m_nameMap
        Map from class names in imports set to names used (null until getNameMap() called).
      • m_frozen

        private boolean m_frozen
        Further imports blocked flag.
    • Constructor Detail

      • ImportsTracker

        public ImportsTracker​(java.lang.String pkgname)
        Constructor.
        Parameters:
        pkgname - containing package name
    • Method Detail

      • addLocalType

        public void addLocalType​(java.lang.String name,
                                 java.lang.String fqname)
        Add local definition name to those visible in class. If the name conflicts with an import, the import is removed to force fully-qualified references.
        Parameters:
        name - simple class name
        fqname - fully qualified class name
      • addImport

        protected boolean addImport​(java.lang.String fqname,
                                    boolean force)
        Add import for class. If the requested import doesn't conflict with the current set it's added.
        Parameters:
        fqname - fully qualified class name
        force - force replacement of current import
        Returns:
        true if added as import
      • isQualified

        public boolean isQualified​(java.lang.String fqname)
        Check if type needs qualified references.
        Parameters:
        fqname - fully qualified class name
        Returns:
        true if needs qualification
      • getNameMap

        public java.util.Map getNameMap()
        Get map from imported fully-qualified class names to short names. Once this method is called, overrides of existing imports are blocked (since the existing imports may have been used), though added non-conflicting imports can still be added.
        Returns:
        map
      • getName

        public java.lang.String getName​(java.lang.String type)
        Get the name to be used for a type. If the type has been imported this returns the short form of the name; otherwise it just returns the fully-qualified name. This method forces a call to getNameMap(), which in turn blocks removing any imports later.
        Parameters:
        type - fully-qualified type name
        Returns:
        name
      • freeze

        public java.util.List freeze​(java.lang.String cname)
        Freeze imports and return a list of imports.
        Parameters:
        cname - simple name of class (used to identify inner class references)
        Returns:
        list