< prev index next >

src/com/sun/javatest/util/DirectoryClassLoader.java

Print this page
rev 145 : 7902237: Fixing raw use of parameterized class
Reviewed-by: jjg


  56      * Constructor that provides the name of the directory in which to search
  57      * for a given class.
  58      *
  59      * @param dirName  A file pathname specifying the directory to search.
  60      */
  61     public DirectoryClassLoader(String dirName) {
  62         loadDir = new File(dirName);
  63     }
  64 
  65     //----------ClassLoader methods---------------------------------------------
  66 
  67     /**
  68      * Attempt to load a class if it is not already loaded, and optionally
  69      * resolve any imports it might have.
  70      *
  71      * @param name The fully-qualified name of the class to load.
  72      * @param resolve True if imports should be resolved, false otherwise.
  73      * @return the class that was loaded
  74      * @throws ClassNotFoundException if the class was not found.
  75      */
  76     protected Class loadClass(String name, boolean resolve)
  77         throws ClassNotFoundException {
  78 
  79         // check the cache first
  80         Class<?> cl = classes.get(name);
  81 
  82         // not found in the cache?
  83         if (cl == null) {
  84             // see if it is a system class
  85             try {
  86                 cl = findSystemClass(name);
  87             }
  88             catch (ClassNotFoundException e) {
  89                 // It's not a system class, look in the directory for this
  90                 // class loader.  If there is an IO problem, we'll throw
  91                 // ClassNotFoundException; if the class file is read correctly
  92                 // but is an invalid class, we'll get a LinkageError
  93                 cl = locateClass(name);
  94             }
  95         }
  96 


 156      * @param  name the resource name
 157      * @return a URL for reading the resource, or <code>null</code>
 158      *         if the resource could not be found
 159      * @since  JDK1.2
 160      */
 161     public URL findResource(String name) {
 162        try {
 163            File f = new File(loadDir, name);
 164            if (f.exists())
 165                return new URL("file:" + f.getAbsolutePath());
 166            else
 167                return null;
 168        }
 169        catch (java.net.MalformedURLException e) {
 170            return null;
 171        }
 172     }
 173 
 174     //----------internal methods------------------------------------------------
 175 
 176     private synchronized Class locateClass(String name)
 177         throws ClassNotFoundException {
 178         //This check is currently necessary; we just
 179         // check the cache at the one call site, but that was not
 180         // synchronized, so there is a very small remote chance another
 181         // caller has just loaded this class.
 182         Class<?> cl = classes.get(name);
 183         if (cl != null)
 184             return cl;
 185 
 186         String cname = name.replace('.', '/') + ".class";
 187 
 188         File file = new File(loadDir, cname);
 189 
 190         try {
 191             byte[] data;
 192             try (InputStream in = new FileInputStream(file)) {
 193                 int len = in.available();
 194                 data = new byte[len];
 195                 for (int total = 0; total < data.length; ) {
 196                     total += in.read(data, total, data.length - total);




  56      * Constructor that provides the name of the directory in which to search
  57      * for a given class.
  58      *
  59      * @param dirName  A file pathname specifying the directory to search.
  60      */
  61     public DirectoryClassLoader(String dirName) {
  62         loadDir = new File(dirName);
  63     }
  64 
  65     //----------ClassLoader methods---------------------------------------------
  66 
  67     /**
  68      * Attempt to load a class if it is not already loaded, and optionally
  69      * resolve any imports it might have.
  70      *
  71      * @param name The fully-qualified name of the class to load.
  72      * @param resolve True if imports should be resolved, false otherwise.
  73      * @return the class that was loaded
  74      * @throws ClassNotFoundException if the class was not found.
  75      */
  76     protected Class<?> loadClass(String name, boolean resolve)
  77         throws ClassNotFoundException {
  78 
  79         // check the cache first
  80         Class<?> cl = classes.get(name);
  81 
  82         // not found in the cache?
  83         if (cl == null) {
  84             // see if it is a system class
  85             try {
  86                 cl = findSystemClass(name);
  87             }
  88             catch (ClassNotFoundException e) {
  89                 // It's not a system class, look in the directory for this
  90                 // class loader.  If there is an IO problem, we'll throw
  91                 // ClassNotFoundException; if the class file is read correctly
  92                 // but is an invalid class, we'll get a LinkageError
  93                 cl = locateClass(name);
  94             }
  95         }
  96 


 156      * @param  name the resource name
 157      * @return a URL for reading the resource, or <code>null</code>
 158      *         if the resource could not be found
 159      * @since  JDK1.2
 160      */
 161     public URL findResource(String name) {
 162        try {
 163            File f = new File(loadDir, name);
 164            if (f.exists())
 165                return new URL("file:" + f.getAbsolutePath());
 166            else
 167                return null;
 168        }
 169        catch (java.net.MalformedURLException e) {
 170            return null;
 171        }
 172     }
 173 
 174     //----------internal methods------------------------------------------------
 175 
 176     private synchronized Class<?> locateClass(String name)
 177         throws ClassNotFoundException {
 178         //This check is currently necessary; we just
 179         // check the cache at the one call site, but that was not
 180         // synchronized, so there is a very small remote chance another
 181         // caller has just loaded this class.
 182         Class<?> cl = classes.get(name);
 183         if (cl != null)
 184             return cl;
 185 
 186         String cname = name.replace('.', '/') + ".class";
 187 
 188         File file = new File(loadDir, cname);
 189 
 190         try {
 191             byte[] data;
 192             try (InputStream in = new FileInputStream(file)) {
 193                 int len = in.available();
 194                 data = new byte[len];
 195                 for (int total = 0; total < data.length; ) {
 196                     total += in.read(data, total, data.length - total);


< prev index next >