Skip to content

Commit

Permalink
Add setting to make fields created via code action final
Browse files Browse the repository at this point in the history
Adds a new `codeGeneration.newFieldsFinal` boolean setting which changes
the behavior of the "Assign parameter to new field" code action to
declare the new field as final.

In many applications it's preferred to make classes immutable if
possible. In such cases making fields final by default saves some
tedious editing.
  • Loading branch information
mfussenegger committed Dec 9, 2022
1 parent 438e619 commit a5ad8a5
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
import org.eclipse.jdt.internal.corext.util.Messages;
import org.eclipse.jdt.ls.core.internal.JavaCodeActionKind;
import org.eclipse.jdt.ls.core.internal.corrections.CorrectionMessages;
import org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager;
import org.eclipse.jdt.ls.core.internal.preferences.Preferences;
import org.eclipse.jdt.ls.core.internal.text.correction.ModifierCorrectionSubProcessor;

/**
Expand Down Expand Up @@ -255,13 +257,18 @@ private ASTRewrite doAddField(ASTRewrite rewrite, ASTNode nodeToAssign, ITypeBin
return null;
}

IJavaProject project= getCompilationUnit().getJavaProject();
ICompilationUnit compilationUnit = getCompilationUnit();
IJavaProject project= compilationUnit.getJavaProject();
boolean isAnonymous= newTypeDecl.getNodeType() == ASTNode.ANONYMOUS_CLASS_DECLARATION;
boolean isStatic= Modifier.isStatic(bodyDecl.getModifiers()) && !isAnonymous;
int modifiers= Modifier.PRIVATE;
if (isStatic) {
modifiers |= Modifier.STATIC;
}
Preferences prefs = PreferenceManager.getPrefs(compilationUnit.getResource());
if (isParamToField && prefs.getCodeGenerationNewFieldsFinal()) {
modifiers |= Modifier.FINAL;
}

VariableDeclarationFragment newDeclFrag= addFieldDeclaration(rewrite, newTypeDecl, modifiers, expression, nodeToAssign, typeBinding, index);
String varName= newDeclFrag.getName().getIdentifier();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,9 @@ public class Preferences {
// A named preference that defines the location to insert the code generated by source actions.
public static final String JAVA_CODEGENERATION_INSERTIONLOCATION = "java.codeGeneration.insertionLocation";

// A named preference that defines that fields created via code generation should be declared final
public static final String JAVA_CODEGENERATION_NEW_FIELDS_FINAL = "java.codeGeneration.newFieldsFinal";

// Specifies the file header snippets for new Java file.
public static final String JAVA_TEMPLATES_FILEHEADER = "java.templates.fileHeader";
// Specifies the type comment snippets for new Java type.
Expand Down Expand Up @@ -571,6 +574,7 @@ public class Preferences {
private boolean generateToStringListArrayContents;
private int generateToStringLimitElements;
private String codeGenerationInsertionLocation;
private boolean codeGenerationNewFieldsFinal;
private List<String> preferredContentProviderIds;
private boolean includeAccessors;
private boolean includeDecompiledSources;
Expand Down Expand Up @@ -812,6 +816,7 @@ public Preferences() {
generateToStringSkipNullValues = false;
generateToStringListArrayContents = true;
generateToStringLimitElements = 0;
codeGenerationNewFieldsFinal = false;
codeGenerationInsertionLocation = null;
preferredContentProviderIds = null;
javaImportExclusions = JAVA_IMPORT_EXCLUSIONS_DEFAULT;
Expand Down Expand Up @@ -1006,6 +1011,9 @@ public static Preferences createFrom(Map<String, Object> configuration) {
String insertionLocation = getString(configuration, JAVA_CODEGENERATION_INSERTIONLOCATION);
prefs.setCodeGenerationInsertionLocation(insertionLocation);

boolean newFieldsFinal = getBoolean(configuration, JAVA_CODEGENERATION_NEW_FIELDS_FINAL);
prefs.setCodeGenerationNewFieldsFinal(newFieldsFinal);

List<String> javaImportExclusions = getList(configuration, JAVA_IMPORT_EXCLUSIONS_KEY, JAVA_IMPORT_EXCLUSIONS_DEFAULT);
if (javaImportExclusions instanceof LinkedList) {
prefs.setJavaImportExclusions(javaImportExclusions);
Expand Down Expand Up @@ -1739,6 +1747,14 @@ public int getGenerateToStringLimitElements() {
return generateToStringLimitElements;
}

public boolean getCodeGenerationNewFieldsFinal() {
return codeGenerationNewFieldsFinal;
}

public void setCodeGenerationNewFieldsFinal(boolean newFieldsFinal) {
this.codeGenerationNewFieldsFinal = newFieldsFinal;
}

public String getCodeGenerationInsertionLocation() {
return codeGenerationInsertionLocation;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,37 @@ public void testAssignParamToField() throws Exception {
assertCodeActions(cu, selection, e1);
}

@Test
public void testAssignParamToFieldWithFinalSetting() throws Exception {
preferences.setCodeGenerationNewFieldsFinal(true);
try {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" public E(int count) {\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);

buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" private final int count;\n");
buf.append("\n");
buf.append(" public E(int count) {\n");
buf.append(" this.count = count;\n");
buf.append(" }\n");
buf.append("}\n");
Expected e1 = new Expected("Assign parameter to new field", buf.toString());

Range selection = CodeActionUtil.getRange(cu, "count");
assertCodeActions(cu, selection, e1);
} finally {
preferences.setCodeGenerationNewFieldsFinal(false);
}
}

@Test
public void testAssignParamToField2() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
Expand Down

0 comments on commit a5ad8a5

Please sign in to comment.