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