Class ClassPath

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public class ClassPath
    extends java.lang.Object
    implements java.io.Closeable
    Loads class files from the CLASSPATH. Inspired by sun.tools.ClassPath.
    • Constructor Summary

      Constructors 
      Constructor Description
      ClassPath()
      Deprecated.
      Use SYSTEM_CLASS_PATH constant
      ClassPath​(java.lang.String classPath)
      Search for classes in given path.
      ClassPath​(ClassPath parent, java.lang.String classPathString)
      Constructs a ClassPath with a parent and class path string.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private static void addJdkModules​(java.lang.String javaHome, java.util.List<java.lang.String> list)  
      void close()  
      boolean equals​(java.lang.Object obj)  
      byte[] getBytes​(java.lang.String name)
      Gets byte array for the given class.
      byte[] getBytes​(java.lang.String name, java.lang.String suffix)
      Gets byte array for the given file.
      ClassPath.ClassFile getClassFile​(java.lang.String name)
      Gets the input stream for the given class.
      ClassPath.ClassFile getClassFile​(java.lang.String name, java.lang.String suffix)
      Gets the class file for the given Java class.
      private ClassPath.ClassFile getClassFileInternal​(java.lang.String name, java.lang.String suffix)  
      static java.lang.String getClassPath()
      Checks for class path components in the following properties: "java.class.path", "sun.boot.class.path", "java.ext.dirs"
      java.io.InputStream getInputStream​(java.lang.String name)
      Gets an InputStream.
      java.io.InputStream getInputStream​(java.lang.String name, java.lang.String suffix)
      Gets an InputStream for a class or resource on the classpath.
      java.lang.String getPath​(java.lang.String name)
      Gets the full canonical path for the given file.
      java.lang.String getPath​(java.lang.String name, java.lang.String suffix)
      Gets the full canonical path for the given file.
      private static void getPathComponents​(java.lang.String path, java.util.List<java.lang.String> list)  
      java.net.URL getResource​(java.lang.String name)
      Gets the URL for the given resource.
      java.io.InputStream getResourceAsStream​(java.lang.String name)
      Gets the InputStream for the given resource.
      java.util.Enumeration<java.net.URL> getResources​(java.lang.String name)
      Gets an Enumeration of URLs for the given resource.
      int hashCode()  
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

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

      • ARCHIVE_FILTER

        private static final java.io.FilenameFilter ARCHIVE_FILTER
        Filter for archive files (.zip and .jar).
      • MODULES_FILTER

        private static final java.io.FilenameFilter MODULES_FILTER
        Filter for module files.
      • SYSTEM_CLASS_PATH

        public static final ClassPath SYSTEM_CLASS_PATH
        The system class path.
      • classPathString

        private final java.lang.String classPathString
    • Constructor Detail

      • ClassPath

        @Deprecated
        public ClassPath()
        Deprecated.
        Use SYSTEM_CLASS_PATH constant
        Search for classes in CLASSPATH.
      • ClassPath

        public ClassPath​(ClassPath parent,
                         java.lang.String classPathString)
        Constructs a ClassPath with a parent and class path string.
        Parameters:
        parent - the parent ClassPath.
        classPathString - the class path string.
      • ClassPath

        public ClassPath​(java.lang.String classPath)
        Search for classes in given path.
        Parameters:
        classPath - the class path string.
    • Method Detail

      • addJdkModules

        private static void addJdkModules​(java.lang.String javaHome,
                                          java.util.List<java.lang.String> list)
      • getClassPath

        public static java.lang.String getClassPath()
        Checks for class path components in the following properties: "java.class.path", "sun.boot.class.path", "java.ext.dirs"
        Returns:
        class path as used by default by BCEL.
      • getPathComponents

        private static void getPathComponents​(java.lang.String path,
                                              java.util.List<java.lang.String> list)
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • getBytes

        public byte[] getBytes​(java.lang.String name)
                        throws java.io.IOException
        Gets byte array for the given class.
        Parameters:
        name - fully qualified file name, for example java/lang/String.
        Returns:
        byte array for class.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • getBytes

        public byte[] getBytes​(java.lang.String name,
                               java.lang.String suffix)
                        throws java.io.IOException
        Gets byte array for the given file.
        Parameters:
        name - fully qualified file name, for example java/lang/String.
        suffix - file name ends with suffix, for example .java.
        Returns:
        byte array for file on class path.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • getClassFile

        public ClassPath.ClassFile getClassFile​(java.lang.String name)
                                         throws java.io.IOException
        Gets the input stream for the given class.
        Parameters:
        name - fully qualified class name, for example String.
        Returns:
        input stream for class.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • getClassFile

        public ClassPath.ClassFile getClassFile​(java.lang.String name,
                                                java.lang.String suffix)
                                         throws java.io.IOException
        Gets the class file for the given Java class.
        Parameters:
        name - fully qualified file name, for example java/lang/String.
        suffix - file name ends with suffix, for example .java.
        Returns:
        class file for the Java class.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • getClassFileInternal

        private ClassPath.ClassFile getClassFileInternal​(java.lang.String name,
                                                         java.lang.String suffix)
      • getInputStream

        public java.io.InputStream getInputStream​(java.lang.String name)
                                           throws java.io.IOException
        Gets an InputStream.

        The caller is responsible for closing the InputStream.

        Parameters:
        name - fully qualified class name, for example String.
        Returns:
        input stream for class.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • getInputStream

        public java.io.InputStream getInputStream​(java.lang.String name,
                                                  java.lang.String suffix)
                                           throws java.io.IOException
        Gets an InputStream for a class or resource on the classpath.

        The caller is responsible for closing the InputStream.

        Parameters:
        name - fully qualified file name, for example java/lang/String.
        suffix - file name ends with suff, for example .java.
        Returns:
        input stream for file on class path.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • getPath

        public java.lang.String getPath​(java.lang.String name)
                                 throws java.io.IOException
        Gets the full canonical path for the given file.
        Parameters:
        name - name of file to search for, for example java/lang/String.java.
        Returns:
        full (canonical) path for file.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • getPath

        public java.lang.String getPath​(java.lang.String name,
                                        java.lang.String suffix)
                                 throws java.io.IOException
        Gets the full canonical path for the given file.
        Parameters:
        name - name of file to search for, for example java/lang/String.
        suffix - file name suffix, for example .java.
        Returns:
        full (canonical) path for file, if it exists.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • getResource

        public java.net.URL getResource​(java.lang.String name)
        Gets the URL for the given resource.
        Parameters:
        name - fully qualified resource name, for example java/lang/String.class.
        Returns:
        URL supplying the resource, or null if no resource with that name.
        Since:
        6.0
      • getResourceAsStream

        public java.io.InputStream getResourceAsStream​(java.lang.String name)
        Gets the InputStream for the given resource.
        Parameters:
        name - fully qualified resource name, for example java/lang/String.class.
        Returns:
        InputStream supplying the resource, or null if no resource with that name.
        Since:
        6.0
      • getResources

        public java.util.Enumeration<java.net.URL> getResources​(java.lang.String name)
        Gets an Enumeration of URLs for the given resource.
        Parameters:
        name - fully qualified resource name, for example java/lang/String.class.
        Returns:
        An Enumeration of URLs supplying the resource, or an empty Enumeration if no resource with that name.
        Since:
        6.0
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
        Returns:
        used class path string.