Mon, 16 Oct 2017 16:07:48 +0800
merge
aoqi@0 | 1 | /* |
mchung@2538 | 2 | * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | package com.sun.tools.jdeps; |
aoqi@0 | 26 | |
aoqi@0 | 27 | import com.sun.tools.classfile.ClassFile; |
aoqi@0 | 28 | import com.sun.tools.classfile.ConstantPoolException; |
aoqi@0 | 29 | import com.sun.tools.classfile.Dependencies.ClassFileError; |
aoqi@0 | 30 | import java.io.*; |
aoqi@0 | 31 | import java.nio.file.FileVisitResult; |
aoqi@0 | 32 | import java.nio.file.Files; |
aoqi@0 | 33 | import java.nio.file.Path; |
aoqi@0 | 34 | import java.nio.file.SimpleFileVisitor; |
aoqi@0 | 35 | import java.nio.file.attribute.BasicFileAttributes; |
aoqi@0 | 36 | import java.util.*; |
aoqi@0 | 37 | import java.util.jar.JarEntry; |
aoqi@0 | 38 | import java.util.jar.JarFile; |
aoqi@0 | 39 | |
aoqi@0 | 40 | /** |
aoqi@0 | 41 | * ClassFileReader reads ClassFile(s) of a given path that can be |
aoqi@0 | 42 | * a .class file, a directory, or a JAR file. |
aoqi@0 | 43 | */ |
aoqi@0 | 44 | public class ClassFileReader { |
aoqi@0 | 45 | /** |
aoqi@0 | 46 | * Returns a ClassFileReader instance of a given path. |
aoqi@0 | 47 | */ |
aoqi@0 | 48 | public static ClassFileReader newInstance(Path path) throws IOException { |
aoqi@0 | 49 | if (!Files.exists(path)) { |
aoqi@0 | 50 | throw new FileNotFoundException(path.toString()); |
aoqi@0 | 51 | } |
aoqi@0 | 52 | |
aoqi@0 | 53 | if (Files.isDirectory(path)) { |
aoqi@0 | 54 | return new DirectoryReader(path); |
aoqi@0 | 55 | } else if (path.getFileName().toString().endsWith(".jar")) { |
aoqi@0 | 56 | return new JarFileReader(path); |
aoqi@0 | 57 | } else { |
aoqi@0 | 58 | return new ClassFileReader(path); |
aoqi@0 | 59 | } |
aoqi@0 | 60 | } |
aoqi@0 | 61 | |
aoqi@0 | 62 | /** |
aoqi@0 | 63 | * Returns a ClassFileReader instance of a given JarFile. |
aoqi@0 | 64 | */ |
aoqi@0 | 65 | public static ClassFileReader newInstance(Path path, JarFile jf) throws IOException { |
aoqi@0 | 66 | return new JarFileReader(path, jf); |
aoqi@0 | 67 | } |
aoqi@0 | 68 | |
aoqi@0 | 69 | protected final Path path; |
aoqi@0 | 70 | protected final String baseFileName; |
mchung@2538 | 71 | protected final List<String> skippedEntries = new ArrayList<>(); |
mchung@2538 | 72 | protected ClassFileReader(Path path) { |
aoqi@0 | 73 | this.path = path; |
aoqi@0 | 74 | this.baseFileName = path.getFileName() != null |
aoqi@0 | 75 | ? path.getFileName().toString() |
aoqi@0 | 76 | : path.toString(); |
aoqi@0 | 77 | } |
aoqi@0 | 78 | |
aoqi@0 | 79 | public String getFileName() { |
aoqi@0 | 80 | return baseFileName; |
aoqi@0 | 81 | } |
aoqi@0 | 82 | |
mchung@2538 | 83 | public List<String> skippedEntries() { |
mchung@2538 | 84 | return skippedEntries; |
mchung@2538 | 85 | } |
mchung@2538 | 86 | |
aoqi@0 | 87 | /** |
aoqi@0 | 88 | * Returns the ClassFile matching the given binary name |
aoqi@0 | 89 | * or a fully-qualified class name. |
aoqi@0 | 90 | */ |
aoqi@0 | 91 | public ClassFile getClassFile(String name) throws IOException { |
aoqi@0 | 92 | if (name.indexOf('.') > 0) { |
aoqi@0 | 93 | int i = name.lastIndexOf('.'); |
aoqi@0 | 94 | String pathname = name.replace('.', File.separatorChar) + ".class"; |
aoqi@0 | 95 | if (baseFileName.equals(pathname) || |
aoqi@0 | 96 | baseFileName.equals(pathname.substring(0, i) + "$" + |
aoqi@0 | 97 | pathname.substring(i+1, pathname.length()))) { |
aoqi@0 | 98 | return readClassFile(path); |
aoqi@0 | 99 | } |
aoqi@0 | 100 | } else { |
aoqi@0 | 101 | if (baseFileName.equals(name.replace('/', File.separatorChar) + ".class")) { |
aoqi@0 | 102 | return readClassFile(path); |
aoqi@0 | 103 | } |
aoqi@0 | 104 | } |
aoqi@0 | 105 | return null; |
aoqi@0 | 106 | } |
aoqi@0 | 107 | |
aoqi@0 | 108 | public Iterable<ClassFile> getClassFiles() throws IOException { |
aoqi@0 | 109 | return new Iterable<ClassFile>() { |
aoqi@0 | 110 | public Iterator<ClassFile> iterator() { |
aoqi@0 | 111 | return new FileIterator(); |
aoqi@0 | 112 | } |
aoqi@0 | 113 | }; |
aoqi@0 | 114 | } |
aoqi@0 | 115 | |
aoqi@0 | 116 | protected ClassFile readClassFile(Path p) throws IOException { |
aoqi@0 | 117 | InputStream is = null; |
aoqi@0 | 118 | try { |
aoqi@0 | 119 | is = Files.newInputStream(p); |
aoqi@0 | 120 | return ClassFile.read(is); |
aoqi@0 | 121 | } catch (ConstantPoolException e) { |
aoqi@0 | 122 | throw new ClassFileError(e); |
aoqi@0 | 123 | } finally { |
aoqi@0 | 124 | if (is != null) { |
aoqi@0 | 125 | is.close(); |
aoqi@0 | 126 | } |
aoqi@0 | 127 | } |
aoqi@0 | 128 | } |
aoqi@0 | 129 | |
aoqi@0 | 130 | class FileIterator implements Iterator<ClassFile> { |
aoqi@0 | 131 | int count; |
aoqi@0 | 132 | FileIterator() { |
aoqi@0 | 133 | this.count = 0; |
aoqi@0 | 134 | } |
aoqi@0 | 135 | public boolean hasNext() { |
aoqi@0 | 136 | return count == 0 && baseFileName.endsWith(".class"); |
aoqi@0 | 137 | } |
aoqi@0 | 138 | |
aoqi@0 | 139 | public ClassFile next() { |
aoqi@0 | 140 | if (!hasNext()) { |
aoqi@0 | 141 | throw new NoSuchElementException(); |
aoqi@0 | 142 | } |
aoqi@0 | 143 | try { |
aoqi@0 | 144 | ClassFile cf = readClassFile(path); |
aoqi@0 | 145 | count++; |
aoqi@0 | 146 | return cf; |
aoqi@0 | 147 | } catch (IOException e) { |
aoqi@0 | 148 | throw new ClassFileError(e); |
aoqi@0 | 149 | } |
aoqi@0 | 150 | } |
aoqi@0 | 151 | |
aoqi@0 | 152 | public void remove() { |
aoqi@0 | 153 | throw new UnsupportedOperationException("Not supported yet."); |
aoqi@0 | 154 | } |
aoqi@0 | 155 | } |
aoqi@0 | 156 | |
aoqi@0 | 157 | public String toString() { |
aoqi@0 | 158 | return path.toString(); |
aoqi@0 | 159 | } |
aoqi@0 | 160 | |
aoqi@0 | 161 | private static class DirectoryReader extends ClassFileReader { |
aoqi@0 | 162 | DirectoryReader(Path path) throws IOException { |
aoqi@0 | 163 | super(path); |
aoqi@0 | 164 | } |
aoqi@0 | 165 | |
aoqi@0 | 166 | public ClassFile getClassFile(String name) throws IOException { |
aoqi@0 | 167 | if (name.indexOf('.') > 0) { |
aoqi@0 | 168 | int i = name.lastIndexOf('.'); |
aoqi@0 | 169 | String pathname = name.replace('.', File.separatorChar) + ".class"; |
aoqi@0 | 170 | Path p = path.resolve(pathname); |
aoqi@0 | 171 | if (!Files.exists(p)) { |
aoqi@0 | 172 | p = path.resolve(pathname.substring(0, i) + "$" + |
aoqi@0 | 173 | pathname.substring(i+1, pathname.length())); |
aoqi@0 | 174 | } |
aoqi@0 | 175 | if (Files.exists(p)) { |
aoqi@0 | 176 | return readClassFile(p); |
aoqi@0 | 177 | } |
aoqi@0 | 178 | } else { |
aoqi@0 | 179 | Path p = path.resolve(name + ".class"); |
aoqi@0 | 180 | if (Files.exists(p)) { |
aoqi@0 | 181 | return readClassFile(p); |
aoqi@0 | 182 | } |
aoqi@0 | 183 | } |
aoqi@0 | 184 | return null; |
aoqi@0 | 185 | } |
aoqi@0 | 186 | |
aoqi@0 | 187 | public Iterable<ClassFile> getClassFiles() throws IOException { |
aoqi@0 | 188 | final Iterator<ClassFile> iter = new DirectoryIterator(); |
aoqi@0 | 189 | return new Iterable<ClassFile>() { |
aoqi@0 | 190 | public Iterator<ClassFile> iterator() { |
aoqi@0 | 191 | return iter; |
aoqi@0 | 192 | } |
aoqi@0 | 193 | }; |
aoqi@0 | 194 | } |
aoqi@0 | 195 | |
aoqi@0 | 196 | private List<Path> walkTree(Path dir) throws IOException { |
aoqi@0 | 197 | final List<Path> files = new ArrayList<Path>(); |
aoqi@0 | 198 | Files.walkFileTree(dir, new SimpleFileVisitor<Path>() { |
aoqi@0 | 199 | public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) |
aoqi@0 | 200 | throws IOException { |
aoqi@0 | 201 | if (file.getFileName().toString().endsWith(".class")) { |
aoqi@0 | 202 | files.add(file); |
aoqi@0 | 203 | } |
aoqi@0 | 204 | return FileVisitResult.CONTINUE; |
aoqi@0 | 205 | } |
aoqi@0 | 206 | }); |
aoqi@0 | 207 | return files; |
aoqi@0 | 208 | } |
aoqi@0 | 209 | |
aoqi@0 | 210 | class DirectoryIterator implements Iterator<ClassFile> { |
aoqi@0 | 211 | private List<Path> entries; |
aoqi@0 | 212 | private int index = 0; |
aoqi@0 | 213 | DirectoryIterator() throws IOException { |
aoqi@0 | 214 | entries = walkTree(path); |
aoqi@0 | 215 | index = 0; |
aoqi@0 | 216 | } |
aoqi@0 | 217 | |
aoqi@0 | 218 | public boolean hasNext() { |
aoqi@0 | 219 | return index != entries.size(); |
aoqi@0 | 220 | } |
aoqi@0 | 221 | |
aoqi@0 | 222 | public ClassFile next() { |
aoqi@0 | 223 | if (!hasNext()) { |
aoqi@0 | 224 | throw new NoSuchElementException(); |
aoqi@0 | 225 | } |
aoqi@0 | 226 | Path path = entries.get(index++); |
aoqi@0 | 227 | try { |
aoqi@0 | 228 | return readClassFile(path); |
aoqi@0 | 229 | } catch (IOException e) { |
aoqi@0 | 230 | throw new ClassFileError(e); |
aoqi@0 | 231 | } |
aoqi@0 | 232 | } |
aoqi@0 | 233 | |
aoqi@0 | 234 | public void remove() { |
aoqi@0 | 235 | throw new UnsupportedOperationException("Not supported yet."); |
aoqi@0 | 236 | } |
aoqi@0 | 237 | } |
aoqi@0 | 238 | } |
aoqi@0 | 239 | |
mchung@2538 | 240 | static class JarFileReader extends ClassFileReader { |
mchung@2538 | 241 | private final JarFile jarfile; |
aoqi@0 | 242 | JarFileReader(Path path) throws IOException { |
mchung@2538 | 243 | this(path, new JarFile(path.toFile(), false)); |
aoqi@0 | 244 | } |
mchung@2538 | 245 | |
aoqi@0 | 246 | JarFileReader(Path path, JarFile jf) throws IOException { |
aoqi@0 | 247 | super(path); |
aoqi@0 | 248 | this.jarfile = jf; |
aoqi@0 | 249 | } |
aoqi@0 | 250 | |
aoqi@0 | 251 | public ClassFile getClassFile(String name) throws IOException { |
aoqi@0 | 252 | if (name.indexOf('.') > 0) { |
aoqi@0 | 253 | int i = name.lastIndexOf('.'); |
aoqi@0 | 254 | String entryName = name.replace('.', '/') + ".class"; |
aoqi@0 | 255 | JarEntry e = jarfile.getJarEntry(entryName); |
aoqi@0 | 256 | if (e == null) { |
aoqi@0 | 257 | e = jarfile.getJarEntry(entryName.substring(0, i) + "$" |
aoqi@0 | 258 | + entryName.substring(i + 1, entryName.length())); |
aoqi@0 | 259 | } |
aoqi@0 | 260 | if (e != null) { |
mchung@2538 | 261 | return readClassFile(jarfile, e); |
aoqi@0 | 262 | } |
aoqi@0 | 263 | } else { |
aoqi@0 | 264 | JarEntry e = jarfile.getJarEntry(name + ".class"); |
aoqi@0 | 265 | if (e != null) { |
mchung@2538 | 266 | return readClassFile(jarfile, e); |
aoqi@0 | 267 | } |
aoqi@0 | 268 | } |
aoqi@0 | 269 | return null; |
aoqi@0 | 270 | } |
aoqi@0 | 271 | |
mchung@2538 | 272 | protected ClassFile readClassFile(JarFile jarfile, JarEntry e) throws IOException { |
aoqi@0 | 273 | InputStream is = null; |
aoqi@0 | 274 | try { |
aoqi@0 | 275 | is = jarfile.getInputStream(e); |
aoqi@0 | 276 | return ClassFile.read(is); |
aoqi@0 | 277 | } catch (ConstantPoolException ex) { |
aoqi@0 | 278 | throw new ClassFileError(ex); |
aoqi@0 | 279 | } finally { |
aoqi@0 | 280 | if (is != null) |
aoqi@0 | 281 | is.close(); |
aoqi@0 | 282 | } |
aoqi@0 | 283 | } |
aoqi@0 | 284 | |
aoqi@0 | 285 | public Iterable<ClassFile> getClassFiles() throws IOException { |
mchung@2538 | 286 | final Iterator<ClassFile> iter = new JarFileIterator(this, jarfile); |
aoqi@0 | 287 | return new Iterable<ClassFile>() { |
aoqi@0 | 288 | public Iterator<ClassFile> iterator() { |
aoqi@0 | 289 | return iter; |
aoqi@0 | 290 | } |
aoqi@0 | 291 | }; |
aoqi@0 | 292 | } |
mchung@2538 | 293 | } |
aoqi@0 | 294 | |
mchung@2538 | 295 | class JarFileIterator implements Iterator<ClassFile> { |
mchung@2538 | 296 | protected final JarFileReader reader; |
mchung@2538 | 297 | protected Enumeration<JarEntry> entries; |
mchung@2538 | 298 | protected JarFile jf; |
mchung@2538 | 299 | protected JarEntry nextEntry; |
mchung@2538 | 300 | protected ClassFile cf; |
mchung@2538 | 301 | JarFileIterator(JarFileReader reader) { |
mchung@2538 | 302 | this(reader, null); |
mchung@2538 | 303 | } |
mchung@2538 | 304 | JarFileIterator(JarFileReader reader, JarFile jarfile) { |
mchung@2538 | 305 | this.reader = reader; |
mchung@2538 | 306 | setJarFile(jarfile); |
mchung@2538 | 307 | } |
mchung@2538 | 308 | |
mchung@2538 | 309 | void setJarFile(JarFile jarfile) { |
mchung@2538 | 310 | if (jarfile == null) return; |
mchung@2538 | 311 | |
mchung@2538 | 312 | this.jf = jarfile; |
mchung@2538 | 313 | this.entries = jf.entries(); |
mchung@2538 | 314 | this.nextEntry = nextEntry(); |
mchung@2538 | 315 | } |
mchung@2538 | 316 | |
mchung@2538 | 317 | public boolean hasNext() { |
mchung@2538 | 318 | if (nextEntry != null && cf != null) { |
mchung@2538 | 319 | return true; |
mchung@2538 | 320 | } |
mchung@2538 | 321 | while (nextEntry != null) { |
mchung@2538 | 322 | try { |
mchung@2538 | 323 | cf = reader.readClassFile(jf, nextEntry); |
mchung@2538 | 324 | return true; |
mchung@2538 | 325 | } catch (ClassFileError | IOException ex) { |
mchung@2538 | 326 | skippedEntries.add(nextEntry.getName()); |
mchung@2538 | 327 | } |
mchung@2538 | 328 | nextEntry = nextEntry(); |
mchung@2538 | 329 | } |
mchung@2538 | 330 | return false; |
mchung@2538 | 331 | } |
mchung@2538 | 332 | |
mchung@2538 | 333 | public ClassFile next() { |
mchung@2538 | 334 | if (!hasNext()) { |
mchung@2538 | 335 | throw new NoSuchElementException(); |
mchung@2538 | 336 | } |
mchung@2538 | 337 | ClassFile classFile = cf; |
mchung@2538 | 338 | cf = null; |
mchung@2538 | 339 | nextEntry = nextEntry(); |
mchung@2538 | 340 | return classFile; |
mchung@2538 | 341 | } |
mchung@2538 | 342 | |
mchung@2538 | 343 | protected JarEntry nextEntry() { |
mchung@2538 | 344 | while (entries.hasMoreElements()) { |
mchung@2538 | 345 | JarEntry e = entries.nextElement(); |
mchung@2538 | 346 | String name = e.getName(); |
mchung@2538 | 347 | if (name.endsWith(".class")) { |
mchung@2538 | 348 | return e; |
aoqi@0 | 349 | } |
aoqi@0 | 350 | } |
mchung@2538 | 351 | return null; |
mchung@2538 | 352 | } |
aoqi@0 | 353 | |
mchung@2538 | 354 | public void remove() { |
mchung@2538 | 355 | throw new UnsupportedOperationException("Not supported yet."); |
aoqi@0 | 356 | } |
aoqi@0 | 357 | } |
aoqi@0 | 358 | } |