KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > apiscan > classfile > ClosureCompilerImplBase


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24
25 package com.sun.enterprise.tools.verifier.apiscan.classfile;
26
27 import java.io.IOException JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.logging.Level JavaDoc;
32 import java.util.logging.Logger JavaDoc;
33
34 /**
35  * This is the base class for classes that actually implement ClosureCompiler
36  * interface, i.e. {@link BCELClosureCompilerImpl} and
37  * {@link ASMClosureCompilerImpl}.
38  * It contains common implementation for above classes.
39  *
40  * @author Sanjeeb.Sahoo@Sun.COM
41  */

42 public abstract class ClosureCompilerImplBase implements ClosureCompiler {
43
44     protected ClassFileLoader loader;
45
46     protected HashSet JavaDoc<String JavaDoc> excludedClasses = new HashSet JavaDoc<String JavaDoc>();
47
48     protected HashSet JavaDoc<String JavaDoc> excludedPackages = new HashSet JavaDoc<String JavaDoc>();
49
50     protected HashSet JavaDoc<String JavaDoc> excludedPatterns = new HashSet JavaDoc<String JavaDoc>();
51
52     protected HashSet JavaDoc<String JavaDoc> visitedClasses = new HashSet JavaDoc<String JavaDoc>();
53
54     private static String JavaDoc resourceBundleName = "com.sun.enterprise.tools.verifier.apiscan.LocalStrings";
55     protected static Logger JavaDoc logger = Logger.getLogger("apiscan.classfile", resourceBundleName); // NOI18N
56

57     // used for logging
58
private static final String JavaDoc myClassName = "ClosureCompilerImplBase"; // NOI18N
59

60     /**
61      * @param loader the ClassFileLoader that is used to load the referenced
62      * classes.
63      */

64     protected ClosureCompilerImplBase(ClassFileLoader loader) {
65         this.loader = loader;
66     }
67
68     /**
69      * @param className the class name to be excluded from closure
70      * computation. It is in the external class name format
71      * (i.e. java.util.Map$Entry instead of java.util.Map.Entry).
72      * When the closure compiler sees a class matches this
73      * name, it does not try to compute its closure any
74      * more. It merely adds this name to the closure. So the
75      * final closure will contain this class name, but not
76      * its dependencies.
77      */

78     public void addExcludedClass(String JavaDoc className) {
79         excludedClasses.add(className);
80     }
81
82     /**
83      * @param pkgName the package name of classes to be excluded from
84      * closure computation. It is in the external format
85      * (i.e. java.lang (See no trailing '.'). When the
86      * closure compiler sees a class whose package name
87      * matches this name, it does not try to compute the
88      * closure of that class any more. It merely adds that
89      * class name to the closure. So the final closure will
90      * contain that class name, but not its dependencies.
91      */

92     public void addExcludedPackage(String JavaDoc pkgName) {
93         excludedPackages.add(pkgName);
94     }
95
96     /**
97      * @param pattern the pattern for the names of classes to be excluded from
98      * closure computation. It is in the external format (i.e.
99      * org.apache.). When the closure compiler sees a class whose
100      * name begins with this pattern, it does not try to compute
101      * the closure of that class any more. It merely adds that
102      * class name to the closure. So the final closure will
103      * contain that class name, but not its dependencies. Among
104      * all the excluded list, it is given the lowest priority in
105      * search order.
106      */

107     public void addExcludedPattern(String JavaDoc pattern) {
108         excludedPatterns.add(pattern);
109     }
110
111     /**
112      * @param jar whose classes it will try to build closure of. This is a
113      * convenience method which iterates over all the entries in a
114      * jar file and computes their closure.
115      */

116     public boolean buildClosure(java.util.jar.JarFile JavaDoc jar) throws IOException JavaDoc {
117         boolean result = true;
118         for (java.util.Enumeration JavaDoc entries = jar.entries();
119              entries.hasMoreElements();) {
120             String JavaDoc clsName = ((java.util.jar.JarEntry JavaDoc) entries.nextElement()).getName();
121             if (clsName.endsWith(".class")) { // NOI18N
122
String JavaDoc externalClsName = clsName.substring(0,
123                         clsName.lastIndexOf(".class")) // NOI18N
124
.replace('/', '.');
125                 boolean newresult = this.buildClosure(externalClsName);
126                 result = newresult && result;
127             }
128         }//for all jar entries
129
return result;
130     }
131
132     public Collection JavaDoc<String JavaDoc> getNativeMethods() {
133         throw new UnsupportedOperationException JavaDoc();
134     }
135
136     /**
137      * @param className name of class in external format.
138      * @return
139      */

140     protected boolean needToBuildClosure(String JavaDoc className) {
141         boolean result = true;
142         if (visitedClasses.contains(className))
143             result = false;
144         else if (excludedClasses.contains(className)) {
145             result = false;
146         } else if (excludedPackages.contains(getPackageName(className))) {
147             result = false;
148         } else {
149             for (Iterator JavaDoc i = excludedPatterns.iterator(); i.hasNext();) {
150                 String JavaDoc pattern = (String JavaDoc) i.next();
151                 if (className.startsWith(pattern)) {
152                     result = false;
153                     break;
154                 }
155             }
156         }
157         logger.logp(Level.FINEST, myClassName, "needToBuildClosure", // NOI18N
158
className + " " + result); // NOI18N
159
return result;
160     }
161
162     /**
163      * @param className name of class in external format.
164      * @return package name in dotted format, e.g. java.lang for java.lang.void
165      */

166     protected static String JavaDoc getPackageName(String JavaDoc className) {
167         int idx = className.lastIndexOf('.');
168         if (idx != -1) {
169             return className.substring(0, idx);
170         } else
171             return "";
172     }
173
174 }
175
Popular Tags