diff -r 9ff885f3bede java.hints/src/org/netbeans/modules/java/hints/Bundle.properties --- a/java.hints/src/org/netbeans/modules/java/hints/Bundle.properties Wed Apr 22 12:51:12 2009 +0200 +++ b/java.hints/src/org/netbeans/modules/java/hints/Bundle.properties Wed Apr 22 22:49:31 2009 +0100 @@ -241,6 +241,7 @@ ERR_EQUALS_NOT_CHECKING_TYPE=equals() method not checking type of its parameter DN_FieldUnusedParam=Assign Unused Parameter to Field +DSC_FieldUnusedParam=Assign Unused Parameter to Field FIX_AssignToExisting=Assign to Existing Field FIX_CreateField=Create Field ERR_UnusedParameter=Unused Parameter @@ -266,3 +267,4 @@ HINT_SuspiciousCallIncompatibleTypes=Suspicious call to {0}:\nGiven object cannot contain instances of {1} (expected {2}) DN_CollectionRemove=Suspicous method call DESC_CollectionRemove=Warns about suspicous calls to Collection.remove/contains and Map.containsKey/containsValue/remove +FieldForUnusedParamCustomizer.finalFields.text=Fields are final diff -r 9ff885f3bede java.hints/src/org/netbeans/modules/java/hints/FieldForUnusedParam.java --- a/java.hints/src/org/netbeans/modules/java/hints/FieldForUnusedParam.java Wed Apr 22 12:51:12 2009 +0200 +++ b/java.hints/src/org/netbeans/modules/java/hints/FieldForUnusedParam.java Wed Apr 22 22:49:32 2009 +0100 @@ -55,12 +55,14 @@ import java.util.List; import java.util.Set; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.prefs.Preferences; import javax.lang.model.element.Element; import javax.lang.model.element.ElementKind; import javax.lang.model.element.Modifier; import javax.lang.model.element.Name; import javax.lang.model.element.VariableElement; import javax.lang.model.util.ElementFilter; +import javax.swing.JComponent; import org.netbeans.api.java.source.CompilationInfo; import org.netbeans.api.java.source.GeneratorUtilities; import org.netbeans.api.java.source.JavaSource; @@ -71,7 +73,7 @@ import org.netbeans.api.java.source.WorkingCopy; import org.netbeans.api.java.source.support.CancellableTreePathScanner; import org.netbeans.api.java.source.support.CaretAwareJavaSourceTaskFactory; -import org.netbeans.modules.java.hints.spi.TreeRule; +import org.netbeans.modules.java.hints.spi.AbstractHint; import org.netbeans.spi.editor.hints.ChangeInfo; import org.netbeans.spi.editor.hints.ErrorDescription; import org.netbeans.spi.editor.hints.ErrorDescriptionFactory; @@ -82,10 +84,25 @@ /** * * @author Jan Lahoda + * @author Sam Halliday */ -public class FieldForUnusedParam implements TreeRule { +public class FieldForUnusedParam extends AbstractHint { - private AtomicBoolean cancel = new AtomicBoolean(); + private static final String FINAL_FIELDS = "final-fields"; + + public static boolean isFinalFields() { + return new FieldForUnusedParam().getPreferences(null).getBoolean(FINAL_FIELDS, true); + } + + public static void setFinalFields(Preferences p, boolean selected) { + p.putBoolean(FINAL_FIELDS, selected); + } + + public FieldForUnusedParam() { + super(true, true, HintSeverity.CURRENT_LINE_WARNING); + } + + private final AtomicBoolean cancel = new AtomicBoolean(); public Set getTreeKinds() { return EnumSet.of(Kind.VARIABLE); @@ -181,8 +198,18 @@ return NbBundle.getMessage(FieldForUnusedParam.class, "DN_FieldUnusedParam"); } + @Override + public JComponent getCustomizer(Preferences node) { + return new FieldForUnusedParamCustomizer(node); + } + public void cancel() { cancel.set(true); + } + + @Override + public String getDescription() { + return NbBundle.getMessage(FieldForUnusedParam.class, "DSC_FieldUnusedParam"); } static final class FixImpl implements Fix { @@ -232,7 +259,11 @@ } if (!existing) { - VariableTree field = make.Variable(make.Modifiers(EnumSet.of(Modifier.PRIVATE)), vt.getName(), vt.getType(), null); + Set modifiers = EnumSet.of(Modifier.PRIVATE); + if (isFinalFields()) { + modifiers.add(Modifier.FINAL); + } + VariableTree field = make.Variable(make.Modifiers(modifiers), vt.getName(), vt.getType(), null); int insertPlace = -1; index = 0; diff -r 9ff885f3bede java.hints/src/org/netbeans/modules/java/hints/FieldForUnusedParamCustomizer.form --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java.hints/src/org/netbeans/modules/java/hints/FieldForUnusedParamCustomizer.form Wed Apr 22 22:49:32 2009 +0100 @@ -0,0 +1,51 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff -r 9ff885f3bede java.hints/src/org/netbeans/modules/java/hints/FieldForUnusedParamCustomizer.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java.hints/src/org/netbeans/modules/java/hints/FieldForUnusedParamCustomizer.java Wed Apr 22 22:49:32 2009 +0100 @@ -0,0 +1,91 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2009 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * Portions Copyrighted 2009 Sun Microsystems, Inc. + */ + +package org.netbeans.modules.java.hints; +import java.util.prefs.Preferences; + +/** + * + * @author Sam Halliday + */ +@SuppressWarnings("serial") +public class FieldForUnusedParamCustomizer extends javax.swing.JPanel { + + private Preferences p; + + /** Creates new form LocalVariableFixCustomizer */ + public FieldForUnusedParamCustomizer(Preferences p) { + initComponents(); + this.p = p; + finalFields.setSelected(FieldForUnusedParam.isFinalFields()); + } + + /** This method is called from within the constructor to + * initialize the form. + * WARNING: Do NOT modify this code. The content of this method is + * always regenerated by the Form Editor. + */ + @SuppressWarnings("unchecked") + // //GEN-BEGIN:initComponents + private void initComponents() { + + finalFields = new javax.swing.JCheckBox(); + + org.openide.awt.Mnemonics.setLocalizedText(finalFields, org.openide.util.NbBundle.getBundle(FieldForUnusedParamCustomizer.class).getString("FieldForUnusedParamCustomizer.finalFields.text")); // NOI18N + finalFields.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + finalFieldsActionPerformed(evt); + } + }); + + org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); + this.setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) + .add(layout.createSequentialGroup() + .addContainerGap() + .add(finalFields, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addContainerGap()) + ); + layout.setVerticalGroup( + layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) + .add(layout.createSequentialGroup() + .addContainerGap() + .add(finalFields) + .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + ); + }// //GEN-END:initComponents + + private void finalFieldsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_finalFieldsActionPerformed + FieldForUnusedParam.setFinalFields(p, finalFields.isSelected()); +}//GEN-LAST:event_finalFieldsActionPerformed + + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JCheckBox finalFields; + // End of variables declaration//GEN-END:variables +} diff -r 9ff885f3bede java.hints/src/org/netbeans/modules/java/hints/errors/CreateElement.java --- a/java.hints/src/org/netbeans/modules/java/hints/errors/CreateElement.java Wed Apr 22 12:51:12 2009 +0200 +++ b/java.hints/src/org/netbeans/modules/java/hints/errors/CreateElement.java Wed Apr 22 22:49:32 2009 +0100 @@ -77,6 +77,7 @@ import org.netbeans.api.java.source.CompilationInfo; import org.netbeans.api.java.source.ElementHandle; import org.netbeans.api.java.source.SourceUtils; +import org.netbeans.modules.java.hints.FieldForUnusedParam; import org.netbeans.modules.java.hints.errors.CreateClassFix.CreateInnerClassFix; import org.netbeans.modules.java.hints.errors.CreateClassFix.CreateOuterClassFix; import org.netbeans.modules.java.hints.infrastructure.ErrorHintsProvider; @@ -378,6 +379,9 @@ if (target.getKind() == ElementKind.ENUM) { result.add(new CreateEnumConstant(info, simpleName, modifiers, target, type, targetFile)); } else { + if (firstMethod != null && info.getTrees().getElement(firstMethod).getKind() == ElementKind.CONSTRUCTOR && FieldForUnusedParam.isFinalFields()) { + modifiers.add(Modifier.FINAL); + } result.add(new CreateFieldFix(info, simpleName, modifiers, target, type, targetFile)); } } diff -r 9ff885f3bede java.hints/src/org/netbeans/modules/java/hints/resources/layer.xml --- a/java.hints/src/org/netbeans/modules/java/hints/resources/layer.xml Wed Apr 22 12:51:12 2009 +0200 +++ b/java.hints/src/org/netbeans/modules/java/hints/resources/layer.xml Wed Apr 22 22:49:32 2009 +0100 @@ -139,6 +139,7 @@ + @@ -287,7 +288,6 @@ -