KickJava   Java API By Example, From Geeks To Geeks.

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


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 static com.sun.corba.ee.org.objectweb.asm.Opcodes.*;
28 import com.sun.corba.ee.org.objectweb.asm.ClassReader;
29 import com.sun.corba.ee.org.objectweb.asm.MethodVisitor;
30 import com.sun.corba.ee.org.objectweb.asm.commons.EmptyVisitor;
31
32 import java.io.IOException JavaDoc;
33 import java.io.InputStream JavaDoc;
34 import java.util.Collection JavaDoc;
35 import java.util.HashSet JavaDoc;
36 import java.util.Set JavaDoc;
37 import java.util.logging.Logger JavaDoc;
38
39 /**
40  * @author Sanjeeb.Sahoo@Sun.COM
41  */

42 class ASMClassFile implements ClassFile {
43
44     private static Logger JavaDoc logger = Logger.getLogger("apiscan.classfile"); // NOI18N
45

46     private String JavaDoc externalName;
47
48     private String JavaDoc internalName;
49
50     private String JavaDoc packageName;
51
52     private ClassReader cr;
53
54     private Set JavaDoc<Method> methods = new HashSet JavaDoc<Method>();
55
56     private String JavaDoc internalNameOfSuperClass;
57
58     private String JavaDoc[] internalNamesOfInterfaces;
59
60     private int access;
61
62     public boolean isInterface() {
63         return (access & ACC_INTERFACE) == ACC_INTERFACE;
64     }
65
66     public Method getMethod(MethodRef methodRef) {
67         for (Method m : methods) {
68             if (m.getName().equals(methodRef.getName()) &&
69                     m.getDescriptor().equals(methodRef.getDescriptor())) {
70                 return m;
71             }
72         }
73         return null;
74     }
75
76     public Collection JavaDoc<Method> getMethods() {
77         return methods;
78     }
79
80     public ASMClassFile(InputStream JavaDoc is)
81             throws IOException JavaDoc {
82         cr = new ClassReader(is);
83         cr.accept(new MyVisitor(this), true);
84         is.close();
85     }
86
87     public Collection JavaDoc<String JavaDoc> getAllReferencedClassNames() {
88         throw new UnsupportedOperationException JavaDoc();
89     }
90
91     public Collection JavaDoc getAllReferencedClassNamesInInternalForm() {
92         throw new UnsupportedOperationException JavaDoc();
93     }
94
95    public String JavaDoc getName() {
96         return externalName;
97     }
98
99     public String JavaDoc getInternalName() {
100         return internalName;
101     }
102
103     public String JavaDoc getPackageName() {
104         return packageName;
105     }
106
107     public String JavaDoc getNameOfSuperClass() {
108         return Util.convertToExternalClassName(internalNameOfSuperClass);
109     }
110
111     public String JavaDoc getInternalNameOfSuperClass() {
112         return internalNameOfSuperClass;
113     }
114
115     public String JavaDoc[] getNamesOfInterfaces() {
116         String JavaDoc[] result = new String JavaDoc[internalNamesOfInterfaces.length];
117         for(int i = 0; i< internalNamesOfInterfaces.length; ++i){
118             result[i] = Util.convertToExternalClassName(internalNamesOfInterfaces[i]);
119         }
120         return result;
121     }
122
123     public String JavaDoc[] getInternalNamesOfInterfaces() {
124         return internalNamesOfInterfaces;
125     }
126
127     @Override JavaDoc public String JavaDoc toString() {
128         StringBuilder JavaDoc sb = new StringBuilder JavaDoc(decodeAccessFlag(access)+ externalName);
129         if(internalNameOfSuperClass!=null) sb.append(" extends "+getNameOfSuperClass()); // NOI18N
130
if(internalNamesOfInterfaces.length>0){
131             sb.append(" implements "); // NOI18N
132
for(String JavaDoc s : getNamesOfInterfaces()) sb.append(s);
133         }
134         sb.append("{\n"); // NOI18N
135
for(Method m : methods){
136             sb.append(m).append("\n"); // NOI18N
137
}
138         sb.append("}"); // NOI18N
139
return sb.toString();
140     }
141
142     private static class MyVisitor extends EmptyVisitor {
143         ASMClassFile cf;
144
145         public MyVisitor(ASMClassFile cf) {
146             this.cf = cf;
147         }
148
149         @Override JavaDoc public void visit(
150                 int version, int access, String JavaDoc name, String JavaDoc signature,
151                 String JavaDoc superName, String JavaDoc[] interfaces) {
152             logger.entering(
153                     "com.sun.enterprise.tools.verifier.apiscan.classfile.ASMClassFile$MyVisitor", "visit", // NOI18N
154
new Object JavaDoc[]{version, access, name, signature});
155             cf.internalName = name;
156             cf.externalName = Util.convertToExternalClassName(name);
157             cf.internalNameOfSuperClass = superName;
158             cf.internalNamesOfInterfaces = interfaces;
159             int index = name.lastIndexOf('/');
160             if (index < 0)
161                 cf.packageName = "";
162             else
163                 cf.packageName = name.substring(0, index);
164             cf.access = access;
165         }
166
167         @Override JavaDoc public MethodVisitor visitMethod(
168                 int access, String JavaDoc name, String JavaDoc desc, String JavaDoc signature,
169                 String JavaDoc[] exceptions) {
170             logger.entering(
171                     "com.sun.enterprise.tools.verifier.apiscan.classfile.ASMClassFile$MyVisitor", "visitMethod", // NOI18N
172
new Object JavaDoc[]{access, name, signature, desc});
173             ASMMethod method = new ASMMethod(cf, name, desc, access,
174                     signature, exceptions);
175             cf.methods.add(method);
176             return method;
177         }
178
179         @Override JavaDoc public void visitEnd() {
180             logger.entering("ASMClassFile$MyVisitor", "visitEnd", // NOI18N
181
new Object JavaDoc[]{cf.getName()});
182         }
183     }
184
185     public static String JavaDoc decodeAccessFlag(int access) {
186         StringBuilder JavaDoc result = new StringBuilder JavaDoc("");
187         if ((access & ACC_PRIVATE) == ACC_PRIVATE) {
188             result.append("private "); // NOI18N
189
} else if ((access & ACC_PROTECTED) == ACC_PROTECTED) {
190             result.append("protected "); // NOI18N
191
} else if ((access & ACC_PUBLIC) == ACC_PUBLIC) {
192             result.append("public "); // NOI18N
193
}
194         if ((access & ACC_ABSTRACT) == ACC_ABSTRACT) {
195             result.append("abstract "); // NOI18N
196
} else if ((access & ACC_FINAL) == ACC_FINAL) {
197             result.append("final "); // NOI18N
198
}
199         if ((access & ACC_INTERFACE) == ACC_INTERFACE) {
200             result.append("interface "); // NOI18N
201
} else {
202             result.append("class "); // NOI18N
203
}
204         return result.toString();
205     }
206
207 }
Popular Tags