1 25 26 package org.netbeans.modules.classfile; 27 28 import java.io.DataInputStream ; 29 import java.io.IOException ; 30 import java.util.List ; 31 import java.util.Arrays ; 32 33 38 public final class Method extends Field { 39 40 private Code code; 41 private CPClassInfo[] exceptions; 42 private Parameter[] parameters; 43 private ElementValue annotationDefault; 44 45 46 private static final ElementValue notloadedAnnotationDefault = new ElementValue() {}; 47 48 static Method[] loadMethods(DataInputStream in, ConstantPool pool, 49 ClassFile cls, boolean includeCode) 50 throws IOException { 51 int count = in.readUnsignedShort(); 52 Method[] methods = new Method[count]; 53 for (int i = 0; i < count; i++) 54 methods[i] = new Method(in, pool, cls, includeCode); 55 return methods; 56 } 57 58 59 Method(DataInputStream in, ConstantPool pool, ClassFile cls, 60 boolean includeCode) throws IOException { 61 super(in, pool, cls, includeCode); 62 annotationDefault = notloadedAnnotationDefault; 63 } 64 65 72 public final Code getCode() { 73 if (code == null) { 74 DataInputStream in = attributes.getStream("Code"); if (in != null) { 76 try { 77 code = new Code(in, classFile.constantPool); 78 in.close(); 79 } catch (IOException e) { 80 throw new InvalidClassFileAttributeException("invalid Code attribute", e); 81 } 82 } 83 } 84 return code; } 86 87 public final CPClassInfo[] getExceptionClasses() { 88 if (exceptions == null) { 89 DataInputStream in = attributes.getStream("Exceptions"); if (in != null) { 91 try { 92 exceptions = 93 ClassFile.getCPClassList(in, classFile.constantPool); 94 in.close(); 95 } catch (IOException e) { 96 throw new InvalidClassFileAttributeException("invalid Exceptions attribute", e); 97 } 98 } 99 if (exceptions == null) 100 exceptions = new CPClassInfo[0]; 101 } 102 return exceptions.clone(); 103 } 104 105 109 public final boolean isBridge() { 110 return (access & Access.BRIDGE) == Access.BRIDGE; 111 } 112 113 117 public final boolean isVarArgs() { 118 return (access & Access.VARARGS) == Access.VARARGS; 119 } 120 121 124 public final boolean isSynchronized() { 125 return (access & Access.SYNCHRONIZED) == Access.SYNCHRONIZED; 126 } 127 128 131 public final boolean isNative() { 132 return (access & Access.NATIVE) == Access.NATIVE; 133 } 134 135 138 public final boolean isAbstract() { 139 return (access & Access.ABSTRACT) == Access.ABSTRACT; 140 } 141 142 145 public final List <Parameter> getParameters() { 146 if (parameters == null) 147 parameters = Parameter.makeParams(this); 148 return Arrays.asList(parameters); 149 } 150 151 155 public final String getReturnType() { 156 String desc = getDescriptor(); 157 int i = desc.indexOf(')') + 1; 158 return desc.substring(i); 159 } 160 161 165 public final String getReturnSignature() { 166 String type = getReturnType(); 167 return CPFieldMethodInfo.getSignature(type, true); 168 } 169 170 176 public ElementValue getAnnotationDefault() { 177 if (annotationDefault == notloadedAnnotationDefault) { 178 annotationDefault = null; 179 DataInputStream in = 180 attributes.getStream("AnnotationDefault"); if (in != null) { 182 try { 183 annotationDefault = 184 ElementValue.load(in, classFile.constantPool, false); 185 in.close(); 186 } catch (IOException e) { 187 throw new InvalidClassFileAttributeException("invalid AnnotationDefault attribute", e); 188 } 189 } 190 } 191 return annotationDefault; 192 } 193 194 public String toString() { 195 StringBuffer sb = new StringBuffer (super.toString()); 196 sb.append(", params ("); 197 getParameters(); 198 for (int i = 0; i < parameters.length; i++) { 199 sb.append(parameters[i].toString()); 200 if (i+1 < parameters.length) 201 sb.append(", "); 202 } 203 sb.append("), returns "); 204 sb.append(getReturnSignature()); 205 CPClassInfo[] ec = getExceptionClasses(); 206 if (ec.length > 0) { 207 sb.append(", throws"); for (int i = 0; i < ec.length; i++) { 209 sb.append(' '); sb.append(ec[i].getName()); 211 } 212 } 213 if (getAnnotationDefault() != null) { 214 sb.append(", default \""); 215 sb.append(annotationDefault.toString()); 216 sb.append("\" "); 217 } 218 Code code = getCode(); 219 if (code != null) { 220 sb.append(' '); 221 sb.append(code.toString()); 222 } 223 return sb.toString(); 224 } 225 226 public final String getDeclaration() { 227 return CPMethodInfo.getFullMethodName(getName(), getDescriptor()); 228 } 229 } 230 | Popular Tags |