KickJava   Java API By Example, From Geeks To Geeks.

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


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  * BCELClassFileLoader.java
26  *
27  * Created on August 17, 2004, 9:28 AM
28  */

29
30 package com.sun.enterprise.tools.verifier.apiscan.classfile;
31
32 import java.io.IOException JavaDoc;
33 import java.io.InputStream JavaDoc;
34 import java.util.logging.Logger JavaDoc;
35
36 import com.sun.org.apache.bcel.internal.util.ClassPath;
37
38 /**
39  * Yet another factory for {@link BCELClassFile}. This is not a public class, as
40  * I expect users to use {@link ClassFileLoaderFactory} interface. It differs
41  * from {@link BCELClassFileLoader} in the sense that it loads classfiles using
42  * bcel ClassPath class. Known Issues: Currently it ignores any INDEX
43  * information if available in a Jar file. This is because BCEL provided class
44  * ClassPath does not understand INDEX info. We should add this feature in
45  * future.
46  *
47  * @author Sanjeeb.Sahoo@Sun.COM
48  */

49 class BCELClassFileLoader1 implements ClassFileLoader {
50
51     private static String JavaDoc resourceBundleName = "com.sun.enterprise.tools.verifier.apiscan.LocalStrings";
52     private static Logger JavaDoc logger = Logger.getLogger("apiscan.classfile", resourceBundleName); // NOI18N
53
private ClassPath cp;
54
55     /**
56      * Creates a new instance of BCELClassFileLoader User should use {link
57      * ClassFileLoaderFactory} to create new instance of a loader.
58      *
59      * @param classPath represents the search path that is used by this loader.
60      * Please note that, it does not read the manifest entries
61      * for any jar file specified in the classpath, so if the
62      * jar files have optional package dependency, that must be
63      * taken care of in the classpath by ther caller.
64      */

65     public BCELClassFileLoader1(String JavaDoc classPath) {
66         logger.entering("BCELClassFileLoader1", "<init>(String)", classPath); // NOI18N
67
cp = new ClassPath(classPath);
68     }
69
70     //See ClassFileLoader for description of this method.
71
public ClassFile load(String JavaDoc externalClassName) throws IOException JavaDoc {
72         logger.entering("BCELClassFileLoader1", "load", externalClassName); // NOI18N
73
//BCEL library expects me to pass in internal form.
74
String JavaDoc internalClassName = externalClassName.replace('.', '/');
75         //don't call getInputStream as it first tries to load using the
76
// getClass().getClassLoader().getResourceAsStream()
77
//return cp.getInputStream(extClassName);
78
InputStream JavaDoc is = cp.getClassFile(internalClassName, ".class") // NOI18N
79
.getInputStream();
80         try {
81             ClassFile cf = new BCELClassFile(is, internalClassName + ".class"); // NOI18N
82
matchClassSignature(cf, externalClassName);
83             return cf;
84         } finally {
85             is.close();
86         }
87     }
88
89     //This method is neede to be protected against users who are passing us
90
//internal class names instead of external class names or
91
//when the file actually represents some other class, but it isnot
92
//available in in proper package hierarchy.
93
private void matchClassSignature(ClassFile cf, String JavaDoc externalClassName)
94             throws IOException JavaDoc {
95         String JavaDoc nameOfLoadedClass = cf.getName();
96         if (!nameOfLoadedClass.equals(externalClassName)) {
97             throw new IOException JavaDoc(
98                     externalClassName + ".class represents " +
99                     cf.getName() +
100                     ". Perhaps your package name is incorrect or you passed the" +
101                     " name using internal form instead of using external form.");
102         }
103     }
104 }
105
Popular Tags