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

Thu, 24 May 2018 18:02:46 +0800

author
aoqi
date
Thu, 24 May 2018 18:02:46 +0800
changeset 3446
e468915bad3a
parent 3368
f206126308bc
parent 2702
9ca8d8713094
permissions
-rw-r--r--

Merge

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

mercurial