Exemple #1
0
 public static String getReturnTypeString(IMethod method, boolean classTypesOnly) {
   try {
     String qualifiedReturnType = Signature.getReturnType(method.getSignature());
     if (!classTypesOnly
         || qualifiedReturnType.startsWith("L")
         || qualifiedReturnType.startsWith("Q")) {
       return Signature.getSignatureSimpleName(qualifiedReturnType.replace('/', '.'));
     }
   } catch (IllegalArgumentException e) {
   } catch (JavaModelException e) {
   }
   return null;
 }
  /**
   * Takes a method signature <code>
   * [&lt; typeVariableName : formalTypeDecl &gt;] ( paramTypeSig1* ) retTypeSig</code> and returns
   * it with any parameter signatures filtered through <code>getLowerBound</code> and the return
   * type filtered through <code>getUpperBound</code>. Any preceding formal type variable
   * declarations are removed.
   *
   * <p>TODO this is a temporary workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=83600
   *
   * @param signature the method signature to convert
   * @return the signature with no bounded types
   */
  public static char[] unboundedSignature(char[] signature) {
    if (signature == null || signature.length < 2) return signature;

    final boolean BUG_83600 = true;
    // XXX the signatures from CompletionRequestor contain a superfluous '+'
    // before type parameters to parameter types
    if (BUG_83600) {
      signature = fix83600(signature);
    }

    StringBuffer res = new StringBuffer("("); // $NON-NLS-1$
    char[][] parameters = Signature.getParameterTypes(signature);
    for (int i = 0; i < parameters.length; i++) {
      char[] param = parameters[i];
      res.append(getLowerBound(param));
    }
    res.append(')');
    res.append(getUpperBound(Signature.getReturnType(signature)));
    return res.toString().toCharArray();
  }
  boolean matchMethod(MethodPattern pattern, Object binaryInfo, IBinaryType enclosingBinaryType) {
    if (!pattern.findDeclarations) return false; // only relevant when finding declarations
    if (!(binaryInfo instanceof IBinaryMethod)) return false;

    IBinaryMethod method = (IBinaryMethod) binaryInfo;
    if (!pattern.matchesName(pattern.selector, method.getSelector())) return false;
    if (!checkDeclaringType(
        enclosingBinaryType,
        pattern.declaringSimpleName,
        pattern.declaringQualification,
        pattern.isCaseSensitive(),
        pattern.isCamelCase())) return false;

    // look at return type only if declaring type is not specified
    boolean checkReturnType =
        pattern.declaringSimpleName == null
            && (pattern.returnSimpleName != null || pattern.returnQualification != null);
    boolean checkParameters = pattern.parameterSimpleNames != null;
    if (checkReturnType || checkParameters) {
      char[] methodDescriptor = convertClassFileFormat(method.getMethodDescriptor());
      if (checkReturnType) {
        char[] returnTypeSignature =
            Signature.toCharArray(Signature.getReturnType(methodDescriptor));
        if (!checkTypeName(
            pattern.returnSimpleName,
            pattern.returnQualification,
            returnTypeSignature,
            pattern.isCaseSensitive(),
            pattern.isCamelCase())) return false;
      }
      if (checkParameters
          && !checkParameters(
              methodDescriptor,
              pattern.parameterSimpleNames,
              pattern.parameterQualifications,
              pattern.isCaseSensitive(),
              pattern.isCamelCase())) return false;
    }
    return true;
  }
Exemple #4
0
 /**
  * Determines is the java element contains a specific method.
  *
  * <p>The syntax for the property tester is of the form: methodname, signature, modifiers.
  *
  * <ol>
  *   <li>methodname - case sensitive method name, required. For example, <code>toString</code>.
  *   <li>signature - JLS style method signature, required. For example, <code>(QString;)V</code>.
  *   <li>modifiers - optional space separated list of modifiers, for example, <code>public static
  *       </code>.
  * </ol>
  *
  * @param element the element to check for the method
  * @param args first arg is method name, secondary args are parameter types signatures
  * @return true if the method is found in the element, false otherwise
  */
 private boolean hasMethod(IJavaElement element, Object[] args) {
   try {
     if (args.length > 1) {
       IType type = getType(element);
       if (type != null && type.exists()) {
         String name = (String) args[0];
         String signature = (String) args[1];
         String[] parms = Signature.getParameterTypes(signature);
         String returnType = Signature.getReturnType(signature);
         IMethod candidate = type.getMethod(name, parms);
         if (candidate.exists()) {
           // check return type
           if (candidate.getReturnType().equals(returnType)) {
             // check modifiers
             if (args.length > 2) {
               String modifierText = (String) args[2];
               String[] modifiers = modifierText.split(" "); // $NON-NLS-1$
               int flags = 0;
               for (int j = 0; j < modifiers.length; j++) {
                 String modifier = modifiers[j];
                 Integer flag = (Integer) fgModifiers.get(modifier);
                 if (flag != null) {
                   flags = flags | flag.intValue();
                 }
               }
               if (candidate.getFlags() == flags) {
                 return true;
               }
             }
           }
         }
       }
     }
   } catch (JavaModelException e) {
   }
   return false;
 }