예제 #1
0
  public static UnusedCodeFix createUnusedMemberFix(
      CompilationUnit compilationUnit,
      IProblemLocation problem,
      boolean removeAllAssignements,
      Document document) {
    if (isUnusedMember(problem)) {
      SimpleName name = getUnusedName(compilationUnit, problem);
      if (name != null) {
        IBinding binding = name.resolveBinding();
        if (binding != null) {
          if (isFormalParameterInEnhancedForStatement(name)) return null;

          String label = getDisplayString(name, binding, removeAllAssignements);
          RemoveUnusedMemberOperation operation =
              new RemoveUnusedMemberOperation(new SimpleName[] {name}, removeAllAssignements);
          return new UnusedCodeFix(
              label,
              compilationUnit,
              new CompilationUnitRewriteOperation[] {operation},
              getCleanUpOptions(binding, removeAllAssignements),
              document);
        }
      }
    }
    return null;
  }
예제 #2
0
 private static boolean isSideEffectFree(SimpleName simpleName, CompilationUnit completeRoot) {
   SimpleName nameNode =
       (SimpleName)
           NodeFinder.perform(completeRoot, simpleName.getStartPosition(), simpleName.getLength());
   SimpleName[] references =
       LinkedNodeFinder.findByBinding(completeRoot, nameNode.resolveBinding());
   for (int i = 0; i < references.length; i++) {
     if (hasSideEffect(references[i])) return false;
   }
   return true;
 }
예제 #3
0
    private void removeUnusedName(CompilationUnitRewrite cuRewrite, SimpleName simpleName) {
      ASTRewrite rewrite = cuRewrite.getASTRewrite();
      CompilationUnit completeRoot = cuRewrite.getRoot();

      IBinding binding = simpleName.resolveBinding();
      CompilationUnit root = (CompilationUnit) simpleName.getRoot();
      String displayString = getDisplayString(binding);
      TextEditGroup group = createTextEditGroup(displayString, cuRewrite);
      if (binding.getKind() == IBinding.METHOD) {
        IMethodBinding decl = ((IMethodBinding) binding).getMethodDeclaration();
        ASTNode declaration = root.findDeclaringNode(decl);
        rewrite.remove(declaration, group);
      } else if (binding.getKind() == IBinding.TYPE) {
        ITypeBinding decl = ((ITypeBinding) binding).getTypeDeclaration();
        ASTNode declaration = root.findDeclaringNode(decl);
        if (declaration.getParent() instanceof TypeDeclarationStatement) {
          declaration = declaration.getParent();
        }
        rewrite.remove(declaration, group);
      } else if (binding.getKind() == IBinding.VARIABLE) {
        SimpleName nameNode =
            (SimpleName)
                NodeFinder.perform(
                    completeRoot, simpleName.getStartPosition(), simpleName.getLength());
        SimpleName[] references =
            LinkedNodeFinder.findByBinding(completeRoot, nameNode.resolveBinding());
        for (int i = 0; i < references.length; i++) {
          removeVariableReferences(rewrite, references[i], group);
        }

        IVariableBinding bindingDecl =
            ((IVariableBinding) nameNode.resolveBinding()).getVariableDeclaration();
        ASTNode declaringNode = completeRoot.findDeclaringNode(bindingDecl);
        if (declaringNode instanceof SingleVariableDeclaration) {
          removeParamTag(rewrite, (SingleVariableDeclaration) declaringNode, group);
        }
      } else {
        // unexpected
      }
    }
예제 #4
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);
  }