diff options
| author | Markus Keller | 2012-04-16 17:49:58 +0000 |
|---|---|---|
| committer | Markus Keller | 2012-04-16 17:49:58 +0000 |
| commit | e1164504c89003e2a6ee1c3c312ae6247c431cfb (patch) | |
| tree | 7b3dc80d991613cfef8c235824631f0aad8b6142 | |
| parent | e8e53568a96489ee4817e4649f1a5aa3f31fa6e7 (diff) | |
| download | eclipse.jdt.ui-e1164504c89003e2a6ee1c3c312ae6247c431cfb.tar.gz eclipse.jdt.ui-e1164504c89003e2a6ee1c3c312ae6247c431cfb.tar.xz eclipse.jdt.ui-e1164504c89003e2a6ee1c3c312ae6247c431cfb.zip | |
Bug 130922: [quick assist] 'Assign parameter to new field' must not make
field final'
3 files changed, 43 insertions, 9 deletions
diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/AssistQuickFixTest.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/AssistQuickFixTest.java index a7447f5de7..01d8af414b 100644 --- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/AssistQuickFixTest.java +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/AssistQuickFixTest.java @@ -895,7 +895,7 @@ public class AssistQuickFixTest extends QuickFixTest { buf= new StringBuffer(); buf.append("package test1;\n"); buf.append("public class E {\n"); - buf.append(" private final int count;\n"); + buf.append(" private int count;\n"); buf.append("\n"); buf.append(" public E(int count) {\n"); buf.append(" this.count = count;\n"); @@ -934,7 +934,7 @@ public class AssistQuickFixTest extends QuickFixTest { buf.append("package test1;\n"); buf.append("import java.util.Vector;\n"); buf.append("public class E {\n"); - buf.append(" private final Vector[] fVec;\n"); + buf.append(" private Vector[] fVec;\n"); buf.append("\n"); buf.append(" public E(int count, Vector vec[]) {\n"); buf.append(" super();\n"); @@ -1193,7 +1193,7 @@ public class AssistQuickFixTest extends QuickFixTest { buf.append("package test1;\n"); buf.append("import java.util.Vector;\n"); buf.append("public class E<T> {\n"); - buf.append(" private final Vector<String>[] fVec;\n"); + buf.append(" private Vector<String>[] fVec;\n"); buf.append("\n"); buf.append(" public E(int count, Vector<String>[] vec) {\n"); buf.append(" super();\n"); diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/saveparticipant/AbstractSaveParticipantPreferenceConfiguration.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/saveparticipant/AbstractSaveParticipantPreferenceConfiguration.java index 663889e139..ce901494fa 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/saveparticipant/AbstractSaveParticipantPreferenceConfiguration.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/saveparticipant/AbstractSaveParticipantPreferenceConfiguration.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2011 IBM Corporation and others. + * Copyright (c) 2000, 2012 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -35,7 +35,7 @@ import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogFie public abstract class AbstractSaveParticipantPreferenceConfiguration implements ISaveParticipantPreferenceConfiguration { /** - * Preference prefix that is appended to the id of {@link SaveParticipantDescriptor save participants}. + * Preference prefix that is prepended to the id of {@link SaveParticipantDescriptor save participants}. * * <p> * Value is of type <code>Boolean</code>. @@ -44,7 +44,7 @@ public abstract class AbstractSaveParticipantPreferenceConfiguration implements * @see SaveParticipantDescriptor * @since 3.3 */ - private static final String EDITOR_SAVE_PARTICIPANT_PREFIX= "editor_save_participant_"; //$NON-NLS-1$ + public static final String EDITOR_SAVE_PARTICIPANT_PREFIX= "editor_save_participant_"; //$NON-NLS-1$ private SelectionButtonDialogField fEnableField; private IScopeContext fContext; diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/proposals/AssignToVariableAssistProposal.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/proposals/AssignToVariableAssistProposal.java index 6d92be1017..322f89f61c 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/proposals/AssignToVariableAssistProposal.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/proposals/AssignToVariableAssistProposal.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2011 IBM Corporation and others. + * Copyright (c) 2000, 2012 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -17,6 +17,11 @@ import java.util.HashSet; import java.util.List; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.preferences.IScopeContext; +import org.eclipse.core.runtime.preferences.InstanceScope; + +import org.eclipse.core.resources.ProjectScope; import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jdt.core.IJavaProject; @@ -57,9 +62,17 @@ import org.eclipse.jdt.internal.corext.dom.ASTNodes; import org.eclipse.jdt.internal.corext.dom.Bindings; import org.eclipse.jdt.internal.corext.dom.LinkedNodeFinder; import org.eclipse.jdt.internal.corext.dom.TokenScanner; +import org.eclipse.jdt.internal.corext.fix.CleanUpConstants; +import org.eclipse.jdt.internal.corext.fix.CleanUpPostSaveListener; +import org.eclipse.jdt.internal.corext.fix.CleanUpPreferenceUtil; import org.eclipse.jdt.internal.corext.util.Messages; +import org.eclipse.jdt.ui.PreferenceConstants; +import org.eclipse.jdt.ui.cleanup.CleanUpOptions; + +import org.eclipse.jdt.internal.ui.JavaPlugin; import org.eclipse.jdt.internal.ui.JavaPluginImages; +import org.eclipse.jdt.internal.ui.javaeditor.saveparticipant.AbstractSaveParticipantPreferenceConfiguration; import org.eclipse.jdt.internal.ui.text.correction.ASTResolving; import org.eclipse.jdt.internal.ui.text.correction.CorrectionMessages; import org.eclipse.jdt.internal.ui.text.correction.ModifierCorrectionSubProcessor; @@ -209,6 +222,7 @@ public class AssignToVariableAssistProposal extends LinkedCorrectionProposal { return null; } + IJavaProject project= getCompilationUnit().getJavaProject(); boolean isAnonymous= newTypeDecl.getNodeType() == ASTNode.ANONYMOUS_CLASS_DECLARATION; boolean isStatic= Modifier.isStatic(bodyDecl.getModifiers()) && !isAnonymous; boolean isConstructorParam= isParamToField && fNodeToAssign.getParent() instanceof MethodDeclaration && ((MethodDeclaration) fNodeToAssign.getParent()).isConstructor(); @@ -216,7 +230,27 @@ public class AssignToVariableAssistProposal extends LinkedCorrectionProposal { if (isStatic) { modifiers |= Modifier.STATIC; } else if (isConstructorParam) { - modifiers |= Modifier.FINAL; + String saveActionsKey= AbstractSaveParticipantPreferenceConfiguration.EDITOR_SAVE_PARTICIPANT_PREFIX + CleanUpPostSaveListener.POSTSAVELISTENER_ID; + IScopeContext[] scopes= { InstanceScope.INSTANCE, new ProjectScope(project.getProject()) }; + boolean safeActionsEnabled= Platform.getPreferencesService().getBoolean(JavaPlugin.getPluginId(), saveActionsKey, false, scopes); + if (safeActionsEnabled + && CleanUpOptions.TRUE.equals(PreferenceConstants.getPreference(CleanUpPreferenceUtil.SAVE_PARTICIPANT_KEY_PREFIX + CleanUpConstants.CLEANUP_ON_SAVE_ADDITIONAL_OPTIONS, project)) + && CleanUpOptions.TRUE.equals(PreferenceConstants.getPreference(CleanUpPreferenceUtil.SAVE_PARTICIPANT_KEY_PREFIX + CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL, project)) + && CleanUpOptions.TRUE.equals(PreferenceConstants.getPreference(CleanUpPreferenceUtil.SAVE_PARTICIPANT_KEY_PREFIX + CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_PRIVATE_FIELDS, project)) + ) { + int constructors= 0; + if (newTypeDecl instanceof AbstractTypeDeclaration) { + List<BodyDeclaration> bodyDeclarations= ((AbstractTypeDeclaration) newTypeDecl).bodyDeclarations(); + for (BodyDeclaration decl : bodyDeclarations) { + if (decl instanceof MethodDeclaration && ((MethodDeclaration) decl).isConstructor()) { + constructors++; + } + } + } + if (constructors == 1) { + modifiers |= Modifier.FINAL; + } + } } VariableDeclarationFragment newDeclFrag= addFieldDeclaration(rewrite, newTypeDecl, modifiers, expression); @@ -225,7 +259,7 @@ public class AssignToVariableAssistProposal extends LinkedCorrectionProposal { Assignment assignment= ast.newAssignment(); assignment.setRightHandSide((Expression) rewrite.createCopyTarget(expression)); - boolean needsThis= StubUtility.useThisForFieldAccess(getCompilationUnit().getJavaProject()); + boolean needsThis= StubUtility.useThisForFieldAccess(project); if (isParamToField) { needsThis |= varName.equals(((SimpleName) expression).getIdentifier()); } |
