< prev index next >

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

Print this page




  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.tools.jdeps;
  27 
  28 import com.sun.tools.classfile.AccessFlags;
  29 import com.sun.tools.classfile.ClassFile;
  30 import com.sun.tools.classfile.ConstantPoolException;
  31 import com.sun.tools.classfile.Dependencies.ClassFileError;
  32 
  33 import jdk.internal.util.jar.VersionedStream;
  34 
  35 import java.io.Closeable;
  36 import java.io.File;
  37 import java.io.FileNotFoundException;
  38 import java.io.IOException;
  39 import java.io.InputStream;
  40 import java.io.UncheckedIOException;
  41 import java.nio.file.FileSystem;
  42 import java.nio.file.FileSystems;
  43 import java.nio.file.Files;
  44 import java.nio.file.Path;
  45 import java.util.ArrayList;
  46 import java.util.Collections;
  47 import java.util.Enumeration;
  48 import java.util.Iterator;
  49 import java.util.List;
  50 import java.util.NoSuchElementException;
  51 import java.util.Set;
  52 import java.util.jar.JarEntry;
  53 import java.util.jar.JarFile;
  54 import java.util.stream.Collectors;


 319 
 320         private static JarFile openJarFile(File f, Runtime.Version version)
 321                 throws IOException {
 322             JarFile jf;
 323             if (version == null) {
 324                 jf = new JarFile(f, false);
 325                 if (jf.isMultiRelease()) {
 326                     throw new MultiReleaseException("err.multirelease.option.notfound", f.getName());
 327                 }
 328             } else {
 329                 jf = new JarFile(f, false, ZipFile.OPEN_READ, version);
 330                 if (!jf.isMultiRelease()) {
 331                     throw new MultiReleaseException("err.multirelease.option.exists", f.getName());
 332                 }
 333             }
 334             return jf;
 335         }
 336 
 337         protected Set<String> scan() {
 338             try (JarFile jf = openJarFile(path.toFile(), version)) {
 339                 return VersionedStream.stream(jf).map(JarEntry::getName)
 340                          .filter(n -> n.endsWith(".class"))
 341                          .collect(Collectors.toSet());
 342             } catch (IOException e) {
 343                 throw new UncheckedIOException(e);
 344             }
 345         }
 346 
 347         public ClassFile getClassFile(String name) throws IOException {
 348             if (name.indexOf('.') > 0) {
 349                 int i = name.lastIndexOf('.');
 350                 String entryName = name.replace('.', '/') + ".class";
 351                 JarEntry e = jarfile.getJarEntry(entryName);
 352                 if (e == null) {
 353                     e = jarfile.getJarEntry(entryName.substring(0, i) + "$"
 354                             + entryName.substring(i + 1, entryName.length()));
 355                 }
 356                 if (e != null) {
 357                     return readClassFile(jarfile, e);
 358                 }
 359             } else {


 366         }
 367 
 368         protected ClassFile readClassFile(JarFile jarfile, JarEntry e) throws IOException {
 369             try (InputStream is = jarfile.getInputStream(e)) {
 370                 ClassFile cf = ClassFile.read(is);
 371                 if (jarfile.isMultiRelease()) {
 372                     VersionHelper.add(jarfile, e, cf);
 373                 }
 374                 return cf;
 375             } catch (ConstantPoolException ex) {
 376                 throw new ClassFileError(ex);
 377             }
 378         }
 379 
 380         public Iterable<ClassFile> getClassFiles() throws IOException {
 381             final Iterator<ClassFile> iter = new JarFileIterator(this, jarfile);
 382             return () -> iter;
 383         }
 384     }
 385 
 386     Enumeration<JarEntry> versionedEntries(JarFile jf) {
 387         Iterator<JarEntry> it = VersionedStream.stream(jf).iterator();
 388         return new Enumeration<>() {
 389             @Override
 390             public boolean hasMoreElements() {
 391                 return it.hasNext();
 392             }
 393 
 394             @Override
 395             public JarEntry nextElement() {
 396                 return it.next();
 397             }
 398         };
 399     }
 400 
 401     class JarFileIterator implements Iterator<ClassFile> {
 402         protected final JarFileReader reader;
 403         protected Enumeration<JarEntry> entries;
 404         protected JarFile jf;
 405         protected JarEntry nextEntry;
 406         protected ClassFile cf;
 407         JarFileIterator(JarFileReader reader) {
 408             this(reader, null);
 409         }
 410         JarFileIterator(JarFileReader reader, JarFile jarfile) {
 411             this.reader = reader;
 412             setJarFile(jarfile);
 413         }
 414 
 415         void setJarFile(JarFile jarfile) {
 416             if (jarfile == null) return;
 417 
 418             this.jf = jarfile;
 419             this.entries = versionedEntries(jf);
 420             this.nextEntry = nextEntry();
 421         }
 422 
 423         public boolean hasNext() {
 424             if (nextEntry != null && cf != null) {
 425                 return true;
 426             }
 427             while (nextEntry != null) {
 428                 try {
 429                     cf = reader.readClassFile(jf, nextEntry);
 430                     return true;
 431                 } catch (ClassFileError | IOException ex) {
 432                     skippedEntries.add(String.format("%s: %s (%s)",
 433                                                      ex.getMessage(),
 434                                                      nextEntry.getName(),
 435                                                      jf.getName()));
 436                 }
 437                 nextEntry = nextEntry();
 438             }
 439             return false;
 440         }
 441 
 442         public ClassFile next() {
 443             if (!hasNext()) {
 444                 throw new NoSuchElementException();
 445             }
 446             ClassFile classFile = cf;
 447             cf = null;
 448             nextEntry = nextEntry();
 449             return classFile;
 450         }
 451 
 452         protected JarEntry nextEntry() {
 453             while (entries.hasMoreElements()) {
 454                 JarEntry e = entries.nextElement();
 455                 String name = e.getName();
 456                 if (name.endsWith(".class")) {
 457                     return e;
 458                 }
 459             }
 460             return null;
 461         }
 462 
 463         public void remove() {
 464             throw new UnsupportedOperationException("Not supported yet.");
 465         }
 466     }
 467     private static final String MODULE_INFO = "module-info.class";
 468 }


  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.tools.jdeps;
  27 
  28 import com.sun.tools.classfile.AccessFlags;
  29 import com.sun.tools.classfile.ClassFile;
  30 import com.sun.tools.classfile.ConstantPoolException;
  31 import com.sun.tools.classfile.Dependencies.ClassFileError;
  32 


  33 import java.io.Closeable;
  34 import java.io.File;
  35 import java.io.FileNotFoundException;
  36 import java.io.IOException;
  37 import java.io.InputStream;
  38 import java.io.UncheckedIOException;
  39 import java.nio.file.FileSystem;
  40 import java.nio.file.FileSystems;
  41 import java.nio.file.Files;
  42 import java.nio.file.Path;
  43 import java.util.ArrayList;
  44 import java.util.Collections;
  45 import java.util.Enumeration;
  46 import java.util.Iterator;
  47 import java.util.List;
  48 import java.util.NoSuchElementException;
  49 import java.util.Set;
  50 import java.util.jar.JarEntry;
  51 import java.util.jar.JarFile;
  52 import java.util.stream.Collectors;


 317 
 318         private static JarFile openJarFile(File f, Runtime.Version version)
 319                 throws IOException {
 320             JarFile jf;
 321             if (version == null) {
 322                 jf = new JarFile(f, false);
 323                 if (jf.isMultiRelease()) {
 324                     throw new MultiReleaseException("err.multirelease.option.notfound", f.getName());
 325                 }
 326             } else {
 327                 jf = new JarFile(f, false, ZipFile.OPEN_READ, version);
 328                 if (!jf.isMultiRelease()) {
 329                     throw new MultiReleaseException("err.multirelease.option.exists", f.getName());
 330                 }
 331             }
 332             return jf;
 333         }
 334 
 335         protected Set<String> scan() {
 336             try (JarFile jf = openJarFile(path.toFile(), version)) {
 337                 return jf.versionedStream().map(JarEntry::getName)
 338                          .filter(n -> n.endsWith(".class"))
 339                          .collect(Collectors.toSet());
 340             } catch (IOException e) {
 341                 throw new UncheckedIOException(e);
 342             }
 343         }
 344 
 345         public ClassFile getClassFile(String name) throws IOException {
 346             if (name.indexOf('.') > 0) {
 347                 int i = name.lastIndexOf('.');
 348                 String entryName = name.replace('.', '/') + ".class";
 349                 JarEntry e = jarfile.getJarEntry(entryName);
 350                 if (e == null) {
 351                     e = jarfile.getJarEntry(entryName.substring(0, i) + "$"
 352                             + entryName.substring(i + 1, entryName.length()));
 353                 }
 354                 if (e != null) {
 355                     return readClassFile(jarfile, e);
 356                 }
 357             } else {


 364         }
 365 
 366         protected ClassFile readClassFile(JarFile jarfile, JarEntry e) throws IOException {
 367             try (InputStream is = jarfile.getInputStream(e)) {
 368                 ClassFile cf = ClassFile.read(is);
 369                 if (jarfile.isMultiRelease()) {
 370                     VersionHelper.add(jarfile, e, cf);
 371                 }
 372                 return cf;
 373             } catch (ConstantPoolException ex) {
 374                 throw new ClassFileError(ex);
 375             }
 376         }
 377 
 378         public Iterable<ClassFile> getClassFiles() throws IOException {
 379             final Iterator<ClassFile> iter = new JarFileIterator(this, jarfile);
 380             return () -> iter;
 381         }
 382     }
 383 















 384     class JarFileIterator implements Iterator<ClassFile> {
 385         protected final JarFileReader reader;
 386         protected Iterator<JarEntry> entries;
 387         protected JarFile jf;
 388         protected JarEntry nextEntry;
 389         protected ClassFile cf;
 390         JarFileIterator(JarFileReader reader) {
 391             this(reader, null);
 392         }
 393         JarFileIterator(JarFileReader reader, JarFile jarfile) {
 394             this.reader = reader;
 395             setJarFile(jarfile);
 396         }
 397 
 398         void setJarFile(JarFile jarfile) {
 399             if (jarfile == null) return;
 400 
 401             this.jf = jarfile;
 402             this.entries = jarfile.versionedStream().iterator();
 403             this.nextEntry = nextEntry();
 404         }
 405 
 406         public boolean hasNext() {
 407             if (nextEntry != null && cf != null) {
 408                 return true;
 409             }
 410             while (nextEntry != null) {
 411                 try {
 412                     cf = reader.readClassFile(jf, nextEntry);
 413                     return true;
 414                 } catch (ClassFileError | IOException ex) {
 415                     skippedEntries.add(String.format("%s: %s (%s)",
 416                                                      ex.getMessage(),
 417                                                      nextEntry.getName(),
 418                                                      jf.getName()));
 419                 }
 420                 nextEntry = nextEntry();
 421             }
 422             return false;
 423         }
 424 
 425         public ClassFile next() {
 426             if (!hasNext()) {
 427                 throw new NoSuchElementException();
 428             }
 429             ClassFile classFile = cf;
 430             cf = null;
 431             nextEntry = nextEntry();
 432             return classFile;
 433         }
 434 
 435         protected JarEntry nextEntry() {
 436             while (entries.hasNext()) {
 437                 JarEntry e = entries.next();
 438                 String name = e.getName();
 439                 if (name.endsWith(".class")) {
 440                     return e;
 441                 }
 442             }
 443             return null;
 444         }
 445 
 446         public void remove() {
 447             throw new UnsupportedOperationException("Not supported yet.");
 448         }
 449     }
 450     private static final String MODULE_INFO = "module-info.class";
 451 }
< prev index next >