Ejemplo n.º 1
0
  public void testAddUsage_Method1() throws ParseException {
    addType("Class1", "class Class1 {\n" + "  public void method() {}\n" + "}\n", true);

    TypeDeclaration typeDecl = _project.getType("Class1");

    _testObject = new FeatureUsages(typeDecl);
    _testObject.addUsage(typeDecl, typeDecl.getMethods().get(0));

    // check used types
    assertNotNull(_testObject.getUsedTypes());
    assertEquals(1, _testObject.getUsedTypes().getCount());
    assertTrue(_testObject.isUsed(typeDecl));
    assertNotNull(_testObject.getTypeUsage(typeDecl));
    assertEquals(1, _testObject.getTypeUsage(typeDecl).getUsedFeatureCount());

    // how many features are used?
    assertEquals(1, _testObject.getUsedFeatureCount());
    assertEquals(0, _testObject.getUsedRemoteFeatureCount());
    assertEquals(0, _testObject.getUsedInheritedFeatureCount());
    assertEquals(1, _testObject.getUsedLocalFeatureCount());

    // how many usages are counted?
    assertEquals(1, _testObject.getUsageCount());
    assertEquals(0, _testObject.getRemoteUsageCount());
    assertEquals(0, _testObject.getInheritedUsageCount());
    assertEquals(1, _testObject.getLocalUsageCount());

    // how many remote types are used?
    assertEquals(0, _testObject.getUsedRemoteTypeCount());
    assertEquals(0, _testObject.getUsedFeatureCountOfMaxUsedRemoteType());
  }
Ejemplo n.º 2
0
  private void addPossibleBaseInterfaces(TypeDeclaration typeDecl) {
    if (typeDecl.hasBaseInterfaces()) {
      TypeDeclaration curAncestorDecl;

      for (TypeIterator iter = typeDecl.getBaseInterfaces().getIterator(); iter.hasNext(); ) {
        curAncestorDecl = iter.getNext().getReferenceBaseTypeDecl();
        if (curAncestorDecl != null) {
          _ancestors.add(curAncestorDecl);
          addAncestors((InterfaceDeclaration) curAncestorDecl);
        }
      }
    }
  }
Ejemplo n.º 3
0
  private boolean checkForSubType(
      TypeDeclaration curTypeDecl,
      Hashtable knownSubTypes,
      Hashtable finishedTypes,
      boolean descent) {
    if (curTypeDecl == null) {
      return false;
    }
    if (curTypeDecl == _localType) {
      return true;
    }
    if (knownSubTypes.containsKey(curTypeDecl)) {
      return true;
    }
    if (finishedTypes.containsKey(curTypeDecl)) {
      return false;
    }

    boolean result = false;

    if (curTypeDecl instanceof ClassDeclaration) {
      ClassDeclaration classDecl = (ClassDeclaration) curTypeDecl;

      if (classDecl.hasBaseClass()) {
        if (descent) {
          if (checkForSubType(
              classDecl.getBaseClass().getReferenceBaseTypeDecl(),
              knownSubTypes,
              finishedTypes,
              true)) {
            knownSubTypes.put(curTypeDecl, curTypeDecl);
            result = true;
          }
        } else {
          if (_localType == classDecl.getBaseClass().getReferenceBaseTypeDecl()) {
            knownSubTypes.put(curTypeDecl, curTypeDecl);
            result = true;
          }
        }
      }
    }
    if (!result) {
      for (TypeIterator it = curTypeDecl.getBaseInterfaces().getIterator(); it.hasNext(); ) {
        if (descent) {
          if (checkForSubType(
              it.getNext().getReferenceBaseTypeDecl(), knownSubTypes, finishedTypes, true)) {
            knownSubTypes.put(curTypeDecl, curTypeDecl);
            result = true;
          }
        } else {
          if (_localType == it.getNext().getReferenceBaseTypeDecl()) {
            knownSubTypes.put(curTypeDecl, curTypeDecl);
            result = true;
          }
        }
      }
    }
    finishedTypes.put(curTypeDecl, curTypeDecl);
    return result;
  }
Ejemplo n.º 4
0
  public void testAddUsage_Method6() throws ParseException {
    addType(
        "Class1",
        "class Class1 {\n"
            + "  class InnerClass {\n"
            + "     public void method() {}\n"
            + "  };\n"
            + "}\n",
        true);

    resolve();

    TypeDeclaration typeDecl1 = _project.getType("Class1");
    TypeDeclaration typeDecl2 = typeDecl1.getInnerTypes().get("InnerClass");
    MethodDeclaration methodDecl = typeDecl2.getMethods().get(0);

    _testObject = new FeatureUsages(typeDecl1);
    _testObject.addUsage(typeDecl1, methodDecl);
    _testObject.addUsage(typeDecl2, methodDecl);
    _testObject.addUsage(typeDecl2, methodDecl);

    // check used types
    assertNotNull(_testObject.getUsedTypes());
    assertEquals(2, _testObject.getUsedTypes().getCount());
    assertTrue(_testObject.isUsed(typeDecl1));
    assertTrue(_testObject.isUsed(typeDecl2));
    assertNotNull(_testObject.getTypeUsage(typeDecl1));
    assertNotNull(_testObject.getTypeUsage(typeDecl2));
    assertEquals(1, _testObject.getTypeUsage(typeDecl1).getUsedFeatureCount());
    assertEquals(1, _testObject.getTypeUsage(typeDecl2).getUsedFeatureCount());

    // how many features are used?
    assertEquals(2, _testObject.getUsedFeatureCount());
    assertEquals(0, _testObject.getUsedRemoteFeatureCount());
    assertEquals(0, _testObject.getUsedInheritedFeatureCount());
    assertEquals(2, _testObject.getUsedLocalFeatureCount());

    // how many usages are counted?
    assertEquals(3, _testObject.getUsageCount());
    assertEquals(0, _testObject.getRemoteUsageCount());
    assertEquals(0, _testObject.getInheritedUsageCount());
    assertEquals(3, _testObject.getLocalUsageCount());

    // how many remote types are used?
    assertEquals(0, _testObject.getUsedRemoteTypeCount());
    assertEquals(0, _testObject.getUsedFeatureCountOfMaxUsedRemoteType());
  }
Ejemplo n.º 5
0
  public static boolean areUnRelated(TypeDeclaration typeA, TypeDeclaration typeB) {
    if ((typeA == null) || (typeB == null)) {
      return false;
    }

    // same type
    if (typeA.equals(typeB)) {
      return false;
    }

    // both are defined in same compilation unit
    if (ASTHelper.isInSameCompilationUnit(typeA, typeB)) {
      return false;
    }

    // typeA is child of typeB or vice versa
    if (InheritanceAnalyzer.areRelated(typeA, typeB)) {
      return false;
    }

    return true;
  }
Ejemplo n.º 6
0
 private boolean isRedefined(MethodDeclaration method) {
   return _localType.getMethods().contains(method.getSignature());
 }
Ejemplo n.º 7
0
 private boolean isRedefined(FieldDeclaration field) {
   return _localType.getFields().contains(field.getName());
 }
Ejemplo n.º 8
0
 /** Returns true if pkg is the package where the type is defined */
 private boolean isLocal(jast.ast.nodes.Package pkg) {
   return _localType.getPackage().equals(pkg);
 }
Ejemplo n.º 9
0
  public void testAddUsage_Method5() throws ParseException {
    addType("Class1", "class Class1 extends Class2 {}\n", true);
    addType("Class2", "class Class2 extends Class3 {}\n", true);
    addType(
        "Class3", "class Class3 extends Class4 {\n" + " public void method() {}\n" + "}\n", true);
    addType(
        "Class4", "class Class4 extends Class5 {\n" + " public void method() {}\n" + "}\n", true);
    addType("Class5", "class Class5 {\n" + " public void method() {}\n" + "}\n", true);

    resolve();

    TypeDeclaration typeDecl1 = _project.getType("Class1");
    TypeDeclaration typeDecl2 = _project.getType("Class2");
    TypeDeclaration typeDecl3 = _project.getType("Class3");
    TypeDeclaration typeDecl4 = _project.getType("Class4");
    TypeDeclaration typeDecl5 = _project.getType("Class5");
    MethodDeclaration methodDecl1 = typeDecl3.getMethods().get(0);
    MethodDeclaration methodDecl2 = typeDecl4.getMethods().get(0);
    MethodDeclaration methodDecl3 = typeDecl5.getMethods().get(0);

    _testObject = new FeatureUsages(typeDecl1);
    _testObject.addUsage(typeDecl1, methodDecl1);
    _testObject.addUsage(typeDecl2, methodDecl2);
    _testObject.addUsage(typeDecl3, methodDecl2);
    _testObject.addUsage(typeDecl4, methodDecl2);
    _testObject.addUsage(typeDecl5, methodDecl3);

    // check used types
    assertNotNull(_testObject.getUsedTypes());
    assertEquals(5, _testObject.getUsedTypes().getCount());
    assertTrue(_testObject.isUsed(typeDecl1));
    assertTrue(_testObject.isUsed(typeDecl2));
    assertTrue(_testObject.isUsed(typeDecl3));
    assertTrue(_testObject.isUsed(typeDecl4));
    assertTrue(_testObject.isUsed(typeDecl5));
    assertNotNull(_testObject.getTypeUsage(typeDecl1));
    assertNotNull(_testObject.getTypeUsage(typeDecl2));
    assertNotNull(_testObject.getTypeUsage(typeDecl3));
    assertNotNull(_testObject.getTypeUsage(typeDecl4));
    assertNotNull(_testObject.getTypeUsage(typeDecl5));
    assertEquals(1, _testObject.getTypeUsage(typeDecl1).getUsedFeatureCount());
    assertEquals(1, _testObject.getTypeUsage(typeDecl2).getUsedFeatureCount());
    assertEquals(1, _testObject.getTypeUsage(typeDecl3).getUsedFeatureCount());
    assertEquals(1, _testObject.getTypeUsage(typeDecl4).getUsedFeatureCount());
    assertEquals(1, _testObject.getTypeUsage(typeDecl5).getUsedFeatureCount());

    // how many features are used?
    assertEquals(5, _testObject.getUsedFeatureCount());
    // 5 features are used, since methodDecl2
    // is every time accessed on a different type
    assertEquals(0, _testObject.getUsedRemoteFeatureCount());
    assertEquals(4, _testObject.getUsedInheritedFeatureCount());
    assertEquals(1, _testObject.getUsedLocalFeatureCount());

    // how many usages are counted?
    assertEquals(5, _testObject.getUsageCount());
    assertEquals(0, _testObject.getRemoteUsageCount());
    assertEquals(4, _testObject.getInheritedUsageCount());
    assertEquals(1, _testObject.getLocalUsageCount());

    // how many remote types are used?
    assertEquals(0, _testObject.getUsedRemoteTypeCount());
    assertEquals(0, _testObject.getUsedFeatureCountOfMaxUsedRemoteType());
  }