1 /*
   2  * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 package com.sun.classanalyzer;
  25 
  26 import java.io.BufferedInputStream;
  27 import java.io.File;
  28 import java.io.FileInputStream;
  29 import java.io.IOException;
  30 import java.io.InputStream;
  31 import java.util.ArrayList;
  32 import java.util.Enumeration;
  33 import java.util.HashSet;
  34 import java.util.List;
  35 import java.util.Set;
  36 import java.util.jar.JarEntry;
  37 import java.util.jar.JarFile;
  38 
  39 /**
  40  *
  41  * @author mchung
  42  */
  43 public class ClassPath {
  44 
  45     public class FileInfo {
  46 
  47         File file;
  48         JarFile jarfile;
  49         int classCount;
  50         long filesize;
  51 
  52         FileInfo(File f) throws IOException {
  53             this.file = f;
  54             this.classCount = 0;
  55             if (file.getName().endsWith(".jar")) {
  56                 this.filesize = file.length();
  57                 jarfile = new JarFile(f);
  58             }
  59         }
  60 
  61         File getFile() {
  62             return file;
  63         }
  64 
  65         JarFile getJarFile() {
  66             return jarfile;
  67         }
  68 
  69         String getName() throws IOException {
  70             return file.getCanonicalPath();
  71         }
  72     }
  73     private List<FileInfo> fileList = new ArrayList<FileInfo>();
  74     private static ClassPath instance = new ClassPath();
  75 
  76     static List<FileInfo> getFileInfos() {
  77         return instance.fileList;
  78     }
  79 
  80     static ClassPath setJDKHome(String jdkhome) throws IOException {
  81         List<File> files = new ArrayList<File>();
  82         File jre = new File(jdkhome, "jre");
  83         File lib = new File(jdkhome, "lib");
  84         if (jre.exists() && jre.isDirectory()) {
  85             listFiles(new File(jre, "lib"), ".jar", files);
  86             listFiles(lib, ".jar", files);
  87         } else if (lib.exists() && lib.isDirectory()) {
  88             // either a JRE or a jdk build image
  89             listFiles(lib, ".jar", files);
  90 
  91             File classes = new File(jdkhome, "classes");
  92             if (classes.exists() && classes.isDirectory()) {
  93                 // jdk build outputdir
  94                 instance.add(classes);
  95             }
  96         } else {
  97             throw new RuntimeException("\"" + jdkhome + "\" not a JDK home");
  98         }
  99 
 100         for (File f : files) {
 101             instance.add(f);
 102         }
 103         return instance;
 104     }
 105 
 106     static ClassPath setClassPath(String path) throws IOException {
 107         if (path.endsWith(".class")) {
 108             // one class file
 109             File f = new File(path);
 110             if (!f.exists()) {
 111                 throw new RuntimeException("Classfile \"" + f + "\" doesn't exist");
 112             }
 113 
 114             instance.add(f);
 115         } else {
 116             List<File> jarFiles = new ArrayList<File>();
 117             String[] locs = path.split(File.pathSeparator);
 118             for (String p : locs) {
 119                 File f = new File(p);
 120                 if (!f.exists()) {
 121                     throw new RuntimeException("\"" + f + "\" doesn't exist");
 122                 }
 123 
 124                 if (f.isDirectory()) {
 125                     instance.add(f);  // add the directory to look up .class files
 126                     listFiles(f, ".jar", jarFiles);
 127                 } else if (p.endsWith(".jar")) {
 128                     // jar files
 129                     jarFiles.add(f);
 130                 } else {
 131                     throw new RuntimeException("Invalid file \"" + f);
 132                 }
 133             }
 134             // add jarFiles if any
 135             for (File f : jarFiles) {
 136                 instance.add(f);
 137             }
 138         }
 139 
 140         return instance;
 141     }
 142 
 143     private void add(File f) throws IOException {
 144         fileList.add(new FileInfo(f));
 145     }
 146 
 147     public static InputStream open(String pathname) throws IOException {
 148         for (FileInfo fi : instance.fileList) {
 149             if (fi.getName().endsWith(".jar")) {
 150                 String path = pathname.replace(File.separatorChar, '/');
 151                 JarEntry e = fi.jarfile.getJarEntry(path);
 152                 if (e != null) {
 153                     return fi.jarfile.getInputStream(e);
 154                 }
 155             } else if (fi.getFile().isDirectory()) {
 156                 File f = new File(fi.getFile(), pathname);
 157                 if (f.exists()) {
 158                     return new FileInputStream(f);
 159                 }
 160             } else if (fi.file.isFile()) {
 161                 if (fi.getName().endsWith(File.separator + pathname)) {
 162                     return new FileInputStream(fi.file);
 163                 }
 164             }
 165         }
 166         return null;
 167     }
 168 
 169     static ClassFileParser parserForClass(String classname) throws IOException {
 170         String pathname = classname.replace('.', File.separatorChar) + ".class";
 171 
 172         ClassFileParser cfparser = null;
 173         for (FileInfo fi : instance.fileList) {
 174             if (fi.getName().endsWith(".class")) {
 175                 if (fi.getName().endsWith(File.separator + pathname)) {
 176                     cfparser = ClassFileParser.newParser(fi.getFile(), true);
 177                     break;
 178                 }
 179             } else if (fi.getName().endsWith(".jar")) {
 180                 JarEntry e = fi.jarfile.getJarEntry(classname.replace('.', '/') + ".class");
 181                 if (e != null) {
 182                     cfparser = ClassFileParser.newParser(fi.jarfile.getInputStream(e), e.getSize(), true);
 183                     break;
 184                 }
 185             } else if (fi.getFile().isDirectory()) {
 186                 File f = new File(fi.getFile(), pathname);
 187                 if (f.exists()) {
 188                     cfparser = ClassFileParser.newParser(f, true);
 189                     break;
 190                 }
 191             }
 192         }
 193         return cfparser;
 194     }
 195 
 196     public static void parseAllClassFiles() throws IOException {
 197         instance.parseFiles();
 198     }
 199 
 200     private void parseFiles() throws IOException {
 201         Set<Klass> classes = new HashSet<Klass>();
 202 
 203         int count = 0;
 204         for (FileInfo fi : fileList) {
 205             // filter out public generated classes (i.e. not public API)
 206             // javax.management.remote.rmi._RMIConnectionImpl_Tie
 207             // javax.management.remote.rmi._RMIServerImpl_Tie
 208             if (fi.getName().endsWith(".class")) {
 209                 parseClass(fi);
 210             } else if (fi.getName().endsWith(".jar")) {
 211                 Enumeration<JarEntry> entries = fi.jarfile.entries();
 212                 while (entries.hasMoreElements()) {
 213                     JarEntry e = entries.nextElement();
 214                     if (e.getName().endsWith(".class")) {
 215                         ClassFileParser cfparser = ClassFileParser.newParser(fi.jarfile.getInputStream(e), e.getSize(), true);
 216                         cfparser.parseDependency(false);
 217                         fi.classCount++;
 218                     } else if (!e.isDirectory() && ResourceFile.isResource(e.getName())) {
 219                         ResourceFile.addResource(e.getName(), fi.jarfile.getInputStream(e));
 220                     }
 221                 }
 222             } else if (fi.getFile().isDirectory()) {
 223                 List<File> files = new ArrayList<File>();
 224                 listFiles(fi.getFile(), "", files);
 225                 for (File f : files) {
 226                     if (f.getName().endsWith(".class")) {
 227                         parseClass(fi, f);
 228                     } else if (!f.isDirectory() && ResourceFile.isResource(f.getCanonicalPath())) {
 229                         String pathname = f.getCanonicalPath();
 230                         String dir = fi.getName();
 231                         if (!pathname.startsWith(dir)) {
 232                             throw new RuntimeException("Incorrect pathname " + pathname);
 233                         }
 234                         String name = pathname.substring(dir.length() + 1, pathname.length());
 235                         BufferedInputStream in = new BufferedInputStream(new FileInputStream(f));
 236                         try {
 237                             ResourceFile.addResource(name, in);
 238                         } finally {
 239                             in.close();
 240                         }
 241                     }
 242                 }
 243             } else {
 244                 // should not reach here
 245                 throw new RuntimeException("Unexpected class path: " + fi.getFile());
 246             }
 247         }
 248     }
 249 
 250     private void parseClass(FileInfo fi) throws IOException {
 251         parseClass(fi, fi.getFile());
 252     }
 253 
 254     private void parseClass(FileInfo fi, File f) throws IOException {
 255         ClassFileParser cfparser = ClassFileParser.newParser(f, true);
 256         cfparser.parseDependency(false);
 257         fi.classCount++;
 258         // need to update the filesize for this directory
 259         fi.filesize += fi.getFile().length();
 260 
 261     }
 262 
 263     public static void listFiles(File path, String suffix, List<File> result) {
 264         if (path.isDirectory()) {
 265             File[] children = path.listFiles();
 266             for (File c : children) {
 267                 listFiles(c, suffix, result);
 268             }
 269 
 270         } else {
 271             if (suffix.isEmpty() || path.getName().endsWith(suffix)) {
 272                 result.add(path);
 273             }
 274         }
 275     }
 276 }