com.sun.jini.tool.classdepend
Class ClassDepend

java.lang.Object
  extended by com.sun.jini.tool.classdepend.ClassDepend

public class ClassDepend
extends Object

Provides a utility for computing which classes are depended on by a set of classes. This class is not thread safe.


Field Summary
private  ClassLoader loader
          The class loader used to load classes being checked for dependencies.
private  PackageClasses packageClasses
          Used to compute the classes available in the classpath for a package.
private  ClassLoader platformLoader
          The class loader for classes that should be excluded because they are considered part of the platform.
private  boolean printClassesWithFileSeparator
           
private static String systemClasspath
          The system classpath.
 
Constructor Summary
ClassDepend(String classpath, String platform)
          Creates an instance of this class.
 
Method Summary
private  boolean classPresent(String name, ClassLoader loader)
          Checks if the class is present in the given loader.
private  Set computeClasses(Collection names)
          Returns the classes in the classpath that match the specified names by expanding package wildcards.
 Pattern createPattern(Collection names)
          Creates a pattern that matches class names for any of the names in the argument.
 Set filterClassDependencyRelationShipMap(Map dependencyRelationShipMap, ClassDependParameters cdp)
          This method applies optional filters to provide methods to support the original API of ClassDep.
private  URL[] getClasspathURLs(String classpath)
          Returns the URLs associated with a classpath.
 Map getDependencyRelationshipMap(Collection rootClasses, boolean recurse)
          This method builds the entire DependencyRelationShipMap that can be used to analyse class dependencies.
private  String getResourceName(String classname)
          Returns the name of the resource associated with a class name.
static void main(String[] args)
           
 boolean matches(String string, Pattern pattern)
          Checks if the string matches the pattern, returning false if the pattern is null.
static ClassDepend newInstance(String classpath, String platform, boolean warn)
          Public Factory method for creating a new instance of ClassDepend.
protected  void noteClassLoadingFailed(String name, IOException e)
          Called when attempts to load the bytecodes for the specified class fail.
protected  void noteClassNotFound(String name)
          Called when the specified class is not found.
 boolean printClassesWithFileSeparator()
           
private  String quote(String s)
          Returns a literal pattern String for the specified String.
 void setPrintClassesWithFileSeparator(boolean printClassesWithFileSeparator)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

systemClasspath

private static final String systemClasspath
The system classpath.


loader

private final ClassLoader loader
The class loader used to load classes being checked for dependencies.


platformLoader

private final ClassLoader platformLoader
The class loader for classes that should be excluded because they are considered part of the platform.


packageClasses

private final PackageClasses packageClasses
Used to compute the classes available in the classpath for a package.


printClassesWithFileSeparator

private volatile boolean printClassesWithFileSeparator
Constructor Detail

ClassDepend

ClassDepend(String classpath,
            String platform)
      throws MalformedURLException,
             IOException
Creates an instance of this class. The classpath argument specifies the classpath that will be used to look up the class bytecode for classes whose dependencies are being computed. If the value specified is null, then the system class loader will be used. Otherwise, a URLClassLoader will be constructed using the URLs specified by classpath and with a parent class loader that is the parent of the system class loader. The platform argument specifies the classpath that will be used to find classes that should be considered part of the platform and should be excluded from consideration when computing dependencies. If the value specified is null, then the parent of the system class loader will be used. Otherwise, a URLClassLoader will be constructed using the URLs specified by platform and with a parent class loader that is the parent of the system class loader.

Parameters:
classpath - the classpath for finding classes, or null
platform - the classpath for finding platform classes, or null
Throws:
MalformedURLException - if the URLs specified in the arguments are malformed
IOException - if an I/O error occurs while accessing files in the classpath
Method Detail

newInstance

public static ClassDepend newInstance(String classpath,
                                      String platform,
                                      boolean warn)
                               throws MalformedURLException,
                                      IOException
Public Factory method for creating a new instance of ClassDepend. The classpath argument specifies the classpath that will be used to look up the class bytecode for classes whose dependencies are being computed. If the value specified is null, then the system class loader will be used. Otherwise, a URLClassLoader will be constructed using the URLs specified by classpath and with a parent class loader that is the parent of the system class loader. The platform argument specifies the classpath that will be used to find classes that should be considered part of the platform and should be excluded from consideration when computing dependencies. If the value specified is null, then the parent of the system class loader will be used. Otherwise, a URLClassLoader will be constructed using the URLs specified by platform and with a parent class loader that is the parent of the system class loader.

Parameters:
classpath - the classpath for finding classes, or null
platform - the classpath for finding platform classes, or null
warn - print warnings instead of throwing an exception when a Class can't be found or when ClassLoading fails.
Returns:
ClassDepend
Throws:
MalformedURLException
IOException

main

public static void main(String[] args)

getDependencyRelationshipMap

public Map getDependencyRelationshipMap(Collection rootClasses,
                                        boolean recurse)
                                 throws IOException,
                                        ClassNotFoundException
This method builds the entire DependencyRelationShipMap that can be used to analyse class dependencies. Computes information about class dependencies. The computation of dependencies starts with the classes that match the names in rootClasses. Classes are found in a URL class loader by the classpath specified in the constructor.

Parameters:
rootClasses -
recurse - If true, this option causes ClassDepend to inspect class files for dependencies. If false, ClassDepend doesn't inspect class files, it simply gathers the names of class files from within Package directories and JAR files.
Returns:
result
Throws:
IOException
ClassNotFoundException
See Also:
ClassDependencyRelationship

filterClassDependencyRelationShipMap

public Set filterClassDependencyRelationShipMap(Map dependencyRelationShipMap,
                                                ClassDependParameters cdp)
This method applies optional filters to provide methods to support the original API of ClassDep.

Parameters:
dependencyRelationShipMap - The initial map before filtering.
cdp - The parameters for filtration.
Returns:
Set result The result after filtration.
See Also:
ClassDependParameters, ClassDependencyRelationship

noteClassNotFound

protected void noteClassNotFound(String name)
                          throws ClassNotFoundException
Called when the specified class is not found.

This implementation throws a ClassNotFoundException.

Parameters:
name - the class name
Throws:
ClassNotFoundException - to signal that processing should terminate and the exception should be thrown to the caller

noteClassLoadingFailed

protected void noteClassLoadingFailed(String name,
                                      IOException e)
                               throws IOException
Called when attempts to load the bytecodes for the specified class fail.

Parameters:
name - the class name
e - the exception caused by the failure
Throws:
IOException - to signal that processing should terminate and the exception should be thrown to the caller

computeClasses

private Set computeClasses(Collection names)
                    throws IOException
Returns the classes in the classpath that match the specified names by expanding package wildcards.

Throws:
IOException

getClasspathURLs

private URL[] getClasspathURLs(String classpath)
                        throws MalformedURLException
Returns the URLs associated with a classpath.

Throws:
MalformedURLException

classPresent

private boolean classPresent(String name,
                             ClassLoader loader)
Checks if the class is present in the given loader.


getResourceName

private String getResourceName(String classname)
Returns the name of the resource associated with a class name.


createPattern

public Pattern createPattern(Collection names)
Creates a pattern that matches class names for any of the names in the argument. Returns null if the argument is empty. xNames that end in '.*' match classes in the package, names that end in '.**' match classes in the package and it's subpackage. Other names match the class.

Parameters:
names -
Returns:
Pattern

matches

public boolean matches(String string,
                       Pattern pattern)
Checks if the string matches the pattern, returning false if the pattern is null.

Parameters:
string -
pattern -
Returns:
True if the Pattern Matches

quote

private String quote(String s)
Returns a literal pattern String for the specified String. Added to backport Java 1.5 sources to 1.4. adds the functionality of java.util.regex.Pattern.quote() method missing from Java 1.4 version This method produces a String that can be used to create a Pattern that would match the string s as if it were a literal pattern. Metacharacters or escape sequences in the input sequence will be given no special meaning.

Parameters:
s - - The String to be literalised
Returns:
A literal string replacement

printClassesWithFileSeparator

public boolean printClassesWithFileSeparator()

setPrintClassesWithFileSeparator

public void setPrintClassesWithFileSeparator(boolean printClassesWithFileSeparator)


Copyright 2007-2010, multiple authors.
Licensed under the Apache License, Version 2.0, see the NOTICE file for attributions.