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