use of org.eclipse.jdt.ls.core.internal.corrections.proposals.ChangeCorrectionProposal in project eclipse.jdt.ls by eclipse.
the class CodeActionResolveHandler method resolve.
public CodeAction resolve(CodeAction params, IProgressMonitor monitor) {
Map<String, String> data = JSONUtility.toModel(params.getData(), Map.class);
// clean resolve data
params.setData(null);
int proposalId = Integer.parseInt(data.get(DATA_FIELD_PROPOSAL_ID));
long requestId = Long.parseLong(data.get(DATA_FIELD_REQUEST_ID));
ResponseStore.ResponseItem<Either<ChangeCorrectionProposal, CodeActionProposal>> response = CodeActionHandler.codeActionStore.get(requestId);
if (response == null || response.getProposals().size() <= proposalId) {
throw new IllegalStateException("Invalid codeAction proposal");
}
try {
Either<ChangeCorrectionProposal, CodeActionProposal> proposal = response.getProposals().get(proposalId);
WorkspaceEdit edit = proposal.isLeft() ? ChangeUtil.convertToWorkspaceEdit(proposal.getLeft().getChange()) : proposal.getRight().resolveEdit(monitor);
if (ChangeUtil.hasChanges(edit)) {
params.setEdit(edit);
}
} catch (CoreException e) {
JavaLanguageServerPlugin.logException("Problem converting code action proposal to workspace edit", e);
}
return params;
}
use of org.eclipse.jdt.ls.core.internal.corrections.proposals.ChangeCorrectionProposal in project eclipse.jdt.ls by eclipse.
the class RefactorProcessor method getInlineProposal.
private boolean getInlineProposal(IInvocationContext context, ASTNode node, Collection<ChangeCorrectionProposal> resultingCollections) {
if (resultingCollections == null) {
return false;
}
if (!(node instanceof SimpleName)) {
return false;
}
SimpleName name = (SimpleName) node;
IBinding binding = name.resolveBinding();
try {
if (binding instanceof IVariableBinding) {
IVariableBinding varBinding = (IVariableBinding) binding;
if (varBinding.isParameter()) {
return false;
}
if (varBinding.isField()) {
// Inline Constant (static final field)
if (RefactoringAvailabilityTesterCore.isInlineConstantAvailable((IField) varBinding.getJavaElement())) {
InlineConstantRefactoring refactoring = new InlineConstantRefactoring(context.getCompilationUnit(), context.getASTRoot(), context.getSelectionOffset(), context.getSelectionLength());
if (refactoring != null && refactoring.checkInitialConditions(new NullProgressMonitor()).isOK() && refactoring.getReferences(new NullProgressMonitor(), new RefactoringStatus()).length > 0) {
refactoring.setRemoveDeclaration(refactoring.isDeclarationSelected());
refactoring.setReplaceAllReferences(refactoring.isDeclarationSelected());
CheckConditionsOperation check = new CheckConditionsOperation(refactoring, CheckConditionsOperation.FINAL_CONDITIONS);
final CreateChangeOperation create = new CreateChangeOperation(check, RefactoringStatus.FATAL);
create.run(new NullProgressMonitor());
String label = ActionMessages.InlineConstantRefactoringAction_label;
int relevance = IProposalRelevance.INLINE_LOCAL;
ChangeCorrectionProposal proposal = new ChangeCorrectionProposal(label, CodeActionKind.RefactorInline, create.getChange(), relevance);
resultingCollections.add(proposal);
return true;
}
}
return false;
}
ASTNode decl = context.getASTRoot().findDeclaringNode(varBinding);
if (!(decl instanceof VariableDeclarationFragment) || decl.getLocationInParent() != VariableDeclarationStatement.FRAGMENTS_PROPERTY) {
return false;
}
// Inline Local Variable
if (binding.getJavaElement() instanceof ILocalVariable && RefactoringAvailabilityTesterCore.isInlineTempAvailable((ILocalVariable) binding.getJavaElement())) {
InlineTempRefactoring refactoring = new InlineTempRefactoring((VariableDeclaration) decl);
boolean status;
try {
status = refactoring.checkAllConditions(new NullProgressMonitor()).isOK();
} catch (Exception e) {
// ignore
status = false;
}
if (status && refactoring.getReferences().length > 0) {
String label = CorrectionMessages.QuickAssistProcessor_inline_local_description;
int relevance = IProposalRelevance.INLINE_LOCAL;
RefactoringCorrectionProposal proposal = new RefactoringCorrectionProposal(label, CodeActionKind.RefactorInline, context.getCompilationUnit(), refactoring, relevance);
resultingCollections.add(proposal);
return true;
}
}
} else if (binding instanceof IMethodBinding) {
// Inline Method
if (RefactoringAvailabilityTesterCore.isInlineMethodAvailable((IMethod) binding.getJavaElement())) {
InlineMethodRefactoring refactoring = InlineMethodRefactoring.create(context.getCompilationUnit(), context.getASTRoot(), context.getSelectionOffset(), context.getSelectionLength());
if (refactoring != null && refactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
CheckConditionsOperation check = new CheckConditionsOperation(refactoring, CheckConditionsOperation.FINAL_CONDITIONS);
final CreateChangeOperation create = new CreateChangeOperation(check, RefactoringStatus.FATAL);
create.run(new NullProgressMonitor());
String label = ActionMessages.InlineMethodRefactoringAction_label;
int relevance = IProposalRelevance.INLINE_LOCAL;
ChangeCorrectionProposal proposal = new ChangeCorrectionProposal(label, CodeActionKind.RefactorInline, create.getChange(), relevance);
resultingCollections.add(proposal);
return true;
}
}
}
} catch (CoreException e) {
JavaLanguageServerPlugin.log(e);
}
return false;
}
use of org.eclipse.jdt.ls.core.internal.corrections.proposals.ChangeCorrectionProposal in project eclipse.jdt.ls by eclipse.
the class RefactorProcessor method getInverseLocalVariableProposals.
private boolean getInverseLocalVariableProposals(CodeActionParams params, IInvocationContext context, ASTNode covering, Collection<ChangeCorrectionProposal> proposals) {
if (proposals == null) {
return false;
}
ChangeCorrectionProposal proposal = null;
if (this.preferenceManager.getClientPreferences().isAdvancedExtractRefactoringSupported()) {
proposal = InvertBooleanUtility.getInvertVariableProposal(params, context, covering, true);
} else {
proposal = InvertBooleanUtility.getInvertVariableProposal(params, context, covering, false);
}
if (proposal == null) {
return false;
}
proposals.add(proposal);
return true;
}
use of org.eclipse.jdt.ls.core.internal.corrections.proposals.ChangeCorrectionProposal in project eclipse.jdt.ls by eclipse.
the class RefactorProcessor method getAddStaticImportProposals.
/**
* Create static import proposal, which converts invocations to static import.
*
* @param context
* the invocation context
* @param node
* the node to work on
* @param proposals
* the receiver of proposals, may be {@code null}
* @return {@code true} if the operation could or has been performed,
* {@code false otherwise}
*/
private static boolean getAddStaticImportProposals(IInvocationContext context, ASTNode node, Collection<ChangeCorrectionProposal> proposals) {
if (!(node instanceof SimpleName)) {
return false;
}
final SimpleName name = (SimpleName) node;
final IBinding binding;
final ITypeBinding declaringClass;
if (name.getParent() instanceof MethodInvocation) {
MethodInvocation mi = (MethodInvocation) name.getParent();
Expression expression = mi.getExpression();
if (expression == null || expression.equals(name)) {
return false;
}
binding = mi.resolveMethodBinding();
if (binding == null) {
return false;
}
declaringClass = ((IMethodBinding) binding).getDeclaringClass();
} else if (name.getParent() instanceof QualifiedName) {
QualifiedName qn = (QualifiedName) name.getParent();
if (name.equals(qn.getQualifier()) || qn.getParent() instanceof ImportDeclaration) {
return false;
}
binding = qn.resolveBinding();
if (!(binding instanceof IVariableBinding)) {
return false;
}
declaringClass = ((IVariableBinding) binding).getDeclaringClass();
} else {
return false;
}
if (!Modifier.isStatic(binding.getModifiers())) {
// only work with static bindings
return false;
}
boolean needImport = false;
if (!isDirectlyAccessible(name, declaringClass)) {
if (Modifier.isPrivate(declaringClass.getModifiers())) {
return false;
}
needImport = true;
}
if (proposals == null) {
// return early, just testing if we could do it
return true;
}
try {
ImportRewrite importRewrite = StubUtility.createImportRewrite(context.getCompilationUnit(), true);
ASTRewrite astRewrite = ASTRewrite.create(node.getAST());
ASTRewrite astRewriteReplaceAllOccurrences = ASTRewrite.create(node.getAST());
ImportRemover remover = new ImportRemover(context.getCompilationUnit().getJavaProject(), context.getASTRoot());
ImportRemover removerAllOccurences = new ImportRemover(context.getCompilationUnit().getJavaProject(), context.getASTRoot());
MethodInvocation mi = null;
QualifiedName qn = null;
if (name.getParent() instanceof MethodInvocation) {
mi = (MethodInvocation) name.getParent();
// convert the method invocation
astRewrite.remove(mi.getExpression(), null);
remover.registerRemovedNode(mi.getExpression());
removerAllOccurences.registerRemovedNode(mi.getExpression());
mi.typeArguments().forEach(typeObject -> {
Type type = (Type) typeObject;
astRewrite.remove(type, null);
remover.registerRemovedNode(type);
removerAllOccurences.registerRemovedNode(type);
});
} else if (name.getParent() instanceof QualifiedName) {
qn = (QualifiedName) name.getParent();
// convert the field access
astRewrite.replace(qn, ASTNodeFactory.newName(node.getAST(), name.getFullyQualifiedName()), null);
remover.registerRemovedNode(qn);
removerAllOccurences.registerRemovedNode(qn);
} else {
return false;
}
MethodInvocation miFinal = mi;
name.getRoot().accept(new ASTVisitor() {
@Override
public boolean visit(MethodInvocation methodInvocation) {
Expression methodInvocationExpression = methodInvocation.getExpression();
if (methodInvocationExpression == null) {
return super.visit(methodInvocation);
}
if (methodInvocationExpression instanceof Name) {
String fullyQualifiedName = ((Name) methodInvocationExpression).getFullyQualifiedName();
if (miFinal != null && miFinal.getExpression() instanceof Name && ((Name) miFinal.getExpression()).getFullyQualifiedName().equals(fullyQualifiedName) && miFinal.getName().getIdentifier().equals(methodInvocation.getName().getIdentifier())) {
methodInvocation.typeArguments().forEach(type -> {
astRewriteReplaceAllOccurrences.remove((Type) type, null);
removerAllOccurences.registerRemovedNode((Type) type);
});
astRewriteReplaceAllOccurrences.remove(methodInvocationExpression, null);
removerAllOccurences.registerRemovedNode(methodInvocationExpression);
}
}
return super.visit(methodInvocation);
}
});
QualifiedName qnFinal = qn;
name.getRoot().accept(new ASTVisitor() {
@Override
public boolean visit(QualifiedName qualifiedName) {
if (qnFinal != null && qualifiedName.getFullyQualifiedName().equals(qnFinal.getFullyQualifiedName())) {
astRewriteReplaceAllOccurrences.replace(qualifiedName, ASTNodeFactory.newName(node.getAST(), name.getFullyQualifiedName()), null);
removerAllOccurences.registerRemovedNode(qualifiedName);
}
return super.visit(qualifiedName);
}
});
if (needImport) {
importRewrite.addStaticImport(binding);
}
ASTRewriteRemoveImportsCorrectionProposal proposal = new ASTRewriteRemoveImportsCorrectionProposal(CorrectionMessages.QuickAssistProcessor_convert_to_static_import, CodeActionKind.Refactor, context.getCompilationUnit(), astRewrite, IProposalRelevance.ADD_STATIC_IMPORT);
proposal.setImportRewrite(importRewrite);
proposal.setImportRemover(remover);
proposals.add(proposal);
ASTRewriteRemoveImportsCorrectionProposal proposalReplaceAllOccurrences = new ASTRewriteRemoveImportsCorrectionProposal(CorrectionMessages.QuickAssistProcessor_convert_to_static_import_replace_all, CodeActionKind.Refactor, context.getCompilationUnit(), astRewriteReplaceAllOccurrences, IProposalRelevance.ADD_STATIC_IMPORT);
proposalReplaceAllOccurrences.setImportRewrite(importRewrite);
proposalReplaceAllOccurrences.setImportRemover(removerAllOccurences);
proposals.add(proposalReplaceAllOccurrences);
} catch (IllegalArgumentException e) {
// Wrong use of ASTRewrite or ImportRewrite API, see bug 541586
JavaLanguageServerPlugin.logException("Failed to get static import proposal", e);
return false;
} catch (JavaModelException e) {
return false;
}
return true;
}
use of org.eclipse.jdt.ls.core.internal.corrections.proposals.ChangeCorrectionProposal in project eclipse.jdt.ls by eclipse.
the class QuickFixProcessor method getCorrections.
public List<ChangeCorrectionProposal> getCorrections(IInvocationContext context, IProblemLocationCore[] locations) throws CoreException {
if (locations == null || locations.length == 0) {
return Collections.emptyList();
}
ArrayList<ChangeCorrectionProposal> resultingCollections = new ArrayList<>();
Set<Integer> handledProblems = new HashSet<>(locations.length);
for (int i = 0; i < locations.length; i++) {
IProblemLocationCore curr = locations[i];
if (handledProblems(curr, locations, handledProblems)) {
process(context, curr, resultingCollections);
}
}
return resultingCollections;
}
Aggregations