1 23 24 29 30 package com.sun.enterprise.tools.verifier.apiscan.classfile; 31 32 import java.io.IOException ; 33 import java.io.InputStream ; 34 import java.util.ArrayList ; 35 import java.util.Collection ; 36 import java.util.Collections ; 37 import java.util.HashSet ; 38 import java.util.Iterator ; 39 import java.util.List ; 40 import java.util.Set ; 41 import java.util.logging.Level ; 42 import java.util.logging.Logger ; 43 44 import com.sun.org.apache.bcel.internal.classfile.ClassParser; 45 import com.sun.org.apache.bcel.internal.classfile.ConstantClass; 46 import com.sun.org.apache.bcel.internal.classfile.DescendingVisitor; 47 import com.sun.org.apache.bcel.internal.classfile.EmptyVisitor; 48 import com.sun.org.apache.bcel.internal.classfile.Field; 49 import com.sun.org.apache.bcel.internal.classfile.JavaClass; 50 import com.sun.org.apache.bcel.internal.classfile.Method; 51 52 61 class BCELClassFile implements ClassFile { 62 63 private JavaClass jc; 64 private Set <String > classNames; 65 private HashSet <BCELMethod> methods = new HashSet <BCELMethod>(); 66 private static Logger logger = Logger.getLogger("apiscan.classfile"); private static final String myClassName = "apiscan.classfile.BCELClassFile"; 70 87 public BCELClassFile(InputStream is, String file_name) throws IOException { 88 logger.entering(myClassName, "<init>(InputStream, String)", file_name); jc = new ClassParser(is, file_name).parse(); 90 } 91 92 97 public BCELClassFile(String file_path) throws IOException { 98 logger.entering(myClassName, "<init>(String)", file_path); jc = new ClassParser(file_path).parse(); 100 } 101 102 103 104 public synchronized Collection getAllReferencedClassNamesInInternalForm() { 106 if (classNames == null) { 107 classNames = new HashSet <String >(); logger.logp(Level.FINER, myClassName, "getAllReferencedClassNames", "Starting to visit"); jc.accept(new DescendingVisitor(jc, new Visitor(this))); 111 logger.logp(Level.FINER, myClassName, "getAllReferencedClassNames", "Finished visting"); classNames = Collections.unmodifiableSet(classNames); 114 } 115 return classNames; 116 } 117 118 public synchronized Collection <String > getAllReferencedClassNames() { 119 if (classNames == null) { 120 getAllReferencedClassNamesInInternalForm(); 121 } 122 HashSet <String > extClassNames = new HashSet <String >(classNames.size()); 123 for (Iterator i = classNames.iterator(); i.hasNext();) { 124 extClassNames.add(Util.convertToExternalClassName((String ) i.next())); 125 } 126 return extClassNames; 127 } 128 129 public String getName() { 136 return jc.getClassName(); 137 } 138 139 public String getInternalName() { 141 return Util.convertToInternalClassName(getName()); 142 } 143 144 public String getPackageName() { 146 return jc.getPackageName(); 148 } 149 150 public Collection <? extends com.sun.enterprise.tools.verifier.apiscan.classfile.Method> 151 getMethods() { 152 return Collections.unmodifiableSet(methods); 153 } 154 155 public com.sun.enterprise.tools.verifier.apiscan.classfile.Method 156 getMethod(MethodRef methodRef) { 157 throw new UnsupportedOperationException (); 158 } 159 160 public String getNameOfSuperClass() { 161 return jc.getSuperclassName(); 162 } 163 164 public String getInternalNameOfSuperClass() { 165 return Util.convertToInternalClassName(getNameOfSuperClass()); 166 } 167 168 public String [] getNamesOfInterfaces() { 169 return jc.getInterfaceNames(); 170 } 171 172 public String [] getInternalNamesOfInterfaces() { 173 String [] result = getNamesOfInterfaces(); 174 for(int i = 0; i< result.length; ++i) { 175 result[i] = Util.convertToInternalClassName(result[i]); 176 } 177 return result; 178 } 179 180 public boolean isInterface() { 181 return !jc.isClass(); 182 } 183 184 public String toString() { 185 return 186 "External Name: " + getName() + "\n" + "Internal Name: " + getInternalName() + "\n" + jc.toString() 189 + "\n------------CONSTANT POOL BEGIN--------------\n" + jc.getConstantPool() 191 + "\n------------CONSTANT POOL END--------------"; } 193 194 private static List <String > signatureToClassNames(String signature) { 206 logger.entering(myClassName, "signatureToClassNames", signature); List <String > result = new ArrayList <String >(); 208 int i = 0; 209 while ((i = signature.indexOf('L', i)) != -1) { 210 int j = signature.indexOf(';', i); 211 if (j > i) { 212 String className = signature.substring(i + 1, j); 214 if (!Util.isPrimitive(className)) result.add(className); 215 i = j + 1; 216 } else 217 break; 218 } 219 if (logger.isLoggable(Level.FINE)) { 220 StringBuffer sb = new StringBuffer ("Class Names are {"); int size = result.size(); 222 for (int k = 0; k < size; k++) { 223 sb.append((String ) result.get(k)); 224 if (k != size - 1) sb.append(", "); } 226 sb.append("}"); logger.finer(sb.toString()); 228 } 229 return result; 230 } 231 232 private class Visitor extends EmptyVisitor { 234 BCELClassFile cf; 235 public Visitor(BCELClassFile cf) { 236 this.cf = cf; 237 } 238 239 240 242 public void visitConstantClass(ConstantClass obj) { 243 logger.entering(myClassName, "visitConstantClass", obj); String className = obj.getBytes(jc.getConstantPool()); 245 logger.finer("Class name is " + className); if (className.indexOf(';') != -1 || className.indexOf('[') != -1) { 250 classNames.addAll(signatureToClassNames(className)); 251 } else { 252 classNames.add(className); 253 } 254 } 255 256 public void visitField(Field field) { 257 logger.entering(myClassName, "visitField", field); String signature = field.getSignature(); 259 logger.finer("Signature is " + signature); classNames.addAll(signatureToClassNames(signature)); 263 } 264 265 public synchronized void visitMethod(Method method) { 266 logger.entering(myClassName, "visitMethod", method); String signature = method.getSignature(); 268 logger.finer("Signature is " + signature); methods.add(new BCELMethod(cf, method)); 270 classNames.addAll(signatureToClassNames(signature)); 271 } 272 }} 274 | Popular Tags |