1.1 --- a/src/share/classes/com/sun/tools/jdeps/ClassFileReader.java Mon Jul 07 18:03:08 2014 -0700 1.2 +++ b/src/share/classes/com/sun/tools/jdeps/ClassFileReader.java Thu Jul 17 15:23:08 2014 -0700 1.3 @@ -1,5 +1,5 @@ 1.4 /* 1.5 - * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. 1.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.8 * 1.9 * This code is free software; you can redistribute it and/or modify it 1.10 @@ -68,7 +68,8 @@ 1.11 1.12 protected final Path path; 1.13 protected final String baseFileName; 1.14 - private ClassFileReader(Path path) { 1.15 + protected final List<String> skippedEntries = new ArrayList<>(); 1.16 + protected ClassFileReader(Path path) { 1.17 this.path = path; 1.18 this.baseFileName = path.getFileName() != null 1.19 ? path.getFileName().toString() 1.20 @@ -79,6 +80,10 @@ 1.21 return baseFileName; 1.22 } 1.23 1.24 + public List<String> skippedEntries() { 1.25 + return skippedEntries; 1.26 + } 1.27 + 1.28 /** 1.29 * Returns the ClassFile matching the given binary name 1.30 * or a fully-qualified class name. 1.31 @@ -232,11 +237,12 @@ 1.32 } 1.33 } 1.34 1.35 - private static class JarFileReader extends ClassFileReader { 1.36 - final JarFile jarfile; 1.37 + static class JarFileReader extends ClassFileReader { 1.38 + private final JarFile jarfile; 1.39 JarFileReader(Path path) throws IOException { 1.40 - this(path, new JarFile(path.toFile())); 1.41 + this(path, new JarFile(path.toFile(), false)); 1.42 } 1.43 + 1.44 JarFileReader(Path path, JarFile jf) throws IOException { 1.45 super(path); 1.46 this.jarfile = jf; 1.47 @@ -252,18 +258,18 @@ 1.48 + entryName.substring(i + 1, entryName.length())); 1.49 } 1.50 if (e != null) { 1.51 - return readClassFile(e); 1.52 + return readClassFile(jarfile, e); 1.53 } 1.54 } else { 1.55 JarEntry e = jarfile.getJarEntry(name + ".class"); 1.56 if (e != null) { 1.57 - return readClassFile(e); 1.58 + return readClassFile(jarfile, e); 1.59 } 1.60 } 1.61 return null; 1.62 } 1.63 1.64 - private ClassFile readClassFile(JarEntry e) throws IOException { 1.65 + protected ClassFile readClassFile(JarFile jarfile, JarEntry e) throws IOException { 1.66 InputStream is = null; 1.67 try { 1.68 is = jarfile.getInputStream(e); 1.69 @@ -277,60 +283,76 @@ 1.70 } 1.71 1.72 public Iterable<ClassFile> getClassFiles() throws IOException { 1.73 - final Iterator<ClassFile> iter = new JarFileIterator(); 1.74 + final Iterator<ClassFile> iter = new JarFileIterator(this, jarfile); 1.75 return new Iterable<ClassFile>() { 1.76 public Iterator<ClassFile> iterator() { 1.77 return iter; 1.78 } 1.79 }; 1.80 } 1.81 + } 1.82 1.83 - class JarFileIterator implements Iterator<ClassFile> { 1.84 - private Enumeration<JarEntry> entries; 1.85 - private JarEntry nextEntry; 1.86 - JarFileIterator() { 1.87 - this.entries = jarfile.entries(); 1.88 - while (entries.hasMoreElements()) { 1.89 - JarEntry e = entries.nextElement(); 1.90 - String name = e.getName(); 1.91 - if (name.endsWith(".class")) { 1.92 - this.nextEntry = e; 1.93 - break; 1.94 - } 1.95 + class JarFileIterator implements Iterator<ClassFile> { 1.96 + protected final JarFileReader reader; 1.97 + protected Enumeration<JarEntry> entries; 1.98 + protected JarFile jf; 1.99 + protected JarEntry nextEntry; 1.100 + protected ClassFile cf; 1.101 + JarFileIterator(JarFileReader reader) { 1.102 + this(reader, null); 1.103 + } 1.104 + JarFileIterator(JarFileReader reader, JarFile jarfile) { 1.105 + this.reader = reader; 1.106 + setJarFile(jarfile); 1.107 + } 1.108 + 1.109 + void setJarFile(JarFile jarfile) { 1.110 + if (jarfile == null) return; 1.111 + 1.112 + this.jf = jarfile; 1.113 + this.entries = jf.entries(); 1.114 + this.nextEntry = nextEntry(); 1.115 + } 1.116 + 1.117 + public boolean hasNext() { 1.118 + if (nextEntry != null && cf != null) { 1.119 + return true; 1.120 + } 1.121 + while (nextEntry != null) { 1.122 + try { 1.123 + cf = reader.readClassFile(jf, nextEntry); 1.124 + return true; 1.125 + } catch (ClassFileError | IOException ex) { 1.126 + skippedEntries.add(nextEntry.getName()); 1.127 + } 1.128 + nextEntry = nextEntry(); 1.129 + } 1.130 + return false; 1.131 + } 1.132 + 1.133 + public ClassFile next() { 1.134 + if (!hasNext()) { 1.135 + throw new NoSuchElementException(); 1.136 + } 1.137 + ClassFile classFile = cf; 1.138 + cf = null; 1.139 + nextEntry = nextEntry(); 1.140 + return classFile; 1.141 + } 1.142 + 1.143 + protected JarEntry nextEntry() { 1.144 + while (entries.hasMoreElements()) { 1.145 + JarEntry e = entries.nextElement(); 1.146 + String name = e.getName(); 1.147 + if (name.endsWith(".class")) { 1.148 + return e; 1.149 } 1.150 } 1.151 + return null; 1.152 + } 1.153 1.154 - public boolean hasNext() { 1.155 - return nextEntry != null; 1.156 - } 1.157 - 1.158 - public ClassFile next() { 1.159 - if (!hasNext()) { 1.160 - throw new NoSuchElementException(); 1.161 - } 1.162 - 1.163 - ClassFile cf; 1.164 - try { 1.165 - cf = readClassFile(nextEntry); 1.166 - } catch (IOException ex) { 1.167 - throw new ClassFileError(ex); 1.168 - } 1.169 - JarEntry entry = nextEntry; 1.170 - nextEntry = null; 1.171 - while (entries.hasMoreElements()) { 1.172 - JarEntry e = entries.nextElement(); 1.173 - String name = e.getName(); 1.174 - if (name.endsWith(".class")) { 1.175 - nextEntry = e; 1.176 - break; 1.177 - } 1.178 - } 1.179 - return cf; 1.180 - } 1.181 - 1.182 - public void remove() { 1.183 - throw new UnsupportedOperationException("Not supported yet."); 1.184 - } 1.185 + public void remove() { 1.186 + throw new UnsupportedOperationException("Not supported yet."); 1.187 } 1.188 } 1.189 }