コード例 #1
0
 public static boolean isUnusedImport(IProblemLocation problem) {
   int id = problem.getProblemId();
   return id == IProblem.UnusedImport
       || id == IProblem.DuplicateImport
       || id == IProblem.ConflictingImport
       || id == IProblem.CannotImportPackage
       || id == IProblem.ImportNotFound;
 }
コード例 #2
0
 public static boolean isUnusedMember(IProblemLocation problem) {
   int id = problem.getProblemId();
   return id == IProblem.UnusedPrivateMethod
       || id == IProblem.UnusedPrivateConstructor
       || id == IProblem.UnusedPrivateField
       || id == IProblem.UnusedPrivateType
       || id == IProblem.LocalVariableIsNeverUsed
       || id == IProblem.ArgumentIsNeverUsed;
 }
コード例 #3
0
 private static ImportDeclaration getImportDeclaration(
     IProblemLocation problem, CompilationUnit compilationUnit) {
   ASTNode selectedNode = problem.getCoveringNode(compilationUnit);
   if (selectedNode != null) {
     ASTNode node = ASTNodes.getParent(selectedNode, ASTNode.IMPORT_DECLARATION);
     if (node instanceof ImportDeclaration) {
       return (ImportDeclaration) node;
     }
   }
   return null;
 }
コード例 #4
0
  private static SimpleName getUnusedName(
      CompilationUnit compilationUnit, IProblemLocation problem) {
    ASTNode selectedNode = problem.getCoveringNode(compilationUnit);

    if (selectedNode instanceof MethodDeclaration) {
      return ((MethodDeclaration) selectedNode).getName();
    } else if (selectedNode instanceof SimpleName) {
      return (SimpleName) selectedNode;
    }

    return null;
  }
コード例 #5
0
  public static UnusedCodeFix createRemoveUnusedCastFix(
      CompilationUnit compilationUnit, IProblemLocation problem, Document document) {
    if (problem.getProblemId() != IProblem.UnnecessaryCast) return null;

    ASTNode selectedNode = problem.getCoveringNode(compilationUnit);

    ASTNode curr = selectedNode;
    while (curr instanceof ParenthesizedExpression) {
      curr = ((ParenthesizedExpression) curr).getExpression();
    }

    if (!(curr instanceof CastExpression)) return null;

    return new UnusedCodeFix(
        FixMessages.INSTANCE.UnusedCodeFix_RemoveCast_description(),
        compilationUnit,
        new CompilationUnitRewriteOperation[] {
          new RemoveCastOperation((CastExpression) curr, selectedNode)
        },
        document);
  }
コード例 #6
0
  public static ICleanUpFix createCleanUp(
      CompilationUnit compilationUnit,
      IProblemLocation[] problems,
      boolean removeUnusedPrivateMethods,
      boolean removeUnusedPrivateConstructors,
      boolean removeUnusedPrivateFields,
      boolean removeUnusedPrivateTypes,
      boolean removeUnusedLocalVariables,
      boolean removeUnusedImports,
      boolean removeUnusedCast,
      Document document) {

    List<CompilationUnitRewriteOperation> result = new ArrayList<CompilationUnitRewriteOperation>();
    HashMap<ASTNode, List<SimpleName>> variableDeclarations =
        new HashMap<ASTNode, List<SimpleName>>();
    HashSet<CastExpression> unnecessaryCasts = new HashSet<CastExpression>();
    for (int i = 0; i < problems.length; i++) {
      IProblemLocation problem = problems[i];
      int id = problem.getProblemId();

      if (removeUnusedImports
          && (id == IProblem.UnusedImport
              || id == IProblem.DuplicateImport
              || id == IProblem.ConflictingImport
              || id == IProblem.CannotImportPackage
              || id == IProblem.ImportNotFound)) {
        ImportDeclaration node = UnusedCodeFix.getImportDeclaration(problem, compilationUnit);
        if (node != null) {
          result.add(new RemoveImportOperation(node));
        }
      }

      if ((removeUnusedPrivateMethods && id == IProblem.UnusedPrivateMethod)
          || (removeUnusedPrivateConstructors && id == IProblem.UnusedPrivateConstructor)
          || (removeUnusedPrivateTypes && id == IProblem.UnusedPrivateType)) {

        SimpleName name = getUnusedName(compilationUnit, problem);
        if (name != null) {
          IBinding binding = name.resolveBinding();
          if (binding != null) {
            result.add(new RemoveUnusedMemberOperation(new SimpleName[] {name}, false));
          }
        }
      }

      if ((removeUnusedLocalVariables && id == IProblem.LocalVariableIsNeverUsed)
          || (removeUnusedPrivateFields && id == IProblem.UnusedPrivateField)) {
        SimpleName name = getUnusedName(compilationUnit, problem);
        if (name != null) {
          IBinding binding = name.resolveBinding();
          if (binding instanceof IVariableBinding
              && !isFormalParameterInEnhancedForStatement(name)
              && (!((IVariableBinding) binding).isField()
                  || isSideEffectFree(name, compilationUnit))) {
            VariableDeclarationFragment parent =
                (VariableDeclarationFragment)
                    ASTNodes.getParent(
                        name, VariableDeclarationFragment.VARIABLE_DECLARATION_FRAGMENT);
            if (parent != null) {
              ASTNode varDecl = parent.getParent();
              if (!variableDeclarations.containsKey(varDecl)) {
                variableDeclarations.put(varDecl, new ArrayList<SimpleName>());
              }
              variableDeclarations.get(varDecl).add(name);
            } else {
              result.add(new RemoveUnusedMemberOperation(new SimpleName[] {name}, false));
            }
          }
        }
      }

      if (removeUnusedCast && id == IProblem.UnnecessaryCast) {
        ASTNode selectedNode = problem.getCoveringNode(compilationUnit);

        ASTNode curr = selectedNode;
        while (curr instanceof ParenthesizedExpression) {
          curr = ((ParenthesizedExpression) curr).getExpression();
        }

        if (curr instanceof CastExpression) {
          unnecessaryCasts.add((CastExpression) curr);
        }
      }
    }
    for (Iterator<ASTNode> iter = variableDeclarations.keySet().iterator(); iter.hasNext(); ) {
      ASTNode node = iter.next();
      List<SimpleName> names = variableDeclarations.get(node);
      result.add(
          new RemoveUnusedMemberOperation(names.toArray(new SimpleName[names.size()]), false));
    }
    if (unnecessaryCasts.size() > 0) result.add(new RemoveAllCastOperation(unnecessaryCasts));

    if (result.size() == 0) return null;

    return new UnusedCodeFix(
        FixMessages.INSTANCE.UnusedCodeFix_change_name(), compilationUnit,
        result.toArray(new CompilationUnitRewriteOperation[result.size()]), document);
  }