1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/agent/make/ClosureFinder.java Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,254 @@ 1.4 +/* 1.5 + * Copyright 2003-2004 Sun Microsystems, Inc. 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. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +import java.io.*; 1.29 +import java.util.*; 1.30 + 1.31 + 1.32 +/** 1.33 +<p> This class finds transitive closure of dependencies from a given 1.34 +root set of classes. If your project has lots of .class files and you 1.35 +want to ship only those .class files which are used (transitively) 1.36 +from a root set of classes, then you can use this utility. </p> <p> 1.37 +How does it work?</p> 1.38 + 1.39 +<p> We walk through all constant pool entries of a given class and 1.40 +find all modified UTF-8 entries. Anything that looks like a class name is 1.41 +considered as a class and we search for that class in the given 1.42 +classpath. If we find a .class of that name, then we add that class to 1.43 +list.</p> 1.44 + 1.45 +<p> We could have used CONSTANT_ClassInfo type constants only. But 1.46 +that will miss classes used through Class.forName or xyz.class 1.47 +construct. But, if you refer to a class name in some other string we 1.48 +would include it as dependency :(. But this is quite unlikely 1.49 +anyway. To look for exact Class.forName argument(s) would involve 1.50 +bytecode analysis. Also, we handle only simple reflection. If you 1.51 +accept name of a class from externally (for eg properties file or 1.52 +command line args for example, this utility will not be able to find 1.53 +that dependency. In such cases, include those classes in the root set. 1.54 +</p> 1.55 +*/ 1.56 + 1.57 +public class ClosureFinder { 1.58 + private Collection roots; // root class names Collection<String> 1.59 + private Map visitedClasses; // set of all dependencies as a Map 1.60 + private String classPath; // classpath to look for .class files 1.61 + private String[] pathComponents; // classpath components 1.62 + private static final boolean isWindows = File.separatorChar != '/'; 1.63 + 1.64 + public ClosureFinder(Collection roots, String classPath) { 1.65 + this.roots = roots; 1.66 + this.classPath = classPath; 1.67 + parseClassPath(); 1.68 + } 1.69 + 1.70 + // parse classPath into pathComponents array 1.71 + private void parseClassPath() { 1.72 + List paths = new ArrayList(); 1.73 + StringTokenizer st = new StringTokenizer(classPath, File.pathSeparator); 1.74 + while (st.hasMoreTokens()) 1.75 + paths.add(st.nextToken()); 1.76 + 1.77 + Object[] arr = paths.toArray(); 1.78 + pathComponents = new String[arr.length]; 1.79 + System.arraycopy(arr, 0, pathComponents, 0, arr.length); 1.80 + } 1.81 + 1.82 + // if output is aleady not computed, compute it now 1.83 + // result is a map from class file name to base path where the .class was found 1.84 + public Map find() { 1.85 + if (visitedClasses == null) { 1.86 + visitedClasses = new HashMap(); 1.87 + computeClosure(); 1.88 + } 1.89 + return visitedClasses; 1.90 + } 1.91 + 1.92 + // compute closure for all given root classes 1.93 + private void computeClosure() { 1.94 + for (Iterator rootsItr = roots.iterator(); rootsItr.hasNext();) { 1.95 + String name = (String) rootsItr.next(); 1.96 + name = name.substring(0, name.indexOf(".class")); 1.97 + computeClosure(name); 1.98 + } 1.99 + } 1.100 + 1.101 + 1.102 + // looks up for .class in pathComponents and returns 1.103 + // base path if found, else returns null 1.104 + private String lookupClassFile(String classNameAsPath) { 1.105 + for (int i = 0; i < pathComponents.length; i++) { 1.106 + File f = new File(pathComponents[i] + File.separator + 1.107 + classNameAsPath + ".class"); 1.108 + if (f.exists()) { 1.109 + if (isWindows) { 1.110 + String name = f.getName(); 1.111 + // Windows reports special devices AUX,NUL,CON as files 1.112 + // under any directory. It does not care about file extention :-( 1.113 + if (name.compareToIgnoreCase("AUX.class") == 0 || 1.114 + name.compareToIgnoreCase("NUL.class") == 0 || 1.115 + name.compareToIgnoreCase("CON.class") == 0) { 1.116 + return null; 1.117 + } 1.118 + } 1.119 + return pathComponents[i]; 1.120 + } 1.121 + } 1.122 + return null; 1.123 + } 1.124 + 1.125 + 1.126 + // from JVM spec. 2'nd edition section 4.4 1.127 + private static final int CONSTANT_Class = 7; 1.128 + private static final int CONSTANT_FieldRef = 9; 1.129 + private static final int CONSTANT_MethodRef = 10; 1.130 + private static final int CONSTANT_InterfaceMethodRef = 11; 1.131 + private static final int CONSTANT_String = 8; 1.132 + private static final int CONSTANT_Integer = 3; 1.133 + private static final int CONSTANT_Float = 4; 1.134 + private static final int CONSTANT_Long = 5; 1.135 + private static final int CONSTANT_Double = 6; 1.136 + private static final int CONSTANT_NameAndType = 12; 1.137 + private static final int CONSTANT_Utf8 = 1; 1.138 + 1.139 + // whether a given string may be a class name? 1.140 + private boolean mayBeClassName(String internalClassName) { 1.141 + int len = internalClassName.length(); 1.142 + for (int s = 0; s < len; s++) { 1.143 + char c = internalClassName.charAt(s); 1.144 + if (!Character.isJavaIdentifierPart(c) && c != '/') 1.145 + return false; 1.146 + } 1.147 + return true; 1.148 + } 1.149 + 1.150 + // compute closure for a given class 1.151 + private void computeClosure(String className) { 1.152 + if (visitedClasses.get(className) != null) return; 1.153 + String basePath = lookupClassFile(className); 1.154 + if (basePath != null) { 1.155 + visitedClasses.put(className, basePath); 1.156 + try { 1.157 + File classFile = new File(basePath + File.separator + className + ".class"); 1.158 + FileInputStream fis = new FileInputStream(classFile); 1.159 + DataInputStream dis = new DataInputStream(fis); 1.160 + // look for .class signature 1.161 + if (dis.readInt() != 0xcafebabe) { 1.162 + System.err.println(classFile.getAbsolutePath() + " is not a valid .class file"); 1.163 + return; 1.164 + } 1.165 + 1.166 + // ignore major and minor version numbers 1.167 + dis.readShort(); 1.168 + dis.readShort(); 1.169 + 1.170 + // read number of constant pool constants 1.171 + int numConsts = (int) dis.readShort(); 1.172 + String[] strings = new String[numConsts]; 1.173 + 1.174 + // zero'th entry is unused 1.175 + for (int cpIndex = 1; cpIndex < numConsts; cpIndex++) { 1.176 + int constType = (int) dis.readByte(); 1.177 + switch (constType) { 1.178 + case CONSTANT_Class: 1.179 + case CONSTANT_String: 1.180 + dis.readShort(); // string name index; 1.181 + break; 1.182 + 1.183 + case CONSTANT_FieldRef: 1.184 + case CONSTANT_MethodRef: 1.185 + case CONSTANT_InterfaceMethodRef: 1.186 + case CONSTANT_NameAndType: 1.187 + case CONSTANT_Integer: 1.188 + case CONSTANT_Float: 1.189 + // all these are 4 byte constants 1.190 + dis.readInt(); 1.191 + break; 1.192 + 1.193 + case CONSTANT_Long: 1.194 + case CONSTANT_Double: 1.195 + // 8 byte constants 1.196 + dis.readLong(); 1.197 + // occupies 2 cp entries 1.198 + cpIndex++; 1.199 + break; 1.200 + 1.201 + 1.202 + case CONSTANT_Utf8: { 1.203 + strings[cpIndex] = dis.readUTF(); 1.204 + break; 1.205 + } 1.206 + 1.207 + default: 1.208 + System.err.println("invalid constant pool entry"); 1.209 + return; 1.210 + } 1.211 + } 1.212 + 1.213 + // now walk thru the string constants and look for class names 1.214 + for (int s = 0; s < numConsts; s++) { 1.215 + if (strings[s] != null && mayBeClassName(strings[s])) 1.216 + computeClosure(strings[s].replace('/', File.separatorChar)); 1.217 + } 1.218 + 1.219 + } catch (IOException exp) { 1.220 + // ignore for now 1.221 + } 1.222 + 1.223 + } 1.224 + } 1.225 + 1.226 + // a sample main that accepts roots classes in a file and classpath as args 1.227 + public static void main(String[] args) { 1.228 + if (args.length != 2) { 1.229 + System.err.println("Usage: ClosureFinder <root class file> <class path>"); 1.230 + System.exit(1); 1.231 + } 1.232 + 1.233 + List roots = new ArrayList(); 1.234 + try { 1.235 + FileInputStream fis = new FileInputStream(args[0]); 1.236 + DataInputStream dis = new DataInputStream(fis); 1.237 + String line = null; 1.238 + while ((line = dis.readLine()) != null) { 1.239 + if (isWindows) { 1.240 + line = line.replace('/', File.separatorChar); 1.241 + } 1.242 + roots.add(line); 1.243 + } 1.244 + } catch (IOException exp) { 1.245 + System.err.println(exp.getMessage()); 1.246 + System.exit(2); 1.247 + } 1.248 + 1.249 + ClosureFinder cf = new ClosureFinder(roots, args[1]); 1.250 + Map out = cf.find(); 1.251 + Iterator res = out.keySet().iterator(); 1.252 + for(; res.hasNext(); ) { 1.253 + String className = (String) res.next(); 1.254 + System.out.println(className + ".class"); 1.255 + } 1.256 + } 1.257 +}