Skip to content

Commit

Permalink
Add quick fix for sealed class within empty switch expression.
Browse files Browse the repository at this point in the history
- When a sealed class is the target of an empty switch expression, add
  all the types it permits as case statements

Signed-off-by: Roland Grunberg <[email protected]>
  • Loading branch information
rgrunber committed Dec 17, 2024
1 parent 3a6cd74 commit 25c9dea
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -548,8 +548,11 @@ private void process(CodeActionParams params, IInvocationContext context, IProbl
case IProblem.SwitchExpressionsYieldMissingEnumConstantCase:
LocalCorrectionsSubProcessor.getMissingEnumConstantCaseProposals(context, problem, proposals);
break;
case IProblem.MissingDefaultCase:
case IProblem.EnhancedSwitchMissingDefault:
case IProblem.SwitchExpressionsYieldMissingDefaultCase:
LocalCorrectionsSubProcessor.addTypeAsPermittedSubTypeProposal(context, problem, proposals);
break;
case IProblem.MissingDefaultCase:
LocalCorrectionsSubProcessor.addMissingDefaultCaseProposal(context, problem, proposals);
break;
case IProblem.MissingEnumConstantCaseDespiteDefault:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,23 @@
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
import org.eclipse.jdt.internal.ui.fix.CodeStyleCleanUpCore;
import org.eclipse.jdt.internal.ui.fix.UnnecessaryCodeCleanUpCore;
import org.eclipse.jdt.internal.ui.text.correction.LocalCorrectionsBaseSubProcessor;
import org.eclipse.jdt.internal.ui.text.correction.proposals.ChangeMethodSignatureProposalCore;
import org.eclipse.jdt.internal.ui.text.correction.proposals.ChangeMethodSignatureProposalCore.ChangeDescription;
import org.eclipse.jdt.internal.ui.text.correction.proposals.ChangeMethodSignatureProposalCore.InsertDescription;
import org.eclipse.jdt.internal.ui.text.correction.proposals.ChangeMethodSignatureProposalCore.RemoveDescription;
import org.eclipse.jdt.internal.ui.text.correction.proposals.ConstructorFromSuperclassProposalCore;
import org.eclipse.jdt.internal.ui.text.correction.proposals.CreateNewObjectProposalCore;
import org.eclipse.jdt.internal.ui.text.correction.proposals.CreateObjectReferenceProposalCore;
import org.eclipse.jdt.internal.ui.text.correction.proposals.CreateVariableReferenceProposalCore;
import org.eclipse.jdt.internal.ui.text.correction.proposals.FixCorrectionProposalCore;
import org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposalCore;
import org.eclipse.jdt.internal.ui.text.correction.proposals.MissingAnnotationAttributesProposalCore;
import org.eclipse.jdt.internal.ui.text.correction.proposals.ModifierChangeCorrectionProposalCore;
import org.eclipse.jdt.internal.ui.text.correction.proposals.NewMethodCorrectionProposalCore;
import org.eclipse.jdt.internal.ui.text.correction.proposals.NewVariableCorrectionProposalCore;
import org.eclipse.jdt.internal.ui.text.correction.proposals.RefactoringCorrectionProposalCore;
import org.eclipse.jdt.internal.ui.text.correction.proposals.ReplaceCorrectionProposalCore;
import org.eclipse.jdt.internal.ui.util.ASTHelper;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.eclipse.jdt.ls.core.internal.corrections.CorrectionMessages;
Expand All @@ -126,7 +134,7 @@
import org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposalCore;
import org.eclipse.lsp4j.CodeActionKind;

public class LocalCorrectionsSubProcessor {
public class LocalCorrectionsSubProcessor extends LocalCorrectionsBaseSubProcessor<ProposalKindWrapper> {

private static final String ADD_STATIC_ACCESS_ID = "org.eclipse.jdt.ui.correction.changeToStatic"; //$NON-NLS-1$

Expand Down Expand Up @@ -1085,22 +1093,7 @@ public static void getTryWithResourceProposals(IInvocationContext context, IProb
}

public static void addTypeAsPermittedSubTypeProposal(IInvocationContext context, IProblemLocation problem, Collection<ProposalKindWrapper> proposals) {
SealedClassFixCore fix = SealedClassFixCore.addTypeAsPermittedSubTypeProposal(context.getASTRoot(), problem);
if (fix != null) {
String label = fix.getDisplayString();
CompilationUnitChange change;
try {
change = fix.createChange(null);

ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
IType sealedType = SealedClassFixCore.getSealedType(selectedNode);
ICompilationUnit unit = SealedClassFixCore.getCompilationUnitForSealedType(sealedType);
CUCorrectionProposalCore proposal = new CUCorrectionProposalCore(label, unit, change, IProposalRelevance.DECLARE_SEALED_AS_DIRECT_SUPER_TYPE);
proposals.add(CodeActionHandler.wrap(proposal, CodeActionKind.QuickFix));
} catch (CoreException e) {
// do nothing
}
}
new LocalCorrectionsSubProcessor().getPermittedTypesProposal(context, problem, proposals);
}

public static void addSealedAsDirectSuperTypeProposal(IInvocationContext context, IProblemLocation problem, Collection<ProposalKindWrapper> proposals) {
Expand Down Expand Up @@ -1130,4 +1123,135 @@ public static void addValueForAnnotationProposals(IInvocationContext context, IP
}
}

/* (non-Javadoc)
* @see org.eclipse.jdt.internal.ui.text.correction.LocalCorrectionsBaseSubProcessor#refactoringCorrectionProposalToT(org.eclipse.jdt.internal.ui.text.correction.proposals.RefactoringCorrectionProposalCore, int)
*/
@Override
protected ProposalKindWrapper refactoringCorrectionProposalToT(RefactoringCorrectionProposalCore core, int uid) {
// TODO Auto-generated method stub
return null;
}

@Override
protected ProposalKindWrapper linkedCorrectionProposalToT(LinkedCorrectionProposalCore core, int uid) {
return CodeActionHandler.wrap(core, CodeActionKind.QuickFix);
}

/* (non-Javadoc)
* @see org.eclipse.jdt.internal.ui.text.correction.LocalCorrectionsBaseSubProcessor#changeMethodSignatureProposalToT(org.eclipse.jdt.internal.ui.text.correction.proposals.ChangeMethodSignatureProposalCore, int)
*/
@Override
protected ProposalKindWrapper changeMethodSignatureProposalToT(ChangeMethodSignatureProposalCore core, int uid) {
// TODO Auto-generated method stub
return null;
}

/* (non-Javadoc)
* @see org.eclipse.jdt.internal.ui.text.correction.LocalCorrectionsBaseSubProcessor#fixCorrectionProposalToT(org.eclipse.jdt.internal.ui.text.correction.proposals.FixCorrectionProposalCore, int)
*/
@Override
protected ProposalKindWrapper fixCorrectionProposalToT(FixCorrectionProposalCore core, int uid) {
// TODO Auto-generated method stub
return null;
}

/* (non-Javadoc)
* @see org.eclipse.jdt.internal.ui.text.correction.LocalCorrectionsBaseSubProcessor#constructorFromSuperClassProposalToT(org.eclipse.jdt.internal.ui.text.correction.proposals.ConstructorFromSuperclassProposalCore, int)
*/
@Override
protected ProposalKindWrapper constructorFromSuperClassProposalToT(ConstructorFromSuperclassProposalCore core, int uid) {
// TODO Auto-generated method stub
return null;
}

/* (non-Javadoc)
* @see org.eclipse.jdt.internal.ui.text.correction.LocalCorrectionsBaseSubProcessor#createNewObjectProposalToT(org.eclipse.jdt.internal.ui.text.correction.proposals.CreateNewObjectProposalCore, int)
*/
@Override
protected ProposalKindWrapper createNewObjectProposalToT(CreateNewObjectProposalCore core, int uid) {
// TODO Auto-generated method stub
return null;
}

/* (non-Javadoc)
* @see org.eclipse.jdt.internal.ui.text.correction.LocalCorrectionsBaseSubProcessor#createObjectReferenceProposalToT(org.eclipse.jdt.internal.ui.text.correction.proposals.CreateObjectReferenceProposalCore, int)
*/
@Override
protected ProposalKindWrapper createObjectReferenceProposalToT(CreateObjectReferenceProposalCore core, int uid) {
// TODO Auto-generated method stub
return null;
}

/* (non-Javadoc)
* @see org.eclipse.jdt.internal.ui.text.correction.LocalCorrectionsBaseSubProcessor#createVariableReferenceProposalToT(org.eclipse.jdt.internal.ui.text.correction.proposals.CreateVariableReferenceProposalCore, int)
*/
@Override
protected ProposalKindWrapper createVariableReferenceProposalToT(CreateVariableReferenceProposalCore core, int uid) {
// TODO Auto-generated method stub
return null;
}

/* (non-Javadoc)
* @see org.eclipse.jdt.internal.ui.text.correction.LocalCorrectionsBaseSubProcessor#astRewriteCorrectionProposalToT(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposalCore, int)
*/
@Override
protected ProposalKindWrapper astRewriteCorrectionProposalToT(ASTRewriteCorrectionProposalCore core, int uid) {
// TODO Auto-generated method stub
return null;
}

/* (non-Javadoc)
* @see org.eclipse.jdt.internal.ui.text.correction.LocalCorrectionsBaseSubProcessor#replaceCorrectionProposalToT(org.eclipse.jdt.internal.ui.text.correction.proposals.ReplaceCorrectionProposalCore, int)
*/
@Override
protected ProposalKindWrapper replaceCorrectionProposalToT(ReplaceCorrectionProposalCore core, int uid) {
// TODO Auto-generated method stub
return null;
}

/* (non-Javadoc)
* @see org.eclipse.jdt.internal.ui.text.correction.LocalCorrectionsBaseSubProcessor#cuCorrectionProposalToT(org.eclipse.jdt.core.manipulation.CUCorrectionProposalCore, int)
*/
@Override
protected ProposalKindWrapper cuCorrectionProposalToT(CUCorrectionProposalCore core, int uid) {
// TODO Auto-generated method stub
return null;
}

/* (non-Javadoc)
* @see org.eclipse.jdt.internal.ui.text.correction.LocalCorrectionsBaseSubProcessor#newVariableCorrectionProposalToT(org.eclipse.jdt.internal.ui.text.correction.proposals.NewVariableCorrectionProposalCore, int)
*/
@Override
protected ProposalKindWrapper newVariableCorrectionProposalToT(NewVariableCorrectionProposalCore core, int uid) {
// TODO Auto-generated method stub
return null;
}

/* (non-Javadoc)
* @see org.eclipse.jdt.internal.ui.text.correction.LocalCorrectionsBaseSubProcessor#missingAnnotationAttributesProposalToT(org.eclipse.jdt.internal.ui.text.correction.proposals.MissingAnnotationAttributesProposalCore, int)
*/
@Override
protected ProposalKindWrapper missingAnnotationAttributesProposalToT(MissingAnnotationAttributesProposalCore core, int uid) {
// TODO Auto-generated method stub
return null;
}

/* (non-Javadoc)
* @see org.eclipse.jdt.internal.ui.text.correction.LocalCorrectionsBaseSubProcessor#newMethodCorrectionProposalToT(org.eclipse.jdt.internal.ui.text.correction.proposals.NewMethodCorrectionProposalCore, int)
*/
@Override
protected ProposalKindWrapper newMethodCorrectionProposalToT(NewMethodCorrectionProposalCore core, int uid) {
// TODO Auto-generated method stub
return null;
}

/* (non-Javadoc)
* @see org.eclipse.jdt.internal.ui.text.correction.LocalCorrectionsBaseSubProcessor#modifierChangeCorrectionProposalToT(org.eclipse.jdt.internal.ui.text.correction.proposals.ModifierChangeCorrectionProposalCore, int)
*/
@Override
protected ProposalKindWrapper modifierChangeCorrectionProposalToT(ModifierChangeCorrectionProposalCore core, int uid) {
// TODO Auto-generated method stub
return null;
}

}

0 comments on commit 25c9dea

Please sign in to comment.