1 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 ; 33 import java.io.InputStream ; 34 import java.util.Collection ; 35 import java.util.HashSet ; 36 import java.util.Set ; 37 import java.util.logging.Logger ; 38 39 42 class ASMClassFile implements ClassFile { 43 44 private static Logger logger = Logger.getLogger("apiscan.classfile"); 46 private String externalName; 47 48 private String internalName; 49 50 private String packageName; 51 52 private ClassReader cr; 53 54 private Set <Method> methods = new HashSet <Method>(); 55 56 private String internalNameOfSuperClass; 57 58 private String [] 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 <Method> getMethods() { 77 return methods; 78 } 79 80 public ASMClassFile(InputStream is) 81 throws IOException { 82 cr = new ClassReader(is); 83 cr.accept(new MyVisitor(this), true); 84 is.close(); 85 } 86 87 public Collection <String > getAllReferencedClassNames() { 88 throw new UnsupportedOperationException (); 89 } 90 91 public Collection getAllReferencedClassNamesInInternalForm() { 92 throw new UnsupportedOperationException (); 93 } 94 95 public String getName() { 96 return externalName; 97 } 98 99 public String getInternalName() { 100 return internalName; 101 } 102 103 public String getPackageName() { 104 return packageName; 105 } 106 107 public String getNameOfSuperClass() { 108 return Util.convertToExternalClassName(internalNameOfSuperClass); 109 } 110 111 public String getInternalNameOfSuperClass() { 112 return internalNameOfSuperClass; 113 } 114 115 public String [] getNamesOfInterfaces() { 116 String [] result = new String [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 [] getInternalNamesOfInterfaces() { 124 return internalNamesOfInterfaces; 125 } 126 127 @Override public String toString() { 128 StringBuilder sb = new StringBuilder (decodeAccessFlag(access)+ externalName); 129 if(internalNameOfSuperClass!=null) sb.append(" extends "+getNameOfSuperClass()); if(internalNamesOfInterfaces.length>0){ 131 sb.append(" implements "); for(String s : getNamesOfInterfaces()) sb.append(s); 133 } 134 sb.append("{\n"); for(Method m : methods){ 136 sb.append(m).append("\n"); } 138 sb.append("}"); 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 public void visit( 150 int version, int access, String name, String signature, 151 String superName, String [] interfaces) { 152 logger.entering( 153 "com.sun.enterprise.tools.verifier.apiscan.classfile.ASMClassFile$MyVisitor", "visit", new Object []{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 public MethodVisitor visitMethod( 168 int access, String name, String desc, String signature, 169 String [] exceptions) { 170 logger.entering( 171 "com.sun.enterprise.tools.verifier.apiscan.classfile.ASMClassFile$MyVisitor", "visitMethod", new Object []{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 public void visitEnd() { 180 logger.entering("ASMClassFile$MyVisitor", "visitEnd", new Object []{cf.getName()}); 182 } 183 } 184 185 public static String decodeAccessFlag(int access) { 186 StringBuilder result = new StringBuilder (""); 187 if ((access & ACC_PRIVATE) == ACC_PRIVATE) { 188 result.append("private "); } else if ((access & ACC_PROTECTED) == ACC_PROTECTED) { 190 result.append("protected "); } else if ((access & ACC_PUBLIC) == ACC_PUBLIC) { 192 result.append("public "); } 194 if ((access & ACC_ABSTRACT) == ACC_ABSTRACT) { 195 result.append("abstract "); } else if ((access & ACC_FINAL) == ACC_FINAL) { 197 result.append("final "); } 199 if ((access & ACC_INTERFACE) == ACC_INTERFACE) { 200 result.append("interface "); } else { 202 result.append("class "); } 204 return result.toString(); 205 } 206 207 } | Popular Tags |