FileDocCategorySizeDatePackage
ClosureCompilerImpl.javaAPI DocGlassfish v2 API10678Fri May 04 22:33:24 BST 2007com.sun.enterprise.tools.verifier.apiscan.classfile

ClosureCompilerImpl

public class ClosureCompilerImpl extends Object implements ClosureCompiler
This is single most important class of the apiscan package. This class is used to compute the complete closure of a set of classes. It uses {@link ClassFile} to find out dependencies of a class. For each references class name, it loads the corresponding ClassFile using the ClassFileLoader passed to it in constructor. Then it recurssively computes the dependencies until it visits either Java primitive classes or the class name matches the exclude list. Example of using this class is given below...
String classpath="your own classpath";
ClassFileLoader cfl=ClassFileLoaderFactory.newInstance(new
Object[]{classpath});
ClosureCompilerImpl cc=new ClosureCompilerImpl(cfl);
cc.addExcludePattern("java.");//exclude all classes that start with java.
Most of the J2SE classes will be excluded thus.
cc.addExcludePattern("javax.");//Most of the J2EE classes can be excluded
like this.
cc.addExcludePackage("org.omg.CORBA");//Exclude classes whose package name is
org.omg.CORBA
cc.addExcludeClass("mypackage.Foo");//Exclude class whose name is
myPackage.Foo
boolean successful=cc.buildClosure("a.b.MyEjb");
successful=cc.buildClosure("a.b.YourEjb") && successful; //note the order of
&&.
Collection closure=cc.getClosure();//now closure contains the union of
closure for the two classes.
Map failed=cc.getFailed();//now failure contains the union of failures for
the two classes.
cc.reset();//clear the results collected so far so that we can start afresh.
//Now you can again start computing closure of another set of classes.
//The excluded stuff are still valid.
author
Sanjeeb.Sahoo@Sun.COM

Fields Summary
private ClosureCompilerImplBase
imp
an implementation to whom this abstraction delegates. refer to bridge design pattern.
private static String
resourceBundleName
private static Logger
logger
private static final String
myClassName
Constructors Summary
public ClosureCompilerImpl(ClassFileLoader loader)

param
loader the ClassFileLoader that is used to load the referenced classes.

 // NOI18N

                                   
       
        /*
         * See how it binds to a runtime implementation
         * TODO: Ideally we should introduce an AbstractFactory for
         * both ClassFileLoader & ClassClosureCompiler product types.
         */
        if(loader instanceof ASMClassFileLoader){
            imp = new ASMClosureCompilerImpl(loader);
        } else if(loader instanceof BCELClassFileLoader ||
                loader instanceof BCELClassFileLoader1) {
            imp = new BCELClosureCompilerImpl(loader);
        } else {
            throw new RuntimeException("Unknown loader type [" + loader + "]");
        }
    
public ClosureCompilerImpl(ClosureCompilerImplBase imp)
I don't expect this constructor to be used. Only defined for testing purpose.

param
imp the implementation in the bridge design pattern.

        this.imp = imp;
    
Methods Summary
public voidaddExcludedClass(java.lang.String className)

param
className the class name to be excluded from closure computation. It is in the external class name format (i.e. java.util.Map$Entry instead of java.util.Map.Entry). When the closure compiler sees a class matches this name, it does not try to compute its closure any more. It merely adds this name to the closure. So the final closure will contain this class name, but not its dependencies.

        imp.addExcludedClass(className);
    
public voidaddExcludedPackage(java.lang.String pkgName)

param
pkgName the package name of classes to be excluded from closure computation. It is in the external format (i.e. java.lang (See no trailing '.'). When the closure compiler sees a class whose package name matches this name, it does not try to compute the closure of that class any more. It merely adds that class name to the closure. So the final closure will contain that class name, but not its dependencies.

        imp.addExcludedPackage(pkgName);
    
public voidaddExcludedPattern(java.lang.String pattern)

param
pattern the pattern for the names of classes to be excluded from closure computation. It is in the external format (i.e. org.apache.). When the closure compiler sees a class whose name begins with this pattern, it does not try to compute the closure of that class any more. It merely adds that class name to the closure. So the final closure will contain that class name, but not its dependencies. Among all the excluded list, it is given the lowest priority in search order.

        imp.addExcludedPattern(pattern);
    
public booleanbuildClosure(java.lang.String className)

        logger.entering(myClassName, "buildClosure", className); // NOI18N
        return imp.buildClosure(className);
    
public booleanbuildClosure(java.util.jar.JarFile jar)

param
jar whose classes it will try to build closure of. This is a convenience method which iterates over all the entries in a jar file and computes their closure.

        return imp.buildClosure(jar);
    
public java.util.CollectiongetClosure()

        return imp.getClosure();
    
public java.util.MapgetFailed()

        return imp.getFailed();
    
public java.util.CollectiongetNativeMethods()

        return imp.getNativeMethods();
    
public static voidmain(java.lang.String[] args)

        if (args.length < 2) {
            System.out.println(
                    "Usage : java " + com.sun.enterprise.tools.verifier.apiscan.classfile.ClosureCompilerImpl.class.getName() + // NOI18N
                    " <classpath> <external class name(s)>"); // NOI18N
            System.out.println("Example: to find the closure of " + // NOI18N
                    "mypkg.MySessionBean which is packaged in myejb.jar run\n" + // NOI18N
                    " java " + com.sun.enterprise.tools.verifier.apiscan.classfile.ClosureCompilerImpl.class.getName() + // NOI18N
                    " path_to_j2ee.jar"+File.pathSeparator+"path_to_myejb.jar"+ // NOI18N
                    " mypkg.MySessionBean"); // NOI18N
            System.exit(1);
        }

        String cp=args[0];
        System.out.println("Using classpath " + cp); // NOI18N
        ClassFileLoader cfl = ClassFileLoaderFactory.newInstance(
                new Object[]{cp});
        ClosureCompilerImpl closure = new ClosureCompilerImpl(cfl);
        closure.addExcludedPattern("java."); // NOI18N
        for (int i = 1; i < args.length; i++) {
            String clsName = args[i];
            System.out.println("Building closure for " + clsName); // NOI18N
            closure.reset();
            closure.buildClosure(clsName);
            System.out.println("The closure is [" + closure+"\n]"); // NOI18N
        }
    
public voidreset()
Reset the closure for next closure computation. Clear the internal cache. It includes the result it has collected since last reset(). But it does not clear the excludedd list. If you want to reset the excluded list, create a new ClosureCompiler.

        imp.reset();
    
public java.lang.StringtoString()

        return imp.toString();