| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2008, 2009 IBM Corporation and others. |
| 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
| 7 | * |
| 8 | * Contributors: |
| 9 | * IBM Corporation - initial API and implementation |
| 10 | *******************************************************************************/ |
| 11 | package org.eclipse.pde.api.tools.internal.util; |
| 12 | |
| 13 | import java.util.ArrayList; |
| 14 | import java.util.Iterator; |
| 15 | import java.util.List; |
| 16 | import java.util.StringTokenizer; |
| 17 | |
| 18 | import org.eclipse.core.runtime.Assert; |
| 19 | import org.eclipse.core.runtime.CoreException; |
| 20 | import org.eclipse.jdt.core.Signature; |
| 21 | import org.eclipse.jdt.core.dom.AST; |
| 22 | import org.eclipse.jdt.core.dom.ASTNode; |
| 23 | import org.eclipse.jdt.core.dom.AbstractTypeDeclaration; |
| 24 | import org.eclipse.jdt.core.dom.ArrayType; |
| 25 | import org.eclipse.jdt.core.dom.CompilationUnit; |
| 26 | import org.eclipse.jdt.core.dom.IExtendedModifier; |
| 27 | import org.eclipse.jdt.core.dom.MethodDeclaration; |
| 28 | import org.eclipse.jdt.core.dom.Modifier; |
| 29 | import org.eclipse.jdt.core.dom.PackageDeclaration; |
| 30 | import org.eclipse.jdt.core.dom.ParameterizedType; |
| 31 | import org.eclipse.jdt.core.dom.PrimitiveType; |
| 32 | import org.eclipse.jdt.core.dom.QualifiedType; |
| 33 | import org.eclipse.jdt.core.dom.SimpleType; |
| 34 | import org.eclipse.jdt.core.dom.SingleVariableDeclaration; |
| 35 | import org.eclipse.jdt.core.dom.Type; |
| 36 | import org.eclipse.pde.api.tools.internal.provisional.descriptors.IFieldDescriptor; |
| 37 | import org.eclipse.pde.api.tools.internal.provisional.descriptors.IMethodDescriptor; |
| 38 | import org.eclipse.pde.api.tools.internal.provisional.descriptors.IReferenceTypeDescriptor; |
| 39 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiField; |
| 40 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiMethod; |
| 41 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiType; |
| 42 | |
| 43 | /** |
| 44 | * This class contains utility methods for creating and working with |
| 45 | * Java element signatures. |
| 46 | * |
| 47 | * @since 1.0.0 |
| 48 | * @noinstantiate This class is not intended to be instantiated by clients. |
| 49 | */ |
| 50 | public final class Signatures { |
| 51 | |
| 52 | /** |
| 53 | * Constructor |
| 54 | */ |
| 55 | private Signatures() {} |
| 56 | |
| 57 | /** |
| 58 | * Collects which signature to use and de-qualifies it. If there is a generic signature |
| 59 | * it is returned, otherwise the standard signature is used |
| 60 | * @param method |
| 61 | * @return the de-qualified signature for the method |
| 62 | */ |
| 63 | public static String processMethodSignature(IApiMethod method) { |
| 64 | String signature = method.getGenericSignature(); |
| 65 | if(signature == null) { |
| 66 | signature = method.getSignature(); |
| 67 | } |
| 68 | return dequalifySignature(signature); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Strips member type names off the signature and returns the primary type name. Member types are assumed |
| 73 | * to be delimited by the '$' character<br><br> |
| 74 | * For example:<br> |
| 75 | * <code>a.b.c.Type$Member -> a.b.c.Type</code><br> |
| 76 | * <code>x.y.z.Type -> x.y.z.Type</code><br> |
| 77 | * @param typename the type name to process |
| 78 | * @return the primary type name stripped off of the given type name |
| 79 | */ |
| 80 | public static String getPrimaryTypeName(String typename) { |
| 81 | int idx = typename.indexOf('$'); |
| 82 | if(idx > -1){ |
| 83 | return typename.substring(0, idx); |
| 84 | } |
| 85 | return typename; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Returns the signature to use to display this {@link IApiMethod}. |
| 90 | * This method will load the enclosing type in the event the method is a constructor. |
| 91 | * @param method |
| 92 | * @return the display signature to use for this {@link IApiMethod} |
| 93 | * @throws CoreException if a lookup to the parent type of the method fails |
| 94 | */ |
| 95 | public static String getMethodSignature(IApiMethod method) throws CoreException { |
| 96 | String methodsig = method.getGenericSignature(); |
| 97 | if(methodsig == null) { |
| 98 | methodsig = method.getSignature(); |
| 99 | } |
| 100 | String methodname = getMethodName(method); |
| 101 | return Signature.toString(dequalifySignature(methodsig), methodname, null, false, false); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Returns the signature of the method qualified with the given type |
| 106 | * @param type |
| 107 | * @param method |
| 108 | * @return the given type qualified signature of the given method |
| 109 | * @throws CoreException if a lookup to the parent type of the given method fails |
| 110 | */ |
| 111 | public static String getQualifiedMethodSignature(IApiMethod method) throws CoreException { |
| 112 | StringBuffer buffer = new StringBuffer(); |
| 113 | IApiType type = method.getEnclosingType(); |
| 114 | if(type != null) { |
| 115 | buffer.append(getQualifiedTypeSignature(type)).append('.'); |
| 116 | } |
| 117 | String methodsig = method.getGenericSignature(); |
| 118 | if(methodsig == null) { |
| 119 | methodsig = method.getSignature(); |
| 120 | } |
| 121 | String methodname = getMethodName(method); |
| 122 | buffer.append(Signature.toString(dequalifySignature(methodsig), methodname, null, false, false)); |
| 123 | return buffer.toString(); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Returns the signature of the method qualified with the given type |
| 128 | * @param method |
| 129 | * @return the given type qualified signature of the given method |
| 130 | * @throws CoreException if a lookup to the parent type of the given method fails |
| 131 | */ |
| 132 | public static String getQualifiedMethodSignature(IMethodDescriptor method) throws CoreException { |
| 133 | StringBuffer buffer = new StringBuffer(); |
| 134 | IReferenceTypeDescriptor type = method.getEnclosingType(); |
| 135 | if(type != null) { |
| 136 | buffer.append(getQualifiedTypeSignature(type)).append('.'); |
| 137 | } |
| 138 | String methodsig = method.getSignature(); |
| 139 | String methodname = getMethodName(method); |
| 140 | buffer.append(Signature.toString(dequalifySignature(methodsig), methodname, null, false, false)); |
| 141 | return buffer.toString(); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Returns the de-qualified method signature |
| 146 | * @param method |
| 147 | * @return the de-qualified method signature |
| 148 | * @throws CoreException |
| 149 | */ |
| 150 | public static String getMethodSignature(IMethodDescriptor method) throws CoreException { |
| 151 | StringBuffer buffer = new StringBuffer(); |
| 152 | String methodsig = method.getSignature(); |
| 153 | String methodname = getMethodName(method); |
| 154 | buffer.append(Signature.toString(dequalifySignature(methodsig), methodname, null, false, false)); |
| 155 | return buffer.toString(); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Returns the name to use for the method. If the method is a constructor, |
| 160 | * the enclosing type is loaded to get its simple name |
| 161 | * @param method |
| 162 | * @return the name for the method. If the method is a constructor the simple name |
| 163 | * of the enclosing type is substituted. |
| 164 | * @throws CoreException |
| 165 | */ |
| 166 | public static String getMethodName(IApiMethod method) throws CoreException { |
| 167 | String mname = method.getName(); |
| 168 | if("<init>".equals(method.getName())) { //$NON-NLS-1$ |
| 169 | IApiType type = method.getEnclosingType(); |
| 170 | if(type != null) { |
| 171 | return type.getSimpleName(); |
| 172 | } |
| 173 | } |
| 174 | return mname; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Returns the name to use for the method. If the method is a constructor, |
| 179 | * the enclosing type is loaded to get its simple name |
| 180 | * @param method |
| 181 | * @return the name for the method. If the method is a constructor the simple name |
| 182 | * of the enclosing type is substituted. |
| 183 | * @throws CoreException |
| 184 | */ |
| 185 | public static String getMethodName(IMethodDescriptor method) throws CoreException { |
| 186 | String mname = method.getName(); |
| 187 | if("<init>".equals(method.getName())) { //$NON-NLS-1$ |
| 188 | IReferenceTypeDescriptor type = method.getEnclosingType(); |
| 189 | if(type != null) { |
| 190 | return type.getName(); |
| 191 | } |
| 192 | } |
| 193 | return mname; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Returns the unqualified signature of the given {@link IApiField} |
| 198 | * |
| 199 | * @param field |
| 200 | * @return the unqualified signature of the given {@link IApiField} |
| 201 | */ |
| 202 | public static String getFieldSignature(IApiField field) { |
| 203 | return field.getName(); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Returns the type-qualified field signature |
| 208 | * @param field |
| 209 | * @return the type-qualified field signature |
| 210 | */ |
| 211 | public static String getQualifiedFieldSignature(IApiField field) throws CoreException { |
| 212 | StringBuffer buffer = new StringBuffer(); |
| 213 | IApiType type = field.getEnclosingType(); |
| 214 | if(type != null) { |
| 215 | buffer.append(getQualifiedTypeSignature(type)).append('.'); |
| 216 | } |
| 217 | buffer.append(field.getName()); |
| 218 | return buffer.toString(); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Returns the type-qualified field signature |
| 223 | * @param field |
| 224 | * @return the type-qualified field signature |
| 225 | */ |
| 226 | public static String getQualifiedFieldSignature(IFieldDescriptor field) throws CoreException { |
| 227 | StringBuffer buffer = new StringBuffer(); |
| 228 | IReferenceTypeDescriptor type = field.getEnclosingType(); |
| 229 | if(type != null) { |
| 230 | buffer.append(getQualifiedTypeSignature(type)).append('.'); |
| 231 | } |
| 232 | buffer.append(field.getName()); |
| 233 | return buffer.toString(); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Returns the type signature to use for displaying the given {@link IApiType} |
| 238 | * @param type |
| 239 | * @return the display signature to use for the given {@link IApiType} |
| 240 | */ |
| 241 | public static String getQualifiedTypeSignature(IApiType type) { |
| 242 | return getTypeSignature(type.getSignature(), type.getGenericSignature(), true); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Returns the type signature to use for displaying the given {@link IReferenceTypeDescriptor} |
| 247 | * @param type |
| 248 | * @return the display signature to use for the given {@link IReferenceTypeDescriptor} |
| 249 | */ |
| 250 | public static String getQualifiedTypeSignature(IReferenceTypeDescriptor type) { |
| 251 | return getTypeSignature(type.getSignature(), type.getGenericSignature(), true); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Returns the de-qualified signature for the given {@link IApiType} |
| 256 | * |
| 257 | * @param type the type to get the signature for |
| 258 | * @return the de-qualified signature for the given {@link IApiType} |
| 259 | */ |
| 260 | public static String getTypeSignature(IApiType type) { |
| 261 | return getTypeSignature(type.getSignature(), type.getGenericSignature(), false); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Returns the display-able representation of the given signature and generic signature |
| 266 | * @param signature |
| 267 | * @param genericsignature |
| 268 | * @param qualified |
| 269 | * @return |
| 270 | */ |
| 271 | public static String getTypeSignature(String signature, String genericsignature, boolean qualified) { |
| 272 | StringBuffer buffer = new StringBuffer(); |
| 273 | String sig = signature.replace('/', '.'); |
| 274 | if(qualified == false) { |
| 275 | sig = dequalifySignature(sig); |
| 276 | } |
| 277 | buffer.append(Signature.toString(sig.replace('$', '.'))); |
| 278 | if(genericsignature != null) { |
| 279 | appendTypeParameters(buffer, Signature.getTypeParameters(genericsignature.replace('/', '.'))); |
| 280 | } |
| 281 | return buffer.toString(); |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Returns the name of an anonymous or local type with all |
| 286 | * qualification removed. |
| 287 | * For example: |
| 288 | * <pre><code> |
| 289 | * Class$3inner --> inner |
| 290 | * Class$3 --> null |
| 291 | * </code></pre> |
| 292 | * @param name the name to resolve |
| 293 | * @return the name of an anonymous or local type with qualification removed or <code>null</code> |
| 294 | * if the anonymous type has no name |
| 295 | */ |
| 296 | public static String getAnonymousTypeName(String name) { |
| 297 | if(name != null) { |
| 298 | int idx = name.lastIndexOf('$'); |
| 299 | if(idx > -1) { |
| 300 | String num = name.substring(idx+1, name.length()); |
| 301 | try { |
| 302 | Integer.parseInt(num); |
| 303 | return null; |
| 304 | } |
| 305 | catch(NumberFormatException nfe) {} |
| 306 | for(int i = 0; i < name.length(); i++) { |
| 307 | if(!Character.isDigit(num.charAt(i))) { |
| 308 | return num.substring(i, num.length()); |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | return null; |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Appends the given listing of type parameter names to the signature contained in the |
| 318 | * given buffer |
| 319 | * @param buffer |
| 320 | * @param parameters |
| 321 | */ |
| 322 | public static void appendTypeParameters(StringBuffer buffer, String[] parameters) { |
| 323 | if(parameters == null) { |
| 324 | return; |
| 325 | } |
| 326 | if(parameters.length == 0) { |
| 327 | return; |
| 328 | } |
| 329 | buffer.append(getLT()); |
| 330 | for(int i = 0; i < parameters.length; i++) { |
| 331 | if(i > 0) { |
| 332 | buffer.append(getComma()); |
| 333 | } |
| 334 | buffer.append(Signature.getTypeVariable(parameters[i])); |
| 335 | } |
| 336 | buffer.append(getGT()); |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Returns a comma and space used for displaying comma-separated lists |
| 341 | * in signatures. |
| 342 | * |
| 343 | * @return the string rendering for a comma and following space |
| 344 | */ |
| 345 | public static String getComma() { |
| 346 | return ", "; //$NON-NLS-1$ |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Returns the string for rendering the '<code><</code>' character. |
| 351 | * |
| 352 | * @return the string for rendering '<code><</code>' |
| 353 | */ |
| 354 | public static String getLT() { |
| 355 | return "<"; //$NON-NLS-1$ |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * Returns the string for rendering the '<code>></code>' character. |
| 360 | * |
| 361 | * @return the string for rendering '<code>></code>' |
| 362 | */ |
| 363 | public static String getGT() { |
| 364 | return ">"; //$NON-NLS-1$ |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Convert fully qualified signature to unqualified one. |
| 369 | * The descriptor can be dot or slashed based. |
| 370 | * |
| 371 | * @param descriptor the given descriptor to convert |
| 372 | * @return the converted signature |
| 373 | */ |
| 374 | public static String dequalifySignature(String signature) { |
| 375 | StringBuffer buffer = new StringBuffer(); |
| 376 | char[] chars = signature.toCharArray(); |
| 377 | for (int i = 0, max = chars.length; i < max; i++) { |
| 378 | char currentChar = chars[i]; |
| 379 | switch(currentChar) { |
| 380 | case 'L' : { |
| 381 | if(chars[i+1] != ';') { |
| 382 | buffer.append('Q'); |
| 383 | // read reference type |
| 384 | int lastDotPosition = i; |
| 385 | i++; |
| 386 | while(i < chars.length && currentChar != ';' && currentChar != '<' ) { |
| 387 | switch(currentChar) { |
| 388 | case '/' : |
| 389 | case '.' : |
| 390 | lastDotPosition = i; |
| 391 | break; |
| 392 | } |
| 393 | i++; |
| 394 | currentChar = chars[i]; |
| 395 | } |
| 396 | buffer.append(chars, lastDotPosition + 1, i - lastDotPosition - 1); |
| 397 | buffer.append(currentChar); |
| 398 | } |
| 399 | else { |
| 400 | buffer.append(currentChar); |
| 401 | } |
| 402 | break; |
| 403 | } |
| 404 | case 'Q': { |
| 405 | while(i < chars.length && currentChar != ';') { |
| 406 | buffer.append(currentChar); |
| 407 | currentChar = chars[++i]; |
| 408 | } |
| 409 | } |
| 410 | //$FALL-THROUGH$ |
| 411 | default: { |
| 412 | buffer.append(currentChar); |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | return String.valueOf(buffer); |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * Creates a method signature from a specified {@link MethodDeclaration} |
| 421 | * @param node |
| 422 | * @return the signature for the given method node or <code>null</code> |
| 423 | */ |
| 424 | public static String getMethodSignatureFromNode(MethodDeclaration node) { |
| 425 | Assert.isNotNull(node); |
| 426 | List params = node.parameters(); |
| 427 | List rparams = Signatures.getParametersTypeNames(params); |
| 428 | if(rparams.size() == params.size()) { |
| 429 | if(!node.isConstructor()) { |
| 430 | Type returnType = getType(node); |
| 431 | if (returnType != null) { |
| 432 | String rtype = Signatures.getTypeSignature(returnType); |
| 433 | if(rtype != null) { |
| 434 | return Signature.createMethodSignature((String[]) rparams.toArray(new String[rparams.size()]), rtype); |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | else { |
| 439 | Signatures.collectSyntheticParam(node, rparams); |
| 440 | return Signature.createMethodSignature((String[]) rparams.toArray(new String[rparams.size()]), Signature.SIG_VOID); |
| 441 | } |
| 442 | } |
| 443 | return null; |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * Returns the listing of the signatures of the parameters passed in, where the |
| 448 | * list elements are all {@link SingleVariableDeclaration}s and the elements in the returned list are |
| 449 | * of type {@link String} |
| 450 | * @param rawparams |
| 451 | * @return a listing of signatures for the specified parameters |
| 452 | */ |
| 453 | private static List getParametersTypeNames(List rawparams) { |
| 454 | List rparams = new ArrayList(rawparams.size()); |
| 455 | SingleVariableDeclaration param = null; |
| 456 | String pname = null; |
| 457 | for(Iterator iter = rawparams.iterator(); iter.hasNext();) { |
| 458 | param = (SingleVariableDeclaration) iter.next(); |
| 459 | pname = Signatures.getTypeSignature(getType(param)); |
| 460 | if(pname != null) { |
| 461 | rparams.add(pname); |
| 462 | } |
| 463 | } |
| 464 | return rparams; |
| 465 | } |
| 466 | |
| 467 | /* |
| 468 | * This is called for SingleVariableDeclaration and MethodDeclaration |
| 469 | */ |
| 470 | private static Type getType(ASTNode node) { |
| 471 | switch(node.getNodeType()) { |
| 472 | case ASTNode.SINGLE_VARIABLE_DECLARATION : { |
| 473 | SingleVariableDeclaration param = (SingleVariableDeclaration) node; |
| 474 | Type type = param.getType(); |
| 475 | int extraDim = param.getExtraDimensions(); |
| 476 | |
| 477 | if (extraDim == 0) { |
| 478 | return type; |
| 479 | } |
| 480 | AST ast = type.getAST(); |
| 481 | type = (Type) ASTNode.copySubtree(ast, type); |
| 482 | for (int i = 0; i < extraDim; i++) { |
| 483 | type = ast.newArrayType(type); |
| 484 | } |
| 485 | return type; |
| 486 | } |
| 487 | default: { |
| 488 | // ASTNode.METHOD_DECLARATION |
| 489 | MethodDeclaration methodDeclaration = (MethodDeclaration) node; |
| 490 | Type type = methodDeclaration.getReturnType2(); |
| 491 | int extraDim = methodDeclaration.getExtraDimensions(); |
| 492 | |
| 493 | if (extraDim == 0) { |
| 494 | return type; |
| 495 | } |
| 496 | AST ast = type.getAST(); |
| 497 | type = (Type) ASTNode.copySubtree(ast, type); |
| 498 | for (int i = 0; i < extraDim; i++) { |
| 499 | type = ast.newArrayType(type); |
| 500 | } |
| 501 | return type; |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | /** |
| 507 | * Returns the simple name of the type, by stripping off the last '.' segment and returning it. |
| 508 | * This method assumes that qualified type names are '.' separated. If the type specified is a package |
| 509 | * than an empty string is returned. |
| 510 | * @param qualifiedname the fully qualified name of a type, '.' separated (e.g. a.b.c.Type) |
| 511 | * @return the simple name from the qualified name. For example if the qualified name is a.b.c.Type this method |
| 512 | * will return Type (stripping off the package qualification) |
| 513 | */ |
| 514 | public static String getTypeName(String qualifiedname) { |
| 515 | int idx = qualifiedname.lastIndexOf('.'); |
| 516 | idx++; |
| 517 | if(idx > 0) { |
| 518 | return qualifiedname.substring(idx, qualifiedname.length()); |
| 519 | } |
| 520 | // default package |
| 521 | return qualifiedname; |
| 522 | } |
| 523 | |
| 524 | /** |
| 525 | * Processes the signature for the given {@link Type} |
| 526 | * @param type the type to process |
| 527 | * @return the signature for the type or <code>null</code> if one could not be |
| 528 | * derived |
| 529 | */ |
| 530 | public static String getTypeSignature(Type type) { |
| 531 | switch(type.getNodeType()) { |
| 532 | case ASTNode.SIMPLE_TYPE: { |
| 533 | return Signature.createTypeSignature(((SimpleType) type).getName().getFullyQualifiedName(), false); |
| 534 | } |
| 535 | case ASTNode.QUALIFIED_TYPE: { |
| 536 | return Signature.createTypeSignature(((QualifiedType)type).getName().getFullyQualifiedName(), false); |
| 537 | } |
| 538 | case ASTNode.ARRAY_TYPE: { |
| 539 | ArrayType a = (ArrayType) type; |
| 540 | return Signature.createArraySignature(getTypeSignature(a.getElementType()), a.getDimensions()); |
| 541 | } |
| 542 | case ASTNode.PARAMETERIZED_TYPE: { |
| 543 | //we don't need to care about the other scoping types only the base type |
| 544 | return getTypeSignature(((ParameterizedType) type).getType()); |
| 545 | } |
| 546 | case ASTNode.PRIMITIVE_TYPE: { |
| 547 | return Signature.createTypeSignature(((PrimitiveType)type).getPrimitiveTypeCode().toString(), false); |
| 548 | } |
| 549 | } |
| 550 | return null; |
| 551 | } |
| 552 | |
| 553 | /** |
| 554 | * Returns if the given signatures match. Where signatures are considered to match |
| 555 | * iff the return type, name and parameters are the same. |
| 556 | * @param signature |
| 557 | * @param signature2 |
| 558 | * @return true if the signatures are equal, false otherwise |
| 559 | */ |
| 560 | public static boolean matchesSignatures(String signature, String signature2) { |
| 561 | if (!matches(Signature.getReturnType(signature), Signature.getReturnType(signature2))) { |
| 562 | return false; |
| 563 | } |
| 564 | String[] parameterTypes = Signature.getParameterTypes(signature); |
| 565 | String[] parameterTypes2 = Signature.getParameterTypes(signature2); |
| 566 | int length = parameterTypes.length; |
| 567 | int length2 = parameterTypes2.length; |
| 568 | if (length != length2) return false; |
| 569 | for (int i = 0; i < length2; i++) { |
| 570 | if (!matches(parameterTypes[i], parameterTypes2[i])) { |
| 571 | return false; |
| 572 | } |
| 573 | } |
| 574 | return true; |
| 575 | } |
| 576 | |
| 577 | /** |
| 578 | * Returns if the two types match. Types are considered to match |
| 579 | * iff the type name and array count (if any) are the same |
| 580 | * @param type |
| 581 | * @param type2 |
| 582 | * @return true if the type names match, false otherwise |
| 583 | */ |
| 584 | private static boolean matches(String type, String type2) { |
| 585 | if (Signature.getArrayCount(type) == Signature.getArrayCount(type2)) { |
| 586 | String el1 = Signature.getElementType(type); |
| 587 | String el2 = Signature.getElementType(type2); |
| 588 | String[] typeargs1 = Signature.getTypeArguments(el1); |
| 589 | String[] typeargs2 = Signature.getTypeArguments(el2); |
| 590 | if(typeargs1.length == typeargs2.length) { |
| 591 | if(typeargs1.length > 0) { |
| 592 | for(int i = 0; i < typeargs1.length; i++) { |
| 593 | if(!matches(typeargs1[i], typeargs2[i])) { |
| 594 | return false; |
| 595 | } |
| 596 | } |
| 597 | return true; |
| 598 | } |
| 599 | else { |
| 600 | String signatureSimpleName = Signature.getSignatureSimpleName(el1); |
| 601 | String signatureSimpleName2 = Signature.getSignatureSimpleName(el2); |
| 602 | if (signatureSimpleName.equals(signatureSimpleName2)) { |
| 603 | return true; |
| 604 | } |
| 605 | int index = signatureSimpleName2.lastIndexOf('.'); |
| 606 | if (index != -1) { |
| 607 | // the right side is a member type |
| 608 | return signatureSimpleName.equals(signatureSimpleName2.subSequence(index + 1, signatureSimpleName2.length())); |
| 609 | } |
| 610 | } |
| 611 | } |
| 612 | } |
| 613 | return false; |
| 614 | } |
| 615 | |
| 616 | /** |
| 617 | * Returns if the specified signature is qualified or not. |
| 618 | * Qualification is determined if there is a token in the signature the begins with an 'L'. |
| 619 | * @param signature |
| 620 | * @return true if the signature is qualified, false otherwise |
| 621 | */ |
| 622 | public static boolean isQualifiedSignature(String signature) { |
| 623 | StringTokenizer tokenizer = new StringTokenizer(signature, "();IJCSBDFTZ!["); //$NON-NLS-1$ |
| 624 | if(tokenizer.hasMoreTokens()) { |
| 625 | return tokenizer.nextToken().charAt(0) == 'L'; |
| 626 | } |
| 627 | return false; |
| 628 | } |
| 629 | |
| 630 | /** |
| 631 | * The type name is dot-separated |
| 632 | * @param typeName the given type name |
| 633 | * @return the package name for the given type name or an empty string if none |
| 634 | */ |
| 635 | public static String getPackageName(String typeName) { |
| 636 | int index = typeName.lastIndexOf('.'); |
| 637 | return index == -1 ? Util.DEFAULT_PACKAGE_NAME : typeName.substring(0, index); |
| 638 | } |
| 639 | |
| 640 | /** |
| 641 | * Collects the synthetic parameter of the fully qualified name of the enclosing context for a constructor of an inner type |
| 642 | * @param method the constructor declaration |
| 643 | * @param rparams the listing of parameters to add to |
| 644 | */ |
| 645 | static void collectSyntheticParam(final MethodDeclaration method, List rparams) { |
| 646 | Assert.isNotNull(method); |
| 647 | if(Signatures.isInTopLevelType(method)) { |
| 648 | return; |
| 649 | } |
| 650 | ASTNode parent = method.getParent(); |
| 651 | AbstractTypeDeclaration type = (AbstractTypeDeclaration) parent; |
| 652 | if (Signatures.isStatic(type)) { |
| 653 | // if the type is static it doesn't need the enclosing type |
| 654 | return; |
| 655 | } |
| 656 | StringBuffer name = new StringBuffer(); |
| 657 | while(parent != null) { |
| 658 | parent = parent.getParent(); |
| 659 | if(parent instanceof AbstractTypeDeclaration) { |
| 660 | type = (AbstractTypeDeclaration) parent; |
| 661 | name.insert(0, type.getName().getFullyQualifiedName()); |
| 662 | if(type.isMemberTypeDeclaration()) { |
| 663 | name.insert(0, '$'); |
| 664 | } |
| 665 | continue; |
| 666 | } |
| 667 | if(parent instanceof CompilationUnit) { |
| 668 | CompilationUnit cunit = (CompilationUnit) parent; |
| 669 | PackageDeclaration pdec = cunit.getPackage(); |
| 670 | if(pdec != null) { |
| 671 | name.insert(0, '.'); |
| 672 | name.insert(0, cunit.getPackage().getName().getFullyQualifiedName()); |
| 673 | } |
| 674 | } |
| 675 | } |
| 676 | name.insert(0, "L"); //$NON-NLS-1$ |
| 677 | name.append(';'); |
| 678 | if(name.length() > 2) { |
| 679 | rparams.add(0, name.toString()); |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | /** |
| 684 | * Returns if the {@link AbstractTypeDeclaration} is static or not (has the static |
| 685 | * keyword or not) |
| 686 | * |
| 687 | * @param typeDeclaration |
| 688 | * @return true if it is static, false otherwise |
| 689 | */ |
| 690 | static boolean isStatic(AbstractTypeDeclaration typeDeclaration) { |
| 691 | List modifiers = typeDeclaration.modifiers(); |
| 692 | if (modifiers.isEmpty()) return false; |
| 693 | for (Iterator iterator = modifiers.iterator(); iterator.hasNext(); ) { |
| 694 | IExtendedModifier modifier = (IExtendedModifier) iterator.next(); |
| 695 | if (!modifier.isModifier()) { |
| 696 | continue; |
| 697 | } |
| 698 | Modifier modifier2 = (Modifier) modifier; |
| 699 | if (modifier2.isStatic()) return true; |
| 700 | } |
| 701 | return false; |
| 702 | } |
| 703 | |
| 704 | /** |
| 705 | * Determines if the given {@link MethodDeclaration} is present in a top level type |
| 706 | * @param method the given method |
| 707 | * @return true if the given {@link MethodDeclaration} is present in a top level type, false otherwise |
| 708 | */ |
| 709 | static boolean isInTopLevelType(final MethodDeclaration method) { |
| 710 | AbstractTypeDeclaration type = (AbstractTypeDeclaration) method.getParent(); |
| 711 | return type != null && type.isPackageMemberTypeDeclaration(); |
| 712 | } |
| 713 | |
| 714 | /** |
| 715 | * Returns the type name with any package qualification removed.<br><br> |
| 716 | * For example:<br> |
| 717 | * <code>a.b.c.Type -> Type</code><br> |
| 718 | * <code>a.b.c.Type$Member -> Type$Member</code> |
| 719 | * @param referencedTypeName |
| 720 | * @return the type name with package qualification removed |
| 721 | */ |
| 722 | public static String getSimpleTypeName(String referencedTypeName) { |
| 723 | int index = referencedTypeName.lastIndexOf('.'); |
| 724 | if (index == -1) { |
| 725 | return referencedTypeName; |
| 726 | } |
| 727 | return referencedTypeName.substring(index + 1); |
| 728 | } |
| 729 | } |