Search in sources :

Example 1 with IProblemLocation

use of org.eclipse.jdt.ls.core.internal.corrections.IProblemLocation in project eclipse.jdt.ls by eclipse.

the class UnusedCodeFix method createCleanUp.

/*
	 * public static ICleanUpFix createCleanUp(CompilationUnit compilationUnit,
	 * boolean removeUnusedPrivateMethods, boolean
	 * removeUnusedPrivateConstructors, boolean removeUnusedPrivateFields,
	 * boolean removeUnusedPrivateTypes, boolean removeUnusedLocalVariables,
	 * boolean removeUnusedImports, boolean removeUnusedCast) {
	 *
	 * IProblem[] problems= compilationUnit.getProblems(); IProblemLocation[]
	 * locations= new IProblemLocation[problems.length]; for (int i= 0; i <
	 * problems.length; i++) { locations[i]= new ProblemLocation(problems[i]); }
	 *
	 * return createCleanUp(compilationUnit, locations,
	 * removeUnusedPrivateMethods, removeUnusedPrivateConstructors,
	 * removeUnusedPrivateFields, removeUnusedPrivateTypes,
	 * removeUnusedLocalVariables, removeUnusedImports, removeUnusedCast); }
	 */
public static ICleanUpFix createCleanUp(CompilationUnit compilationUnit, IProblemLocation[] problems, boolean removeUnusedPrivateMethods, boolean removeUnusedPrivateConstructors, boolean removeUnusedPrivateFields, boolean removeUnusedPrivateTypes, boolean removeUnusedLocalVariables, boolean removeUnusedImports, boolean removeUnusedCast) {
    List<CompilationUnitRewriteOperation> result = new ArrayList<>();
    Hashtable<ASTNode, List<SimpleName>> variableDeclarations = new Hashtable<>();
    LinkedHashSet<CastExpression> unnecessaryCasts = new LinkedHashSet<>();
    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.class);
                    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.UnusedCodeFix_change_name, compilationUnit, result.toArray(new CompilationUnitRewriteOperation[result.size()]));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) ArrayList(java.util.ArrayList) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) List(java.util.List) ArrayList(java.util.ArrayList) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) Hashtable(java.util.Hashtable) IProblemLocation(org.eclipse.jdt.ls.core.internal.corrections.IProblemLocation) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration) CastExpression(org.eclipse.jdt.core.dom.CastExpression)

Example 2 with IProblemLocation

use of org.eclipse.jdt.ls.core.internal.corrections.IProblemLocation in project eclipse.jdt.ls by eclipse.

the class CodeActionHandler method getProblemLocations.

private IProblemLocation[] getProblemLocations(ICompilationUnit unit, List<Diagnostic> diagnostics) {
    IProblemLocation[] locations = new IProblemLocation[diagnostics.size()];
    for (int i = 0; i < diagnostics.size(); i++) {
        Diagnostic diagnostic = diagnostics.get(i);
        int problemId = getProblemId(diagnostic);
        int start = DiagnosticsHelper.getStartOffset(unit, diagnostic.getRange());
        int end = DiagnosticsHelper.getEndOffset(unit, diagnostic.getRange());
        boolean isError = diagnostic.getSeverity() == DiagnosticSeverity.Error;
        locations[i] = new ProblemLocation(start, end - start, problemId, isError);
    }
    return locations;
}
Also used : Diagnostic(org.eclipse.lsp4j.Diagnostic) IProblemLocation(org.eclipse.jdt.ls.core.internal.corrections.IProblemLocation) ProblemLocation(org.eclipse.jdt.ls.core.internal.corrections.ProblemLocation) IProblemLocation(org.eclipse.jdt.ls.core.internal.corrections.IProblemLocation)

Example 3 with IProblemLocation

use of org.eclipse.jdt.ls.core.internal.corrections.IProblemLocation in project eclipse.jdt.ls by eclipse.

the class UnimplementedCodeFix method createCleanUp.

public static ICleanUpFix createCleanUp(CompilationUnit root, boolean addMissingMethod, boolean makeTypeAbstract, IProblemLocation[] problems) {
    Assert.isLegal(!addMissingMethod || !makeTypeAbstract);
    if (!addMissingMethod && !makeTypeAbstract) {
        return null;
    }
    if (problems.length == 0) {
        return null;
    }
    ArrayList<CompilationUnitRewriteOperation> operations = new ArrayList<>();
    for (int i = 0; i < problems.length; i++) {
        IProblemLocation problem = problems[i];
        if (addMissingMethod) {
            ASTNode typeNode = getSelectedTypeNode(root, problem);
            if (typeNode != null && !isTypeBindingNull(typeNode)) {
                operations.add(new AddUnimplementedMethodsOperation(typeNode));
            }
        } else {
            ASTNode typeNode = getSelectedTypeNode(root, problem);
            if (typeNode instanceof TypeDeclaration) {
                operations.add(new MakeTypeAbstractOperation((TypeDeclaration) typeNode));
            }
        }
    }
    if (operations.size() == 0) {
        return null;
    }
    String label;
    if (addMissingMethod) {
        label = CorrectionMessages.UnimplementedMethodsCorrectionProposal_description;
    } else {
        label = CorrectionMessages.UnimplementedCodeFix_MakeAbstractFix_label;
    }
    return new UnimplementedCodeFix(label, root, operations.toArray(new CompilationUnitRewriteOperation[operations.size()]));
}
Also used : ArrayList(java.util.ArrayList) ASTNode(org.eclipse.jdt.core.dom.ASTNode) IProblemLocation(org.eclipse.jdt.ls.core.internal.corrections.IProblemLocation) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 4 with IProblemLocation

use of org.eclipse.jdt.ls.core.internal.corrections.IProblemLocation in project eclipse.jdt.ls by eclipse.

the class PotentialProgrammingProblemsFix method createCleanUp.

public static ICleanUpFix createCleanUp(CompilationUnit compilationUnit, boolean addSerialVersionIds) {
    IProblem[] problems = compilationUnit.getProblems();
    IProblemLocation[] locations = new IProblemLocation[problems.length];
    for (int i = 0; i < problems.length; i++) {
        // locations[i]= new ProblemLocation(problems[i]);
        boolean isError = problems[i].isError();
        int problemId = problems[i].getID();
        int length = problems[i].getSourceEnd() - problems[i].getSourceStart();
        int offset = problems[i].getSourceStart();
        locations[i] = new ProblemLocation(offset, length, problemId, isError);
    }
    return createCleanUp(compilationUnit, locations, addSerialVersionIds);
}
Also used : IProblemLocation(org.eclipse.jdt.ls.core.internal.corrections.IProblemLocation) ProblemLocation(org.eclipse.jdt.ls.core.internal.corrections.ProblemLocation) IProblemLocation(org.eclipse.jdt.ls.core.internal.corrections.IProblemLocation) IProblem(org.eclipse.jdt.core.compiler.IProblem)

Aggregations

IProblemLocation (org.eclipse.jdt.ls.core.internal.corrections.IProblemLocation)4 ArrayList (java.util.ArrayList)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 ProblemLocation (org.eclipse.jdt.ls.core.internal.corrections.ProblemLocation)2 Hashtable (java.util.Hashtable)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 IProblem (org.eclipse.jdt.core.compiler.IProblem)1 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)1 CastExpression (org.eclipse.jdt.core.dom.CastExpression)1 IBinding (org.eclipse.jdt.core.dom.IBinding)1 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)1 ImportDeclaration (org.eclipse.jdt.core.dom.ImportDeclaration)1 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)1 SimpleName (org.eclipse.jdt.core.dom.SimpleName)1 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)1 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)1 Diagnostic (org.eclipse.lsp4j.Diagnostic)1