1 25 26 package org.netbeans.modules.classfile; 27 28 import java.io.*; 29 import java.util.*; 30 31 40 public final class Parameter extends Field { 41 42 static Parameter[] makeParams(Method method) { 43 List<Parameter> paramList = new ArrayList<Parameter>(); 44 for (Iterator<Parameter> it = new ParamIterator(method); it.hasNext();) 45 paramList.add(it.next()); 46 return paramList.toArray(new Parameter[paramList.size()]); 47 } 48 49 private static Parameter createParameter (String name, String type, ClassFile classFile, 50 DataInputStream visibleAnnotations, DataInputStream invisibleAnnotations) { 51 return new Parameter (name, type, classFile, 52 visibleAnnotations, invisibleAnnotations); 53 } 54 55 56 private Parameter(String name, String type, ClassFile classFile, 57 DataInputStream visibleAnnotations, DataInputStream invisibleAnnotations) { 58 super(name, type, classFile); 59 loadParameterAnnotations(visibleAnnotations, invisibleAnnotations); 60 } 61 62 private void loadParameterAnnotations(DataInputStream visible, DataInputStream invisible) { 63 super.loadAnnotations(); 64 if (annotations == null && (visible != null || invisible != null)) 65 annotations = new HashMap<ClassName,Annotation>(2); 66 try { 67 if (visible != null && visible.available() > 0) 68 Annotation.load(visible, classFile.getConstantPool(), true, annotations); 69 } catch (IOException e) { 70 throw new InvalidClassFileAttributeException("invalid RuntimeVisibleParameterAnnotations attribute", e); 71 } 72 try { 73 if (invisible != null && invisible.available() > 0) 74 Annotation.load(invisible, classFile.getConstantPool(), false, annotations); 75 } catch (IOException e) { 76 throw new InvalidClassFileAttributeException("invalid RuntimeInvisibleParameterAnnotations attribute", e); 77 } 78 } 79 80 87 public final String getDeclaration() { 88 StringBuffer sb = new StringBuffer (); 89 sb.append(CPFieldMethodInfo.getSignature(getDescriptor(), false)); 90 String name = getName(); 91 if (name != null) { 92 sb.append(' '); 93 sb.append(name); 94 } 95 return sb.toString(); 96 } 97 98 public String toString() { 99 StringBuffer sb = new StringBuffer ("name="); 100 sb.append(getName()); 101 sb.append(" type="); sb.append(getDescriptor()); 103 if (getTypeSignature() != null) { 104 sb.append(", signature="); sb.append(typeSignature); 106 } 107 loadAnnotations(); 108 if (annotations.size() > 0) { 109 Iterator iter = annotations.values().iterator(); 110 sb.append(", annotations={ "); 111 while (iter.hasNext()) { 112 sb.append(iter.next().toString()); 113 if (iter.hasNext()) 114 sb.append(", "); 115 } 116 sb.append(" }"); 117 } 118 return sb.toString(); 119 } 120 121 private static class ParamIterator implements Iterator<Parameter> { 122 ClassFile classFile; 123 String signature; 124 LocalVariableTableEntry[] localVars; 125 126 127 int ivar; 128 129 130 int isig; 131 132 133 DataInputStream visibleAnnotations; 134 DataInputStream invisibleAnnotations; 135 136 139 ParamIterator(Method method) { 140 classFile = method.getClassFile(); 141 signature = method.getDescriptor(); 142 assert signature.charAt(0) == '('; 143 isig = 1; ivar = method.isStatic() ? 0 : 1; 145 Code code = method.getCode(); 146 localVars = code != null ? 147 code.getLocalVariableTable() : 148 new LocalVariableTableEntry[0]; 149 AttributeMap attrs = method.getAttributes(); 150 try { 151 visibleAnnotations = 152 getParamAttr(attrs, "RuntimeVisibleParameterAnnotations"); } catch (IOException e) { 154 throw new InvalidClassFileAttributeException("invalid RuntimeVisibleParameterAnnotations attribute", e); 155 } 156 try { 157 invisibleAnnotations = 158 getParamAttr(attrs, "RuntimeInvisibleParameterAnnotations"); } catch (IOException e) { 160 throw new InvalidClassFileAttributeException("invalid RuntimeInvisibleParameterAnnotations attribute", e); 161 } 162 } 163 164 private DataInputStream getParamAttr(AttributeMap attrs, String name) throws IOException { 165 DataInputStream in = attrs.getStream(name); 166 if (in != null) 167 in.readByte(); return in; 169 } 170 171 public boolean hasNext() { 172 return signature.charAt(isig) != ')'; 173 } 174 175 public Parameter next() { 176 if (hasNext()) { 177 String name = ""; 178 for (int i = 0; i < localVars.length; i++) { 179 LocalVariableTableEntry lvte = localVars[i]; 180 if (lvte.index == ivar && lvte.startPC == 0) { 182 name = localVars[i].getName(); 183 break; 184 } 185 } 186 ivar++; 187 int sigStart = isig; 188 while (isig < signature.length()) { 189 char ch = signature.charAt(isig); 190 switch (ch) { 191 case '[': 192 isig++; 193 break; 194 case 'B': 195 case 'C': 196 case 'F': 197 case 'I': 198 case 'S': 199 case 'Z': 200 case 'V': { 201 String type = signature.substring(sigStart, ++isig); 202 return Parameter.createParameter(name, type, classFile, 203 visibleAnnotations, invisibleAnnotations); 204 } 205 case 'D': 206 case 'J': { 207 ivar++; String type = signature.substring(sigStart, ++isig); 209 return Parameter.createParameter(name, type, classFile, 210 visibleAnnotations, invisibleAnnotations); 211 } 212 case 'L': { 213 int end = signature.indexOf(';', isig) + 1; 214 String type = signature.substring(isig, end); 215 isig = end; 216 return Parameter.createParameter(name, type, classFile, 217 visibleAnnotations, invisibleAnnotations); 218 } 219 220 } 221 } 222 } 223 throw new NoSuchElementException(); 224 } 225 226 public void remove() { 227 throw new UnsupportedOperationException (); 228 } 229 } 230 } 231 | Popular Tags |