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

Fri, 28 Dec 2012 22:25:21 -0800

author
mchung
date
Fri, 28 Dec 2012 22:25:21 -0800
changeset 1472
0c244701188e
child 1638
fd3fdaff0257
permissions
-rw-r--r--

8003562: Provide a CLI tool to analyze class dependencies
Reviewed-by: jjg, alanb, ulfzibis, erikj

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

mercurial