Thu, 27 Apr 2017 16:18:18 -0700
8176329: jdeps to detect MR jar file and output a warning
Reviewed-by: mchung
mchung@1472 | 1 | /* |
bchristi@3368 | 2 | * Copyright (c) 2012, 2017, 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.*; |
bchristi@3368 | 37 | import java.util.jar.Attributes; |
mchung@1472 | 38 | import java.util.jar.JarEntry; |
mchung@1472 | 39 | import java.util.jar.JarFile; |
bchristi@3368 | 40 | import java.util.jar.Manifest; |
mchung@1472 | 41 | |
mchung@1472 | 42 | /** |
mchung@1472 | 43 | * ClassFileReader reads ClassFile(s) of a given path that can be |
mchung@1472 | 44 | * a .class file, a directory, or a JAR file. |
mchung@1472 | 45 | */ |
mchung@1472 | 46 | public class ClassFileReader { |
mchung@1472 | 47 | /** |
mchung@1472 | 48 | * Returns a ClassFileReader instance of a given path. |
mchung@1472 | 49 | */ |
mchung@2139 | 50 | public static ClassFileReader newInstance(Path path) throws IOException { |
mchung@2139 | 51 | if (!Files.exists(path)) { |
mchung@2139 | 52 | throw new FileNotFoundException(path.toString()); |
mchung@1472 | 53 | } |
mchung@1472 | 54 | |
mchung@2139 | 55 | if (Files.isDirectory(path)) { |
mchung@2139 | 56 | return new DirectoryReader(path); |
mchung@2139 | 57 | } else if (path.getFileName().toString().endsWith(".jar")) { |
mchung@2139 | 58 | return new JarFileReader(path); |
mchung@1472 | 59 | } else { |
mchung@2139 | 60 | return new ClassFileReader(path); |
mchung@1472 | 61 | } |
mchung@1472 | 62 | } |
mchung@1472 | 63 | |
mchung@1638 | 64 | /** |
mchung@1638 | 65 | * Returns a ClassFileReader instance of a given JarFile. |
mchung@1638 | 66 | */ |
mchung@1638 | 67 | public static ClassFileReader newInstance(Path path, JarFile jf) throws IOException { |
mchung@1638 | 68 | return new JarFileReader(path, jf); |
mchung@1638 | 69 | } |
mchung@1638 | 70 | |
mchung@1472 | 71 | protected final Path path; |
mchung@1472 | 72 | protected final String baseFileName; |
mchung@2538 | 73 | protected final List<String> skippedEntries = new ArrayList<>(); |
mchung@2538 | 74 | protected ClassFileReader(Path path) { |
mchung@1472 | 75 | this.path = path; |
mchung@1472 | 76 | this.baseFileName = path.getFileName() != null |
mchung@1472 | 77 | ? path.getFileName().toString() |
mchung@1472 | 78 | : path.toString(); |
mchung@1472 | 79 | } |
mchung@1472 | 80 | |
mchung@1472 | 81 | public String getFileName() { |
mchung@1472 | 82 | return baseFileName; |
mchung@1472 | 83 | } |
mchung@1472 | 84 | |
mchung@2538 | 85 | public List<String> skippedEntries() { |
mchung@2538 | 86 | return skippedEntries; |
mchung@2538 | 87 | } |
mchung@2538 | 88 | |
mchung@1472 | 89 | /** |
mchung@1472 | 90 | * Returns the ClassFile matching the given binary name |
mchung@1472 | 91 | * or a fully-qualified class name. |
mchung@1472 | 92 | */ |
mchung@1472 | 93 | public ClassFile getClassFile(String name) throws IOException { |
mchung@1472 | 94 | if (name.indexOf('.') > 0) { |
mchung@1472 | 95 | int i = name.lastIndexOf('.'); |
mchung@1472 | 96 | String pathname = name.replace('.', File.separatorChar) + ".class"; |
mchung@1472 | 97 | if (baseFileName.equals(pathname) || |
mchung@1472 | 98 | baseFileName.equals(pathname.substring(0, i) + "$" + |
mchung@1472 | 99 | pathname.substring(i+1, pathname.length()))) { |
mchung@1472 | 100 | return readClassFile(path); |
mchung@1472 | 101 | } |
mchung@1472 | 102 | } else { |
mchung@1472 | 103 | if (baseFileName.equals(name.replace('/', File.separatorChar) + ".class")) { |
mchung@1472 | 104 | return readClassFile(path); |
mchung@1472 | 105 | } |
mchung@1472 | 106 | } |
mchung@1472 | 107 | return null; |
mchung@1472 | 108 | } |
mchung@1472 | 109 | |
mchung@1472 | 110 | public Iterable<ClassFile> getClassFiles() throws IOException { |
mchung@1472 | 111 | return new Iterable<ClassFile>() { |
mchung@1472 | 112 | public Iterator<ClassFile> iterator() { |
mchung@1472 | 113 | return new FileIterator(); |
mchung@1472 | 114 | } |
mchung@1472 | 115 | }; |
mchung@1472 | 116 | } |
mchung@1472 | 117 | |
mchung@1472 | 118 | protected ClassFile readClassFile(Path p) throws IOException { |
mchung@1472 | 119 | InputStream is = null; |
mchung@1472 | 120 | try { |
mchung@1472 | 121 | is = Files.newInputStream(p); |
mchung@1472 | 122 | return ClassFile.read(is); |
mchung@1472 | 123 | } catch (ConstantPoolException e) { |
mchung@1472 | 124 | throw new ClassFileError(e); |
mchung@1472 | 125 | } finally { |
mchung@1472 | 126 | if (is != null) { |
mchung@1472 | 127 | is.close(); |
mchung@1472 | 128 | } |
mchung@1472 | 129 | } |
mchung@1472 | 130 | } |
mchung@1472 | 131 | |
mchung@1472 | 132 | class FileIterator implements Iterator<ClassFile> { |
mchung@1472 | 133 | int count; |
mchung@1472 | 134 | FileIterator() { |
mchung@1472 | 135 | this.count = 0; |
mchung@1472 | 136 | } |
mchung@1472 | 137 | public boolean hasNext() { |
mchung@1472 | 138 | return count == 0 && baseFileName.endsWith(".class"); |
mchung@1472 | 139 | } |
mchung@1472 | 140 | |
mchung@1472 | 141 | public ClassFile next() { |
mchung@1472 | 142 | if (!hasNext()) { |
mchung@1472 | 143 | throw new NoSuchElementException(); |
mchung@1472 | 144 | } |
mchung@1472 | 145 | try { |
mchung@1472 | 146 | ClassFile cf = readClassFile(path); |
mchung@1472 | 147 | count++; |
mchung@1472 | 148 | return cf; |
mchung@1472 | 149 | } catch (IOException e) { |
mchung@1472 | 150 | throw new ClassFileError(e); |
mchung@1472 | 151 | } |
mchung@1472 | 152 | } |
mchung@1472 | 153 | |
mchung@1472 | 154 | public void remove() { |
mchung@1472 | 155 | throw new UnsupportedOperationException("Not supported yet."); |
mchung@1472 | 156 | } |
mchung@1472 | 157 | } |
mchung@1472 | 158 | |
bchristi@3368 | 159 | public boolean isMultiReleaseJar() throws IOException { return false; } |
bchristi@3368 | 160 | |
mchung@1472 | 161 | public String toString() { |
mchung@1472 | 162 | return path.toString(); |
mchung@1472 | 163 | } |
mchung@1472 | 164 | |
mchung@1472 | 165 | private static class DirectoryReader extends ClassFileReader { |
mchung@1472 | 166 | DirectoryReader(Path path) throws IOException { |
mchung@1472 | 167 | super(path); |
mchung@1472 | 168 | } |
mchung@1472 | 169 | |
mchung@1472 | 170 | public ClassFile getClassFile(String name) throws IOException { |
mchung@1472 | 171 | if (name.indexOf('.') > 0) { |
mchung@1472 | 172 | int i = name.lastIndexOf('.'); |
mchung@1472 | 173 | String pathname = name.replace('.', File.separatorChar) + ".class"; |
mchung@1472 | 174 | Path p = path.resolve(pathname); |
mchung@2139 | 175 | if (!Files.exists(p)) { |
mchung@1472 | 176 | p = path.resolve(pathname.substring(0, i) + "$" + |
mchung@1472 | 177 | pathname.substring(i+1, pathname.length())); |
mchung@1472 | 178 | } |
mchung@2139 | 179 | if (Files.exists(p)) { |
mchung@1472 | 180 | return readClassFile(p); |
mchung@1472 | 181 | } |
mchung@1472 | 182 | } else { |
mchung@1472 | 183 | Path p = path.resolve(name + ".class"); |
mchung@2139 | 184 | if (Files.exists(p)) { |
mchung@1472 | 185 | return readClassFile(p); |
mchung@1472 | 186 | } |
mchung@1472 | 187 | } |
mchung@1472 | 188 | return null; |
mchung@1472 | 189 | } |
mchung@1472 | 190 | |
mchung@1472 | 191 | public Iterable<ClassFile> getClassFiles() throws IOException { |
mchung@1472 | 192 | final Iterator<ClassFile> iter = new DirectoryIterator(); |
mchung@1472 | 193 | return new Iterable<ClassFile>() { |
mchung@1472 | 194 | public Iterator<ClassFile> iterator() { |
mchung@1472 | 195 | return iter; |
mchung@1472 | 196 | } |
mchung@1472 | 197 | }; |
mchung@1472 | 198 | } |
mchung@1472 | 199 | |
mchung@1472 | 200 | private List<Path> walkTree(Path dir) throws IOException { |
mchung@1472 | 201 | final List<Path> files = new ArrayList<Path>(); |
mchung@1472 | 202 | Files.walkFileTree(dir, new SimpleFileVisitor<Path>() { |
mchung@1472 | 203 | public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) |
mchung@1472 | 204 | throws IOException { |
mchung@2139 | 205 | if (file.getFileName().toString().endsWith(".class")) { |
mchung@1472 | 206 | files.add(file); |
mchung@1472 | 207 | } |
mchung@1472 | 208 | return FileVisitResult.CONTINUE; |
mchung@1472 | 209 | } |
mchung@1472 | 210 | }); |
mchung@1472 | 211 | return files; |
mchung@1472 | 212 | } |
mchung@1472 | 213 | |
mchung@1472 | 214 | class DirectoryIterator implements Iterator<ClassFile> { |
mchung@1472 | 215 | private List<Path> entries; |
mchung@1472 | 216 | private int index = 0; |
mchung@1472 | 217 | DirectoryIterator() throws IOException { |
mchung@1472 | 218 | entries = walkTree(path); |
mchung@1472 | 219 | index = 0; |
mchung@1472 | 220 | } |
mchung@1472 | 221 | |
mchung@1472 | 222 | public boolean hasNext() { |
mchung@1472 | 223 | return index != entries.size(); |
mchung@1472 | 224 | } |
mchung@1472 | 225 | |
mchung@1472 | 226 | public ClassFile next() { |
mchung@1472 | 227 | if (!hasNext()) { |
mchung@1472 | 228 | throw new NoSuchElementException(); |
mchung@1472 | 229 | } |
mchung@1472 | 230 | Path path = entries.get(index++); |
mchung@1472 | 231 | try { |
mchung@1472 | 232 | return readClassFile(path); |
mchung@1472 | 233 | } catch (IOException e) { |
mchung@1472 | 234 | throw new ClassFileError(e); |
mchung@1472 | 235 | } |
mchung@1472 | 236 | } |
mchung@1472 | 237 | |
mchung@1472 | 238 | public void remove() { |
mchung@1472 | 239 | throw new UnsupportedOperationException("Not supported yet."); |
mchung@1472 | 240 | } |
mchung@1472 | 241 | } |
mchung@1472 | 242 | } |
mchung@1472 | 243 | |
mchung@2538 | 244 | static class JarFileReader extends ClassFileReader { |
mchung@2538 | 245 | private final JarFile jarfile; |
mchung@1472 | 246 | JarFileReader(Path path) throws IOException { |
mchung@2538 | 247 | this(path, new JarFile(path.toFile(), false)); |
mchung@1638 | 248 | } |
mchung@2538 | 249 | |
mchung@1638 | 250 | JarFileReader(Path path, JarFile jf) throws IOException { |
mchung@1472 | 251 | super(path); |
mchung@1638 | 252 | this.jarfile = jf; |
mchung@1472 | 253 | } |
mchung@1472 | 254 | |
mchung@1472 | 255 | public ClassFile getClassFile(String name) throws IOException { |
mchung@1472 | 256 | if (name.indexOf('.') > 0) { |
mchung@1472 | 257 | int i = name.lastIndexOf('.'); |
mchung@1472 | 258 | String entryName = name.replace('.', '/') + ".class"; |
mchung@1472 | 259 | JarEntry e = jarfile.getJarEntry(entryName); |
mchung@1472 | 260 | if (e == null) { |
mchung@1472 | 261 | e = jarfile.getJarEntry(entryName.substring(0, i) + "$" |
mchung@1472 | 262 | + entryName.substring(i + 1, entryName.length())); |
mchung@1472 | 263 | } |
mchung@1472 | 264 | if (e != null) { |
mchung@2538 | 265 | return readClassFile(jarfile, e); |
mchung@1472 | 266 | } |
mchung@1472 | 267 | } else { |
mchung@1472 | 268 | JarEntry e = jarfile.getJarEntry(name + ".class"); |
mchung@1472 | 269 | if (e != null) { |
mchung@2538 | 270 | return readClassFile(jarfile, e); |
mchung@1472 | 271 | } |
mchung@1472 | 272 | } |
mchung@1472 | 273 | return null; |
mchung@1472 | 274 | } |
mchung@1472 | 275 | |
mchung@2538 | 276 | protected ClassFile readClassFile(JarFile jarfile, JarEntry e) throws IOException { |
mchung@1472 | 277 | InputStream is = null; |
mchung@1472 | 278 | try { |
mchung@1472 | 279 | is = jarfile.getInputStream(e); |
mchung@1472 | 280 | return ClassFile.read(is); |
mchung@1472 | 281 | } catch (ConstantPoolException ex) { |
mchung@1472 | 282 | throw new ClassFileError(ex); |
mchung@1472 | 283 | } finally { |
mchung@1472 | 284 | if (is != null) |
mchung@1472 | 285 | is.close(); |
mchung@1472 | 286 | } |
mchung@1472 | 287 | } |
mchung@1472 | 288 | |
mchung@1472 | 289 | public Iterable<ClassFile> getClassFiles() throws IOException { |
mchung@2538 | 290 | final Iterator<ClassFile> iter = new JarFileIterator(this, jarfile); |
mchung@1472 | 291 | return new Iterable<ClassFile>() { |
mchung@1472 | 292 | public Iterator<ClassFile> iterator() { |
mchung@1472 | 293 | return iter; |
mchung@1472 | 294 | } |
mchung@1472 | 295 | }; |
mchung@1472 | 296 | } |
bchristi@3368 | 297 | |
bchristi@3368 | 298 | @Override |
bchristi@3368 | 299 | public boolean isMultiReleaseJar() throws IOException { |
bchristi@3368 | 300 | Manifest mf = this.jarfile.getManifest(); |
bchristi@3368 | 301 | if (mf != null) { |
bchristi@3368 | 302 | Attributes atts = mf.getMainAttributes(); |
bchristi@3368 | 303 | return "true".equalsIgnoreCase(atts.getValue("Multi-Release")); |
bchristi@3368 | 304 | } |
bchristi@3368 | 305 | return false; |
bchristi@3368 | 306 | } |
mchung@2538 | 307 | } |
mchung@1472 | 308 | |
mchung@2538 | 309 | class JarFileIterator implements Iterator<ClassFile> { |
mchung@2538 | 310 | protected final JarFileReader reader; |
mchung@2538 | 311 | protected Enumeration<JarEntry> entries; |
mchung@2538 | 312 | protected JarFile jf; |
mchung@2538 | 313 | protected JarEntry nextEntry; |
mchung@2538 | 314 | protected ClassFile cf; |
mchung@2538 | 315 | JarFileIterator(JarFileReader reader) { |
mchung@2538 | 316 | this(reader, null); |
mchung@2538 | 317 | } |
mchung@2538 | 318 | JarFileIterator(JarFileReader reader, JarFile jarfile) { |
mchung@2538 | 319 | this.reader = reader; |
mchung@2538 | 320 | setJarFile(jarfile); |
mchung@2538 | 321 | } |
mchung@2538 | 322 | |
mchung@2538 | 323 | void setJarFile(JarFile jarfile) { |
mchung@2538 | 324 | if (jarfile == null) return; |
mchung@2538 | 325 | |
mchung@2538 | 326 | this.jf = jarfile; |
mchung@2538 | 327 | this.entries = jf.entries(); |
mchung@2538 | 328 | this.nextEntry = nextEntry(); |
mchung@2538 | 329 | } |
mchung@2538 | 330 | |
mchung@2538 | 331 | public boolean hasNext() { |
mchung@2538 | 332 | if (nextEntry != null && cf != null) { |
mchung@2538 | 333 | return true; |
mchung@2538 | 334 | } |
mchung@2538 | 335 | while (nextEntry != null) { |
mchung@2538 | 336 | try { |
mchung@2538 | 337 | cf = reader.readClassFile(jf, nextEntry); |
mchung@2538 | 338 | return true; |
mchung@2538 | 339 | } catch (ClassFileError | IOException ex) { |
mchung@2538 | 340 | skippedEntries.add(nextEntry.getName()); |
mchung@2538 | 341 | } |
mchung@2538 | 342 | nextEntry = nextEntry(); |
mchung@2538 | 343 | } |
mchung@2538 | 344 | return false; |
mchung@2538 | 345 | } |
mchung@2538 | 346 | |
mchung@2538 | 347 | public ClassFile next() { |
mchung@2538 | 348 | if (!hasNext()) { |
mchung@2538 | 349 | throw new NoSuchElementException(); |
mchung@2538 | 350 | } |
mchung@2538 | 351 | ClassFile classFile = cf; |
mchung@2538 | 352 | cf = null; |
mchung@2538 | 353 | nextEntry = nextEntry(); |
mchung@2538 | 354 | return classFile; |
mchung@2538 | 355 | } |
mchung@2538 | 356 | |
mchung@2538 | 357 | protected JarEntry nextEntry() { |
mchung@2538 | 358 | while (entries.hasMoreElements()) { |
mchung@2538 | 359 | JarEntry e = entries.nextElement(); |
mchung@2538 | 360 | String name = e.getName(); |
mchung@2538 | 361 | if (name.endsWith(".class")) { |
mchung@2538 | 362 | return e; |
mchung@1472 | 363 | } |
mchung@1472 | 364 | } |
mchung@2538 | 365 | return null; |
mchung@2538 | 366 | } |
mchung@1472 | 367 | |
mchung@2538 | 368 | public void remove() { |
mchung@2538 | 369 | throw new UnsupportedOperationException("Not supported yet."); |
mchung@1472 | 370 | } |
mchung@1472 | 371 | } |
mchung@1472 | 372 | } |