Ejemplo n.º 1
0
  public static String bestNameableType(ITypeBinding type) {
    {
      String result_ = fqTypeName(type);
      if (result_ != null) return removeTypeParam(result_);
    }

    if (type.isCapture() || type.isWildcardType() || type.isTypeVariable()) {
      return bestNameableType(type.getErasure());
    }

    // Find an interface or superclass being implemented.
    // For anonymous class, there can only be one. BUT for
    // local class, things are super f-ed up...
    if (type.isAnonymous()) {
      // any interfaces?
      if (type.getInterfaces().length > 0) {
        String result = fqTypeName(type.getInterfaces()[0]);
        assert (result != null);
        return removeTypeParam(result);
      } else {
        String result = fqTypeName(type.getSuperclass());
        assert (result != null);
        return removeTypeParam(result);
      }
    }

    if (type.isLocal()) {
      // We can only make sense of this is there is just one
      // superclass+inferface.
      if (type.getInterfaces().length == 0) {
        // EASY!
        String result = fqTypeName(type.getSuperclass());
        assert (result != null);
        return removeTypeParam(result);
      } else {
        // Well, maybe superclass is object?
        if (type.getSuperclass().getQualifiedName().equals(Object.class.getName())) {
          String result = fqTypeName(type.getInterfaces()[0]);
          assert (result != null);
          return removeTypeParam(result);
        } else {
          // Otherwise, return the superclass, I guess, but print a warning:
          String result = fqTypeName(type.getSuperclass());
          assert (result != null);
          System.err.println("Local class has multiple supertypes..." + type.getBinaryName());
          return removeTypeParam(result);
        }
      }
    }

    return Utilities.impossible();
  }
  /**
   * Adds the specified method binding to the search results.
   *
   * @param calledMethodBinding
   * @param node
   */
  protected void addMethodCall(IMethodBinding calledMethodBinding, ASTNode node) {
    try {
      if (calledMethodBinding != null) {
        fProgressMonitor.worked(1);

        ITypeBinding calledTypeBinding = calledMethodBinding.getDeclaringClass();
        IType calledType = null;

        if (!calledTypeBinding.isAnonymous()) {
          calledType = (IType) calledTypeBinding.getJavaElement();
        } else {
          if (!"java.lang.Object"
              .equals(calledTypeBinding.getSuperclass().getQualifiedName())) { // $NON-NLS-1$
            calledType = (IType) calledTypeBinding.getSuperclass().getJavaElement();
          } else {
            calledType = (IType) calledTypeBinding.getInterfaces()[0].getJavaElement();
          }
        }

        IMethod calledMethod =
            findIncludingSupertypes(calledMethodBinding, calledType, fProgressMonitor);

        IMember referencedMember = null;
        if (calledMethod == null) {
          if (calledMethodBinding.isConstructor()
              && calledMethodBinding.getParameterTypes().length == 0) {
            referencedMember = calledType;
          }
        } else {
          if (calledType.isInterface()) {
            calledMethod = findImplementingMethods(calledMethod);
          }

          if (!isIgnoredBySearchScope(calledMethod)) {
            referencedMember = calledMethod;
          }
        }
        final int position = node.getStartPosition();
        final int number = fCompilationUnit.getLineNumber(position);
        fSearchResults.addMember(
            fMember,
            referencedMember,
            position,
            position + node.getLength(),
            number < 1 ? 1 : number);
      }
    } catch (JavaModelException jme) {
      JavaPlugin.log(jme);
    }
  }
  /**
   * Ensures that creating a DOM AST and computing the bindings takes the owner's working copies
   * into account.
   *
   * @deprecated using deprecated code
   */
  public void testParseCompilationUnit3() throws CoreException {
    try {
      createJavaProject("P1", new String[] {"src"}, new String[] {"JCL_LIB", "lib"}, "bin");

      // create X.class in lib folder
      /* Evaluate the following in a scrapbook:
      	org.eclipse.jdt.core.tests.model.ModifyingResourceTests.generateClassFile(
      		"X",
      		"public class X {\n" +
      		"}")
      */
      byte[] bytes =
          new byte[] {
            -54, -2, -70, -66, 0, 3, 0, 45, 0, 13, 1, 0, 1, 88, 7, 0, 1, 1, 0, 16, 106, 97, 118, 97,
                47, 108, 97, 110, 103, 47, 79, 98, 106, 101, 99, 116, 7, 0, 3, 1, 0, 6, 60, 105,
                110, 105, 116, 62, 1, 0, 3, 40, 41, 86, 1, 0, 4, 67, 111, 100, 101, 12, 0, 5, 0, 6,
                10, 0, 4, 0, 8, 1, 0, 15, 76, 105, 110, 101, 78, 117,
            109, 98, 101, 114, 84, 97, 98, 108, 101, 1, 0, 10, 83, 111, 117, 114, 99, 101, 70, 105,
                108, 101, 1, 0, 6, 88, 46, 106, 97, 118, 97, 0, 33, 0, 2, 0, 4, 0, 0, 0, 0, 0, 1, 0,
                1, 0, 5, 0, 6, 0, 1, 0, 7, 0, 0, 0, 29, 0, 1, 0, 1, 0, 0, 0, 5, 42, -73, 0, 9, -79,
                0, 0, 0, 1, 0, 10, 0, 0, 0, 6,
            0, 1, 0, 0, 0, 1, 0, 1, 0, 11, 0, 0, 0, 2, 0, 12,
          };
      this.createFile("P1/lib/X.class", bytes);

      // create libsrc and attach source
      createFolder("P1/libsrc");
      createFile("P1/libsrc/X.java", "public class X extends Y {\n" + "}");
      IPackageFragmentRoot lib = getPackageFragmentRoot("P1/lib");
      lib.attachSource(new Path("/P1/libsrc"), null, null);

      // create Y.java in src folder
      createFile("P1/src/Y.java", "");

      // create working copy on Y.java
      TestWorkingCopyOwner owner = new TestWorkingCopyOwner();
      this.workingCopy = getCompilationUnit("P1/src/Y.java").getWorkingCopy(owner, null);
      this.workingCopy.getBuffer().setContents("public class Y {\n" + "}");
      this.workingCopy.makeConsistent(null);

      // parse and resolve class file
      IClassFile classFile = getClassFile("P1/lib/X.class");
      ASTParser parser = ASTParser.newParser(AST.JLS2);
      parser.setSource(classFile);
      parser.setResolveBindings(true);
      parser.setWorkingCopyOwner(owner);
      CompilationUnit cu = (CompilationUnit) parser.createAST(null);
      List types = cu.types();
      assertEquals("Unexpected number of types in AST", 1, types.size());
      TypeDeclaration type = (TypeDeclaration) types.get(0);
      ITypeBinding typeBinding = type.resolveBinding();
      ITypeBinding superType = typeBinding.getSuperclass();
      assertEquals(
          "Unexpected super type",
          "Y",
          superType == null ? "<null>" : superType.getQualifiedName());
    } finally {
      deleteProject("P1");
    }
  }
Ejemplo n.º 4
0
  private boolean initializeAndCollectInhData(
      IMethodBinding binding, List<MethodPathItem> inhMethods) {
    if (binding == null) return false;

    // check if method is abstract
    if (Modifier.isAbstract(binding.getModifiers())) return false;

    ITypeBinding declTypeBinding = binding.getDeclaringClass();

    // check only for classes
    if (declTypeBinding == null || !declTypeBinding.isClass()) return false;

    Set<String> checkedInterfaces = new HashSet<String>();

    // (recursively) collects all keys of methods in abstract classes which
    // belongs to this declaration
    OverridingRelationUtils.collectSimilarMethodKeysInSuperClasses(
        binding, declTypeBinding.getSuperclass(), inhMethods, checkedInterfaces);

    // (recursively) collects all keys of methods in interfaces which
    // belongs to this declaration
    OverridingRelationUtils.collectSimilarMethodKeysInInterfaces(
        binding, declTypeBinding.getInterfaces(), inhMethods, checkedInterfaces);

    // the set should contain at least one inherited method
    if (inhMethods.size() == 0) return false;

    return true;
  }
  private boolean entryMatchesRenamedMethod(
      File javaFile,
      Multimap<String, String> invokers,
      IMethodBinding methodBinding,
      String entry) {
    try {
      String[] columns = entry.split(";");
      String classNamespace = columns[1];
      String renamedMethod = columns[2];

      String methodBindingClassNamespace = (methodBinding.getKey().split("\\."))[0];

      if (simplifyMethodSignature(methodBinding).contains(renamedMethod)) {
        if (classesNamesMatches(methodBindingClassNamespace, classNamespace)) {
          return true;
        } else {
            /*the method belongs to a superclass or interface*/
          ITypeBinding declaringClass = methodBinding.getDeclaringClass();
          ITypeBinding superClass = declaringClass.getSuperclass();
          ITypeBinding[] interfaceBinds = declaringClass.getInterfaces();

          // [MISSING:FrameDecoder]
          if (superClass.toString().contains("MISSING")) {
            String superClassRelaxed = (superClass.toString().split(":")[1]);
            superClassRelaxed = superClassRelaxed.substring(0, superClassRelaxed.length() - 1);
            if (classesNamesMatches(superClassRelaxed, classNamespace)) {
              return true;
            }
          } else {
            if (classesNamesMatches(superClass.getKey(), classNamespace)) {
              return true;
            } else {
              for (ITypeBinding interfce : interfaceBinds) {
                if (interfce.toString().contains("MISSING")) {
                  String interfaceRelaxed = (interfce.toString().split(":")[1]);
                  interfaceRelaxed = interfaceRelaxed.substring(0, interfaceRelaxed.length() - 1);
                  if (classesNamesMatches(interfaceRelaxed, classNamespace)) {
                    return true;
                  }
                } else {
                  if (classesNamesMatches(interfce.getKey(), classNamespace)) {
                    return true;
                  }
                }
              }
            }
          }
          return false;
        }
      } else {
        return false;
      }
    } catch (NullPointerException e) {
      return false;
    }
  }
Ejemplo n.º 6
0
 public static void collectAllInheritedTypes(ITypeBinding type, Set<ITypeBinding> inheritedTypes) {
   collectAllInterfaces(type, inheritedTypes);
   while (true) {
     type = type.getSuperclass();
     if (type == null) {
       break;
     }
     inheritedTypes.add(type);
   }
 }
 public void checkInput(RefactoringStatus status, String methodName, ASTNode destination) {
   ITypeBinding[] arguments = getArgumentTypes();
   ITypeBinding type = ASTNodes.getEnclosingType(destination);
   status.merge(Checks.checkMethodInType(type, methodName, arguments));
   ITypeBinding superClass = type.getSuperclass();
   if (superClass != null) {
     status.merge(Checks.checkMethodInHierarchy(superClass, methodName, null, arguments));
   }
   for (ITypeBinding superInterface : type.getInterfaces()) {
     status.merge(Checks.checkMethodInHierarchy(superInterface, methodName, null, arguments));
   }
 }
Ejemplo n.º 8
0
  public static boolean typeIsAssignableFrom(ITypeBinding typeBinding, Class<?> specifiedClass) {
    String className = specifiedClass.getCanonicalName();
    while (typeBinding != null) {
      if (className.equals(typeBinding.getErasure().getQualifiedName())) {
        return true;
      }
      if (typeImplementsInterfaceDirectly(typeBinding, className)) {
        return true;
      }

      typeBinding = typeBinding.getSuperclass();
    }
    return false;
  }
Ejemplo n.º 9
0
  public static void collectAllInterfaces(ITypeBinding type, Set<ITypeBinding> interfaces) {
    Deque<ITypeBinding> typeQueue = Lists.newLinkedList();

    while (type != null) {
      typeQueue.add(type);
      type = type.getSuperclass();
    }

    while (!typeQueue.isEmpty()) {
      ITypeBinding nextType = typeQueue.poll();
      List<ITypeBinding> newInterfaces = Arrays.asList(nextType.getInterfaces());
      interfaces.addAll(newInterfaces);
      typeQueue.addAll(newInterfaces);
    }
  }
Ejemplo n.º 10
0
  public Types(AST ast) {
    this.ast = ast;

    // Find core java types.
    javaObjectType = ast.resolveWellKnownType("java.lang.Object");
    javaClassType = ast.resolveWellKnownType("java.lang.Class");
    javaCloneableType = ast.resolveWellKnownType("java.lang.Cloneable");
    javaStringType = ast.resolveWellKnownType("java.lang.String");
    javaThrowableType = ast.resolveWellKnownType("java.lang.Throwable");
    javaVoidType = ast.resolveWellKnownType("java.lang.Void");
    ITypeBinding binding = ast.resolveWellKnownType("java.lang.Integer");
    javaNumberType = binding.getSuperclass();

    // Create core IOS types.
    NSCopying = mapIOSType(IOSTypeBinding.newInterface("NSCopying", javaCloneableType));
    NSObject = mapIOSType(IOSTypeBinding.newClass("NSObject", javaObjectType));
    NSNumber = mapIOSType(IOSTypeBinding.newClass("NSNumber", javaNumberType, NSObject));
    NSString = mapIOSType(IOSTypeBinding.newClass("NSString", javaStringType, NSObject));
    NSException = mapIOSType(IOSTypeBinding.newClass("NSException", javaThrowableType, NSObject));
    IOSClass = mapIOSType(IOSTypeBinding.newUnmappedClass("IOSClass"));
    mapIOSType(IOSTypeBinding.newUnmappedClass("NSZone"));
    idType = mapIOSType(IOSTypeBinding.newUnmappedClass("id"));

    initializeArrayTypes();
    initializeTypeMap();
    initializeCommonJavaTypes();
    populatePrimitiveAndWrapperTypeMaps();

    ITypeBinding voidType = ast.resolveWellKnownType("void");

    // Commonly used methods.
    retainMethod =
        IOSMethodBinding.newMethod(NameTable.RETAIN_METHOD, Modifier.PUBLIC, idType, NSObject);
    releaseMethod =
        IOSMethodBinding.newMethod(NameTable.RELEASE_METHOD, Modifier.PUBLIC, voidType, NSObject);
    autoreleaseMethod =
        IOSMethodBinding.newMethod(NameTable.AUTORELEASE_METHOD, Modifier.PUBLIC, idType, NSObject);
    allocMethod =
        IOSMethodBinding.newMethod(NameTable.ALLOC_METHOD, Modifier.PUBLIC, idType, NSObject);
    deallocMethod =
        IOSMethodBinding.newMethod(NameTable.DEALLOC_METHOD, Modifier.PUBLIC, idType, NSObject);
  }
Ejemplo n.º 11
0
 /**
  * Finds the declaration for a given method and receiver in the same way that the ObjC compiler
  * will search for a declaration.
  */
 private IMethodBinding getFirstDeclaration(String methodSig, ITypeBinding type) {
   if (type == null) {
     return null;
   }
   type = type.getTypeDeclaration();
   for (IMethodBinding declaredMethod : type.getDeclaredMethods()) {
     if (methodSig.equals(getObjCMethodSignature(declaredMethod))) {
       return declaredMethod;
     }
   }
   List<ITypeBinding> supertypes = Lists.newArrayList();
   supertypes.addAll(Arrays.asList(type.getInterfaces()));
   supertypes.add(type.isTypeVariable() ? 0 : supertypes.size(), type.getSuperclass());
   for (ITypeBinding supertype : supertypes) {
     IMethodBinding result = getFirstDeclaration(methodSig, supertype);
     if (result != null) {
       return result;
     }
   }
   return null;
 }
Ejemplo n.º 12
0
 private static boolean typeHierarchyHelper(
     HierarchyCallback callback, ITypeBinding root, boolean start_at_root) {
   // First, visit the type, then collect super-types
   if (start_at_root) {
     boolean result = callback.nextType(root);
     if (!result) return result;
   }
   { // super-class
     ITypeBinding sup = root.getSuperclass();
     if (sup != null) {
       boolean result = typeHierarchyHelper(callback, sup, true);
       if (!result) return result;
     }
   }
   // interfaces
   for (ITypeBinding face : root.getInterfaces()) {
     boolean result = typeHierarchyHelper(callback, face, true);
     if (!result) return result;
   }
   return true; // if you get this far, return value doesn't matter.
 }
  /**
   * Ensures that creating a DOM AST and computing the bindings takes the owner's working copies
   * into account.
   *
   * @deprecated using deprecated code
   */
  public void testParseCompilationUnit2() throws CoreException {
    TestWorkingCopyOwner owner = new TestWorkingCopyOwner();
    this.workingCopy = getCompilationUnit("P/Y.java").getWorkingCopy(owner, null);
    this.workingCopy.getBuffer().setContents("public class Y {\n" + "}");
    this.workingCopy.makeConsistent(null);

    char[] source = ("public class Z extends Y {\n" + "}").toCharArray();
    ASTParser parser = ASTParser.newParser(AST.JLS2);
    parser.setSource(source);
    parser.setUnitName("Z.java");
    parser.setProject(getJavaProject("P"));
    parser.setWorkingCopyOwner(owner);
    parser.setResolveBindings(true);
    CompilationUnit cu = (CompilationUnit) parser.createAST(null);

    List types = cu.types();
    assertEquals("Unexpected number of types in AST", 1, types.size());
    TypeDeclaration type = (TypeDeclaration) types.get(0);
    ITypeBinding typeBinding = type.resolveBinding();
    assertNotNull("No binding", typeBinding);
    assertEquals("Unexpected super type", "Y", typeBinding.getSuperclass().getQualifiedName());
  }