1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/classes/com/sun/tools/jdeps/ClassFileReader.java Fri Dec 28 22:25:21 2012 -0800 1.3 @@ -0,0 +1,326 @@ 1.4 +/* 1.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 +package com.sun.tools.jdeps; 1.29 + 1.30 +import com.sun.tools.classfile.ClassFile; 1.31 +import com.sun.tools.classfile.ConstantPoolException; 1.32 +import com.sun.tools.classfile.Dependencies.ClassFileError; 1.33 +import java.io.*; 1.34 +import java.nio.file.FileVisitResult; 1.35 +import java.nio.file.Files; 1.36 +import java.nio.file.Path; 1.37 +import java.nio.file.SimpleFileVisitor; 1.38 +import java.nio.file.attribute.BasicFileAttributes; 1.39 +import java.util.*; 1.40 +import java.util.jar.JarEntry; 1.41 +import java.util.jar.JarFile; 1.42 + 1.43 +/** 1.44 + * ClassFileReader reads ClassFile(s) of a given path that can be 1.45 + * a .class file, a directory, or a JAR file. 1.46 + */ 1.47 +public class ClassFileReader { 1.48 + /** 1.49 + * Returns a ClassFileReader instance of a given path. 1.50 + */ 1.51 + public static ClassFileReader newInstance(File path) throws IOException { 1.52 + if (!path.exists()) { 1.53 + throw new FileNotFoundException(path.getAbsolutePath()); 1.54 + } 1.55 + 1.56 + if (path.isDirectory()) { 1.57 + return new DirectoryReader(path.toPath()); 1.58 + } else if (path.getName().endsWith(".jar")) { 1.59 + return new JarFileReader(path.toPath()); 1.60 + } else { 1.61 + return new ClassFileReader(path.toPath()); 1.62 + } 1.63 + } 1.64 + 1.65 + protected final Path path; 1.66 + protected final String baseFileName; 1.67 + private ClassFileReader(Path path) { 1.68 + this.path = path; 1.69 + this.baseFileName = path.getFileName() != null 1.70 + ? path.getFileName().toString() 1.71 + : path.toString(); 1.72 + } 1.73 + 1.74 + public String getFileName() { 1.75 + return baseFileName; 1.76 + } 1.77 + 1.78 + /** 1.79 + * Returns the ClassFile matching the given binary name 1.80 + * or a fully-qualified class name. 1.81 + */ 1.82 + public ClassFile getClassFile(String name) throws IOException { 1.83 + if (name.indexOf('.') > 0) { 1.84 + int i = name.lastIndexOf('.'); 1.85 + String pathname = name.replace('.', File.separatorChar) + ".class"; 1.86 + if (baseFileName.equals(pathname) || 1.87 + baseFileName.equals(pathname.substring(0, i) + "$" + 1.88 + pathname.substring(i+1, pathname.length()))) { 1.89 + return readClassFile(path); 1.90 + } 1.91 + } else { 1.92 + if (baseFileName.equals(name.replace('/', File.separatorChar) + ".class")) { 1.93 + return readClassFile(path); 1.94 + } 1.95 + } 1.96 + return null; 1.97 + } 1.98 + 1.99 + public Iterable<ClassFile> getClassFiles() throws IOException { 1.100 + return new Iterable<ClassFile>() { 1.101 + public Iterator<ClassFile> iterator() { 1.102 + return new FileIterator(); 1.103 + } 1.104 + }; 1.105 + } 1.106 + 1.107 + protected ClassFile readClassFile(Path p) throws IOException { 1.108 + InputStream is = null; 1.109 + try { 1.110 + is = Files.newInputStream(p); 1.111 + return ClassFile.read(is); 1.112 + } catch (ConstantPoolException e) { 1.113 + throw new ClassFileError(e); 1.114 + } finally { 1.115 + if (is != null) { 1.116 + is.close(); 1.117 + } 1.118 + } 1.119 + } 1.120 + 1.121 + class FileIterator implements Iterator<ClassFile> { 1.122 + int count; 1.123 + FileIterator() { 1.124 + this.count = 0; 1.125 + } 1.126 + public boolean hasNext() { 1.127 + return count == 0 && baseFileName.endsWith(".class"); 1.128 + } 1.129 + 1.130 + public ClassFile next() { 1.131 + if (!hasNext()) { 1.132 + throw new NoSuchElementException(); 1.133 + } 1.134 + try { 1.135 + ClassFile cf = readClassFile(path); 1.136 + count++; 1.137 + return cf; 1.138 + } catch (IOException e) { 1.139 + throw new ClassFileError(e); 1.140 + } 1.141 + } 1.142 + 1.143 + public void remove() { 1.144 + throw new UnsupportedOperationException("Not supported yet."); 1.145 + } 1.146 + } 1.147 + 1.148 + public String toString() { 1.149 + return path.toString(); 1.150 + } 1.151 + 1.152 + private static class DirectoryReader extends ClassFileReader { 1.153 + DirectoryReader(Path path) throws IOException { 1.154 + super(path); 1.155 + } 1.156 + 1.157 + public ClassFile getClassFile(String name) throws IOException { 1.158 + if (name.indexOf('.') > 0) { 1.159 + int i = name.lastIndexOf('.'); 1.160 + String pathname = name.replace('.', File.separatorChar) + ".class"; 1.161 + Path p = path.resolve(pathname); 1.162 + if (!p.toFile().exists()) { 1.163 + p = path.resolve(pathname.substring(0, i) + "$" + 1.164 + pathname.substring(i+1, pathname.length())); 1.165 + } 1.166 + if (p.toFile().exists()) { 1.167 + return readClassFile(p); 1.168 + } 1.169 + } else { 1.170 + Path p = path.resolve(name + ".class"); 1.171 + if (p.toFile().exists()) { 1.172 + return readClassFile(p); 1.173 + } 1.174 + } 1.175 + return null; 1.176 + } 1.177 + 1.178 + public Iterable<ClassFile> getClassFiles() throws IOException { 1.179 + final Iterator<ClassFile> iter = new DirectoryIterator(); 1.180 + return new Iterable<ClassFile>() { 1.181 + public Iterator<ClassFile> iterator() { 1.182 + return iter; 1.183 + } 1.184 + }; 1.185 + } 1.186 + 1.187 + private List<Path> walkTree(Path dir) throws IOException { 1.188 + final List<Path> files = new ArrayList<Path>(); 1.189 + Files.walkFileTree(dir, new SimpleFileVisitor<Path>() { 1.190 + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) 1.191 + throws IOException { 1.192 + if (file.toFile().getName().endsWith(".class")) { 1.193 + files.add(file); 1.194 + } 1.195 + return FileVisitResult.CONTINUE; 1.196 + } 1.197 + }); 1.198 + return files; 1.199 + } 1.200 + 1.201 + class DirectoryIterator implements Iterator<ClassFile> { 1.202 + private List<Path> entries; 1.203 + private int index = 0; 1.204 + DirectoryIterator() throws IOException { 1.205 + entries = walkTree(path); 1.206 + index = 0; 1.207 + } 1.208 + 1.209 + public boolean hasNext() { 1.210 + return index != entries.size(); 1.211 + } 1.212 + 1.213 + public ClassFile next() { 1.214 + if (!hasNext()) { 1.215 + throw new NoSuchElementException(); 1.216 + } 1.217 + Path path = entries.get(index++); 1.218 + try { 1.219 + return readClassFile(path); 1.220 + } catch (IOException e) { 1.221 + throw new ClassFileError(e); 1.222 + } 1.223 + } 1.224 + 1.225 + public void remove() { 1.226 + throw new UnsupportedOperationException("Not supported yet."); 1.227 + } 1.228 + } 1.229 + } 1.230 + 1.231 + private static class JarFileReader extends ClassFileReader { 1.232 + final JarFile jarfile; 1.233 + JarFileReader(Path path) throws IOException { 1.234 + super(path); 1.235 + this.jarfile = new JarFile(path.toFile()); 1.236 + } 1.237 + 1.238 + public ClassFile getClassFile(String name) throws IOException { 1.239 + if (name.indexOf('.') > 0) { 1.240 + int i = name.lastIndexOf('.'); 1.241 + String entryName = name.replace('.', '/') + ".class"; 1.242 + JarEntry e = jarfile.getJarEntry(entryName); 1.243 + if (e == null) { 1.244 + e = jarfile.getJarEntry(entryName.substring(0, i) + "$" 1.245 + + entryName.substring(i + 1, entryName.length())); 1.246 + } 1.247 + if (e != null) { 1.248 + return readClassFile(e); 1.249 + } 1.250 + } else { 1.251 + JarEntry e = jarfile.getJarEntry(name + ".class"); 1.252 + if (e != null) { 1.253 + return readClassFile(e); 1.254 + } 1.255 + } 1.256 + return null; 1.257 + } 1.258 + 1.259 + private ClassFile readClassFile(JarEntry e) throws IOException { 1.260 + InputStream is = null; 1.261 + try { 1.262 + is = jarfile.getInputStream(e); 1.263 + return ClassFile.read(is); 1.264 + } catch (ConstantPoolException ex) { 1.265 + throw new ClassFileError(ex); 1.266 + } finally { 1.267 + if (is != null) 1.268 + is.close(); 1.269 + } 1.270 + } 1.271 + 1.272 + public Iterable<ClassFile> getClassFiles() throws IOException { 1.273 + final Iterator<ClassFile> iter = new JarFileIterator(); 1.274 + return new Iterable<ClassFile>() { 1.275 + public Iterator<ClassFile> iterator() { 1.276 + return iter; 1.277 + } 1.278 + }; 1.279 + } 1.280 + 1.281 + class JarFileIterator implements Iterator<ClassFile> { 1.282 + private Enumeration<JarEntry> entries; 1.283 + private JarEntry nextEntry; 1.284 + JarFileIterator() { 1.285 + this.entries = jarfile.entries(); 1.286 + while (entries.hasMoreElements()) { 1.287 + JarEntry e = entries.nextElement(); 1.288 + String name = e.getName(); 1.289 + if (name.endsWith(".class")) { 1.290 + this.nextEntry = e; 1.291 + break; 1.292 + } 1.293 + } 1.294 + } 1.295 + 1.296 + public boolean hasNext() { 1.297 + return nextEntry != null; 1.298 + } 1.299 + 1.300 + public ClassFile next() { 1.301 + if (!hasNext()) { 1.302 + throw new NoSuchElementException(); 1.303 + } 1.304 + 1.305 + ClassFile cf; 1.306 + try { 1.307 + cf = readClassFile(nextEntry); 1.308 + } catch (IOException ex) { 1.309 + throw new ClassFileError(ex); 1.310 + } 1.311 + JarEntry entry = nextEntry; 1.312 + nextEntry = null; 1.313 + while (entries.hasMoreElements()) { 1.314 + JarEntry e = entries.nextElement(); 1.315 + String name = e.getName(); 1.316 + if (name.endsWith(".class")) { 1.317 + nextEntry = e; 1.318 + break; 1.319 + } 1.320 + } 1.321 + return cf; 1.322 + } 1.323 + 1.324 + public void remove() { 1.325 + throw new UnsupportedOperationException("Not supported yet."); 1.326 + } 1.327 + } 1.328 + } 1.329 +}