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()]));
}
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;
}
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()]));
}
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);
}
Aggregations