1 23 24 25 package com.sun.enterprise.tools.verifier.apiscan.classfile; 26 27 import com.sun.corba.ee.org.objectweb.asm.Label; 28 import com.sun.corba.ee.org.objectweb.asm.Opcodes; 29 import com.sun.corba.ee.org.objectweb.asm.commons.EmptyVisitor; 30 import com.sun.corba.ee.org.objectweb.asm.util.AbstractVisitor; 31 32 import java.util.*; 33 import java.util.logging.Level ; 34 import java.util.logging.Logger ; 35 import java.lang.ref.SoftReference ; 36 37 40 class ASMMethod extends EmptyVisitor implements Method { 41 42 private static String resourceBundleName = "com.sun.enterprise.tools.verifier.apiscan.LocalStrings"; 43 private static Logger logger = Logger.getLogger("apiscan.classfile", resourceBundleName); 45 private SoftReference <ClassFile> owningClass; 46 47 private String descriptor; 48 49 private int access; 50 51 private String signature; 52 53 private String [] exceptions; 54 55 private String name; 56 57 private Set<MethodRef> referencedMethods = new HashSet<MethodRef>(); 58 59 private Set<String > referencedClasses = new HashSet<String >(); 60 61 private MethodRef methodRef; 63 64 public ASMMethod( 65 ClassFile owningClass, String name, String descriptor, int access, 66 String signature, String [] exceptions) { 67 this.owningClass = new SoftReference <ClassFile>(owningClass); 68 this.name = name; 69 this.descriptor = descriptor; 70 this.access = access; 71 this.signature = signature; 72 if(exceptions==null) { 73 this.exceptions = new String [0]; 74 } else { 75 this.exceptions = exceptions; 76 } 77 } 78 79 public ClassFile getOwningClass() { 80 return owningClass.get(); 81 } 82 83 public String getName() { 84 return name; 85 } 86 87 public String getDescriptor() { 88 return descriptor; 89 } 90 91 public Collection<MethodRef> getReferencedMethods() { 92 return Collections.unmodifiableCollection(referencedMethods); 93 } 94 95 public Collection<String > getReferencedClasses() { 96 return Collections.unmodifiableCollection(referencedClasses); 97 } 98 99 public MethodRef getSelfReference() { 101 if(methodRef==null){ 102 methodRef = new MethodRef(owningClass.get().getInternalName(), name, descriptor); 103 } 104 return methodRef; 105 } 106 107 public boolean isNative() { 108 return (access & Opcodes.ACC_NATIVE) == Opcodes.ACC_NATIVE; 109 } 110 111 @Override public void visitFieldInsn( 112 int opcode, String owner, String name, String desc) { 113 logger.entering( 114 "com.sun.enterprise.tools.verifier.apiscan.classfile.ASMMethod", "visitFieldInsn", new Object []{AbstractVisitor.OPCODES[opcode], owner, name, desc}); 116 addClass(owner); 117 } 118 119 @Override public void visitTryCatchBlock( 120 Label start, Label end, Label handler, String type) { 121 logger.entering( 122 "com.sun.enterprise.tools.verifier.apiscan.classfile.ASMMethod", "visitTryCatchBlock", new Object []{type}); 124 if(type!=null) { addClass(type); 126 } 127 } 128 129 public void visitMethodInsn( 130 int opcode, String owner, String name, String desc) { 131 logger.entering( 132 "com.sun.enterprise.tools.verifier.apiscan.classfile.ASMMethod", "visitMethodInsn", new Object []{ AbstractVisitor.OPCODES[opcode], owner, name, desc}); 134 addMethod(owner, name, desc); 135 } 136 137 @Override public void visitTypeInsn(int opcode, String desc) { 139 logger.entering( 140 "com.sun.enterprise.tools.verifier.apiscan.classfile.ASMMethod", "visitTypeInsn", new Object []{ AbstractVisitor.OPCODES[opcode], desc}); 142 switch (opcode) { 143 case Opcodes.INSTANCEOF: 144 case Opcodes.CHECKCAST: 145 case Opcodes.ANEWARRAY: 146 addClass(desc); 147 break; 148 case Opcodes.NEW: 149 break; 151 case Opcodes.NEWARRAY: 152 break; 154 default: 155 logger.logp(Level.WARNING, "com.sun.enterprise.tools.verifier.apiscan.classfile.ASMMethod", "visitTypeInsn", getClass().getName() + ".warning1", AbstractVisitor.OPCODES[opcode]); break; 158 } 159 } 160 161 @Override public void visitMultiANewArrayInsn(String desc, int dims) { 162 logger.entering( 163 "com.sun.enterprise.tools.verifier.apiscan.classfile.ASMMethod", "visitMultiANewArrayInsn", new Object []{desc}); 165 addClass(desc); 166 } 167 168 @Override public String toString() { 169 StringBuilder sb = new StringBuilder ( 170 decodeAccessFlag(access) + 171 name + 172 " " + descriptor+ 174 "{\n"); for(MethodRef mr : referencedMethods){ 176 sb.append(mr).append("\n"); } 178 sb.append("}"); return sb.toString(); 180 } 181 182 public int getAccess() { 183 return access; 184 } 185 186 public String getSignature() { 187 return signature; 188 } 189 190 193 public String [] getExceptions() { 194 return exceptions; 195 } 196 197 private void addClass(String nameOrTypeDescriptor) { 198 if (nameOrTypeDescriptor.indexOf(';') != -1 || 201 nameOrTypeDescriptor.indexOf('[') != -1) { 202 referencedClasses.addAll( 203 typeDescriptorToClassNames(nameOrTypeDescriptor)); 204 } else { 205 referencedClasses.add(nameOrTypeDescriptor); 206 } 207 } 208 209 private void addMethod(String owner, String name, String desc) { 210 addClass(owner); 211 referencedMethods.add(new MethodRef(owner, name, desc)); 217 } 218 219 public static String decodeAccessFlag(int access) { 220 StringBuilder result = new StringBuilder (""); 221 if ((access & Opcodes.ACC_PRIVATE) == Opcodes.ACC_PRIVATE) { 222 result.append("private "); } else if ((access & Opcodes.ACC_PROTECTED) == Opcodes.ACC_PROTECTED) { 224 result.append("protected "); } else if ((access & Opcodes.ACC_PUBLIC) == Opcodes.ACC_PUBLIC) { 226 result.append("public "); } 228 if ((access & Opcodes.ACC_ABSTRACT) == Opcodes.ACC_ABSTRACT) { 229 result.append("abstract "); } else if ((access & Opcodes.ACC_FINAL) == Opcodes.ACC_FINAL) { 231 result.append("final "); } else if ((access & Opcodes.ACC_STATIC) == Opcodes.ACC_STATIC) { 233 result.append("static "); } 235 if ((access & Opcodes.ACC_NATIVE) == Opcodes.ACC_NATIVE) { 236 result.append("native "); } 238 return result.toString(); 239 } 240 241 private static List<String > typeDescriptorToClassNames(String signature) { 242 logger.entering( 243 "com.sun.enterprise.tools.verifier.apiscan.classfile.ASMMethod", "typeDescriptorToClassNames", new Object []{signature}); 245 List<String > result = new ArrayList<String >(); 246 int i = 0; 247 while ((i = signature.indexOf('L', i)) != -1) { 248 int j = signature.indexOf(';', i); 249 if (j > i) { 250 String className = signature.substring(i + 1, j); 252 if (!Util.isPrimitive(className)) result.add(className); 253 i = j + 1; 254 } else 255 break; 256 } 257 if (logger.isLoggable(Level.FINE)) { 258 StringBuffer sb = new StringBuffer ("Class Names are {"); int size = result.size(); 260 for (int k = 0; k < size; k++) { 261 sb.append((String ) result.get(k)); 262 if (k != size - 1) sb.append(", "); } 264 sb.append("}"); logger.finer(sb.toString()); 266 } 267 return result; 268 } 269 270 } 271 | Popular Tags |