/**
   * Returns the qualified signature corresponding to <code>signature</code>.
   *
   * @param signature the signature to qualify
   * @param context the type inside which an unqualified type will be resolved to find the
   *     qualifier, or <code>null</code> if no context is available
   * @return the qualified signature
   */
  public static String qualifySignature(final String signature, final IType context) {
    if (context == null) return signature;

    String qualifier = Signature.getSignatureQualifier(signature);
    if (qualifier.length() > 0) return signature;

    String elementType = Signature.getElementType(signature);
    String erasure = Signature.getTypeErasure(elementType);
    String simpleName = Signature.getSignatureSimpleName(erasure);
    String genericSimpleName = Signature.getSignatureSimpleName(elementType);

    int dim = Signature.getArrayCount(signature);

    try {
      String[][] strings = context.resolveType(simpleName);
      if (strings != null && strings.length > 0) qualifier = strings[0][0];
    } catch (JavaModelException e) {
      // ignore - not found
    }

    if (qualifier.length() == 0) return signature;

    String qualifiedType = Signature.toQualifiedName(new String[] {qualifier, genericSimpleName});
    String qualifiedSignature = Signature.createTypeSignature(qualifiedType, true);
    String newSignature = Signature.createArraySignature(qualifiedSignature, dim);

    return newSignature;
  }
예제 #2
0
 public ITypeModel getBodyType() {
   IMethod iMethod = (IMethod) tm;
   try {
     String[] parameterTypes = iMethod.getParameterTypes();
     for (String s : parameterTypes) {
       if (s.contains("java")) // $NON-NLS-1$
       {
         continue;
       }
       String returnType = s;
       if (returnType.startsWith("Q") && returnType.endsWith(";")) { // $NON-NLS-1$ //$NON-NLS-2$
         IType ownerType = (IType) iMethod.getAncestor(IJavaElement.TYPE);
         String[][] resolveType =
             ownerType.resolveType(returnType.substring(1, returnType.length() - 1));
         if (resolveType.length == 1) {
           IType findType =
               ownerType.getJavaProject().findType(resolveType[0][0] + '.' + resolveType[0][1]);
           if (findType != null && findType instanceof SourceType) {
             return new JDTType(findType);
           }
         }
       }
     }
   } catch (Exception e) {
     throw new IllegalStateException(e);
   }
   return null;
 }
예제 #3
0
 public IMethod findMethod(IType type, String name) throws JavaModelException {
   for (IMethod m : type.getMethods()) if (m.getElementName().equals(name)) return m;
   String superType = type.getSuperclassName();
   if (superType != null)
     for (String[] resolved : type.resolveType(superType)) {
       IType type2 = TypeUtil.findType(type.getJavaProject(), resolved[0] + "." + resolved[1]);
       if (type2 != null) return findMethod(type2, name);
     }
   return null;
 }
예제 #4
0
  public static String resolveClassName(String className, IType type) {
    if (className == null || type == null) {
      return className;
    }
    // replace binary $ inner class name syntax with . for source level
    className = className.replace('$', '.');
    String dotClassName = new StringBuilder().append('.').append(className).toString();

    IProject project = type.getJavaProject().getProject();

    try {
      // Special handling for some well-know classes
      if (className.startsWith("java.lang") && getJavaType(project, className) != null) {
        return className;
      }

      // Check if the class is imported
      if (!type.isBinary()) {

        // Strip className to first segment to support ReflectionUtils.MethodCallback
        int ix = className.lastIndexOf('.');
        String firstClassNameSegment = className;
        if (ix > 0) {
          firstClassNameSegment = className.substring(0, ix);
        }

        // Iterate the imports
        for (IImportDeclaration importDeclaration : type.getCompilationUnit().getImports()) {
          String importName = importDeclaration.getElementName();
          // Wildcard imports -> check if the package + className is a valid type
          if (importDeclaration.isOnDemand()) {
            String newClassName =
                new StringBuilder(importName.substring(0, importName.length() - 1))
                    .append(className)
                    .toString();
            if (getJavaType(project, newClassName) != null) {
              return newClassName;
            }
          }
          // Concrete import matching .className at the end -> check if type exists
          else if (importName.endsWith(dotClassName) && getJavaType(project, importName) != null) {
            return importName;
          }
          // Check if className is multi segmented (ReflectionUtils.MethodCallback)
          // -> check if the first segment
          else if (!className.equals(firstClassNameSegment)) {
            if (importName.endsWith(firstClassNameSegment)) {
              String newClassName =
                  new StringBuilder(importName.substring(0, importName.lastIndexOf('.') + 1))
                      .append(className)
                      .toString();
              if (getJavaType(project, newClassName) != null) {
                return newClassName;
              }
            }
          }
        }
      }

      // Check if the class is in the same package as the type
      String packageName = type.getPackageFragment().getElementName();
      String newClassName = new StringBuilder(packageName).append(dotClassName).toString();
      if (getJavaType(project, newClassName) != null) {
        return newClassName;
      }

      // Check if the className is sufficient (already fully-qualified)
      if (getJavaType(project, className) != null) {
        return className;
      }

      // Check if the class is coming from the java.lang
      newClassName = new StringBuilder("java.lang").append(dotClassName).toString();
      if (getJavaType(project, newClassName) != null) {
        return newClassName;
      }

      // Fall back to full blown resolution
      String[][] fullInter = type.resolveType(className);
      if (fullInter != null && fullInter.length > 0) {
        return fullInter[0][0] + "." + fullInter[0][1];
      }
    } catch (JavaModelException e) {
      SpringCore.log(e);
    }

    return className;
  }