Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add final modifier where possible #1234

Merged
merged 1 commit into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
import org.eclipse.jdt.internal.corext.fix.IProposableFix;
import org.eclipse.jdt.internal.corext.fix.LambdaExpressionsFixCore;
import org.eclipse.jdt.internal.corext.fix.LinkedProposalModelCore;
import org.eclipse.jdt.internal.corext.fix.VariableDeclarationFixCore;
import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTesterCore;
import org.eclipse.jdt.internal.corext.refactoring.code.ConvertAnonymousToNestedRefactoring;
import org.eclipse.jdt.internal.corext.refactoring.code.InlineConstantRefactoring;
Expand Down Expand Up @@ -222,7 +223,7 @@ public List<ChangeCorrectionProposal> getAssists(CodeActionParams params, IInvoc
// }
// getConvertEnhancedForLoopProposal(context, coveringNode, resultingCollections);
// getRemoveBlockProposals(context, coveringNode, resultingCollections);
// getMakeVariableDeclarationFinalProposals(context, resultingCollections);
getMakeVariableDeclarationFinalProposals(context, resultingCollections);
// getConvertStringConcatenationProposals(context, resultingCollections);
// getMissingCaseStatementProposals(context, coveringNode, resultingCollections);
getConvertVarTypeToResolvedTypeProposal(context, coveringNode, resultingCollections);
Expand Down Expand Up @@ -1375,6 +1376,19 @@ private static boolean isNotYetThrown(ITypeBinding binding, List<Type> thrownExc
return true;
}

private static boolean getMakeVariableDeclarationFinalProposals(IInvocationContext context, Collection<ChangeCorrectionProposal> resultingCollections) {
IProposableFix fix = (IProposableFix) VariableDeclarationFixCore.createCleanUp(context.getASTRoot(), true, true, true);

if (fix == null) {
return false;
}

FixCorrectionProposal proposal = new FixCorrectionProposal(fix, null, IProposalRelevance.MAKE_VARIABLE_DECLARATION_FINAL, context);
proposal.setDisplayName("Change modifiers to final where possible");
resultingCollections.add(proposal);
NikolasKomonen marked this conversation as resolved.
Show resolved Hide resolved
return true;
}

/**
* Create static import proposal
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -896,4 +896,28 @@ public void testInvisibleTypeRequestedFromSuperClass() throws Exception {

assertCodeActions(cu, e1);
}

@Test
public void testInsertFinalModifierWherePossible() throws Exception {
IPackageFragment pack = fSourceFolder.createPackageFragment("test", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package test;\n");
buf.append("public class A {\n");
buf.append(" public static void abc(int x){\n");
buf.append(" int b = 3;\n");
buf.append(" }\n");
buf.append("}");
ICompilationUnit cu = pack.createCompilationUnit("A.java", buf.toString(), false, null);

buf = new StringBuilder();
buf.append("package test;\n");
buf.append("public class A {\n");
buf.append(" public static void abc(final int x){\n");
buf.append(" final int b = 3;\n");
buf.append(" }\n");
buf.append("}");
Expected e1 = new Expected("Change modifiers to final where possible", buf.toString());

assertCodeActions(cu, e1);
}
}