< prev index next >

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

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


  75     }
  76 
  77     /**
  78      * Create a PathCloader, specifying an array of files for the path.
  79      * @param path an array of files, identifying a sequence of locations in which
  80      *   to look for classes to be loaded
  81      */
  82     public PathClassLoader(File[] path) {
  83         this.path = path;
  84     }
  85 
  86     /**
  87      * Attempt to load a class if it is not already loaded, and optionally
  88      * resolve any imports it might have.
  89      *
  90      * @param name The fully-qualified name of the class to load.
  91      * @param resolve True if imports should be resolved, false otherwise.
  92      * @return the class that was loaded
  93      * @throws ClassNotFoundException if the class was not found.
  94      */
  95     protected Class loadClass(String name, boolean resolve)
  96         throws ClassNotFoundException {
  97 
  98             Class<?> cl = classes.get(name);
  99 
 100             if (cl == null) {
 101                 try {
 102                     cl = findSystemClass(name);
 103                 }
 104                 catch (ClassNotFoundException e) {
 105                     cl = locateClass(name);
 106                 }
 107             }
 108 
 109             if (resolve)
 110                 resolveClass(cl);
 111 
 112             return cl;
 113     }
 114 
 115 
 116     private synchronized Class locateClass(String name)
 117         throws ClassNotFoundException {
 118         //System.err.println("locateClass: " + name);
 119         Class<?> c = classes.get(name);
 120         if (c != null)
 121             return c;
 122 
 123         for (int i = 0; i < path.length; i++) {
 124             if (path[i].isDirectory())
 125                 c = locateClassInDir(name, path[i]);
 126             else
 127                 c = locateClassInJar(name, path[i]);
 128 
 129             if (c != null) {
 130                 classes.put(name, c);
 131                 return c;
 132             }
 133         }
 134 
 135         throw new ClassNotFoundException(name);
 136     }
 137 
 138     private Class locateClassInDir(String name, File dir)
 139         throws ClassNotFoundException {
 140         //System.err.println("locateClassInDir: " + name + " " + dir);
 141         String cname = name.replace('.', '/') + ".class";
 142         try {
 143             File file = new File(dir, cname);
 144             return readClass(name, new FileInputStream(file), (int)(file.length()));
 145         }
 146         catch (IOException e) {
 147             //System.err.println("locateClassInDir: " + e);
 148             return null;
 149         }
 150     }
 151 
 152     private Class locateClassInJar(String name, File jarFile)
 153         throws ClassNotFoundException {
 154         //System.err.println("locateClassInJar: " + name + " " + jarFile);
 155         String cname = name.replace('.', '/') + ".class";
 156         try {
 157             ZipFile z = zips.get(jarFile);
 158             if (z == null) {
 159                 z = new ZipFile(jarFile);
 160                 zips.put(jarFile, z);
 161             }
 162             ZipEntry ze = z.getEntry(cname);
 163             if (ze == null)
 164                 return null;
 165             return readClass(name, z.getInputStream(ze), (int)(ze.getSize()));
 166         }
 167         catch (IOException e) {
 168             //System.err.println("locateClassInJar: " + e);
 169             return null;
 170         }
 171     }
 172 
 173     private Class readClass(String name, InputStream in, int size) throws IOException {
 174         byte[] data = new byte[size];
 175         try {
 176             for (int total = 0; total < size; ) {
 177                 total += in.read(data, total, size - total);
 178             }
 179         }
 180         finally {
 181             in.close();
 182         }
 183         return defineClass(name, data, 0, data.length);
 184     }
 185 
 186     private File[] split(String s) {
 187         char pathCh = File.pathSeparatorChar;
 188         Vector<File> v = new Vector<>();
 189         int start = 0;
 190         for (int i = s.indexOf(pathCh); i != -1; i = s.indexOf(pathCh, start)) {
 191             add(s.substring(start, i), v);
 192             start = i + 1;
 193         }


  75     }
  76 
  77     /**
  78      * Create a PathCloader, specifying an array of files for the path.
  79      * @param path an array of files, identifying a sequence of locations in which
  80      *   to look for classes to be loaded
  81      */
  82     public PathClassLoader(File[] path) {
  83         this.path = path;
  84     }
  85 
  86     /**
  87      * Attempt to load a class if it is not already loaded, and optionally
  88      * resolve any imports it might have.
  89      *
  90      * @param name The fully-qualified name of the class to load.
  91      * @param resolve True if imports should be resolved, false otherwise.
  92      * @return the class that was loaded
  93      * @throws ClassNotFoundException if the class was not found.
  94      */
  95     protected Class<?> loadClass(String name, boolean resolve)
  96         throws ClassNotFoundException {
  97 
  98             Class<?> cl = classes.get(name);
  99 
 100             if (cl == null) {
 101                 try {
 102                     cl = findSystemClass(name);
 103                 }
 104                 catch (ClassNotFoundException e) {
 105                     cl = locateClass(name);
 106                 }
 107             }
 108 
 109             if (resolve)
 110                 resolveClass(cl);
 111 
 112             return cl;
 113     }
 114 
 115 
 116     private synchronized Class<?> locateClass(String name)
 117         throws ClassNotFoundException {
 118         //System.err.println("locateClass: " + name);
 119         Class<?> c = classes.get(name);
 120         if (c != null)
 121             return c;
 122 
 123         for (int i = 0; i < path.length; i++) {
 124             if (path[i].isDirectory())
 125                 c = locateClassInDir(name, path[i]);
 126             else
 127                 c = locateClassInJar(name, path[i]);
 128 
 129             if (c != null) {
 130                 classes.put(name, c);
 131                 return c;
 132             }
 133         }
 134 
 135         throw new ClassNotFoundException(name);
 136     }
 137 
 138     private Class<?> locateClassInDir(String name, File dir)
 139         throws ClassNotFoundException {
 140         //System.err.println("locateClassInDir: " + name + " " + dir);
 141         String cname = name.replace('.', '/') + ".class";
 142         try {
 143             File file = new File(dir, cname);
 144             return readClass(name, new FileInputStream(file), (int)(file.length()));
 145         }
 146         catch (IOException e) {
 147             //System.err.println("locateClassInDir: " + e);
 148             return null;
 149         }
 150     }
 151 
 152     private Class<?> locateClassInJar(String name, File jarFile)
 153         throws ClassNotFoundException {
 154         //System.err.println("locateClassInJar: " + name + " " + jarFile);
 155         String cname = name.replace('.', '/') + ".class";
 156         try {
 157             ZipFile z = zips.get(jarFile);
 158             if (z == null) {
 159                 z = new ZipFile(jarFile);
 160                 zips.put(jarFile, z);
 161             }
 162             ZipEntry ze = z.getEntry(cname);
 163             if (ze == null)
 164                 return null;
 165             return readClass(name, z.getInputStream(ze), (int)(ze.getSize()));
 166         }
 167         catch (IOException e) {
 168             //System.err.println("locateClassInJar: " + e);
 169             return null;
 170         }
 171     }
 172 
 173     private Class<?> readClass(String name, InputStream in, int size) throws IOException {
 174         byte[] data = new byte[size];
 175         try {
 176             for (int total = 0; total < size; ) {
 177                 total += in.read(data, total, size - total);
 178             }
 179         }
 180         finally {
 181             in.close();
 182         }
 183         return defineClass(name, data, 0, data.length);
 184     }
 185 
 186     private File[] split(String s) {
 187         char pathCh = File.pathSeparatorChar;
 188         Vector<File> v = new Vector<>();
 189         int start = 0;
 190         for (int i = s.indexOf(pathCh); i != -1; i = s.indexOf(pathCh, start)) {
 191             add(s.substring(start, i), v);
 192             start = i + 1;
 193         }
< prev index next >