src/share/classes/com/sun/tools/jdeps/ClassFileReader.java

Print this page




  28 import com.sun.tools.classfile.ConstantPoolException;
  29 import com.sun.tools.classfile.Dependencies.ClassFileError;
  30 import java.io.*;
  31 import java.nio.file.FileVisitResult;
  32 import java.nio.file.Files;
  33 import java.nio.file.Path;
  34 import java.nio.file.SimpleFileVisitor;
  35 import java.nio.file.attribute.BasicFileAttributes;
  36 import java.util.*;
  37 import java.util.jar.JarEntry;
  38 import java.util.jar.JarFile;
  39 
  40 /**
  41  * ClassFileReader reads ClassFile(s) of a given path that can be
  42  * a .class file, a directory, or a JAR file.
  43  */
  44 public class ClassFileReader {
  45     /**
  46      * Returns a ClassFileReader instance of a given path.
  47      */
  48     public static ClassFileReader newInstance(File path) throws IOException {
  49         if (!path.exists()) {
  50             throw new FileNotFoundException(path.getAbsolutePath());
  51         }
  52 
  53         if (path.isDirectory()) {
  54             return new DirectoryReader(path.toPath());
  55         } else if (path.getName().endsWith(".jar")) {
  56             return new JarFileReader(path.toPath());
  57         } else {
  58             return new ClassFileReader(path.toPath());
  59         }
  60     }
  61 
  62     /**
  63      * Returns a ClassFileReader instance of a given JarFile.
  64      */
  65     public static ClassFileReader newInstance(Path path, JarFile jf) throws IOException {
  66         return new JarFileReader(path, jf);
  67     }
  68 
  69     protected final Path path;
  70     protected final String baseFileName;
  71     private ClassFileReader(Path path) {
  72         this.path = path;
  73         this.baseFileName = path.getFileName() != null
  74                                 ? path.getFileName().toString()
  75                                 : path.toString();
  76     }
  77 
  78     public String getFileName() {


 146 
 147         public void remove() {
 148             throw new UnsupportedOperationException("Not supported yet.");
 149         }
 150     }
 151 
 152     public String toString() {
 153         return path.toString();
 154     }
 155 
 156     private static class DirectoryReader extends ClassFileReader {
 157         DirectoryReader(Path path) throws IOException {
 158             super(path);
 159         }
 160 
 161         public ClassFile getClassFile(String name) throws IOException {
 162             if (name.indexOf('.') > 0) {
 163                 int i = name.lastIndexOf('.');
 164                 String pathname = name.replace('.', File.separatorChar) + ".class";
 165                 Path p = path.resolve(pathname);
 166                 if (!p.toFile().exists()) {
 167                     p = path.resolve(pathname.substring(0, i) + "$" +
 168                                      pathname.substring(i+1, pathname.length()));
 169                 }
 170                 if (p.toFile().exists()) {
 171                     return readClassFile(p);
 172                 }
 173             } else {
 174                 Path p = path.resolve(name + ".class");
 175                 if (p.toFile().exists()) {
 176                     return readClassFile(p);
 177                 }
 178             }
 179             return null;
 180         }
 181 
 182         public Iterable<ClassFile> getClassFiles() throws IOException {
 183             final Iterator<ClassFile> iter = new DirectoryIterator();
 184             return new Iterable<ClassFile>() {
 185                 public Iterator<ClassFile> iterator() {
 186                     return iter;
 187                 }
 188             };
 189         }
 190 
 191         private List<Path> walkTree(Path dir) throws IOException {
 192             final List<Path> files = new ArrayList<Path>();
 193             Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
 194                 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
 195                         throws IOException {
 196                     if (file.toFile().getName().endsWith(".class")) {
 197                         files.add(file);
 198                     }
 199                     return FileVisitResult.CONTINUE;
 200                 }
 201             });
 202             return files;
 203         }
 204 
 205         class DirectoryIterator implements Iterator<ClassFile> {
 206             private List<Path> entries;
 207             private int index = 0;
 208             DirectoryIterator() throws IOException {
 209                 entries = walkTree(path);
 210                 index = 0;
 211             }
 212 
 213             public boolean hasNext() {
 214                 return index != entries.size();
 215             }
 216 




  28 import com.sun.tools.classfile.ConstantPoolException;
  29 import com.sun.tools.classfile.Dependencies.ClassFileError;
  30 import java.io.*;
  31 import java.nio.file.FileVisitResult;
  32 import java.nio.file.Files;
  33 import java.nio.file.Path;
  34 import java.nio.file.SimpleFileVisitor;
  35 import java.nio.file.attribute.BasicFileAttributes;
  36 import java.util.*;
  37 import java.util.jar.JarEntry;
  38 import java.util.jar.JarFile;
  39 
  40 /**
  41  * ClassFileReader reads ClassFile(s) of a given path that can be
  42  * a .class file, a directory, or a JAR file.
  43  */
  44 public class ClassFileReader {
  45     /**
  46      * Returns a ClassFileReader instance of a given path.
  47      */
  48     public static ClassFileReader newInstance(Path path) throws IOException {
  49         if (!Files.exists(path)) {
  50             throw new FileNotFoundException(path.toString());
  51         }
  52 
  53         if (Files.isDirectory(path)) {
  54             return new DirectoryReader(path);
  55         } else if (path.getFileName().toString().endsWith(".jar")) {
  56             return new JarFileReader(path);
  57         } else {
  58             return new ClassFileReader(path);
  59         }
  60     }
  61 
  62     /**
  63      * Returns a ClassFileReader instance of a given JarFile.
  64      */
  65     public static ClassFileReader newInstance(Path path, JarFile jf) throws IOException {
  66         return new JarFileReader(path, jf);
  67     }
  68 
  69     protected final Path path;
  70     protected final String baseFileName;
  71     private ClassFileReader(Path path) {
  72         this.path = path;
  73         this.baseFileName = path.getFileName() != null
  74                                 ? path.getFileName().toString()
  75                                 : path.toString();
  76     }
  77 
  78     public String getFileName() {


 146 
 147         public void remove() {
 148             throw new UnsupportedOperationException("Not supported yet.");
 149         }
 150     }
 151 
 152     public String toString() {
 153         return path.toString();
 154     }
 155 
 156     private static class DirectoryReader extends ClassFileReader {
 157         DirectoryReader(Path path) throws IOException {
 158             super(path);
 159         }
 160 
 161         public ClassFile getClassFile(String name) throws IOException {
 162             if (name.indexOf('.') > 0) {
 163                 int i = name.lastIndexOf('.');
 164                 String pathname = name.replace('.', File.separatorChar) + ".class";
 165                 Path p = path.resolve(pathname);
 166                 if (!Files.exists(p)) {
 167                     p = path.resolve(pathname.substring(0, i) + "$" +
 168                                      pathname.substring(i+1, pathname.length()));
 169                 }
 170                 if (Files.exists(p)) {
 171                     return readClassFile(p);
 172                 }
 173             } else {
 174                 Path p = path.resolve(name + ".class");
 175                 if (Files.exists(p)) {
 176                     return readClassFile(p);
 177                 }
 178             }
 179             return null;
 180         }
 181 
 182         public Iterable<ClassFile> getClassFiles() throws IOException {
 183             final Iterator<ClassFile> iter = new DirectoryIterator();
 184             return new Iterable<ClassFile>() {
 185                 public Iterator<ClassFile> iterator() {
 186                     return iter;
 187                 }
 188             };
 189         }
 190 
 191         private List<Path> walkTree(Path dir) throws IOException {
 192             final List<Path> files = new ArrayList<Path>();
 193             Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
 194                 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
 195                         throws IOException {
 196                     if (file.getFileName().toString().endsWith(".class")) {
 197                         files.add(file);
 198                     }
 199                     return FileVisitResult.CONTINUE;
 200                 }
 201             });
 202             return files;
 203         }
 204 
 205         class DirectoryIterator implements Iterator<ClassFile> {
 206             private List<Path> entries;
 207             private int index = 0;
 208             DirectoryIterator() throws IOException {
 209                 entries = walkTree(path);
 210                 index = 0;
 211             }
 212 
 213             public boolean hasNext() {
 214                 return index != entries.size();
 215             }
 216