1 11 package org.eclipse.jdt.internal.core.util; 12 13 import java.util.ArrayList ; 14 15 import org.eclipse.jdt.core.Signature; 16 import org.eclipse.jdt.core.compiler.CharOperation; 17 import org.eclipse.jdt.internal.compiler.ast.Wildcard; 18 import org.eclipse.jdt.internal.compiler.codegen.ConstantPool; 19 20 23 public class KeyToSignature extends BindingKeyParser { 24 25 public static final int SIGNATURE = 0; 26 public static final int TYPE_ARGUMENTS = 1; 27 public static final int DECLARING_TYPE = 2; 28 public static final int THROWN_EXCEPTIONS = 3; 29 30 public StringBuffer signature = new StringBuffer (); 31 private int kind; 32 private ArrayList arguments = new ArrayList (); 33 private ArrayList typeParameters = new ArrayList (); 34 private ArrayList thrownExceptions = new ArrayList (); 35 private int mainTypeStart = -1; 36 private int mainTypeEnd; 37 private int typeSigStart = -1; 38 39 public KeyToSignature(BindingKeyParser parser) { 40 super(parser); 41 this.kind = ((KeyToSignature) parser).kind; 42 } 43 44 public KeyToSignature(String key, int kind) { 45 super(key); 46 this.kind = kind; 47 } 48 49 public void consumeArrayDimension(char[] brakets) { 50 this.signature.append(brakets); 51 } 52 53 public void consumeBaseType(char[] baseTypeSig) { 54 this.typeSigStart = this.signature.length(); 55 this.signature.append(baseTypeSig); 56 } 57 58 public void consumeCapture(int position) { 59 this.signature = ((KeyToSignature) this.arguments.get(0)).signature; 61 } 62 63 public void consumeLocalType(char[] uniqueKey) { 64 this.signature = new StringBuffer (); 65 uniqueKey = CharOperation.subarray(uniqueKey, 0, uniqueKey.length-1); 67 CharOperation.replace(uniqueKey, '/', '.'); 68 this.signature.append(uniqueKey); 69 } 70 71 public void consumeMethod(char[] selector, char[] methodSignature) { 72 this.arguments = new ArrayList (); 73 CharOperation.replace(methodSignature, '/', '.'); 74 switch(this.kind) { 75 case SIGNATURE: 76 this.signature = new StringBuffer (); 77 this.signature.append(methodSignature); 78 break; 79 case THROWN_EXCEPTIONS: 80 if (CharOperation.indexOf('^', methodSignature) > 0) { 81 char[][] types = Signature.getThrownExceptionTypes(methodSignature); 82 int length = types.length; 83 for (int i=0; i<length; i++) { 84 this.thrownExceptions.add(new String (types[i])); 85 } 86 } 87 break; 88 } 89 } 90 91 public void consumeMemberType(char[] simpleTypeName) { 92 this.signature.append('$'); 93 this.signature.append(simpleTypeName); 94 } 95 96 public void consumePackage(char[] pkgName) { 97 this.signature.append(pkgName); 98 } 99 100 public void consumeParameterizedGenericMethod() { 101 int typeParametersSize = this.arguments.size(); 102 if (typeParametersSize > 0) { 103 int sigLength = this.signature.length(); 104 char[] methodSignature = new char[sigLength]; 105 this.signature.getChars(0, sigLength, methodSignature, 0); 106 char[][] typeParameterSigs = Signature.getTypeParameters(methodSignature); 107 if (typeParameterSigs.length != typeParametersSize) 108 return; 109 this.signature = new StringBuffer (); 110 111 for (int i = 0; i < typeParametersSize; i++) 113 typeParameterSigs[i] = CharOperation.concat(Signature.C_TYPE_VARIABLE,Signature.getTypeVariable(typeParameterSigs[i]), Signature.C_SEMICOLON); 114 int paramStart = CharOperation.indexOf(Signature.C_PARAM_START, methodSignature); 115 char[] typeParametersString = CharOperation.subarray(methodSignature, 0, paramStart); 116 this.signature.append(typeParametersString); 117 118 this.signature.append(Signature.C_PARAM_START); 120 char[][] parameters = Signature.getParameterTypes(methodSignature); 121 for (int i = 0, parametersLength = parameters.length; i < parametersLength; i++) 122 substitute(parameters[i], typeParameterSigs, typeParametersSize); 123 this.signature.append(Signature.C_PARAM_END); 124 125 char[] returnType = Signature.getReturnType(methodSignature); 127 substitute(returnType, typeParameterSigs, typeParametersSize); 128 129 char[][] exceptions = Signature.getThrownExceptionTypes(methodSignature); 131 for (int i = 0, exceptionsLength = exceptions.length; i < exceptionsLength; i++) { 132 this.signature.append(Signature.C_EXCEPTION_START); 133 substitute(exceptions[i], typeParameterSigs, typeParametersSize); 134 } 135 136 } 137 } 138 139 144 private void substitute(char[] parameter, char[][] typeParameterSigs, int typeParametersLength) { 145 for (int i = 0; i < typeParametersLength; i++) { 146 if (CharOperation.equals(parameter, typeParameterSigs[i])) { 147 String typeArgument = ((KeyToSignature) this.arguments.get(i)).signature.toString(); 148 this.signature.append(typeArgument); 149 return; 150 } 151 } 152 int genericStart = CharOperation.indexOf(Signature.C_GENERIC_START, parameter); 153 if (genericStart > -1) { 154 this.signature.append(CharOperation.subarray(parameter, 0, genericStart)); 155 char[][] parameters = Signature.getTypeArguments(parameter); 156 this.signature.append(Signature.C_GENERIC_START); 157 for (int j = 0, paramsLength = parameters.length; j < paramsLength; j++) 158 substitute(parameters[j], typeParameterSigs, typeParametersLength); 159 this.signature.append(Signature.C_GENERIC_END); 160 this.signature.append(Signature.C_SEMICOLON); 161 } else { 162 int index = 0; 164 int length = parameter.length; 165 loop: while (index < length) { 166 char current = parameter[index]; 167 switch (current) { 168 case Signature.C_CAPTURE: 169 case Signature.C_EXTENDS: 170 case Signature.C_SUPER: 171 case Signature.C_ARRAY: 172 this.signature.append(current); 173 index++; 174 break; 175 default: 176 break loop; 177 } 178 } 179 if (index > 0) 180 substitute(CharOperation.subarray(parameter, index, length), typeParameterSigs, typeParametersLength); 181 else 182 this.signature.append(parameter); 183 } 184 } 185 186 public void consumeParameterizedType(char[] simpleTypeName, boolean isRaw) { 187 if (simpleTypeName != null) { 188 this.signature.append('.'); 190 this.signature.append(simpleTypeName); 191 } 192 if (!isRaw) { 193 this.signature.append('<'); 194 int length = this.arguments.size(); 195 for (int i = 0; i < length; i++) { 196 this.signature.append(((KeyToSignature) this.arguments.get(i)).signature); 197 } 198 this.signature.append('>'); 199 if (this.kind != TYPE_ARGUMENTS) 200 this.arguments = new ArrayList (); 201 } 202 } 203 204 public void consumeParser(BindingKeyParser parser) { 205 this.arguments.add(parser); 206 } 207 208 public void consumeField(char[] fieldName) { 209 if (this.kind == SIGNATURE) { 210 this.signature = ((KeyToSignature) this.arguments.get(0)).signature; 211 } 212 } 213 214 public void consumeException() { 215 int size = this.arguments.size(); 216 if (size > 0) { 217 for (int i=0; i<size; i++) { 218 this.thrownExceptions.add(((KeyToSignature) this.arguments.get(i)).signature.toString()); 219 } 220 this.arguments = new ArrayList (); 221 } 222 } 223 224 public void consumeFullyQualifiedName(char[] fullyQualifiedName) { 225 this.typeSigStart = this.signature.length(); 226 this.signature.append('L'); 227 this.signature.append(CharOperation.replaceOnCopy(fullyQualifiedName, '/', '.')); 228 } 229 230 public void consumeSecondaryType(char[] simpleTypeName) { 231 this.signature.append('~'); 232 this.mainTypeStart = this.signature.lastIndexOf(".") + 1; if (this.mainTypeStart == 0) 234 this.mainTypeStart = 1; this.mainTypeEnd = this.signature.length(); 236 this.signature.append(simpleTypeName); 237 } 238 239 public void consumeType() { 240 if (this.mainTypeStart != -1) { 242 this.signature.replace(this.mainTypeStart, this.mainTypeEnd, ""); } 244 int length = this.typeParameters.size(); 246 if (length > 0) { 247 StringBuffer typeParametersSig = new StringBuffer (); 248 typeParametersSig.append('<'); 249 for (int i = 0; i < length; i++) { 250 char[] typeParameterSig = Signature.createTypeParameterSignature( 251 (char[]) this.typeParameters.get(i), 252 new char[][]{ ConstantPool.ObjectSignature }); 253 typeParametersSig.append(typeParameterSig); 254 } 256 typeParametersSig.append('>'); 257 this.signature.insert(this.typeSigStart, typeParametersSig); 258 this.typeParameters = new ArrayList (); 259 } 260 this.signature.append(';'); 261 } 262 263 public void consumeTypeParameter(char[] typeParameterName) { 264 this.typeParameters.add(typeParameterName); 265 } 266 267 public void consumeTypeVariable(char[] position, char[] typeVariableName) { 268 this.signature = new StringBuffer (); 269 this.signature.append('T'); 270 this.signature.append(typeVariableName); 271 this.signature.append(';'); 272 } 273 274 public void consumeTypeWithCapture() { 275 KeyToSignature keyToSignature = (KeyToSignature) this.arguments.get(0); 276 this.signature = keyToSignature.signature; 277 this.arguments = keyToSignature.arguments; 278 this.thrownExceptions = keyToSignature.thrownExceptions; 279 } 280 281 public void consumeWildCard(int wildCardKind) { 282 this.signature = new StringBuffer (); 284 switch (wildCardKind) { 285 case Wildcard.UNBOUND: 286 this.signature.append('*'); 287 break; 288 case Wildcard.EXTENDS: 289 this.signature.append('+'); 290 this.signature.append(((KeyToSignature) this.arguments.get(0)).signature); 291 break; 292 case Wildcard.SUPER: 293 this.signature.append('-'); 294 this.signature.append(((KeyToSignature) this.arguments.get(0)).signature); 295 break; 296 default: 297 return; 299 } 300 } 301 302 public String [] getThrownExceptions() { 303 int length = this.thrownExceptions.size(); 304 String [] result = new String [length]; 305 for (int i = 0; i < length; i++) { 306 result[i] = (String ) this.thrownExceptions.get(i); 307 } 308 return result; 309 } 310 311 public String [] getTypeArguments() { 312 int length = this.arguments.size(); 313 String [] result = new String [length]; 314 for (int i = 0; i < length; i++) { 315 result[i] = ((KeyToSignature) this.arguments.get(i)).signature.toString(); 316 } 317 return result; 318 } 319 320 public BindingKeyParser newParser() { 321 return new KeyToSignature(this); 322 } 323 324 327 public String toString() { 328 return this.signature.toString(); 329 } 330 331 } 332 | Popular Tags |