Ejemplo n.º 1
0
 @Override
 public String getAdditionalInfo() {
   StringBuffer sb = new StringBuffer();
   if (fRemovedAssignmentsCount == 1) {
     sb.append(
         FixMessages.INSTANCE
             .UnusedCodeFix_RemoveFieldOrLocal_RemovedAssignments_preview_singular());
   } else if (fRemovedAssignmentsCount > 1) {
     sb.append(
         FixMessages.INSTANCE.UnusedCodeFix_RemoveFieldOrLocal_RemovedAssignments_preview_plural(
             String.valueOf(fRemovedAssignmentsCount)));
   }
   if (fAlteredAssignmentsCount == 1) {
     sb.append(
         FixMessages.INSTANCE
             .UnusedCodeFix_RemoveFieldOrLocal_AlteredAssignments_preview_singular());
   } else if (fAlteredAssignmentsCount > 1) {
     sb.append(
         FixMessages.INSTANCE.UnusedCodeFix_RemoveFieldOrLocal_AlteredAssignments_preview_plural(
             String.valueOf(fAlteredAssignmentsCount)));
   }
   if (sb.length() > 0) {
     return sb.toString();
   } else return null;
 }
Ejemplo n.º 2
0
    /** {@inheritDoc} */
    @Override
    public void rewriteAST(CompilationUnitRewrite cuRewrite) throws CoreException {
      ASTRewrite rewrite = cuRewrite.getASTRewrite();

      TextEditGroup group =
          createTextEditGroup(
              FixMessages.INSTANCE.UnusedCodeFix_RemoveCast_description(), cuRewrite);

      while (fUnnecessaryCasts.size() > 0) {
        CastExpression castExpression = fUnnecessaryCasts.iterator().next();
        fUnnecessaryCasts.remove(castExpression);
        CastExpression down = castExpression;
        while (fUnnecessaryCasts.contains(down.getExpression())) {
          down = (CastExpression) down.getExpression();
          fUnnecessaryCasts.remove(down);
        }

        Expression expression = down.getExpression();
        ASTNode move = rewrite.createMoveTarget(expression);

        CastExpression top = castExpression;
        while (fUnnecessaryCasts.contains(top.getParent())) {
          top = (CastExpression) top.getParent();
          fUnnecessaryCasts.remove(top);
        }

        ASTNode toReplace = top;
        if (top.getParent() instanceof ParenthesizedExpression
            && !NecessaryParenthesesChecker.needsParentheses(
                expression, top.getParent(), top.getLocationInParent())) {
          toReplace = top.getParent();
        }
        rewrite.replace(toReplace, move, group);
      }
    }
Ejemplo n.º 3
0
    /** {@inheritDoc} */
    @Override
    public void rewriteAST(CompilationUnitRewrite cuRewrite) throws CoreException {

      TextEditGroup group =
          createTextEditGroup(
              FixMessages.INSTANCE.UnusedCodeFix_RemoveCast_description(), cuRewrite);

      ASTRewrite rewrite = cuRewrite.getASTRewrite();

      CastExpression cast = fCast;
      Expression expression = cast.getExpression();
      ASTNode placeholder = rewrite.createCopyTarget(expression);

      if (NecessaryParenthesesChecker.needsParentheses(
          expression, cast.getParent(), cast.getLocationInParent())) {
        rewrite.replace(fCast, placeholder, group);
      } else {
        rewrite.replace(
            fCast.getParent() instanceof ParenthesizedExpression
                ? fCast.getParent()
                : fSelectedNode,
            placeholder,
            group);
      }
    }
Ejemplo n.º 4
0
 /** {@inheritDoc} */
 @Override
 public void rewriteAST(CompilationUnitRewrite cuRewrite) throws CoreException {
   ImportDeclaration node = fImportDeclaration;
   TextEditGroup group =
       createTextEditGroup(
           FixMessages.INSTANCE.UnusedCodeFix_RemoveImport_description(), cuRewrite);
   cuRewrite.getASTRewrite().remove(node, group);
 }
Ejemplo n.º 5
0
 private String getDisplayString(IBinding binding) {
   switch (binding.getKind()) {
     case IBinding.TYPE:
       return FixMessages.INSTANCE.UnusedCodeFix_RemoveUnusedType_description();
     case IBinding.METHOD:
       if (((IMethodBinding) binding).isConstructor()) {
         return FixMessages.INSTANCE.UnusedCodeFix_RemoveUnusedConstructor_description();
       } else {
         return FixMessages.INSTANCE.UnusedCodeFix_RemoveUnusedPrivateMethod_description();
       }
     case IBinding.VARIABLE:
       if (((IVariableBinding) binding).isField()) {
         return FixMessages.INSTANCE.UnusedCodeFix_RemoveUnusedField_description();
       } else {
         return FixMessages.INSTANCE.UnusedCodeFix_RemoveUnusedVariabl_description();
       }
     default:
       return ""; //$NON-NLS-1$
   }
 }
Ejemplo n.º 6
0
 private static String getDisplayString(
     SimpleName simpleName, IBinding binding, boolean removeAllAssignements) {
   String name = simpleName.getIdentifier();
   switch (binding.getKind()) {
     case IBinding.TYPE:
       return FixMessages.INSTANCE.UnusedCodeFix_RemoveType_description(name);
     case IBinding.METHOD:
       if (((IMethodBinding) binding).isConstructor()) {
         return FixMessages.INSTANCE.UnusedCodeFix_RemoveConstructor_description(name);
       } else {
         return FixMessages.INSTANCE.UnusedCodeFix_RemoveMethod_description(name);
       }
     case IBinding.VARIABLE:
       if (removeAllAssignements) {
         return FixMessages.INSTANCE.UnusedCodeFix_RemoveFieldOrLocalWithInitializer_description(
             name);
       } else {
         return FixMessages.INSTANCE.UnusedCodeFix_RemoveFieldOrLocal_description(name);
       }
     default:
       return ""; //$NON-NLS-1$
   }
 }
Ejemplo n.º 7
0
 public static UnusedCodeFix createRemoveUnusedImportFix(
     CompilationUnit compilationUnit, IProblemLocation problem, Document document) {
   if (isUnusedImport(problem)) {
     ImportDeclaration node = getImportDeclaration(problem, compilationUnit);
     if (node != null) {
       String label = FixMessages.INSTANCE.UnusedCodeFix_RemoveImport_description();
       RemoveImportOperation operation = new RemoveImportOperation(node);
       Map<String, String> options = new HashMap<String, String>();
       options.put(CleanUpConstants.REMOVE_UNUSED_CODE_IMPORTS, CleanUpOptions.TRUE);
       return new UnusedCodeFix(
           label,
           compilationUnit,
           new CompilationUnitRewriteOperation[] {operation},
           options,
           document);
     }
   }
   return null;
 }
Ejemplo n.º 8
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);
  }
Ejemplo n.º 9
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);
  }