langtools/src/jdk.dev/share/classes/com/sun/tools/jdeps/ClassFileReader.java
changeset 27579 d1a63c99cdd5
parent 25874 83c19f00452c
equal deleted inserted replaced
27578:d61af14a5cf7 27579:d1a63c99cdd5
    25 package com.sun.tools.jdeps;
    25 package com.sun.tools.jdeps;
    26 
    26 
    27 import com.sun.tools.classfile.ClassFile;
    27 import com.sun.tools.classfile.ClassFile;
    28 import com.sun.tools.classfile.ConstantPoolException;
    28 import com.sun.tools.classfile.ConstantPoolException;
    29 import com.sun.tools.classfile.Dependencies.ClassFileError;
    29 import com.sun.tools.classfile.Dependencies.ClassFileError;
    30 import java.io.*;
    30 import java.io.File;
       
    31 import java.io.FileNotFoundException;
       
    32 import java.io.IOException;
       
    33 import java.io.InputStream;
       
    34 import java.nio.file.FileSystem;
       
    35 import java.nio.file.FileSystems;
    31 import java.nio.file.FileVisitResult;
    36 import java.nio.file.FileVisitResult;
    32 import java.nio.file.Files;
    37 import java.nio.file.Files;
    33 import java.nio.file.Path;
    38 import java.nio.file.Path;
    34 import java.nio.file.SimpleFileVisitor;
    39 import java.nio.file.SimpleFileVisitor;
    35 import java.nio.file.attribute.BasicFileAttributes;
    40 import java.nio.file.attribute.BasicFileAttributes;
    36 import java.util.*;
    41 import java.util.*;
    37 import java.util.jar.JarEntry;
    42 import java.util.jar.JarEntry;
    38 import java.util.jar.JarFile;
    43 import java.util.jar.JarFile;
       
    44 import java.util.stream.Collectors;
       
    45 import java.util.stream.Stream;
    39 
    46 
    40 /**
    47 /**
    41  * ClassFileReader reads ClassFile(s) of a given path that can be
    48  * ClassFileReader reads ClassFile(s) of a given path that can be
    42  * a .class file, a directory, or a JAR file.
    49  * a .class file, a directory, or a JAR file.
    43  */
    50  */
   157     public String toString() {
   164     public String toString() {
   158         return path.toString();
   165         return path.toString();
   159     }
   166     }
   160 
   167 
   161     private static class DirectoryReader extends ClassFileReader {
   168     private static class DirectoryReader extends ClassFileReader {
       
   169         protected final String fsSep;
   162         DirectoryReader(Path path) throws IOException {
   170         DirectoryReader(Path path) throws IOException {
       
   171             this(FileSystems.getDefault(), path);
       
   172         }
       
   173         DirectoryReader(FileSystem fs, Path path) throws IOException {
   163             super(path);
   174             super(path);
       
   175             this.fsSep = fs.getSeparator();
   164         }
   176         }
   165 
   177 
   166         public ClassFile getClassFile(String name) throws IOException {
   178         public ClassFile getClassFile(String name) throws IOException {
   167             if (name.indexOf('.') > 0) {
   179             if (name.indexOf('.') > 0) {
   168                 int i = name.lastIndexOf('.');
   180                 int i = name.lastIndexOf('.');
   169                 String pathname = name.replace('.', File.separatorChar) + ".class";
   181                 String pathname = name.replace(".", fsSep) + ".class";
   170                 Path p = path.resolve(pathname);
   182                 Path p = path.resolve(pathname);
   171                 if (!Files.exists(p)) {
   183                 if (!Files.exists(p)) {
   172                     p = path.resolve(pathname.substring(0, i) + "$" +
   184                     p = path.resolve(pathname.substring(0, i) + "$" +
   173                                      pathname.substring(i+1, pathname.length()));
   185                             pathname.substring(i+1, pathname.length()));
   174                 }
   186                 }
   175                 if (Files.exists(p)) {
   187                 if (Files.exists(p)) {
   176                     return readClassFile(p);
   188                     return readClassFile(p);
   177                 }
   189                 }
   178             } else {
   190             } else {
   191                     return iter;
   203                     return iter;
   192                 }
   204                 }
   193             };
   205             };
   194         }
   206         }
   195 
   207 
   196         private List<Path> walkTree(Path dir) throws IOException {
   208         private List<Path> entries;
   197             final List<Path> files = new ArrayList<>();
   209         protected synchronized List<Path> walkTree() throws IOException {
   198             Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
   210             if (entries == null) {
   199                 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
   211                 entries = new ArrayList<>();
   200                         throws IOException {
   212                 Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
   201                     if (file.getFileName().toString().endsWith(".class")) {
   213                     public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
   202                         files.add(file);
   214                             throws IOException {
       
   215                         if (file.getFileName().toString().endsWith(".class")) {
       
   216                             entries.add(file);
       
   217                         }
       
   218                         return FileVisitResult.CONTINUE;
   203                     }
   219                     }
   204                     return FileVisitResult.CONTINUE;
   220                 });
   205                 }
   221             }
   206             });
   222             return entries;
   207             return files;
       
   208         }
   223         }
   209 
   224 
   210         class DirectoryIterator implements Iterator<ClassFile> {
   225         class DirectoryIterator implements Iterator<ClassFile> {
   211             private List<Path> entries;
   226             private List<Path> entries;
   212             private int index = 0;
   227             private int index = 0;
   213             DirectoryIterator() throws IOException {
   228             DirectoryIterator() throws IOException {
   214                 entries = walkTree(path);
   229                 entries = walkTree();
   215                 index = 0;
   230                 index = 0;
   216             }
   231             }
   217 
   232 
   218             public boolean hasNext() {
   233             public boolean hasNext() {
   219                 return index != entries.size();
   234                 return index != entries.size();
   353 
   368 
   354         public void remove() {
   369         public void remove() {
   355             throw new UnsupportedOperationException("Not supported yet.");
   370             throw new UnsupportedOperationException("Not supported yet.");
   356         }
   371         }
   357     }
   372     }
       
   373 
       
   374     /**
       
   375      * ClassFileReader for modules.
       
   376      */
       
   377     static class ModuleClassReader extends DirectoryReader {
       
   378         final String modulename;
       
   379         ModuleClassReader(FileSystem fs, String mn, Path root) throws IOException {
       
   380             super(fs, root);
       
   381             this.modulename = mn;
       
   382         }
       
   383 
       
   384         public Set<String> packages() throws IOException {
       
   385             return walkTree().stream()
       
   386                              .map(this::toPackageName)
       
   387                              .sorted()
       
   388                              .collect(Collectors.toSet());
       
   389         }
       
   390 
       
   391         String toPackageName(Path p) {
       
   392             if (p.getParent() == null) {
       
   393                 return "";
       
   394             }
       
   395             return path.relativize(p.getParent()).toString().replace(fsSep, ".");
       
   396         }
       
   397     }
   358 }
   398 }