GetDeps.java revision 553
0N/A/*
157N/A * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
157N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
157N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
157N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
157N/A * or visit www.oracle.com if you need additional information or have any
157N/A * questions.
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/Aimport java.util.regex.Pattern;
0N/Aimport javax.tools.*;
0N/A
0N/Aimport com.sun.tools.classfile.*;
0N/Aimport com.sun.tools.classfile.Dependencies.*;
0N/Aimport com.sun.tools.classfile.Dependency.Location;
0N/Aimport com.sun.tools.javac.file.JavacFileManager;
0N/Aimport com.sun.tools.javac.util.Context;
0N/A
0N/A/**
0N/A * Demo utility for using the classfile dependency analysis API framework.
0N/A *
0N/A * Usage:
0N/A * getdeps [options] classes
0N/A * where options include:
0N/A * -classpath path where to find classes to analyze
0N/A * -p package-name restrict analysis to classes in this package
0N/A * (may be given multiple times)
0N/A * -r regex restrict analysis to packages matching pattern
0N/A * (-p and -r are exclusive)
0N/A * -rev invert the dependencies in the output
0N/A * -t transitive closure of dependencies
0N/A */
0N/Apublic class GetDeps {
0N/A public static void main(String... args) throws Exception {
0N/A new GetDeps().run(args);
0N/A }
0N/A
0N/A void run(String... args) throws IOException, ClassFileNotFoundException {
0N/A PrintWriter pw = new PrintWriter(System.out);
0N/A try {
0N/A run(pw, args);
0N/A } finally {
0N/A pw.flush();
0N/A }
0N/A }
0N/A
0N/A void run(PrintWriter out, String... args) throws IOException, ClassFileNotFoundException {
0N/A decodeArgs(args);
0N/A
0N/A final StandardJavaFileManager fm = new JavacFileManager(new Context(), false, null);
0N/A if (classpath != null)
0N/A fm.setLocation(StandardLocation.CLASS_PATH, classpath);
0N/A
0N/A ClassFileReader reader = new ClassFileReader(fm);
0N/A
0N/A Dependencies d = new Dependencies();
0N/A
0N/A if (regex != null)
0N/A d.setFilter(Dependencies.getRegexFilter(Pattern.compile(regex)));
0N/A
0N/A if (packageNames.size() > 0)
d.setFilter(Dependencies.getPackageFilter(packageNames, false));
SortedRecorder r = new SortedRecorder(reverse);
d.findAllDependencies(reader, rootClassNames, transitiveClosure, r);
SortedMap<Location,SortedSet<Dependency>> deps = r.getMap();
for (Map.Entry<Location, SortedSet<Dependency>> e: deps.entrySet()) {
out.println(e.getKey());
for (Dependency dep: e.getValue()) {
out.println(" " + dep.getTarget());
}
}
}
void decodeArgs(String... args) {
rootClassNames = new TreeSet<String>();
packageNames = new TreeSet<String>();
for (int i = 0; i < args.length; i++) {
String arg = args[i];
if (arg.equals("-classpath") && (i + 1 < args.length))
classpath = getPathFiles(args[++i]);
else if (arg.equals("-p") && (i + 1 < args.length))
packageNames.add(args[++i]);
else if (arg.equals("-r") && (i + 1 < args.length))
regex = args[++i];
else if (arg.equals("-rev"))
reverse = true;
else if (arg.equals("-t"))
transitiveClosure = true;
else if (arg.startsWith("-"))
throw new Error(arg);
else {
for ( ; i < args.length; i++)
rootClassNames.add(args[i]);
}
}
}
List<File> getPathFiles(String path) {
List<File> files = new ArrayList<File>();
for (String p: path.split(File.pathSeparator)) {
if (p.length() > 0)
files.add(new File(p));
}
return files;
}
boolean transitiveClosure;
List<File> classpath;
Set<String> rootClassNames;
Set<String> packageNames;
String regex;
boolean reverse;
static class ClassFileReader implements Dependencies.ClassFileReader {
private JavaFileManager fm;
ClassFileReader(JavaFileManager fm) {
this.fm = fm;
}
@Override
public ClassFile getClassFile(String className) throws ClassFileNotFoundException {
try {
JavaFileObject fo = fm.getJavaFileForInput(
StandardLocation.CLASS_PATH, className, JavaFileObject.Kind.CLASS);
if (fo == null)
fo = fm.getJavaFileForInput(
StandardLocation.PLATFORM_CLASS_PATH, className, JavaFileObject.Kind.CLASS);
if (fo == null)
throw new ClassFileNotFoundException(className);
InputStream in = fo.openInputStream();
try {
return ClassFile.read(in);
} finally {
in.close();
}
} catch (ConstantPoolException e) {
throw new ClassFileNotFoundException(className, e);
} catch (IOException e) {
throw new ClassFileNotFoundException(className, e);
}
}
};
static class SortedRecorder implements Recorder {
public SortedRecorder(boolean reverse) {
this.reverse = reverse;
}
public void addDependency(Dependency d) {
Location o = (reverse ? d.getTarget() : d.getOrigin());
SortedSet<Dependency> odeps = map.get(o);
if (odeps == null) {
Comparator<Dependency> c = (reverse ? originComparator : targetComparator);
map.put(o, odeps = new TreeSet<Dependency>(c));
}
odeps.add(d);
}
public SortedMap<Location, SortedSet<Dependency>> getMap() {
return map;
}
private Comparator<Dependency> originComparator = new Comparator<Dependency>() {
public int compare(Dependency o1, Dependency o2) {
return o1.getOrigin().toString().compareTo(o2.getOrigin().toString());
}
};
private Comparator<Dependency> targetComparator = new Comparator<Dependency>() {
public int compare(Dependency o1, Dependency o2) {
return o1.getTarget().toString().compareTo(o2.getTarget().toString());
}
};
private Comparator<Location> locationComparator = new Comparator<Location>() {
public int compare(Location o1, Location o2) {
return o1.toString().compareTo(o2.toString());
}
};
private final SortedMap<Location, SortedSet<Dependency>> map =
new TreeMap<Location, SortedSet<Dependency>>(locationComparator);
boolean reverse;
}
}