# HG changeset patch # Parent bdb0f6cd71ec4c13f405d9b5ae66292bf2db263e # User Ralph Benjamin Ruijs [no commit message] diff -r bdb0f6cd71ec -r 2b309a744129 beans/nbproject/project.xml --- a/beans/nbproject/project.xml Tue Apr 23 01:17:09 2013 +0200 +++ b/beans/nbproject/project.xml Tue Apr 23 01:23:45 2013 +0200 @@ -150,7 +150,7 @@ - 0.95 + 0.122 diff -r bdb0f6cd71ec -r 2b309a744129 beans/src/org/netbeans/modules/beans/addproperty/AddPropertyCodeGenerator.java --- a/beans/src/org/netbeans/modules/beans/addproperty/AddPropertyCodeGenerator.java Tue Apr 23 01:17:09 2013 +0200 +++ b/beans/src/org/netbeans/modules/beans/addproperty/AddPropertyCodeGenerator.java Tue Apr 23 01:23:45 2013 +0200 @@ -68,7 +68,7 @@ import javax.swing.text.StyledDocument; import org.netbeans.api.editor.guards.GuardedSection; import org.netbeans.api.editor.guards.GuardedSectionManager; -import org.netbeans.api.java.source.ClasspathInfo; +import org.netbeans.api.java.source.CodeStyle; import org.netbeans.api.java.source.CompilationController; import org.netbeans.api.java.source.CompilationInfo; import org.netbeans.api.java.source.JavaSource; @@ -78,7 +78,6 @@ import org.netbeans.api.java.source.TreeUtilities; import org.netbeans.api.java.source.WorkingCopy; import org.netbeans.editor.GuardedException; -import org.netbeans.editor.Utilities; import org.netbeans.modules.editor.indent.api.Reformat; import org.netbeans.spi.editor.codegen.CodeGenerator; import org.openide.DialogDescriptor; @@ -126,12 +125,16 @@ public void perform(FileObject file, JTextComponent pane) { JButton ok = new JButton(NbBundle.getMessage(AddPropertyCodeGenerator.class, "LBL_ButtonOK")); - final AddPropertyPanel addPropertyPanel = new AddPropertyPanel(file, className, existingFields, pcsName, vcsName, ok); + CodeStyle cs = CodeStyle.getDefault(pane.getDocument()); + if(cs == null) { + cs = CodeStyle.getDefault(file); + } + final AddPropertyPanel addPropertyPanel = new AddPropertyPanel(file, className, cs, existingFields, pcsName, vcsName, ok); String caption = NbBundle.getMessage(AddPropertyCodeGenerator.class, "CAP_AddProperty"); String cancel = NbBundle.getMessage(AddPropertyCodeGenerator.class, "LBL_ButtonCancel"); DialogDescriptor dd = new DialogDescriptor(addPropertyPanel,caption, true, new Object[] {ok,cancel}, ok, DialogDescriptor.DEFAULT_ALIGN, null, null); if (DialogDisplayer.getDefault().notify(dd) == ok) { - insertCode2(file, pane, addPropertyPanel.getAddPropertyConfig()); + insertCode2(file, pane, addPropertyPanel.getAddPropertyConfig(), cs); } } @@ -140,10 +143,10 @@ * @see 162853 * @see 162630 */ - static void insertCode2(final FileObject file, final JTextComponent pane, final AddPropertyConfig config) { + static void insertCode2(final FileObject file, final JTextComponent pane, final AddPropertyConfig config, CodeStyle cs) { final Document doc = pane.getDocument(); final Reformat r = Reformat.get(pane.getDocument()); - final String code = new AddPropertyGenerator().generate(config); + final String code = new AddPropertyGenerator().generate(config, cs); final Position[] bounds = new Position[2]; final int offset[] = new int[1]; diff -r bdb0f6cd71ec -r 2b309a744129 beans/src/org/netbeans/modules/beans/addproperty/AddPropertyGenerator.java --- a/beans/src/org/netbeans/modules/beans/addproperty/AddPropertyGenerator.java Tue Apr 23 01:17:09 2013 +0200 +++ b/beans/src/org/netbeans/modules/beans/addproperty/AddPropertyGenerator.java Tue Apr 23 01:23:45 2013 +0200 @@ -50,6 +50,8 @@ import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; +import org.netbeans.api.java.source.CodeStyle; +import org.netbeans.api.java.source.CodeStyleUtils; import org.openide.filesystems.FileObject; import org.openide.filesystems.FileUtil; import org.openide.util.Exceptions; @@ -60,15 +62,28 @@ */ public class AddPropertyGenerator { - public String generate(AddPropertyConfig addPropertyConfig) { + public String generate(AddPropertyConfig addPropertyConfig, CodeStyle cs) { ScriptEngine scriptEngine = getScriptEngine(); if (scriptEngine != null) { FileObject template = getTemplateFileObject(addPropertyConfig.getTEMPLATE_PATH()); if (template != null && template.isValid()) { final String type = addPropertyConfig.getType().trim(); final String name = addPropertyConfig.getName().trim(); + final String fieldName = CodeStyleUtils.addPrefixSuffix(name, + addPropertyConfig.isStatic() ? cs.getStaticFieldNamePrefix() : cs.getFieldNamePrefix(), + addPropertyConfig.isStatic() ? cs.getStaticFieldNameSuffix() : cs.getFieldNameSuffix()); + final String paramName = CodeStyleUtils.addPrefixSuffix(name, + cs.getParameterNamePrefix(), + cs.getParameterNameSuffix()); + final String paramIndex = CodeStyleUtils.addPrefixSuffix("index", //NOI18N + cs.getParameterNamePrefix(), + cs.getParameterNameSuffix()); + final String propName = CodeStyleUtils.addPrefixSuffix( + addPropertyConfig.getPopName().trim(), + cs.getStaticFieldNamePrefix(), + cs.getStaticFieldNameSuffix()); final String initializer = addPropertyConfig.getInitializer().trim(); - String access = ""; + String access; switch (addPropertyConfig.getAccess()) { case PRIVATE: access = "private "; // NOI18N @@ -93,8 +108,13 @@ scriptContext.setAttribute("type", type, ScriptContext.ENGINE_SCOPE); // NOI18N scriptContext.setAttribute("className", addPropertyConfig.getClassName(), ScriptContext.ENGINE_SCOPE); // NOI18N scriptContext.setAttribute("name", name, ScriptContext.ENGINE_SCOPE); // NOI18N + scriptContext.setAttribute("fieldName", fieldName, ScriptContext.ENGINE_SCOPE); // NOI18N + scriptContext.setAttribute("paramName", paramName, ScriptContext.ENGINE_SCOPE); // NOI18N + scriptContext.setAttribute("paramIndex", paramIndex, ScriptContext.ENGINE_SCOPE); // NOI18N scriptContext.setAttribute("initializer", initializer, ScriptContext.ENGINE_SCOPE); // NOI18N - scriptContext.setAttribute("capitalizedName", capitalize(name).toString(), ScriptContext.ENGINE_SCOPE); // NOI18N + scriptContext.setAttribute("capitalizedName", CodeStyleUtils.getCapitalizedName(name), ScriptContext.ENGINE_SCOPE); // NOI18N + scriptContext.setAttribute("getterName", CodeStyleUtils.computeGetterName(fieldName, type.equalsIgnoreCase("boolean"), addPropertyConfig.isStatic(), cs), ScriptContext.ENGINE_SCOPE); // NOI18N + scriptContext.setAttribute("setterName", CodeStyleUtils.computeSetterName(fieldName, addPropertyConfig.isStatic(), cs), ScriptContext.ENGINE_SCOPE); // NOI18N scriptContext.setAttribute("static", Boolean.valueOf(addPropertyConfig.isStatic()), ScriptContext.ENGINE_SCOPE); // NOI18N scriptContext.setAttribute("final", Boolean.valueOf(addPropertyConfig.isFinale()), ScriptContext.ENGINE_SCOPE); // NOI18N AddPropertyConfig.GENERATE generateGetterSetter = addPropertyConfig.getGenerateGetterSetter(); @@ -106,7 +126,7 @@ ScriptContext.ENGINE_SCOPE); scriptContext.setAttribute("generateJavadoc", Boolean.valueOf(addPropertyConfig.isGenerateJavadoc()), ScriptContext.ENGINE_SCOPE); // NOI18N scriptContext.setAttribute("bound", Boolean.valueOf(addPropertyConfig.isBound()), ScriptContext.ENGINE_SCOPE); // NOI18N - scriptContext.setAttribute("PROP_NAME", addPropertyConfig.getPopName().trim(), ScriptContext.ENGINE_SCOPE); // NOI18N + scriptContext.setAttribute("PROP_NAME", propName, ScriptContext.ENGINE_SCOPE); // NOI18N scriptContext.setAttribute("vetoable", Boolean.valueOf(addPropertyConfig.isVetoable()), ScriptContext.ENGINE_SCOPE); // NOI18N scriptContext.setAttribute("indexed", Boolean.valueOf(addPropertyConfig.isIndexed()), ScriptContext.ENGINE_SCOPE); // NOI18N scriptContext.setAttribute("propertyChangeSupport", addPropertyConfig.getPropertyChangeSupportName(), ScriptContext.ENGINE_SCOPE); // NOI18N @@ -153,21 +173,4 @@ private static ScriptEngine getScriptEngine() { return new ScriptEngineManager().getEngineByName("freemarker"); // NOI18N } - - private static StringBuilder capitalize(String string) { - StringBuilder sb = new StringBuilder(string); - while (sb.length() > 1 && sb.charAt(0) == '_') { //NOI18N - sb.deleteCharAt(0); - } - - //Beans naming convention, #165241 - if (sb.length() > 1 && Character.isUpperCase(sb.charAt(1))) { - return sb; - } - - if (sb.length() > 0) { - sb.setCharAt(0, Character.toUpperCase(sb.charAt(0))); - } - return sb; - } } diff -r bdb0f6cd71ec -r 2b309a744129 beans/src/org/netbeans/modules/beans/addproperty/AddPropertyPanel.form --- a/beans/src/org/netbeans/modules/beans/addproperty/AddPropertyPanel.form Tue Apr 23 01:17:09 2013 +0200 +++ b/beans/src/org/netbeans/modules/beans/addproperty/AddPropertyPanel.form Tue Apr 23 01:23:45 2013 +0200 @@ -1,4 +1,4 @@ - +
diff -r bdb0f6cd71ec -r 2b309a744129 beans/src/org/netbeans/modules/beans/addproperty/AddPropertyPanel.java --- a/beans/src/org/netbeans/modules/beans/addproperty/AddPropertyPanel.java Tue Apr 23 01:17:09 2013 +0200 +++ b/beans/src/org/netbeans/modules/beans/addproperty/AddPropertyPanel.java Tue Apr 23 01:23:45 2013 +0200 @@ -52,6 +52,7 @@ import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; import org.netbeans.api.java.source.ClasspathInfo; +import org.netbeans.api.java.source.CodeStyle; import org.netbeans.api.java.source.ElementHandle; import org.netbeans.api.java.source.ui.TypeElementFinder; import org.openide.filesystems.FileObject; @@ -71,14 +72,16 @@ private DocumentListener propNameTextFieldDocumentListener; private FileObject file; private String className; + private final CodeStyle cs; private List existingFields; private String[] pcsName; private String[] vcsName; private JButton okButton; - public AddPropertyPanel(FileObject file, String className, List existingFields, String[] pcsName, String[] vcsName, JButton okButton) { + public AddPropertyPanel(FileObject file, String className, CodeStyle cs, List existingFields, String[] pcsName, String[] vcsName, JButton okButton) { this.file = file; this.className = className; + this.cs = cs; this.existingFields = existingFields; this.pcsName = pcsName; this.vcsName = vcsName; @@ -162,7 +165,7 @@ } } - final String previewTemplate = new AddPropertyGenerator().generate(getAddPropertyConfig()); + final String previewTemplate = new AddPropertyGenerator().generate(getAddPropertyConfig(), cs); previewEditorPane.setText(previewTemplate); String error = resolveError(); diff -r bdb0f6cd71ec -r 2b309a744129 beans/src/org/netbeans/modules/beans/resources/templates/AddProperty.freemarker --- a/beans/src/org/netbeans/modules/beans/resources/templates/AddProperty.freemarker Tue Apr 23 01:17:09 2013 +0200 +++ b/beans/src/org/netbeans/modules/beans/resources/templates/AddProperty.freemarker Tue Apr 23 01:23:45 2013 +0200 @@ -5,7 +5,7 @@ <#assign vetoableChangeSupport="vetoableChangeSupport"> - ${access}<#if static >static <#if final >final ${type}<#if indexed >[] ${name}<#if final > = ${initializer}<#else><#if initializer != "" > = ${initializer}; + ${access}<#if static >static <#if final >final ${type}<#if indexed >[] ${fieldName}<#if final > = ${initializer}<#else><#if initializer != "" > = ${initializer}; <#if bound > public static final String ${PROP_NAME} = "${name}"; @@ -19,8 +19,8 @@ * @return the value of ${name} */ - public <#if static >static ${type}<#if indexed >[] <#if type = "boolean" >is<#else>get${capitalizedName}() { - return ${name}; + public <#if static >static ${type}<#if indexed >[] ${getterName}() { + return ${fieldName}; } <#if generateSetter > @@ -30,22 +30,22 @@ /** * Set the value of ${name} * - * @param ${name} new value of ${name} + * @param ${paramName} new value of ${name} <#if vetoable> * @throws java.beans.PropertyVetoException */ - public <#if static >static void set${capitalizedName}(${type}<#if indexed >[] ${name})<#if vetoable> throws java.beans.PropertyVetoException { + public <#if static >static void ${setterName}(${type}<#if indexed >[] ${paramName})<#if vetoable> throws java.beans.PropertyVetoException { <#if bound > - ${type}<#if indexed >[] old${capitalizedName} = this.${name}; + ${type}<#if indexed >[] old${capitalizedName} = this.${fieldName}; <#if vetoable> - ${vetoableChangeSupport}.fireVetoableChange(${PROP_NAME}, old${capitalizedName}, ${name}); + ${vetoableChangeSupport}.fireVetoableChange(${PROP_NAME}, old${capitalizedName}, ${paramName}); - <#if static >${className}.<#else>this.${name} = ${name}; + <#if static >${className}.<#else>this.${fieldName} = ${paramName}; <#if bound > - ${propertyChangeSupport}.firePropertyChange(${PROP_NAME}, old${capitalizedName}, ${name}); + ${propertyChangeSupport}.firePropertyChange(${PROP_NAME}, old${capitalizedName}, ${paramName}); } @@ -57,12 +57,12 @@ /** * Get the value of ${name} at specified index * - * @param index + * @param ${paramIndex} the index of ${name} * @return the value of ${name} at specified index */ - public <#if static >static ${type} <#if type = "boolean" >is<#else>get${capitalizedName}(int index) { - return <#if !static >this.${name}[index]; + public <#if static >static ${type} ${getterName}(int ${paramIndex}) { + return <#if !static >this.${fieldName}[${paramIndex}]; } <#if generateSetter > @@ -70,23 +70,23 @@ /** * Set the value of ${name} at specified index. * - * @param index - * @param new${capitalizedName} new value of ${name} at specified index + * @param ${paramIndex} the index of ${name} + * @param ${paramName} new value of ${name} at specified index <#if vetoable> * @throws java.beans.PropertyVetoException */ - public <#if static >static void set${capitalizedName}(int index, ${type} new${capitalizedName})<#if vetoable> throws java.beans.PropertyVetoException { + public <#if static >static void ${setterName}(int ${paramIndex}, ${type} ${paramName})<#if vetoable> throws java.beans.PropertyVetoException { <#if bound > - ${type} old${capitalizedName} = this.${name}[index]; + ${type} old${capitalizedName} = this.${fieldName}[${paramIndex}]; <#if vetoable> - ${vetoableChangeSupport}.fireVetoableChange(${PROP_NAME}, old${capitalizedName}, new${capitalizedName}); + ${vetoableChangeSupport}.fireVetoableChange(${PROP_NAME}, old${capitalizedName}, ${paramName}); - <#if !static >this.${name}[index] = new${capitalizedName}; + <#if !static >this.${fieldName}[${paramIndex}] = ${paramName}; <#if bound > - ${propertyChangeSupport}.fireIndexedPropertyChange(${PROP_NAME}, index, old${capitalizedName}, new${capitalizedName}); + ${propertyChangeSupport}.fireIndexedPropertyChange(${PROP_NAME}, ${paramIndex}, old${capitalizedName}, ${paramName}); } diff -r bdb0f6cd71ec -r 2b309a744129 java.editor/nbproject/project.xml --- a/java.editor/nbproject/project.xml Tue Apr 23 01:17:09 2013 +0200 +++ b/java.editor/nbproject/project.xml Tue Apr 23 01:23:45 2013 +0200 @@ -231,7 +231,7 @@ - 0.121 + 0.122 diff -r bdb0f6cd71ec -r 2b309a744129 java.editor/src/org/netbeans/modules/editor/java/JavaCodeTemplateProcessor.java --- a/java.editor/src/org/netbeans/modules/editor/java/JavaCodeTemplateProcessor.java Tue Apr 23 01:17:09 2013 +0200 +++ b/java.editor/src/org/netbeans/modules/editor/java/JavaCodeTemplateProcessor.java Tue Apr 23 01:23:45 2013 +0200 @@ -868,7 +868,7 @@ }; } }; - Iterator names = Utilities.varNamesSuggestions(type, suggestedName, null, cInfo.getTypes(), cInfo.getElements(), loc, isConst).iterator(); + Iterator names = Utilities.varNamesSuggestions(type, suggestedName, null, cInfo.getTypes(), cInfo.getElements(), loc, isConst, null, null, null).iterator(); if (names.hasNext()) return names.next(); } diff -r bdb0f6cd71ec -r 2b309a744129 java.editor/src/org/netbeans/modules/editor/java/JavaCompletionItem.java --- a/java.editor/src/org/netbeans/modules/editor/java/JavaCompletionItem.java Tue Apr 23 01:17:09 2013 +0200 +++ b/java.editor/src/org/netbeans/modules/editor/java/JavaCompletionItem.java Tue Apr 23 01:23:45 2013 +0200 @@ -53,6 +53,7 @@ import java.awt.Graphics; import java.awt.event.InputEvent; import java.awt.event.KeyEvent; +import java.io.IOException; import java.net.URL; import java.util.*; import java.util.concurrent.atomic.AtomicBoolean; @@ -211,11 +212,11 @@ } } - public static JavaCompletionItem createGetterSetterMethodItem(CompilationInfo info, VariableElement elem, TypeMirror type, int substitutionOffset, boolean setter) { + public static JavaCompletionItem createGetterSetterMethodItem(CompilationInfo info, VariableElement elem, TypeMirror type, int substitutionOffset, String name, boolean setter) { switch (elem.getKind()) { case ENUM_CONSTANT: case FIELD: - return new GetterSetterMethodItem(info, elem, type, substitutionOffset, setter); + return new GetterSetterMethodItem(info, elem, type, substitutionOffset, name, setter); default: throw new IllegalArgumentException("kind=" + elem.getKind()); } @@ -1881,23 +1882,34 @@ protected ElementHandle elementHandle; private boolean setter; - private String simpleName; + private String paramName; private String name; private String typeName; private String sortText; private String leftText; private String rightText; - private GetterSetterMethodItem(CompilationInfo info, VariableElement elem, TypeMirror type, int substitutionOffset, boolean setter) { + private GetterSetterMethodItem(CompilationInfo info, VariableElement elem, TypeMirror type, int substitutionOffset, String name, boolean setter) { super(substitutionOffset); this.elementHandle = ElementHandle.create(elem); this.setter = setter; - this.simpleName = elem.getSimpleName().toString(); - if (setter) { - this.name = "set" + GeneratorUtils.getCapitalizedName(simpleName); //NOI18N - } else { - this.name = (elem.asType().getKind() == TypeKind.BOOLEAN ? "is" : "get") + GeneratorUtils.getCapitalizedName(simpleName); //NOI18N + CodeStyle cs = null; + try { + cs = CodeStyle.getDefault(info.getDocument()); + } catch (IOException ex) { } + if (cs == null) { + cs = CodeStyle.getDefault(info.getFileObject()); + } + boolean isStatic = elem.getModifiers().contains(Modifier.STATIC); + String simpleName = CodeStyleUtils.removePrefixSuffix(elem.getSimpleName(), + isStatic ? cs.getStaticFieldNamePrefix() : cs.getFieldNamePrefix(), + isStatic ? cs.getStaticFieldNameSuffix() : cs.getFieldNameSuffix()); + this.paramName = CodeStyleUtils.addPrefixSuffix( + simpleName, + cs.getParameterNamePrefix(), + cs.getParameterNameSuffix()); + this.name = name; this.typeName = Utilities.getTypeName(info, type, false).toString(); } @@ -1939,7 +1951,7 @@ lText.append(escape(typeName)); lText.append(' '); lText.append(PARAMETER_NAME_COLOR); - lText.append(simpleName); + lText.append(paramName); lText.append(COLOR_END); } lText.append(") - "); //NOI18N @@ -2030,7 +2042,7 @@ if (setter) { sb.append(typeName); sb.append(' '); - sb.append(simpleName); + sb.append(paramName); } sb.append(") - "); //NOI18N sb.append(GENERATE_TEXT); diff -r bdb0f6cd71ec -r 2b309a744129 java.editor/src/org/netbeans/modules/editor/java/JavaCompletionProvider.java --- a/java.editor/src/org/netbeans/modules/editor/java/JavaCompletionProvider.java Tue Apr 23 01:17:09 2013 +0200 +++ b/java.editor/src/org/netbeans/modules/editor/java/JavaCompletionProvider.java Tue Apr 23 01:23:45 2013 +0200 @@ -245,6 +245,7 @@ private ElementHandle element; private boolean hasTask; + private CodeStyle codeStyle; private JavaCompletionQuery(int queryType, int caretOffset, boolean hasTask) { this.queryType = queryType; @@ -268,6 +269,7 @@ @Override protected void prepareQuery(JTextComponent component) { this.component = component; + this.codeStyle = CodeStyle.getDefault(component.getDocument()); if (queryType == TOOLTIP_QUERY_TYPE) { this.toolTip = new MethodParamsTipPaintComponent(component); } @@ -2615,6 +2617,27 @@ } controller.toPhase(Phase.ELEMENTS_RESOLVED); boolean isConst = parent.getKind() == Tree.Kind.VARIABLE && ((VariableTree)parent).getModifiers().getFlags().containsAll(EnumSet.of(FINAL, STATIC)); + boolean preferLong = false; + String csPrefix = null; + String csSuffix = null; + if(codeStyle != null) { + preferLong = codeStyle.preferLongerNames(); + if(env.getScope().getEnclosingMethod() == null) { + if(exPath.getParentPath().getParentPath().getLeaf().getKind() == Tree.Kind.METHOD) { + csPrefix = codeStyle.getParameterNamePrefix(); + csSuffix = codeStyle.getParameterNameSuffix(); + } else if(((VariableTree)parent).getModifiers().getFlags().contains(STATIC)) { + csPrefix = codeStyle.getStaticFieldNamePrefix(); + csSuffix = codeStyle.getStaticFieldNameSuffix(); + } else { + csPrefix = codeStyle.getFieldNamePrefix(); + csSuffix = codeStyle.getFieldNameSuffix(); + } + } else { + csPrefix = codeStyle.getLocalVarNamePrefix(); + csSuffix = codeStyle.getLocalVarNameSuffix(); + } + } if (et.getKind() == Tree.Kind.ANNOTATED_TYPE) { et = ((AnnotatedTypeTree)et).getUnderlyingType(); exPath = new TreePath(exPath, et); @@ -2631,7 +2654,7 @@ !illegalForwardRefs.contains(e); } }; - for (String name : Utilities.varNamesSuggestions(tm, null, prefix, controller.getTypes(), controller.getElements(), controller.getElementUtilities().getLocalMembersAndVars(scope, acceptor), isConst)) + for (String name : Utilities.varNamesSuggestions(tm, null, prefix, controller.getTypes(), controller.getElements(), controller.getElementUtilities().getLocalMembersAndVars(scope, acceptor), isConst, csPrefix, csSuffix, preferLong)) results.add(JavaCompletionItem.createVariableItem(env.getController(), name, anchorOffset, true, false)); return; } @@ -2663,7 +2686,9 @@ !illegalForwardRefs.contains(e); } }; - for (String name : Utilities.varNamesSuggestions(tm, null, prefix, controller.getTypes(), controller.getElements(), controller.getElementUtilities().getLocalMembersAndVars(scope, acceptor), isConst)) + for (String name : Utilities.varNamesSuggestions(tm, null, prefix, controller.getTypes(), controller.getElements(), + controller.getElementUtilities().getLocalMembersAndVars(scope, acceptor), isConst, + csPrefix, csSuffix, preferLong)) results.add(JavaCompletionItem.createVariableItem(env.getController(), name, anchorOffset, true, false)); } VariableElement ve = getFieldOrVar(env, e.getSimpleName().toString()); @@ -2691,7 +2716,9 @@ !illegalForwardRefs.contains(e); } }; - for (String name : Utilities.varNamesSuggestions(controller.getTypes().getDeclaredType(te), null, prefix, controller.getTypes(), controller.getElements(), controller.getElementUtilities().getLocalMembersAndVars(scope, acceptor), isConst)) + for (String name : Utilities.varNamesSuggestions(controller.getTypes().getDeclaredType(te), null, prefix, controller.getTypes(), + controller.getElements(), controller.getElementUtilities().getLocalMembersAndVars(scope, acceptor), isConst, + csPrefix, csSuffix, preferLong)) results.add(JavaCompletionItem.createVariableItem(env.getController(), name, anchorOffset, true, false)); } break; @@ -2795,7 +2822,7 @@ !illegalForwardRefs.contains(e); } }; - for (String name : Utilities.varNamesSuggestions(tm, null, prefix, controller.getTypes(), controller.getElements(), controller.getElementUtilities().getLocalMembersAndVars(scope, acceptor), isConst)) + for (String name : Utilities.varNamesSuggestions(tm, null, prefix, controller.getTypes(), controller.getElements(), controller.getElementUtilities().getLocalMembersAndVars(scope, acceptor), isConst, csPrefix, csSuffix, preferLong)) results.add(JavaCompletionItem.createVariableItem(env.getController(), name, anchorOffset, true, false)); break; case ENUM_CONSTANT: @@ -4145,14 +4172,14 @@ for (VariableElement variableElement : ElementFilter.fieldsIn(members)) { Name name = variableElement.getSimpleName(); if (!name.contentEquals(ERROR)) { - String nameBase = GeneratorUtils.getCapitalizedName(name).toString(); - String setterName = "set" + nameBase; //NOI18N - String getterName = (variableElement.asType().getKind() == TypeKind.BOOLEAN ? "is" : "get") + nameBase; //NOI18N - if ((prefix == null || startsWith(env, getterName)) && !GeneratorUtils.hasGetter(controller, te, variableElement, methods)) { - results.add(JavaCompletionItem.createGetterSetterMethodItem(env.getController(), variableElement, asMemberOf(variableElement, clsType, types), anchorOffset, false)); + boolean isStatic = variableElement.getModifiers().contains(Modifier.STATIC); + String setterName = CodeStyleUtils.computeSetterName(name, isStatic, codeStyle); + String getterName = CodeStyleUtils.computeGetterName(name, variableElement.asType().getKind() == TypeKind.BOOLEAN, isStatic, codeStyle); + if ((prefix == null || startsWith(env, getterName)) && !GeneratorUtils.hasGetter(controller, te, variableElement, methods, codeStyle)) { + results.add(JavaCompletionItem.createGetterSetterMethodItem(env.getController(), variableElement, asMemberOf(variableElement, clsType, types), anchorOffset, getterName, false)); } - if ((prefix == null || startsWith(env, setterName)) && !(variableElement.getModifiers().contains(Modifier.FINAL) || GeneratorUtils.hasSetter(controller, te, variableElement, methods))) { - results.add(JavaCompletionItem.createGetterSetterMethodItem(env.getController(), variableElement, asMemberOf(variableElement, clsType, types), anchorOffset, true)); + if ((prefix == null || startsWith(env, setterName)) && !(variableElement.getModifiers().contains(Modifier.FINAL) || GeneratorUtils.hasSetter(controller, te, variableElement, methods, codeStyle))) { + results.add(JavaCompletionItem.createGetterSetterMethodItem(env.getController(), variableElement, asMemberOf(variableElement, clsType, types), anchorOffset, setterName, true)); } } } diff -r bdb0f6cd71ec -r 2b309a744129 java.editor/src/org/netbeans/modules/editor/java/Utilities.java --- a/java.editor/src/org/netbeans/modules/editor/java/Utilities.java Tue Apr 23 01:17:09 2013 +0200 +++ b/java.editor/src/org/netbeans/modules/editor/java/Utilities.java Tue Apr 23 01:23:45 2013 +0200 @@ -82,6 +82,7 @@ import org.netbeans.api.editor.mimelookup.MimeLookup; import org.netbeans.api.editor.settings.SimpleValueNames; import org.netbeans.api.java.lexer.JavaTokenId; +import org.netbeans.api.java.source.CodeStyleUtils; import org.netbeans.api.java.source.CompilationInfo; import org.netbeans.api.java.source.ElementHandle; import org.netbeans.api.java.source.SourceUtils; @@ -530,7 +531,7 @@ return refs; } - public static List varNamesSuggestions(TypeMirror type, String suggestedName, String prefix, Types types, Elements elements, Iterable locals, boolean isConst) { + public static List varNamesSuggestions(TypeMirror type, String suggestedName, String prefix, Types types, Elements elements, Iterable locals, boolean isConst, String namePrefix, String nameSuffix, Boolean preferLong) { List result = new ArrayList(); if (type == null && suggestedName == null) return result; @@ -566,14 +567,15 @@ } int cnt = 1; String baseName = name; + name = CodeStyleUtils.addPrefixSuffix(name, namePrefix, nameSuffix); while (isClashing(name, type, locals)) { if (isPrimitive) { - char c = name.charAt(0); - name = Character.toString(++c); - if (c == 'z') //NOI18N + char c = name.charAt(namePrefix != null?namePrefix.length():0); + name = CodeStyleUtils.addPrefixSuffix(Character.toString(++c), namePrefix, nameSuffix); + if (c == 'z' || c == 'Z') //NOI18N isPrimitive = false; } else { - name = baseName + cnt++; + name = CodeStyleUtils.addPrefixSuffix(baseName + cnt++, namePrefix, nameSuffix); } } result.add(name); diff -r bdb0f6cd71ec -r 2b309a744129 java.editor/src/org/netbeans/modules/java/editor/codegen/GeneratorUtils.java --- a/java.editor/src/org/netbeans/modules/java/editor/codegen/GeneratorUtils.java Tue Apr 23 01:17:09 2013 +0200 +++ b/java.editor/src/org/netbeans/modules/java/editor/codegen/GeneratorUtils.java Tue Apr 23 01:23:45 2013 +0200 @@ -88,6 +88,7 @@ import org.netbeans.api.annotations.common.NonNull; import org.netbeans.api.java.source.CodeStyle; +import org.netbeans.api.java.source.CodeStyleUtils; import org.netbeans.api.java.source.Comment; import org.netbeans.api.java.source.CompilationInfo; import org.netbeans.api.java.source.ElementUtilities; @@ -275,14 +276,14 @@ } } - public static boolean hasGetter(CompilationInfo info, TypeElement typeElement, VariableElement field, Map> methods) { + public static boolean hasGetter(CompilationInfo info, TypeElement typeElement, VariableElement field, Map> methods, CodeStyle cs) { CharSequence name = field.getSimpleName(); assert name.length() > 0; TypeMirror type = field.asType(); - StringBuilder sb = getCapitalizedName(name); - sb.insert(0, type.getKind() == TypeKind.BOOLEAN ? "is" : "get"); //NOI18N + boolean isStatic = field.getModifiers().contains(Modifier.STATIC); + String getterName = CodeStyleUtils.computeGetterName(name, type.getKind() == TypeKind.BOOLEAN, isStatic, cs); Types types = info.getTypes(); - List candidates = methods.get(sb.toString()); + List candidates = methods.get(getterName); if (candidates != null) { for (ExecutableElement candidate : candidates) { if ((!candidate.getModifiers().contains(Modifier.ABSTRACT) || candidate.getEnclosingElement() == typeElement) @@ -294,14 +295,14 @@ return false; } - public static boolean hasSetter(CompilationInfo info, TypeElement typeElement, VariableElement field, Map> methods) { + public static boolean hasSetter(CompilationInfo info, TypeElement typeElement, VariableElement field, Map> methods, CodeStyle cs) { CharSequence name = field.getSimpleName(); assert name.length() > 0; TypeMirror type = field.asType(); - StringBuilder sb = getCapitalizedName(name); - sb.insert(0, "set"); //NOI18N + boolean isStatic = field.getModifiers().contains(Modifier.STATIC); + String setterName = CodeStyleUtils.computeSetterName(name, isStatic, cs); Types types = info.getTypes(); - List candidates = methods.get(sb.toString()); + List candidates = methods.get(setterName); if (candidates != null) { for (ExecutableElement candidate : candidates) { if ((!candidate.getModifiers().contains(Modifier.ABSTRACT) || candidate.getEnclosingElement() == typeElement) @@ -596,21 +597,6 @@ } return ((PackageElement) e.getEnclosingElement()).getQualifiedName().toString(); } - - public static StringBuilder getCapitalizedName(CharSequence cs) { - StringBuilder sb = new StringBuilder(cs); - while(sb.length() > 1 && sb.charAt(0) == '_') //NOI18N - sb.deleteCharAt(0); - - //Beans naming convention, #165241 - if (sb.length() > 1 && Character.isUpperCase(sb.charAt(1))) { - return sb; - } - - if (sb.length() > 0) - sb.setCharAt(0, Character.toUpperCase(sb.charAt(0))); - return sb; - } public static void guardedCommit(JTextComponent component, ModificationResult mr) throws IOException { try { diff -r bdb0f6cd71ec -r 2b309a744129 java.editor/src/org/netbeans/modules/java/editor/codegen/GetterSetterGenerator.java --- a/java.editor/src/org/netbeans/modules/java/editor/codegen/GetterSetterGenerator.java Tue Apr 23 01:17:09 2013 +0200 +++ b/java.editor/src/org/netbeans/modules/java/editor/codegen/GetterSetterGenerator.java Tue Apr 23 01:23:45 2013 +0200 @@ -92,6 +92,7 @@ public List create(Lookup context) { ArrayList ret = new ArrayList(); JTextComponent component = context.lookup(JTextComponent.class); + CodeStyle codeStyle = CodeStyle.getDefault(component.getDocument()); CompilationController controller = context.lookup(CompilationController.class); TreePath path = context.lookup(TreePath.class); path = path != null ? Utilities.getPathElementOfKind(TreeUtilities.CLASS_TREE_KINDS, path) : null; @@ -122,8 +123,8 @@ if (ERROR.contentEquals(variableElement.getSimpleName())) continue; ElementNode.Description description = ElementNode.Description.create(controller, variableElement, null, true, false); - boolean hasGetter = GeneratorUtils.hasGetter(controller, typeElement, variableElement, methods); - boolean hasSetter = variableElement.getModifiers().contains(Modifier.FINAL) || GeneratorUtils.hasSetter(controller, typeElement, variableElement, methods); + boolean hasGetter = GeneratorUtils.hasGetter(controller, typeElement, variableElement, methods, codeStyle); + boolean hasSetter = variableElement.getModifiers().contains(Modifier.FINAL) || GeneratorUtils.hasSetter(controller, typeElement, variableElement, methods, codeStyle); if (!hasGetter) { List descriptions = gDescriptions.get(variableElement.getEnclosingElement()); if (descriptions == null) { @@ -154,21 +155,21 @@ for (Map.Entry> entry : gDescriptions.entrySet()) descriptions.add(ElementNode.Description.create(controller, entry.getKey(), entry.getValue(), false, false)); Collections.reverse(descriptions); - ret.add(new GetterSetterGenerator(component, ElementNode.Description.create(controller, typeElement, descriptions, false, false), GeneratorUtils.GETTERS_ONLY)); + ret.add(new GetterSetterGenerator(component, ElementNode.Description.create(controller, typeElement, descriptions, false, false), GeneratorUtils.GETTERS_ONLY, codeStyle)); } if (!sDescriptions.isEmpty()) { List descriptions = new ArrayList(); for (Map.Entry> entry : sDescriptions.entrySet()) descriptions.add(ElementNode.Description.create(controller, entry.getKey(), entry.getValue(), false, false)); Collections.reverse(descriptions); - ret.add(new GetterSetterGenerator(component, ElementNode.Description.create(controller, typeElement, descriptions, false, false), GeneratorUtils.SETTERS_ONLY)); + ret.add(new GetterSetterGenerator(component, ElementNode.Description.create(controller, typeElement, descriptions, false, false), GeneratorUtils.SETTERS_ONLY, codeStyle)); } if (!gsDescriptions.isEmpty()) { List descriptions = new ArrayList(); for (Map.Entry> entry : gsDescriptions.entrySet()) descriptions.add(ElementNode.Description.create(controller, entry.getKey(), entry.getValue(), false, false)); Collections.reverse(descriptions); - ret.add(new GetterSetterGenerator(component, ElementNode.Description.create(controller, typeElement, descriptions, false, false), 0)); + ret.add(new GetterSetterGenerator(component, ElementNode.Description.create(controller, typeElement, descriptions, false, false), 0, codeStyle)); } return ret; } @@ -177,12 +178,14 @@ private JTextComponent component; private ElementNode.Description description; private int type; + private CodeStyle codestyle; /** Creates a new instance of GetterSetterGenerator */ - private GetterSetterGenerator(JTextComponent component, ElementNode.Description description, int type) { + private GetterSetterGenerator(JTextComponent component, ElementNode.Description description, int type, CodeStyle codeStyle) { this.component = component; this.description = description; this.type = type; + this.codestyle = codeStyle; } public String getDisplayName() { @@ -256,7 +259,7 @@ @Override public void run(CompilationController parameter) throws Exception { - createGetterSetterLists(parameter, variables, getters, setters); + createGetterSetterLists(parameter, variables, getters, setters, codestyle); } }, true); @@ -277,15 +280,16 @@ } - private void createGetterSetterLists(CompilationController cc, List> variables, List getters, List setters) { + private void createGetterSetterLists(CompilationController cc, List> variables, List getters, List setters, CodeStyle codestyle) { for (ElementHandle handle:variables) { final Element el = handle.resolve(cc); + boolean isStatic = el.getModifiers().contains(Modifier.STATIC); if (type!=GeneratorUtils.GETTERS_ONLY) - setters.add("set" + GeneratorUtils.getCapitalizedName(el.getSimpleName()));//NOI18N + setters.add(CodeStyleUtils.computeSetterName(el.getSimpleName(), isStatic, codestyle)); else setters.add(null); if (type!=GeneratorUtils.SETTERS_ONLY) - getters.add((el.asType().getKind() == TypeKind.BOOLEAN ? "is" : "get") + GeneratorUtils.getCapitalizedName(el.getSimpleName()));//NOI18N + getters.add(CodeStyleUtils.computeGetterName(el.getSimpleName(), el.asType().getKind() == TypeKind.BOOLEAN, isStatic, codestyle)); else getters.add(null); } diff -r bdb0f6cd71ec -r 2b309a744129 java.editor/src/org/netbeans/modules/java/editor/codegen/LoggerGenerator.java --- a/java.editor/src/org/netbeans/modules/java/editor/codegen/LoggerGenerator.java Tue Apr 23 01:17:09 2013 +0200 +++ b/java.editor/src/org/netbeans/modules/java/editor/codegen/LoggerGenerator.java Tue Apr 23 01:23:45 2013 +0200 @@ -72,6 +72,7 @@ import com.sun.source.tree.Scope; import com.sun.source.tree.VariableTree; import com.sun.source.util.TreePath; +import org.netbeans.api.java.source.CodeStyle; import org.netbeans.api.java.source.CompilationController; import org.netbeans.api.java.source.GeneratorUtilities; @@ -160,7 +161,8 @@ org.netbeans.editor.Utilities.setStatusBoldText(component, message); } else { ClassTree cls = (ClassTree) path.getLeaf(); - List names = Utilities.varNamesSuggestions(null, "LOG", null, copy.getTypes(), copy.getElements(), e.getEnclosedElements(), true); + CodeStyle cs = CodeStyle.getDefault(component.getDocument()); + List names = Utilities.varNamesSuggestions(null, "LOG", null, copy.getTypes(), copy.getElements(), e.getEnclosedElements(), true, cs.getStaticFieldNamePrefix(), cs.getStaticFieldNameSuffix(), cs.preferLongerNames()); VariableTree var = createLoggerField(copy.getTreeMaker(), cls, names.size() > 0 ? names.get(0) : "LOG"); //NOI18N copy.rewrite(cls, GeneratorUtils.insertClassMembers(copy, cls, Collections.singletonList(var), caretOffset)); } diff -r bdb0f6cd71ec -r 2b309a744129 java.editor/src/org/netbeans/modules/java/editor/rename/InstantRenamePerformer.java --- a/java.editor/src/org/netbeans/modules/java/editor/rename/InstantRenamePerformer.java Tue Apr 23 01:17:09 2013 +0200 +++ b/java.editor/src/org/netbeans/modules/java/editor/rename/InstantRenamePerformer.java Tue Apr 23 01:23:45 2013 +0200 @@ -59,9 +59,11 @@ import java.util.ArrayList; import java.util.Collections; import java.util.EnumSet; +import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; @@ -71,6 +73,7 @@ import javax.lang.model.element.Modifier; import javax.lang.model.element.Name; import javax.lang.model.element.TypeElement; +import javax.lang.model.element.VariableElement; import javax.lang.model.util.ElementFilter; import javax.lang.model.util.ElementScanner6; import javax.swing.Action; @@ -99,6 +102,7 @@ import org.netbeans.api.editor.settings.FontColorSettings; import org.netbeans.api.java.lexer.JavaTokenId; import org.netbeans.api.java.lexer.JavadocTokenId; +import org.netbeans.api.java.source.CodeStyle; import org.netbeans.api.java.source.CompilationInfo; import org.netbeans.api.java.source.ElementUtilities; import org.netbeans.api.java.source.JavaSource; @@ -115,6 +119,7 @@ import org.netbeans.modules.editor.java.ComputeOffAWT; import org.netbeans.modules.editor.java.ComputeOffAWT.Worker; import org.netbeans.modules.editor.java.JavaKit; +import org.netbeans.modules.java.editor.codegen.GeneratorUtils; import org.netbeans.modules.java.editor.javadoc.JavadocImports; import org.netbeans.modules.java.editor.semantic.FindLocalUsagesQuery; import org.netbeans.modules.refactoring.api.AbstractRefactoring; @@ -380,7 +385,7 @@ el = el.getEnclosingElement(); } - if (allowInstantRename(el, info.getElementUtilities())) { + if (allowInstantRename(info, el, info.getElementUtilities())) { final Set points = new HashSet(new FindLocalUsagesQuery(true).findUsages(el, info, doc)); if (el.getKind().isClass()) { @@ -445,7 +450,32 @@ return result; } - private static boolean allowInstantRename(Element e, ElementUtilities eu) { + private static boolean allowInstantRename(CompilationInfo info, Element e, ElementUtilities eu) { + if(e.getKind() == ElementKind.FIELD) { + VariableElement variableElement = (VariableElement) e; + TypeElement typeElement = eu.enclosingTypeElement(e); + Map> methods = new HashMap>(); + for (ExecutableElement method : ElementFilter.methodsIn(info.getElements().getAllMembers(typeElement))) { + List l = methods.get(method.getSimpleName().toString()); + if (l == null) { + l = new ArrayList(); + methods.put(method.getSimpleName().toString(), l); + } + l.add(method); + } + + boolean isProperty = false; + try { + CodeStyle codeStyle = CodeStyle.getDefault(info.getDocument()); + isProperty = GeneratorUtils.hasGetter(info, typeElement, variableElement, methods, codeStyle); + isProperty = isProperty || (!variableElement.getModifiers().contains(Modifier.FINAL) && + GeneratorUtils.hasSetter(info, typeElement, variableElement, methods, codeStyle)); + } catch (IOException ex) { + } + if(isProperty) { + return false; + } + } if (org.netbeans.modules.java.editor.semantic.Utilities.isPrivateElement(e)) { return true; } diff -r bdb0f6cd71ec -r 2b309a744129 java.hints/src/org/netbeans/modules/java/hints/errors/Utilities.java --- a/java.hints/src/org/netbeans/modules/java/hints/errors/Utilities.java Tue Apr 23 01:17:09 2013 +0200 +++ b/java.hints/src/org/netbeans/modules/java/hints/errors/Utilities.java Tue Apr 23 01:23:45 2013 +0200 @@ -155,6 +155,10 @@ } public static String guessName(CompilationInfo info, TreePath tp, TreePath scope) { + return guessName(info, tp, scope, null, null); + } + + public static String guessName(CompilationInfo info, TreePath tp, TreePath scope, String prefix, String suffix) { String name = getName(tp.getLeaf()); if (name == null) { @@ -163,16 +167,23 @@ Scope s = info.getTrees().getScope(scope); - return makeNameUnique(info, s, name); + return makeNameUnique(info, s, name, prefix, suffix); } - - public static String makeNameUnique(CompilationInfo info, Scope s, String name) { + + public static String makeNameUnique(CompilationInfo info, Scope s, String name, String prefix, String suffix) { + if(prefix != null && prefix.length() > 0) { + if(Character.isAlphabetic(prefix.charAt(prefix.length()-1))) { + StringBuilder nameSb = new StringBuilder(name); + nameSb.setCharAt(0, Character.toUpperCase(nameSb.charAt(0))); + name = nameSb.toString(); + } + } + + boolean cont; + String proposedName; int counter = 0; - boolean cont = true; - String proposedName = name; - - while (cont) { - proposedName = name + (counter != 0 ? String.valueOf(counter) : ""); + do { + proposedName = safeString(prefix) + name + (counter != 0 ? String.valueOf(counter) : "") + safeString(suffix); cont = false; @@ -183,10 +194,18 @@ break; } } - } + } while(cont); return proposedName; } + + private static String safeString(String str) { + return str == null ? "" : str; + } + + public static String makeNameUnique(CompilationInfo info, Scope s, String name) { + return makeNameUnique(info, s, name, null, null); + } private static String guessLiteralName(String str) { if(str.isEmpty()) diff -r bdb0f6cd71ec -r 2b309a744129 java.hints/src/org/netbeans/modules/java/hints/introduce/IntroduceHint.java --- a/java.hints/src/org/netbeans/modules/java/hints/introduce/IntroduceHint.java Tue Apr 23 01:17:09 2013 +0200 +++ b/java.hints/src/org/netbeans/modules/java/hints/introduce/IntroduceHint.java Tue Apr 23 01:23:45 2013 +0200 @@ -116,6 +116,7 @@ import org.netbeans.api.editor.settings.AttributesUtilities; import org.netbeans.api.java.lexer.JavaTokenId; import org.netbeans.api.java.source.CancellableTask; +import org.netbeans.api.java.source.CodeStyle; import org.netbeans.api.java.source.Task; import org.netbeans.api.java.source.CompilationInfo; import org.netbeans.api.java.source.GeneratorUtilities; @@ -443,8 +444,9 @@ String guessedName = Utilities.getName(resolved.getLeaf()); if (guessedName == null) guessedName = "name"; Scope s = info.getTrees().getScope(resolved); - Fix variable = isVariable ? new IntroduceFix(h, info.getJavaSource(), variableRewrite ? guessedName : Utilities.makeNameUnique(info, s, guessedName), duplicatesForVariable.size() + 1, IntroduceKind.CREATE_VARIABLE) : null; - Fix constant = isConstant ? new IntroduceFix(h, info.getJavaSource(), variableRewrite ? guessedName : Utilities.makeNameUnique(info, info.getTrees().getScope(constantTarget), Utilities.toConstantName(guessedName)), duplicatesForConstant.size() + 1, IntroduceKind.CREATE_CONSTANT) : null; + CodeStyle cs = CodeStyle.getDefault(info.getFileObject()); + Fix variable = isVariable ? new IntroduceFix(h, info.getJavaSource(), variableRewrite ? guessedName : Utilities.makeNameUnique(info, s, guessedName, cs.getLocalVarNamePrefix(), cs.getLocalVarNameSuffix()), duplicatesForVariable.size() + 1, IntroduceKind.CREATE_VARIABLE) : null; + Fix constant = isConstant ? new IntroduceFix(h, info.getJavaSource(), variableRewrite ? guessedName : Utilities.makeNameUnique(info, info.getTrees().getScope(constantTarget), Utilities.toConstantName(guessedName), cs.getStaticFieldNamePrefix(), cs.getStaticFieldNameSuffix()), duplicatesForConstant.size() + 1, IntroduceKind.CREATE_CONSTANT) : null; Fix parameter = isVariable ? new IntroduceParameterFix(h) : null; Fix field = null; Fix methodFix = null; @@ -466,7 +468,10 @@ if (resolved.getLeaf().getKind() == Kind.VARIABLE) { //the variable name would incorrectly clash with itself: - guessedName = Utilities.guessName(info, resolved, resolved.getParentPath()); + guessedName = Utilities.guessName(info, resolved, resolved.getParentPath(), cs.getFieldNamePrefix(), cs.getFieldNameSuffix()); + } else if (!variableRewrite) { + guessedName = Utilities.makeNameUnique(info, info.getTrees().getScope(constantTarget), + guessedName, cs.getFieldNamePrefix(), cs.getFieldNameSuffix()); } field = new IntroduceFieldFix(h, info.getJavaSource(), guessedName, duplicatesForConstant.size() + 1, initilizeIn, statik, allowFinalInCurrentMethod); diff -r bdb0f6cd71ec -r 2b309a744129 java.hints/test/unit/src/org/netbeans/modules/java/hints/introduce/IntroduceHintTest.java --- a/java.hints/test/unit/src/org/netbeans/modules/java/hints/introduce/IntroduceHintTest.java Tue Apr 23 01:17:09 2013 +0200 +++ b/java.hints/test/unit/src/org/netbeans/modules/java/hints/introduce/IntroduceHintTest.java Tue Apr 23 01:23:45 2013 +0200 @@ -56,6 +56,7 @@ import javax.lang.model.element.Modifier; import javax.swing.text.Document; import org.netbeans.api.java.lexer.JavaTokenId; +import org.netbeans.api.java.source.CodeStyle; import org.netbeans.api.java.source.CompilationInfo; import org.netbeans.api.java.source.JavaSource; import org.netbeans.api.java.source.JavaSource.Phase; @@ -666,7 +667,7 @@ .of(Modifier.PRIVATE), false, true), 5, 2); } - + public void testFix21() throws Exception { performFixTest("package test; import java.util.List; public class Test {public void test1() {List l = |test()|;} public List test() {return null;}}", "package test; import java.util.List; public class Test { private List name; public void test1() {name = test(); List l = name;} public List test() {return null;}}", diff -r bdb0f6cd71ec -r 2b309a744129 java.source/src/org/netbeans/api/java/source/GeneratorUtilities.java --- a/java.source/src/org/netbeans/api/java/source/GeneratorUtilities.java Tue Apr 23 01:17:09 2013 +0200 +++ b/java.source/src/org/netbeans/api/java/source/GeneratorUtilities.java Tue Apr 23 01:23:45 2013 +0200 @@ -80,13 +80,9 @@ import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.io.PrintWriter; import java.io.Reader; -import java.io.Writer; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Arrays; @@ -471,13 +467,15 @@ public MethodTree createConstructor(ClassTree clazz, Iterable fields) { assert clazz != null && fields != null; TreeMaker make = copy.getTreeMaker(); + CodeStyle cs = DiffContext.getCodeStyle(copy); Set mods = EnumSet.of(copy.getTreeUtilities().isEnum(clazz) ? Modifier.PRIVATE : Modifier.PUBLIC); List parameters = new ArrayList(); List statements = new ArrayList(); ModifiersTree parameterModifiers = make.Modifiers(EnumSet.noneOf(Modifier.class)); for (VariableTree vt : fields) { - parameters.add(make.Variable(parameterModifiers, vt.getName(), vt.getType(), null)); - statements.add(make.ExpressionStatement(make.Assignment(make.MemberSelect(make.Identifier("this"), vt.getName()), make.Identifier(vt.getName())))); //NOI18N + String paramName = addParamPrefixSuffix(removeFieldPrefixSuffix(vt, cs), cs); + parameters.add(make.Variable(parameterModifiers, paramName, vt.getType(), null)); + statements.add(make.ExpressionStatement(make.Assignment(make.MemberSelect(make.Identifier("this"), vt.getName()), make.Identifier(paramName)))); //NOI18N } BlockTree body = make.Block(statements, false); return make.Method(make.Modifiers(mods), "", null, Collections. emptyList(), parameters, Collections.emptyList(), body, null); //NOI18N @@ -494,16 +492,16 @@ public MethodTree createGetter(TypeElement clazz, VariableElement field) { assert clazz != null && field != null; TreeMaker make = copy.getTreeMaker(); + CodeStyle cs = DiffContext.getCodeStyle(copy); Set mods = EnumSet.of(Modifier.PUBLIC); - if (field.getModifiers().contains(Modifier.STATIC)) + boolean isStatic = field.getModifiers().contains(Modifier.STATIC); + if (isStatic) { mods.add(Modifier.STATIC); - CharSequence name = field.getSimpleName(); - assert name.length() > 0; + } TypeMirror type = copy.getTypes().asMemberOf((DeclaredType)clazz.asType(), field); - StringBuilder sb = getCapitalizedName(name); - sb.insert(0, type.getKind() == TypeKind.BOOLEAN ? "is" : "get"); //NOI18N - BlockTree body = make.Block(Collections.singletonList(make.Return(make.Identifier(name))), false); - return make.Method(make.Modifiers(mods), sb, make.Type(type), Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), body, null); + String getterName = CodeStyleUtils.computeGetterName(field.getSimpleName(), type.getKind() == TypeKind.BOOLEAN, isStatic, cs); + BlockTree body = make.Block(Collections.singletonList(make.Return(make.Identifier(field.getSimpleName()))), false); + return make.Method(make.Modifiers(mods), getterName, make.Type(type), Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), body, null); } /** @@ -516,16 +514,17 @@ public MethodTree createGetter(VariableTree field) { assert field != null; TreeMaker make = copy.getTreeMaker(); + CodeStyle cs = DiffContext.getCodeStyle(copy); Set mods = EnumSet.of(Modifier.PUBLIC); - if (field.getModifiers().getFlags().contains(Modifier.STATIC)) + boolean isStatic = field.getModifiers().getFlags().contains(Modifier.STATIC); + if (isStatic) { mods.add(Modifier.STATIC); - CharSequence name = field.getName(); - assert name.length() > 0; + } Tree type = field.getType(); - StringBuilder sb = getCapitalizedName(name); - sb.insert(0, type.getKind() == Tree.Kind.PRIMITIVE_TYPE && ((PrimitiveTypeTree)type).getPrimitiveTypeKind() == TypeKind.BOOLEAN ? "is" : "get"); //NOI18N - BlockTree body = make.Block(Collections.singletonList(make.Return(make.Identifier(name))), false); - return make.Method(make.Modifiers(mods), sb, type, Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), body, null); + boolean isBoolean = type.getKind() == Tree.Kind.PRIMITIVE_TYPE && ((PrimitiveTypeTree) type).getPrimitiveTypeKind() == TypeKind.BOOLEAN; + String getterName = CodeStyleUtils.computeGetterName(field.getName(), isBoolean, isStatic, cs); + BlockTree body = make.Block(Collections.singletonList(make.Return(make.Identifier(field.getName()))), false); + return make.Method(make.Modifiers(mods), getterName, type, Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), body, null); } /** @@ -539,18 +538,20 @@ public MethodTree createSetter(TypeElement clazz, VariableElement field) { assert clazz != null && field != null; TreeMaker make = copy.getTreeMaker(); + CodeStyle cs = DiffContext.getCodeStyle(copy); Set mods = EnumSet.of(Modifier.PUBLIC); boolean isStatic = field.getModifiers().contains(Modifier.STATIC); - if (isStatic) + if (isStatic) { mods.add(Modifier.STATIC); + } CharSequence name = field.getSimpleName(); assert name.length() > 0; TypeMirror type = copy.getTypes().asMemberOf((DeclaredType)clazz.asType(), field); - StringBuilder sb = getCapitalizedName(name); - sb.insert(0, "set"); //NOI18N - List params = Collections.singletonList(make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), name, make.Type(type), null)); - BlockTree body = make.Block(Collections.singletonList(make.ExpressionStatement(make.Assignment(make.MemberSelect(isStatic? make.Identifier(field.getEnclosingElement().getSimpleName()) : make.Identifier("this"), name), make.Identifier(name)))), false); //NOI18N - return make.Method(make.Modifiers(mods), sb, make.Type(copy.getTypes().getNoType(TypeKind.VOID)), Collections.emptyList(), params, Collections.emptyList(), body, null); + String setterName = CodeStyleUtils.computeSetterName(field.getSimpleName(), isStatic, cs); + String paramName = addParamPrefixSuffix(removeFieldPrefixSuffix(field, cs), cs); + List params = Collections.singletonList(make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), paramName, make.Type(type), null)); + BlockTree body = make.Block(Collections.singletonList(make.ExpressionStatement(make.Assignment(make.MemberSelect(isStatic? make.Identifier(field.getEnclosingElement().getSimpleName()) : make.Identifier("this"), name), make.Identifier(paramName)))), false); //NOI18N + return make.Method(make.Modifiers(mods), setterName, make.Type(copy.getTypes().getNoType(TypeKind.VOID)), Collections.emptyList(), params, Collections.emptyList(), body, null); } /** @@ -570,11 +571,13 @@ mods.add(Modifier.STATIC); CharSequence name = field.getName(); assert name.length() > 0; - StringBuilder sb = getCapitalizedName(name); - sb.insert(0, "set"); //NOI18N - List params = Collections.singletonList(make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), name, field.getType(), null)); - BlockTree body = make.Block(Collections.singletonList(make.ExpressionStatement(make.Assignment(make.MemberSelect(isStatic? make.Identifier(clazz.getSimpleName()) : make.Identifier("this"), name), make.Identifier(name)))), false); //NOI18N - return make.Method(make.Modifiers(mods), sb, make.Type(copy.getTypes().getNoType(TypeKind.VOID)), Collections.emptyList(), params, Collections.emptyList(), body, null); + CodeStyle cs = DiffContext.getCodeStyle(copy); + String propName = removeFieldPrefixSuffix(field, cs); + String setterName = CodeStyleUtils.computeSetterName(field.getName(), isStatic, cs); + String paramName = addParamPrefixSuffix(propName, cs); + List params = Collections.singletonList(make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), paramName, field.getType(), null)); + BlockTree body = make.Block(Collections.singletonList(make.ExpressionStatement(make.Assignment(make.MemberSelect(isStatic? make.Identifier(clazz.getSimpleName()) : make.Identifier("this"), name), make.Identifier(paramName)))), false); //NOI18N + return make.Method(make.Modifiers(mods), setterName, make.Type(copy.getTypes().getNoType(TypeKind.VOID)), Collections.emptyList(), params, Collections.emptyList(), body, null); } /** @@ -1158,23 +1161,6 @@ return info.getElements().getTypeElement("java.lang.Override") != null; } - private static StringBuilder getCapitalizedName(CharSequence cs) { - StringBuilder sb = new StringBuilder(cs); - while (sb.length() > 1 && sb.charAt(0) == '_') { //NOI18N - sb.deleteCharAt(0); - } - - //Beans naming convention, #165241 - if (sb.length() > 1 && Character.isUpperCase(sb.charAt(1))) { - return sb; - } - - if (sb.length() > 0) { - sb.setCharAt(0, Character.toUpperCase(sb.charAt(0))); - } - return sb; - } - private Tree resolveWildcard(TypeMirror type) { TreeMaker make = copy.getTreeMaker(); Tree result; @@ -1361,6 +1347,26 @@ return bindings; } + private static String removeFieldPrefixSuffix(VariableElement var, CodeStyle cs) { + boolean isStatic = var.getModifiers().contains(Modifier.STATIC); + return CodeStyleUtils.removePrefixSuffix(var.getSimpleName(), + isStatic ? cs.getStaticFieldNamePrefix() : cs.getFieldNamePrefix(), + isStatic ? cs.getStaticFieldNameSuffix() : cs.getFieldNameSuffix()); + } + + private static String removeFieldPrefixSuffix(VariableTree var, CodeStyle cs) { + boolean isStatic = var.getModifiers().getFlags().contains(Modifier.STATIC); + return CodeStyleUtils.removePrefixSuffix(var.getName(), + isStatic ? cs.getStaticFieldNamePrefix() : cs.getFieldNamePrefix(), + isStatic ? cs.getStaticFieldNameSuffix() : cs.getFieldNameSuffix()); + } + + private static String addParamPrefixSuffix(CharSequence name, CodeStyle cs) { + return CodeStyleUtils.addPrefixSuffix(name, + cs.getParameterNamePrefix(), + cs.getParameterNameSuffix()); + } + private static class ClassMemberComparator implements Comparator { private CodeStyle.MemberGroups groups; diff -r bdb0f6cd71ec -r 2b309a744129 java.source/src/org/netbeans/modules/java/source/resources/layer.xml --- a/java.source/src/org/netbeans/modules/java/source/resources/layer.xml Tue Apr 23 01:17:09 2013 +0200 +++ b/java.source/src/org/netbeans/modules/java/source/resources/layer.xml Tue Apr 23 01:23:45 2013 +0200 @@ -224,6 +224,11 @@ + + + + + diff -r bdb0f6cd71ec -r 2b309a744129 java.source/src/org/netbeans/modules/java/ui/Bundle.properties --- a/java.source/src/org/netbeans/modules/java/ui/Bundle.properties Tue Apr 23 01:17:09 2013 +0200 +++ b/java.source/src/org/netbeans/modules/java/ui/Bundle.properties Tue Apr 23 01:23:45 2013 +0200 @@ -43,7 +43,8 @@ # Formating options LBL_TabsAndIndents=Tabs and Indents -LBL_CodeGeneration=Code Generation +LBL_CodeGeneration=Ordering +LBL_Naming=Naming LBL_Alignment=Alignment LBL_Braces=Braces LBL_Wrapping=Wrapping @@ -527,6 +528,10 @@ private String name;\ } +SAMPLE_Naming=public class ClassA {\ +\ +} + nlFinallyCheckBox1.text="finall&y" AN_Preview=Preview diff -r bdb0f6cd71ec -r 2b309a744129 java.source/src/org/netbeans/modules/java/ui/FmtCodeGeneration.form --- a/java.source/src/org/netbeans/modules/java/ui/FmtCodeGeneration.form Tue Apr 23 01:17:09 2013 +0200 +++ b/java.source/src/org/netbeans/modules/java/ui/FmtCodeGeneration.form Tue Apr 23 01:23:45 2013 +0200 @@ -22,7 +22,6 @@ - @@ -41,30 +40,22 @@ - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + @@ -84,15 +75,6 @@ - - - - - - - - - @@ -140,205 +122,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff -r bdb0f6cd71ec -r 2b309a744129 java.source/src/org/netbeans/modules/java/ui/FmtCodeGeneration.java --- a/java.source/src/org/netbeans/modules/java/ui/FmtCodeGeneration.java Tue Apr 23 01:17:09 2013 +0200 +++ b/java.source/src/org/netbeans/modules/java/ui/FmtCodeGeneration.java Tue Apr 23 01:23:45 2013 +0200 @@ -106,16 +106,6 @@ public FmtCodeGeneration() { initComponents(); - preferLongerNamesCheckBox.putClientProperty(OPTION_ID, preferLongerNames); - isForBooleanGettersCheckBox.putClientProperty(OPTION_ID, useIsForBooleanGetters); - fieldPrefixField.putClientProperty(OPTION_ID, fieldNamePrefix); - fieldSuffixField.putClientProperty(OPTION_ID, fieldNameSuffix); - staticFieldPrefixField.putClientProperty(OPTION_ID, staticFieldNamePrefix); - staticFieldSuffixField.putClientProperty(OPTION_ID, staticFieldNameSuffix); - parameterPrefixField.putClientProperty(OPTION_ID, parameterNamePrefix); - parameterSuffixField.putClientProperty(OPTION_ID, parameterNameSuffix); - localVarPrefixField.putClientProperty(OPTION_ID, localVarNamePrefix); - localVarSuffixField.putClientProperty(OPTION_ID, localVarNameSuffix); qualifyFieldAccessCheckBox.putClientProperty(OPTION_ID, qualifyFieldAccess); addOverrideAnnortationCheckBox.putClientProperty(OPTION_ID, addOverrideAnnotation); parametersFinalCheckBox.putClientProperty(OPTION_ID, makeParametersFinal); @@ -144,24 +134,6 @@ visibilityOrderList.setSelectedIndex(0); visibilityOrderList.addListSelectionListener(this); enableVisibilityOrder(); - namingConventionsLabel.setVisible(false); - preferLongerNamesCheckBox.setVisible(false); - isForBooleanGettersCheckBox.setVisible(false); - prefixLabel.setVisible(false); - suffixLabel.setVisible(false); - fieldLabel.setVisible(false); - fieldPrefixField.setVisible(false); - fieldSuffixField.setVisible(false); - staticFieldLabel.setVisible(false); - staticFieldPrefixField.setVisible(false); - staticFieldSuffixField.setVisible(false); - parameterLabel.setVisible(false); - parameterPrefixField.setVisible(false); - parameterSuffixField.setVisible(false); - localVarLabel.setVisible(false); - localVarPrefixField.setVisible(false); - localVarSuffixField.setVisible(false); - jSeparator1.setVisible(false); otherLabel.setVisible(false); qualifyFieldAccessCheckBox.setVisible(false); addOverrideAnnortationCheckBox.setVisible(false); @@ -187,25 +159,6 @@ private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; - namingConventionsLabel = new javax.swing.JLabel(); - preferLongerNamesCheckBox = new javax.swing.JCheckBox(); - isForBooleanGettersCheckBox = new javax.swing.JCheckBox(); - jPanel1 = new javax.swing.JPanel(); - prefixLabel = new javax.swing.JLabel(); - suffixLabel = new javax.swing.JLabel(); - fieldLabel = new javax.swing.JLabel(); - fieldPrefixField = new javax.swing.JTextField(); - fieldSuffixField = new javax.swing.JTextField(); - staticFieldLabel = new javax.swing.JLabel(); - staticFieldPrefixField = new javax.swing.JTextField(); - staticFieldSuffixField = new javax.swing.JTextField(); - parameterLabel = new javax.swing.JLabel(); - parameterPrefixField = new javax.swing.JTextField(); - parameterSuffixField = new javax.swing.JTextField(); - localVarLabel = new javax.swing.JLabel(); - localVarSuffixField = new javax.swing.JTextField(); - localVarPrefixField = new javax.swing.JTextField(); - jSeparator1 = new javax.swing.JSeparator(); otherLabel = new javax.swing.JLabel(); qualifyFieldAccessCheckBox = new javax.swing.JCheckBox(); addOverrideAnnortationCheckBox = new javax.swing.JCheckBox(); @@ -229,141 +182,6 @@ setName(org.openide.util.NbBundle.getMessage(FmtCodeGeneration.class, "LBL_CodeGeneration")); // NOI18N setOpaque(false); - org.openide.awt.Mnemonics.setLocalizedText(namingConventionsLabel, org.openide.util.NbBundle.getMessage(FmtCodeGeneration.class, "LBL_gen_Naming")); // NOI18N - - org.openide.awt.Mnemonics.setLocalizedText(preferLongerNamesCheckBox, org.openide.util.NbBundle.getMessage(FmtCodeGeneration.class, "LBL_gen_PreferLongerNames")); // NOI18N - preferLongerNamesCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); - preferLongerNamesCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0)); - preferLongerNamesCheckBox.setOpaque(false); - - org.openide.awt.Mnemonics.setLocalizedText(isForBooleanGettersCheckBox, org.openide.util.NbBundle.getMessage(FmtCodeGeneration.class, "LBL_gen_UseIsForBooleanGetters")); // NOI18N - isForBooleanGettersCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); - isForBooleanGettersCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0)); - isForBooleanGettersCheckBox.setOpaque(false); - - jPanel1.setLayout(new java.awt.GridBagLayout()); - - org.openide.awt.Mnemonics.setLocalizedText(prefixLabel, org.openide.util.NbBundle.getMessage(FmtCodeGeneration.class, "LBL_gen_Prefix")); // NOI18N - gridBagConstraints = new java.awt.GridBagConstraints(); - gridBagConstraints.gridx = 1; - gridBagConstraints.gridy = 0; - gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; - gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; - gridBagConstraints.weightx = 0.5; - gridBagConstraints.insets = new java.awt.Insets(0, 8, 4, 0); - jPanel1.add(prefixLabel, gridBagConstraints); - - org.openide.awt.Mnemonics.setLocalizedText(suffixLabel, org.openide.util.NbBundle.getMessage(FmtCodeGeneration.class, "LBL_gen_Suffix")); // NOI18N - gridBagConstraints = new java.awt.GridBagConstraints(); - gridBagConstraints.gridx = 2; - gridBagConstraints.gridy = 0; - gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; - gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; - gridBagConstraints.weightx = 0.5; - gridBagConstraints.insets = new java.awt.Insets(0, 8, 4, 0); - jPanel1.add(suffixLabel, gridBagConstraints); - - org.openide.awt.Mnemonics.setLocalizedText(fieldLabel, org.openide.util.NbBundle.getMessage(FmtCodeGeneration.class, "LBL_gen_Field")); // NOI18N - gridBagConstraints = new java.awt.GridBagConstraints(); - gridBagConstraints.gridx = 0; - gridBagConstraints.gridy = 1; - gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; - jPanel1.add(fieldLabel, gridBagConstraints); - - fieldPrefixField.setColumns(5); - gridBagConstraints = new java.awt.GridBagConstraints(); - gridBagConstraints.gridx = 1; - gridBagConstraints.gridy = 1; - gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; - gridBagConstraints.weightx = 0.5; - gridBagConstraints.insets = new java.awt.Insets(4, 8, 4, 0); - jPanel1.add(fieldPrefixField, gridBagConstraints); - - fieldSuffixField.setColumns(5); - gridBagConstraints = new java.awt.GridBagConstraints(); - gridBagConstraints.gridx = 2; - gridBagConstraints.gridy = 1; - gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; - gridBagConstraints.weightx = 0.5; - gridBagConstraints.insets = new java.awt.Insets(4, 8, 4, 0); - jPanel1.add(fieldSuffixField, gridBagConstraints); - - org.openide.awt.Mnemonics.setLocalizedText(staticFieldLabel, org.openide.util.NbBundle.getMessage(FmtCodeGeneration.class, "LBL_gen_StaticField")); // NOI18N - gridBagConstraints = new java.awt.GridBagConstraints(); - gridBagConstraints.gridx = 0; - gridBagConstraints.gridy = 2; - gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; - jPanel1.add(staticFieldLabel, gridBagConstraints); - - staticFieldPrefixField.setColumns(5); - gridBagConstraints = new java.awt.GridBagConstraints(); - gridBagConstraints.gridx = 1; - gridBagConstraints.gridy = 2; - gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; - gridBagConstraints.weightx = 0.5; - gridBagConstraints.insets = new java.awt.Insets(4, 8, 4, 0); - jPanel1.add(staticFieldPrefixField, gridBagConstraints); - - staticFieldSuffixField.setColumns(5); - gridBagConstraints = new java.awt.GridBagConstraints(); - gridBagConstraints.gridx = 2; - gridBagConstraints.gridy = 2; - gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; - gridBagConstraints.weightx = 0.5; - gridBagConstraints.insets = new java.awt.Insets(4, 8, 4, 0); - jPanel1.add(staticFieldSuffixField, gridBagConstraints); - - org.openide.awt.Mnemonics.setLocalizedText(parameterLabel, org.openide.util.NbBundle.getMessage(FmtCodeGeneration.class, "LBL_gen_Parameter")); // NOI18N - gridBagConstraints = new java.awt.GridBagConstraints(); - gridBagConstraints.gridx = 0; - gridBagConstraints.gridy = 3; - gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; - jPanel1.add(parameterLabel, gridBagConstraints); - - parameterPrefixField.setColumns(5); - gridBagConstraints = new java.awt.GridBagConstraints(); - gridBagConstraints.gridx = 1; - gridBagConstraints.gridy = 3; - gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; - gridBagConstraints.weightx = 0.5; - gridBagConstraints.insets = new java.awt.Insets(4, 8, 4, 0); - jPanel1.add(parameterPrefixField, gridBagConstraints); - - parameterSuffixField.setColumns(5); - gridBagConstraints = new java.awt.GridBagConstraints(); - gridBagConstraints.gridx = 2; - gridBagConstraints.gridy = 3; - gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; - gridBagConstraints.weightx = 0.5; - gridBagConstraints.insets = new java.awt.Insets(4, 8, 4, 0); - jPanel1.add(parameterSuffixField, gridBagConstraints); - - org.openide.awt.Mnemonics.setLocalizedText(localVarLabel, org.openide.util.NbBundle.getMessage(FmtCodeGeneration.class, "LBL_gen_LocalVariable")); // NOI18N - gridBagConstraints = new java.awt.GridBagConstraints(); - gridBagConstraints.gridx = 0; - gridBagConstraints.gridy = 4; - gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; - gridBagConstraints.insets = new java.awt.Insets(4, 0, 0, 0); - jPanel1.add(localVarLabel, gridBagConstraints); - - localVarSuffixField.setColumns(5); - gridBagConstraints = new java.awt.GridBagConstraints(); - gridBagConstraints.gridx = 1; - gridBagConstraints.gridy = 4; - gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; - gridBagConstraints.weightx = 0.5; - gridBagConstraints.insets = new java.awt.Insets(4, 8, 0, 0); - jPanel1.add(localVarSuffixField, gridBagConstraints); - - localVarPrefixField.setColumns(5); - gridBagConstraints = new java.awt.GridBagConstraints(); - gridBagConstraints.gridx = 2; - gridBagConstraints.gridy = 4; - gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; - gridBagConstraints.weightx = 0.5; - gridBagConstraints.insets = new java.awt.Insets(4, 8, 0, 0); - jPanel1.add(localVarPrefixField, gridBagConstraints); - org.openide.awt.Mnemonics.setLocalizedText(otherLabel, org.openide.util.NbBundle.getMessage(FmtCodeGeneration.class, "LBL_gen_Other")); // NOI18N org.openide.awt.Mnemonics.setLocalizedText(qualifyFieldAccessCheckBox, org.openide.util.NbBundle.getMessage(FmtCodeGeneration.class, "LBL_gen_QualifyFieldAccess")); // NOI18N @@ -448,7 +266,6 @@ this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addComponent(jSeparator1) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(otherLabel) @@ -463,48 +280,33 @@ .addComponent(jSeparator3) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) - .addComponent(namingConventionsLabel) .addComponent(memberOrderLabel) .addGroup(layout.createSequentialGroup() .addContainerGap() - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) - .addComponent(isForBooleanGettersCheckBox) - .addComponent(preferLongerNamesCheckBox) - .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 274, javax.swing.GroupLayout.PREFERRED_SIZE) - .addGroup(layout.createSequentialGroup() - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addComponent(jScrollPane1) - .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) - .addComponent(downButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(upButton, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) - .addComponent(visDownButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(visUpButton, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)))))) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) + .addComponent(downButton, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(upButton, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) + .addComponent(visDownButton, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(visUpButton, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addComponent(sortByVisibilityCheckBox) .addGroup(layout.createSequentialGroup() .addGap(5, 5, 5) .addComponent(insertionPointLabel) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(insertionPointComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) - .addContainerGap()) + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addComponent(jSeparator2, javax.swing.GroupLayout.Alignment.TRAILING) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() - .addComponent(namingConventionsLabel) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(preferLongerNamesCheckBox) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(isForBooleanGettersCheckBox) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addContainerGap() .addComponent(otherLabel) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(qualifyFieldAccessCheckBox) @@ -605,38 +407,19 @@ // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JCheckBox addOverrideAnnortationCheckBox; private javax.swing.JButton downButton; - private javax.swing.JLabel fieldLabel; - private javax.swing.JTextField fieldPrefixField; - private javax.swing.JTextField fieldSuffixField; private javax.swing.JComboBox insertionPointComboBox; private javax.swing.JLabel insertionPointLabel; - private javax.swing.JCheckBox isForBooleanGettersCheckBox; - private javax.swing.JPanel jPanel1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; - private javax.swing.JSeparator jSeparator1; private javax.swing.JSeparator jSeparator2; private javax.swing.JSeparator jSeparator3; - private javax.swing.JLabel localVarLabel; - private javax.swing.JTextField localVarPrefixField; - private javax.swing.JTextField localVarSuffixField; private javax.swing.JCheckBox localVarsFinalCheckBox; private javax.swing.JLabel memberOrderLabel; private javax.swing.JList membersOrderList; - private javax.swing.JLabel namingConventionsLabel; private javax.swing.JLabel otherLabel; - private javax.swing.JLabel parameterLabel; - private javax.swing.JTextField parameterPrefixField; - private javax.swing.JTextField parameterSuffixField; private javax.swing.JCheckBox parametersFinalCheckBox; - private javax.swing.JCheckBox preferLongerNamesCheckBox; - private javax.swing.JLabel prefixLabel; private javax.swing.JCheckBox qualifyFieldAccessCheckBox; private javax.swing.JCheckBox sortByVisibilityCheckBox; - private javax.swing.JLabel staticFieldLabel; - private javax.swing.JTextField staticFieldPrefixField; - private javax.swing.JTextField staticFieldSuffixField; - private javax.swing.JLabel suffixLabel; private javax.swing.JButton upButton; private javax.swing.JButton visDownButton; private javax.swing.JButton visUpButton; diff -r bdb0f6cd71ec -r 2b309a744129 java.source/src/org/netbeans/modules/java/ui/FmtNaming.form --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java.source/src/org/netbeans/modules/java/ui/FmtNaming.form Tue Apr 23 01:23:45 2013 +0200 @@ -0,0 +1,255 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff -r bdb0f6cd71ec -r 2b309a744129 java.source/src/org/netbeans/modules/java/ui/FmtNaming.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java.source/src/org/netbeans/modules/java/ui/FmtNaming.java Tue Apr 23 01:23:45 2013 +0200 @@ -0,0 +1,478 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * 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. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle 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): + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun + * Microsystems, Inc. All Rights Reserved. + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + */ +package org.netbeans.modules.java.ui; + +import java.awt.Rectangle; +import java.util.ArrayList; +import java.util.Collections; +import java.util.EnumSet; +import java.util.List; +import java.util.prefs.Preferences; + +import javax.lang.model.element.ElementKind; +import javax.lang.model.element.Modifier; +import javax.swing.DefaultListModel; +import javax.swing.JEditorPane; +import javax.swing.JList; +import javax.swing.JPanel; +import javax.swing.text.BadLocationException; +import javax.swing.text.Document; + +import com.sun.source.tree.AssignmentTree; +import com.sun.source.tree.BlockTree; +import com.sun.source.tree.ClassTree; +import com.sun.source.tree.CompilationUnitTree; +import com.sun.source.tree.ExpressionTree; +import com.sun.source.tree.IdentifierTree; +import com.sun.source.tree.ModifiersTree; +import com.sun.source.tree.NewClassTree; +import com.sun.source.tree.Tree; +import com.sun.source.tree.TypeParameterTree; +import com.sun.source.tree.VariableTree; +import javax.lang.model.type.TypeKind; +import javax.swing.event.DocumentEvent; +import javax.swing.event.DocumentListener; + +import org.netbeans.api.java.source.CodeStyle; +import org.netbeans.api.java.source.CodeStyleUtils; +import org.netbeans.api.java.source.GeneratorUtilities; +import org.netbeans.api.java.source.JavaSource.Phase; +import org.netbeans.api.java.source.ModificationResult; +import org.netbeans.api.java.source.TreeMaker; +import org.netbeans.api.java.source.WorkingCopy; +import org.netbeans.editor.BaseDocument; +import org.netbeans.modules.editor.indent.api.Reformat; +import static org.netbeans.modules.java.ui.FmtOptions.*; +import org.netbeans.modules.java.ui.FmtOptions.CategorySupport; +import static org.netbeans.modules.java.ui.FmtOptions.CategorySupport.OPTION_ID; +import org.netbeans.modules.options.editor.spi.PreferencesCustomizer; +import org.netbeans.modules.parsing.api.ResultIterator; +import org.netbeans.modules.parsing.api.Source; +import org.netbeans.modules.parsing.api.UserTask; +import org.openide.cookies.SaveCookie; +import org.openide.filesystems.FileObject; +import org.openide.filesystems.FileUtil; +import org.openide.loaders.DataObject; +import org.openide.util.NbBundle; + +/** + * + * @author Ralph Benjamin Ruijs + */ +public class FmtNaming extends javax.swing.JPanel implements Runnable { + + /** + * Creates new form FmtCodeGeneration + */ + public FmtNaming() { + initComponents(); + preferLongerNamesCheckBox.putClientProperty(OPTION_ID, preferLongerNames); + isForBooleanGettersCheckBox.putClientProperty(OPTION_ID, useIsForBooleanGetters); + fieldPrefixField.putClientProperty(OPTION_ID, fieldNamePrefix); + fieldSuffixField.putClientProperty(OPTION_ID, fieldNameSuffix); + staticFieldPrefixField.putClientProperty(OPTION_ID, staticFieldNamePrefix); + staticFieldSuffixField.putClientProperty(OPTION_ID, staticFieldNameSuffix); + parameterPrefixField.putClientProperty(OPTION_ID, parameterNamePrefix); + parameterSuffixField.putClientProperty(OPTION_ID, parameterNameSuffix); + localVarPrefixField.putClientProperty(OPTION_ID, localVarNamePrefix); + localVarSuffixField.putClientProperty(OPTION_ID, localVarNameSuffix); + } + + public static PreferencesCustomizer.Factory getController() { + return new PreferencesCustomizer.Factory() { + public PreferencesCustomizer create(Preferences preferences) { + NamingCategorySupport support = new NamingCategorySupport(preferences, new FmtNaming()); + ((Runnable) support.panel).run(); + return support; + } + }; + } + + /** + * 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. + */ + // //GEN-BEGIN:initComponents + private void initComponents() { + java.awt.GridBagConstraints gridBagConstraints; + + namingConventionsLabel = new javax.swing.JLabel(); + preferLongerNamesCheckBox = new javax.swing.JCheckBox(); + isForBooleanGettersCheckBox = new javax.swing.JCheckBox(); + jPanel1 = new javax.swing.JPanel(); + prefixLabel = new javax.swing.JLabel(); + suffixLabel = new javax.swing.JLabel(); + fieldLabel = new javax.swing.JLabel(); + fieldPrefixField = new javax.swing.JTextField(); + fieldSuffixField = new javax.swing.JTextField(); + staticFieldLabel = new javax.swing.JLabel(); + staticFieldPrefixField = new javax.swing.JTextField(); + staticFieldSuffixField = new javax.swing.JTextField(); + parameterLabel = new javax.swing.JLabel(); + parameterPrefixField = new javax.swing.JTextField(); + parameterSuffixField = new javax.swing.JTextField(); + localVarLabel = new javax.swing.JLabel(); + localVarSuffixField = new javax.swing.JTextField(); + localVarPrefixField = new javax.swing.JTextField(); + + setName(org.openide.util.NbBundle.getMessage(FmtNaming.class, "LBL_Naming")); // NOI18N + setOpaque(false); + + org.openide.awt.Mnemonics.setLocalizedText(namingConventionsLabel, org.openide.util.NbBundle.getMessage(FmtNaming.class, "LBL_gen_Naming")); // NOI18N + + org.openide.awt.Mnemonics.setLocalizedText(preferLongerNamesCheckBox, org.openide.util.NbBundle.getMessage(FmtNaming.class, "LBL_gen_PreferLongerNames")); // NOI18N + preferLongerNamesCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); + preferLongerNamesCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0)); + preferLongerNamesCheckBox.setOpaque(false); + + org.openide.awt.Mnemonics.setLocalizedText(isForBooleanGettersCheckBox, org.openide.util.NbBundle.getMessage(FmtNaming.class, "LBL_gen_UseIsForBooleanGetters")); // NOI18N + isForBooleanGettersCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); + isForBooleanGettersCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0)); + isForBooleanGettersCheckBox.setOpaque(false); + + jPanel1.setLayout(new java.awt.GridBagLayout()); + + org.openide.awt.Mnemonics.setLocalizedText(prefixLabel, org.openide.util.NbBundle.getMessage(FmtNaming.class, "LBL_gen_Prefix")); // NOI18N + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridx = 1; + gridBagConstraints.gridy = 0; + gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; + gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; + gridBagConstraints.weightx = 0.5; + gridBagConstraints.insets = new java.awt.Insets(0, 8, 4, 0); + jPanel1.add(prefixLabel, gridBagConstraints); + + org.openide.awt.Mnemonics.setLocalizedText(suffixLabel, org.openide.util.NbBundle.getMessage(FmtNaming.class, "LBL_gen_Suffix")); // NOI18N + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridx = 2; + gridBagConstraints.gridy = 0; + gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; + gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; + gridBagConstraints.weightx = 0.5; + gridBagConstraints.insets = new java.awt.Insets(0, 8, 4, 0); + jPanel1.add(suffixLabel, gridBagConstraints); + + org.openide.awt.Mnemonics.setLocalizedText(fieldLabel, org.openide.util.NbBundle.getMessage(FmtNaming.class, "LBL_gen_Field")); // NOI18N + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridx = 0; + gridBagConstraints.gridy = 1; + gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; + jPanel1.add(fieldLabel, gridBagConstraints); + + fieldPrefixField.setColumns(5); + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridx = 1; + gridBagConstraints.gridy = 1; + gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; + gridBagConstraints.weightx = 0.5; + gridBagConstraints.insets = new java.awt.Insets(4, 8, 4, 0); + jPanel1.add(fieldPrefixField, gridBagConstraints); + + fieldSuffixField.setColumns(5); + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridx = 2; + gridBagConstraints.gridy = 1; + gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; + gridBagConstraints.weightx = 0.5; + gridBagConstraints.insets = new java.awt.Insets(4, 8, 4, 0); + jPanel1.add(fieldSuffixField, gridBagConstraints); + + org.openide.awt.Mnemonics.setLocalizedText(staticFieldLabel, org.openide.util.NbBundle.getMessage(FmtNaming.class, "LBL_gen_StaticField")); // NOI18N + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridx = 0; + gridBagConstraints.gridy = 2; + gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; + jPanel1.add(staticFieldLabel, gridBagConstraints); + + staticFieldPrefixField.setColumns(5); + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridx = 1; + gridBagConstraints.gridy = 2; + gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; + gridBagConstraints.weightx = 0.5; + gridBagConstraints.insets = new java.awt.Insets(4, 8, 4, 0); + jPanel1.add(staticFieldPrefixField, gridBagConstraints); + + staticFieldSuffixField.setColumns(5); + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridx = 2; + gridBagConstraints.gridy = 2; + gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; + gridBagConstraints.weightx = 0.5; + gridBagConstraints.insets = new java.awt.Insets(4, 8, 4, 0); + jPanel1.add(staticFieldSuffixField, gridBagConstraints); + + org.openide.awt.Mnemonics.setLocalizedText(parameterLabel, org.openide.util.NbBundle.getMessage(FmtNaming.class, "LBL_gen_Parameter")); // NOI18N + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridx = 0; + gridBagConstraints.gridy = 3; + gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; + jPanel1.add(parameterLabel, gridBagConstraints); + + parameterPrefixField.setColumns(5); + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridx = 1; + gridBagConstraints.gridy = 3; + gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; + gridBagConstraints.weightx = 0.5; + gridBagConstraints.insets = new java.awt.Insets(4, 8, 4, 0); + jPanel1.add(parameterPrefixField, gridBagConstraints); + + parameterSuffixField.setColumns(5); + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridx = 2; + gridBagConstraints.gridy = 3; + gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; + gridBagConstraints.weightx = 0.5; + gridBagConstraints.insets = new java.awt.Insets(4, 8, 4, 0); + jPanel1.add(parameterSuffixField, gridBagConstraints); + + org.openide.awt.Mnemonics.setLocalizedText(localVarLabel, org.openide.util.NbBundle.getMessage(FmtNaming.class, "LBL_gen_LocalVariable")); // NOI18N + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridx = 0; + gridBagConstraints.gridy = 4; + gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; + gridBagConstraints.insets = new java.awt.Insets(4, 0, 0, 0); + jPanel1.add(localVarLabel, gridBagConstraints); + + localVarSuffixField.setColumns(5); + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridx = 2; + gridBagConstraints.gridy = 4; + gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; + gridBagConstraints.weightx = 0.5; + gridBagConstraints.insets = new java.awt.Insets(4, 8, 0, 0); + jPanel1.add(localVarSuffixField, gridBagConstraints); + + localVarPrefixField.setColumns(5); + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridx = 1; + gridBagConstraints.gridy = 4; + gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; + gridBagConstraints.weightx = 0.5; + gridBagConstraints.insets = new java.awt.Insets(4, 8, 0, 0); + jPanel1.add(localVarPrefixField, gridBagConstraints); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); + this.setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(namingConventionsLabel) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(isForBooleanGettersCheckBox) + .addComponent(preferLongerNamesCheckBox) + .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 274, javax.swing.GroupLayout.PREFERRED_SIZE)))) + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addComponent(namingConventionsLabel) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(preferLongerNamesCheckBox) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(isForBooleanGettersCheckBox) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + ); + }// //GEN-END:initComponents + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JLabel fieldLabel; + private javax.swing.JTextField fieldPrefixField; + private javax.swing.JTextField fieldSuffixField; + private javax.swing.JCheckBox isForBooleanGettersCheckBox; + private javax.swing.JPanel jPanel1; + private javax.swing.JLabel localVarLabel; + private javax.swing.JTextField localVarPrefixField; + private javax.swing.JTextField localVarSuffixField; + private javax.swing.JLabel namingConventionsLabel; + private javax.swing.JLabel parameterLabel; + private javax.swing.JTextField parameterPrefixField; + private javax.swing.JTextField parameterSuffixField; + private javax.swing.JCheckBox preferLongerNamesCheckBox; + private javax.swing.JLabel prefixLabel; + private javax.swing.JLabel staticFieldLabel; + private javax.swing.JTextField staticFieldPrefixField; + private javax.swing.JTextField staticFieldSuffixField; + private javax.swing.JLabel suffixLabel; + // End of variables declaration//GEN-END:variables + + @Override + public void run() { + + } + + private static final class NamingCategorySupport extends CategorySupport { + + private Source source = null; + + private NamingCategorySupport(Preferences preferences, JPanel panel) { + super(preferences, "naming", panel, NbBundle.getMessage(FmtNaming.class, "SAMPLE_Naming"), //NOI18N + new String[]{FmtOptions.blankLinesBeforeFields, "1"}); //NOI18N + } + + @Override + public void refreshPreview() { + final JEditorPane jep = (JEditorPane) getPreviewComponent(); + try { + Class.forName(CodeStyle.class.getName(), true, CodeStyle.class.getClassLoader()); + } catch (ClassNotFoundException cnfe) { + // ignore + } + + final CodeStyle codeStyle = codeStyleProducer.create(previewPrefs); + jep.setIgnoreRepaint(true); + try { + if (source == null) { + FileObject fo = FileUtil.createMemoryFileSystem().getRoot().createData("org.netbeans.samples.ClassA", "java"); //NOI18N + source = Source.create(fo); + } + final Document doc = source.getDocument(true); + if (doc.getLength() > 0) { + doc.remove(0, doc.getLength()); + } + doc.insertString(0, previewText, null); + doc.putProperty(CodeStyle.class, codeStyle); + jep.setDocument(doc); + ModificationResult result = ModificationResult.runModificationTask(Collections.singleton(source), new UserTask() { + @Override + public void run(ResultIterator resultIterator) throws Exception { + WorkingCopy copy = WorkingCopy.get(resultIterator.getParserResult()); + copy.toPhase(Phase.RESOLVED); + TreeMaker tm = copy.getTreeMaker(); + GeneratorUtilities gu = GeneratorUtilities.get(copy); + CompilationUnitTree cut = copy.getCompilationUnit(); + ClassTree ct = (ClassTree) cut.getTypeDecls().get(0); + List members = new ArrayList(); + String name = CodeStyleUtils.addPrefixSuffix("name", + codeStyle.getFieldNamePrefix(), + codeStyle.getFieldNameSuffix()); + VariableTree field = tm.Variable(tm.Modifiers(Collections.singleton(Modifier.PRIVATE)), name, tm.Type("String"), null); + members.add(field); + String cond = CodeStyleUtils.addPrefixSuffix("cond", + codeStyle.getFieldNamePrefix(), + codeStyle.getFieldNameSuffix()); + VariableTree booleanField = tm.Variable(tm.Modifiers(Collections.singleton(Modifier.PRIVATE)), cond, tm.PrimitiveType(TypeKind.BOOLEAN), null); + members.add(booleanField); + members.add(gu.createConstructor(ct, Collections.singletonList(field))); + members.add(gu.createGetter(field)); + members.add(gu.createSetter(ct, field)); + members.add(gu.createGetter(booleanField)); + members.add(gu.createSetter(ct, booleanField)); + ModifiersTree mods = tm.Modifiers(EnumSet.of(Modifier.PRIVATE, Modifier.STATIC)); + ClassTree nested = tm.Class(mods, "Nested", Collections.emptyList(), null, Collections.emptyList(), Collections.emptyList()); //NOI18N + members.add(nested); + IdentifierTree nestedId = tm.Identifier("Nested"); //NOI18N + String instance = CodeStyleUtils.addPrefixSuffix("instance", + codeStyle.getStaticFieldNamePrefix(), + codeStyle.getStaticFieldNameSuffix()); + VariableTree staticField = tm.Variable(mods, instance, nestedId, null); //NOI18N + members.add(staticField); + members.add(gu.createGetter(staticField)); + members.add(gu.createSetter(ct, staticField)); + ClassTree newCT = gu.insertClassMembers(ct, members); + copy.rewrite(ct, newCT); + } + }); + result.commit(); + final Reformat reformat = Reformat.get(doc); + reformat.lock(); + try { + if (doc instanceof BaseDocument) { + ((BaseDocument) doc).runAtomicAsUser(new Runnable() { + @Override + public void run() { + try { + reformat.reformat(0, doc.getLength()); + } catch (BadLocationException ble) { + } + } + }); + } else { + reformat.reformat(0, doc.getLength()); + } + } finally { + reformat.unlock(); + } + DataObject dataObject = DataObject.find(source.getFileObject()); + SaveCookie sc = dataObject.getLookup().lookup(SaveCookie.class); + if (sc != null) { + sc.save(); + } + } catch (Exception ex) { + } + jep.setIgnoreRepaint(false); + jep.scrollRectToVisible(new Rectangle(0, 0, 10, 10)); + jep.repaint(100); + } + + private static class Element { + + private boolean isStatic; + private ElementKind kind; + + @Override + public String toString() { + return (isStatic ? NbBundle.getMessage(FmtNaming.class, "VAL_gen_STATIC") + " " : "") //NOI18N + + NbBundle.getMessage(FmtNaming.class, "VAL_gen_" + kind.name()); //NOI18N + } + } + + private static class Visibility { + + private String kind; + + @Override + public String toString() { + return NbBundle.getMessage(FmtNaming.class, "VAL_gen_" + kind); //NOI18N + } + } + } +} diff -r bdb0f6cd71ec -r 2b309a744129 refactoring.java/nbproject/project.xml --- a/refactoring.java/nbproject/project.xml Tue Apr 23 01:17:09 2013 +0200 +++ b/refactoring.java/nbproject/project.xml Tue Apr 23 01:23:45 2013 +0200 @@ -116,7 +116,7 @@ - 0.110 + 0.122 diff -r bdb0f6cd71ec -r 2b309a744129 refactoring.java/src/org/netbeans/modules/refactoring/java/RefactoringUtils.java --- a/refactoring.java/src/org/netbeans/modules/refactoring/java/RefactoringUtils.java Tue Apr 23 01:17:09 2013 +0200 +++ b/refactoring.java/src/org/netbeans/modules/refactoring/java/RefactoringUtils.java Tue Apr 23 01:23:45 2013 +0200 @@ -56,9 +56,11 @@ import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.Modifier; import javax.lang.model.element.TypeElement; +import javax.lang.model.element.VariableElement; import javax.lang.model.type.*; import javax.lang.model.util.ElementFilter; import javax.lang.model.util.Types; +import javax.swing.text.Document; import org.netbeans.api.annotations.common.NullUnknown; import org.netbeans.api.java.classpath.ClassPath; import org.netbeans.api.java.classpath.ClassPath.Entry; @@ -90,7 +92,6 @@ private static final String JAVA_MIME_TYPE = "text/x-java"; // NOI18N private static final Logger LOG = Logger.getLogger(RefactoringUtils.class.getName()); - private static final RequestProcessor RP = new RequestProcessor(RefactoringUtils.class.getName(), 1, false, false); /** * Get all overriding methods for given ExecutableElement @@ -195,6 +196,27 @@ } return result; } + + public static CodeStyle getCodeStyle(CompilationInfo info) { + if (info != null) { + try { + Document doc = info.getDocument(); + if (doc != null) { + CodeStyle cs = (CodeStyle)doc.getProperty(CodeStyle.class); + return cs != null ? cs : CodeStyle.getDefault(doc); + } + } catch (IOException ioe) { + // ignore + } + + FileObject file = info.getFileObject(); + if (file != null) { + return CodeStyle.getDefault(file); + } + } + + return CodeStyle.getDefault((Document)null); + } /** * @@ -815,11 +837,6 @@ } } - private static String getString(String key) { - return NbBundle.getMessage(RefactoringUtils.class, key); - } - - //XXX: copied from SourceUtils.addImports. Ideally, should be on one place only: public static CompilationUnitTree addImports(CompilationUnitTree cut, List toImport, TreeMaker make) throws IOException { @@ -902,7 +919,11 @@ } public static boolean isSetter(CompilationInfo info, ExecutableElement el, Element propertyElement) { - String setterName = getSetterName(propertyElement.getSimpleName().toString()); + CodeStyle codeStyle = getCodeStyle(info); + String setterName = CodeStyleUtils.computeSetterName( + propertyElement.getSimpleName(), + propertyElement.getModifiers().contains(Modifier.STATIC), + codeStyle); return el.getSimpleName().contentEquals(setterName) && el.getReturnType().getKind() == TypeKind.VOID @@ -911,39 +932,32 @@ } public static boolean isGetter(CompilationInfo info, ExecutableElement el, Element propertyElement) { - String getterName = getGetterName(propertyElement.getSimpleName().toString()); + CodeStyle codeStyle = getCodeStyle(info); + String getterName = CodeStyleUtils.computeGetterName( + propertyElement.getSimpleName(), + propertyElement.asType().getKind() == TypeKind.BOOLEAN, + propertyElement.getModifiers().contains(Modifier.STATIC), + codeStyle); return el.getSimpleName().contentEquals(getterName) && info.getTypes().isSameType(el.getReturnType(),propertyElement.asType()) && el.getParameters().isEmpty(); } + + public static String removeFieldPrefixSuffix(Element var, CodeStyle cs) { + boolean isStatic = var.getModifiers().contains(Modifier.STATIC); + return CodeStyleUtils.removePrefixSuffix(var.getSimpleName(), + isStatic ? cs.getStaticFieldNamePrefix() : cs.getFieldNamePrefix(), + isStatic ? cs.getStaticFieldNameSuffix() : cs.getFieldNameSuffix()); + } + + public static String addParamPrefixSuffix(CharSequence name, CodeStyle cs) { + return CodeStyleUtils.addPrefixSuffix(name, + cs.getParameterNamePrefix(), + cs.getParameterNameSuffix()); + } public static String getTestMethodName(String propertyName) { - return "test" + capitalizeFirstLetter(propertyName); //NOI18N - } - - public static String getGetterName(String propertyName) { - return "get" + capitalizeFirstLetter(propertyName); //NOI18N - } - - public static String getSetterName(String propertyName) { - return "set" + capitalizeFirstLetter(propertyName); //NOI18N - } - - /** - * Utility method capitalizes the first letter of string, used to generate - * method names for patterns - * - * @param str The string for capitalization. - * @return String with the first letter capitalized. - */ - private static String capitalizeFirstLetter(String str) { - if (str == null || str.length() <= 0) { - return str; - } - - char chars[] = str.toCharArray(); - chars[0] = Character.toUpperCase(chars[0]); - return new String(chars); + return "test" + CodeStyleUtils.getCapitalizedName(propertyName); //NOI18N } public static boolean isWeakerAccess(Set modifiers, Set modifiers0) { diff -r bdb0f6cd71ec -r 2b309a744129 refactoring.java/src/org/netbeans/modules/refactoring/java/plugins/EncapsulateFieldRefactoringPlugin.java --- a/refactoring.java/src/org/netbeans/modules/refactoring/java/plugins/EncapsulateFieldRefactoringPlugin.java Tue Apr 23 01:17:09 2013 +0200 +++ b/refactoring.java/src/org/netbeans/modules/refactoring/java/plugins/EncapsulateFieldRefactoringPlugin.java Tue Apr 23 01:23:45 2013 +0200 @@ -97,7 +97,7 @@ private static Set accessModifiers = EnumSet.of(Modifier.PRIVATE, Modifier.PROTECTED, Modifier.PUBLIC); private static List MODIFIERS = Arrays.asList(Modifier.PRIVATE, null, Modifier.PROTECTED, Modifier.PUBLIC); private final EncapsulateFieldRefactoring refactoring; - public static final String CLASS_FIELD_PREFIX = "_"; // NOI18N + /** * path in source with field declaration; refactoring.getSelectedObject() * may contain path to a reference @@ -390,55 +390,6 @@ return false; } - /** - * Removes the class field prefix from the identifer of a field. For - * example, if the class field prefix is "_", the identifier "_name" is - * stripped to become "name". - * - * @param identifierString The identifer to strip. - * @return The stripped identifier. - */ - private static String stripPrefix(String identifierString) { - String stripped; - if (identifierString.startsWith(CLASS_FIELD_PREFIX) && identifierString.length() > 1) { - stripped = identifierString.substring(CLASS_FIELD_PREFIX.length()); - } else { - stripped = identifierString; - } - return stripped; - } - - private static StringBuilder getCapitalizedName(VariableElement field) { - StringBuilder name = new StringBuilder(stripPrefix(field.getSimpleName().toString())); - - //Beans naming convention, #165241 - if (name.length() > 1 && Character.isUpperCase(name.charAt(1))) { - return name; - } - - name.setCharAt(0, Character.toUpperCase(name.charAt(0))); - return name; - } - - public static String computeSetterName(VariableElement field) { - StringBuilder name = getCapitalizedName(field); - - name.insert(0, "set"); //NOI18N - return name.toString(); - } - - public static String computeGetterName(VariableElement field) { - StringBuilder name = getCapitalizedName(field); - - if (TypeKind.BOOLEAN == field.asType().getKind()) { // XXX check autoboxing??? - name.insert(0, "is"); //NOI18N - } else { - name.insert(0, "get"); //NOI18N - } - - return name.toString(); - } - @Override public Problem prepare(RefactoringElementsBag bag) { @@ -605,24 +556,24 @@ ClassTree nct = node; List newMembers = new ArrayList(); int getterIdx = 0; - VariableTree pcs = null; - if (descs.get(0).refactoring.isGeneratePropertyChangeSupport()) { - pcs = getPropertyChangeSupport(clazz, "java.beans.PropertyChangeSupport"); //NOI18N - if (pcs == null) { - pcs = createPropertyChangeSupport("java.beans.PropertyChangeSupport", "propertyChangeSupport");//NOI18N - newMembers.add(pcs); - } + VariableTree pcs = null; + if (descs.get(0).refactoring.isGeneratePropertyChangeSupport()) { + pcs = getPropertyChangeSupport(clazz, "java.beans.PropertyChangeSupport"); //NOI18N + if (pcs == null) { + pcs = createPropertyChangeSupport("java.beans.PropertyChangeSupport", "propertyChangeSupport");//NOI18N + newMembers.add(pcs); } + } - VariableTree vcs = null; - if (descs.get(0).refactoring.isGenerateVetoableChangeSupport()) { - vcs = getPropertyChangeSupport(clazz, "java.beans.VetoableChangeSupport");//NOI18N - if (vcs == null) { - vcs = createPropertyChangeSupport("java.beans.VetoableChangeSupport", "vetoableChangeSupport");//NOI18N - newMembers.add(vcs); - } + VariableTree vcs = null; + if (descs.get(0).refactoring.isGenerateVetoableChangeSupport()) { + vcs = getPropertyChangeSupport(clazz, "java.beans.VetoableChangeSupport");//NOI18N + if (vcs == null) { + vcs = createPropertyChangeSupport("java.beans.VetoableChangeSupport", "vetoableChangeSupport");//NOI18N + newMembers.add(vcs); } - + } + CodeStyle cs = RefactoringUtils.getCodeStyle(workingCopy); for (EncapsulateDesc desc : descs) { VariableTree propName = createPropName(clazz, desc); if (pcs!=null) { @@ -633,6 +584,7 @@ desc.refactoring.getGetterName(), desc.refactoring.getSetterName(), desc.refactoring.getMethodModifiers(), + cs, pcs, vcs, propName); @@ -989,17 +941,18 @@ } private MethodTree[] createGetterAndSetter( - VariableElement field, String getterName, - String setterName, Set useModifiers, + VariableElement field, String getterName, String setterName, + Set useModifiers, CodeStyle cs, VariableTree propertyChange, VariableTree vetoableChange, VariableTree propName) { - - - String fieldName = field.getSimpleName().toString(); boolean staticMod = field.getModifiers().contains(Modifier.STATIC); + String fieldName = CodeStyleUtils.removePrefixSuffix(field.getSimpleName(), + staticMod ? cs.getStaticFieldNamePrefix() : cs.getFieldNamePrefix(), + staticMod ? cs.getStaticFieldNameSuffix() : cs.getFieldNameSuffix()); + String longName = (staticMod ? "" : "this.") + fieldName;//NOI18N - String oldName = "old" + getCapitalizedName(field);//NOI18N - String parName = staticMod ? "a" + getCapitalizedName(field) : Utilities.isJavaIdentifier(stripPrefix(fieldName)) ? stripPrefix(fieldName) : fieldName; //NOI18N - String getterBody = "{return " + fieldName + ";}"; //NOI18N + String oldName = "old" + CodeStyleUtils.getCapitalizedName(fieldName);//NOI18N + String parName = staticMod ? "a" + CodeStyleUtils.getCapitalizedName(fieldName) : fieldName; //NOI18N + String getterBody = "{return " + field.getSimpleName() + ";}"; //NOI18N StringBuilder setterBody = new StringBuilder(); setterBody.append("{");//NOI18N diff -r bdb0f6cd71ec -r 2b309a744129 refactoring.java/src/org/netbeans/modules/refactoring/java/plugins/IntroduceLocalExtensionTransformer.java --- a/refactoring.java/src/org/netbeans/modules/refactoring/java/plugins/IntroduceLocalExtensionTransformer.java Tue Apr 23 01:17:09 2013 +0200 +++ b/refactoring.java/src/org/netbeans/modules/refactoring/java/plugins/IntroduceLocalExtensionTransformer.java Tue Apr 23 01:23:45 2013 +0200 @@ -58,6 +58,7 @@ import javax.lang.model.util.Types; import org.netbeans.api.java.source.*; import org.netbeans.modules.refactoring.api.Problem; +import org.netbeans.modules.refactoring.java.RefactoringUtils; import org.netbeans.modules.refactoring.java.api.IntroduceLocalExtensionRefactoring; import org.netbeans.modules.refactoring.java.api.IntroduceLocalExtensionRefactoring.Equality; import org.netbeans.modules.refactoring.java.spi.RefactoringVisitor; @@ -74,6 +75,7 @@ public class IntroduceLocalExtensionTransformer extends RefactoringVisitor { private static final Logger LOG = Logger.getLogger(IntroduceLocalExtensionTransformer.class.getName()); + private static final String DELEGATE = "delegate"; //NOI18N private final IntroduceLocalExtensionRefactoring refactoring; private Problem problem; private String fqn; @@ -96,6 +98,7 @@ fqn = packageName + '.' + name; GeneratorUtilities genUtils = GeneratorUtilities.get(workingCopy); + CodeStyle cs = RefactoringUtils.getCodeStyle(workingCopy); TypeElement source = (TypeElement) refactoring.getRefactoringSource().lookup(TreePathHandle.class).getElementHandle().resolve(workingCopy); @@ -112,9 +115,9 @@ if (wrap && noInterface) { Tree type = make.Type(source.asType()); - VariableTree field = make.Variable(make.Modifiers(EnumSet.of(Modifier.PRIVATE)), "delegate", type, null); //NOI18N + VariableTree field = make.Variable(make.Modifiers(EnumSet.of(Modifier.PRIVATE)), DELEGATE, type, null); //NOI18N members.add(0, field); - addFields(source, genUtils, members); + addFields(source, cs, members); } if (wrap && noInterface) { @@ -159,7 +162,7 @@ return super.scan(tree, p); } - private void addFields(TypeElement source, GeneratorUtilities genUtils, List members) throws IllegalStateException { + private void addFields(TypeElement source, CodeStyle cs, List members) throws IllegalStateException { for (VariableElement field : ElementFilter.fieldsIn(workingCopy.getElements().getAllMembers(source))) { if (!field.getModifiers().contains(Modifier.NATIVE) && field.getModifiers().contains(Modifier.PUBLIC) @@ -168,23 +171,25 @@ VariableTree variable = make.Variable(field, make.QualIdent(field)); members.add(0, variable); } else { - String getterName = EncapsulateFieldRefactoringPlugin.computeGetterName(field); - String setterName = EncapsulateFieldRefactoringPlugin.computeSetterName(field); - MethodTree[] createdGetterAndSetter = createGetterAndSetter(field, getterName, setterName, field.getModifiers()); + MethodTree[] createdGetterAndSetter = createGetterAndSetter(field, field.getModifiers(), cs); ElementHandle fieldHandle = ElementHandle.create((Element) field); - getterSetterMap.put(fieldHandle, new String[]{getterName, setterName}); + getterSetterMap.put(fieldHandle, new String[]{ + createdGetterAndSetter[0].getName().toString(), + createdGetterAndSetter[1].getName().toString()}); members.addAll(Arrays.asList(createdGetterAndSetter)); } } } } - private MethodTree[] createGetterAndSetter(VariableElement field, String getterName, String setterName, Set useModifiers) { + private MethodTree[] createGetterAndSetter(VariableElement field, Set useModifiers, CodeStyle cs) { + boolean staticMod = field.getModifiers().contains(Modifier.STATIC); + String getterName = CodeStyleUtils.computeGetterName(field.getSimpleName(), field.asType().getKind() == TypeKind.BOOLEAN, staticMod, cs); + String setterName = CodeStyleUtils.computeSetterName(field.getSimpleName(), staticMod, cs); String fieldName = field.getSimpleName().toString(); - boolean staticMod = field.getModifiers().contains(Modifier.STATIC); - String longName = staticMod ? "delegate." : "this.delegate." + fieldName;//NOI18N + String longName = (staticMod ? "" : "this.") + DELEGATE + "." + fieldName;//NOI18N String parName = staticMod ? "a" + getCapitalizedName(field) : Utilities.isJavaIdentifier(stripPrefix(fieldName)) ? stripPrefix(fieldName) : fieldName; //NOI18N - String getterBody = "{return delegate." + fieldName + ";}"; //NOI18N + String getterBody = "{return " + DELEGATE + "." + fieldName + ";}"; //NOI18N StringBuilder setterBody = new StringBuilder(); setterBody.append("{");//NOI18N setterBody.append(longName).append(" = ").append(parName).append(";"); //NOI18N @@ -301,7 +306,7 @@ * return this.delegate.hashCode(); * } */ - BlockTree body = make.Block(Collections.singletonList(make.Return(make.MethodInvocation(Collections.EMPTY_LIST, make.MemberSelect(make.Identifier("this.delegate"), "equals"), Collections.singletonList(make.MemberSelect(make.Identifier("o"), "delegate"))))), false); //NOI18N + BlockTree body = make.Block(Collections.singletonList(make.Return(make.MethodInvocation(Collections.EMPTY_LIST, make.MemberSelect(make.Identifier("this.delegate"), "equals"), Collections.singletonList(make.MemberSelect(make.Identifier("o"), DELEGATE))))), false); //NOI18N MethodTree method = make.Method(make.Modifiers(EnumSet.of(Modifier.PUBLIC)), "equals" + refactoring.getNewName(), //NOI18N make.PrimitiveType(TypeKind.BOOLEAN), @@ -332,7 +337,7 @@ statements.add( make.If(make.InstanceOf(make.Identifier("o"), newSimpleTypeTree), make.Block(Collections.singletonList(make.ExpressionStatement( make.Assignment(make.Identifier("target"), - make.MemberSelect(make.Parenthesized(make.TypeCast(newSimpleTypeTree, make.Identifier("o"))), "delegate")))), false), null)); + make.MemberSelect(make.Parenthesized(make.TypeCast(newSimpleTypeTree, make.Identifier("o"))), DELEGATE)))), false), null)); statements.add(make.Return(make.MethodInvocation(Collections.EMPTY_LIST, make.Identifier("this.delegate.equals"), Collections.singletonList(make.Identifier("target"))))); BlockTree body = make.Block(statements, false); MethodTree method = make.Method(make.Modifiers(EnumSet.of(Modifier.PUBLIC)), diff -r bdb0f6cd71ec -r 2b309a744129 refactoring.java/src/org/netbeans/modules/refactoring/java/plugins/JavaPluginUtils.java --- a/refactoring.java/src/org/netbeans/modules/refactoring/java/plugins/JavaPluginUtils.java Tue Apr 23 01:17:09 2013 +0200 +++ b/refactoring.java/src/org/netbeans/modules/refactoring/java/plugins/JavaPluginUtils.java Tue Apr 23 01:23:45 2013 +0200 @@ -53,8 +53,12 @@ import javax.lang.model.SourceVersion; import javax.lang.model.element.Element; import javax.lang.model.element.ElementKind; +import javax.lang.model.element.ExecutableElement; +import javax.lang.model.element.Modifier; import javax.lang.model.element.TypeElement; +import javax.lang.model.element.VariableElement; import javax.lang.model.type.*; +import javax.lang.model.util.Types; import org.netbeans.api.java.classpath.ClassPath; import org.netbeans.api.java.source.ClasspathInfo.PathKind; import org.netbeans.api.java.source.*; @@ -296,18 +300,66 @@ return source; } + public static boolean hasGetter(CompilationInfo info, TypeElement typeElement, VariableElement field, Map> methods, CodeStyle cs) { + CharSequence name = field.getSimpleName(); + assert name.length() > 0; + TypeMirror type = field.asType(); + boolean isStatic = field.getModifiers().contains(Modifier.STATIC); + String getterName = CodeStyleUtils.computeGetterName(name, type.getKind() == TypeKind.BOOLEAN, isStatic, cs); + Types types = info.getTypes(); + List candidates = methods.get(getterName); + if (candidates != null) { + for (ExecutableElement candidate : candidates) { + if ((!candidate.getModifiers().contains(Modifier.ABSTRACT) || candidate.getEnclosingElement() == typeElement) + && candidate.getParameters().isEmpty() + && types.isSameType(candidate.getReturnType(), type)) + return true; + } + } + return false; + } + + public static boolean hasSetter(CompilationInfo info, TypeElement typeElement, VariableElement field, Map> methods, CodeStyle cs) { + CharSequence name = field.getSimpleName(); + assert name.length() > 0; + TypeMirror type = field.asType(); + boolean isStatic = field.getModifiers().contains(Modifier.STATIC); + String setterName = CodeStyleUtils.computeSetterName(name, isStatic, cs); + Types types = info.getTypes(); + List candidates = methods.get(setterName); + if (candidates != null) { + for (ExecutableElement candidate : candidates) { + if ((!candidate.getModifiers().contains(Modifier.ABSTRACT) || candidate.getEnclosingElement() == typeElement) + && candidate.getReturnType().getKind() == TypeKind.VOID + && candidate.getParameters().size() == 1 + && types.isSameType(candidate.getParameters().get(0).asType(), type)) + return true; + } + } + return false; + } + // public static final String DEFAULT_NAME = "par"; // NOI18N public static String makeNameUnique(CompilationInfo info, Scope s, String name) { - return makeNameUnique(info, s, name, Collections.EMPTY_LIST); + return makeNameUnique(info, s, name, Collections.EMPTY_LIST, null, null); } + public static String makeNameUnique(CompilationInfo info, Scope s, String name, List definedIds) { + return makeNameUnique(info, s, name, definedIds, null, null); + } + + public static String makeNameUnique(CompilationInfo info, Scope s, String name, String prefix, String suffix) { + return makeNameUnique(info, s, name, Collections.EMPTY_LIST, prefix, suffix); + } + + public static String makeNameUnique(CompilationInfo info, Scope s, String name, List definedIds, String prefix, String suffix) { + boolean cont; + String proposedName; + name = CodeStyleUtils.addPrefixSuffix(name, prefix, null); int counter = 0; - boolean cont = true; - String proposedName = name; - - while (cont) { - proposedName = (counter != 0 ? name + String.valueOf(counter) : name); + do { + proposedName = name + (counter != 0 ? String.valueOf(counter) : "") + safeString(suffix); cont = false; @@ -320,20 +372,20 @@ } } for (Element e : info.getElementUtilities().getLocalMembersAndVars(s, new VariablesFilter())) { - if (proposedName.equals(e.getSimpleName().toString())) { - counter++; - cont = true; - break; + if (proposedName.equals(e.getSimpleName().toString())) { + counter++; + cont = true; + break; } } } - } + } while(cont); return proposedName; } - public static String getName(Tree et) { - return adjustName(getNameRaw(et)); + private static String safeString(String str) { + return str == null ? "" : str; } public static String getName(TypeMirror tm) { @@ -352,33 +404,40 @@ } } + public static String getName(ExpressionTree et) { + return getName((Tree) et); + } + + public static String getName(Tree et) { + return adjustName(getNameRaw(et)); + } + private static String getNameRaw(Tree et) { - if (et == null) { + if (et == null) return null; - } - + switch (et.getKind()) { - case IDENTIFIER: - return ((IdentifierTree) et).getName().toString(); - case METHOD_INVOCATION: - return getName(((MethodInvocationTree) et).getMethodSelect()); - case MEMBER_SELECT: - return ((MemberSelectTree) et).getIdentifier().toString(); - case NEW_CLASS: - return firstToLower(getName(((NewClassTree) et).getIdentifier())); - case PARAMETERIZED_TYPE: - return firstToLower(getName(((ParameterizedTypeTree) et).getType())); - case STRING_LITERAL: - String name = guessLiteralName((String) ((LiteralTree) et).getValue()); - if(name == null) { - return firstToLower(String.class.getSimpleName()); - } else { - return firstToLower(name); - } - case VARIABLE: - return ((VariableTree) et).getName().toString(); - default: - return null; + case IDENTIFIER: + return ((IdentifierTree) et).getName().toString(); + case METHOD_INVOCATION: + return getNameRaw(((MethodInvocationTree) et).getMethodSelect()); + case MEMBER_SELECT: + return ((MemberSelectTree) et).getIdentifier().toString(); + case NEW_CLASS: + return firstToLower(getNameRaw(((NewClassTree) et).getIdentifier())); + case PARAMETERIZED_TYPE: + return firstToLower(getNameRaw(((ParameterizedTypeTree) et).getType())); + case STRING_LITERAL: + String name = guessLiteralName((String) ((LiteralTree) et).getValue()); + if(name == null) { + return firstToLower(String.class.getSimpleName()); + } else { + return firstToLower(name); + } + case VARIABLE: + return ((VariableTree) et).getName().toString(); + default: + return null; } } @@ -412,23 +471,23 @@ if (name.length() == 0) { return null; } - + StringBuilder result = new StringBuilder(); boolean toLower = true; char last = Character.toLowerCase(name.charAt(0)); - + for (int i = 1; i < name.length(); i++) { - if (toLower && Character.isUpperCase(name.charAt(i))) { + if (toLower && (Character.isUpperCase(name.charAt(i)) || name.charAt(i) == '_')) { result.append(Character.toLowerCase(last)); } else { result.append(last); toLower = false; } last = name.charAt(i); - + } - - result.append(last); + + result.append(toLower ? Character.toLowerCase(last) : last); if (SourceVersion.isKeyword(result)) { return "a" + name; @@ -438,25 +497,20 @@ } private static String guessLiteralName(String str) { - StringBuilder sb = new StringBuilder(); - if(str.length() == 0) { + if(str.isEmpty()) { return null; } - char first = str.charAt(0); - if(Character.isJavaIdentifierStart(str.charAt(0))) { - sb.append(first); - } - for (int i = 1; i < str.length(); i++) { + StringBuilder sb = new StringBuilder(); + + for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if(ch == ' ') { sb.append('_'); - continue; - } - if (Character.isJavaIdentifierPart(ch)) { + } else if (sb.length() == 0 ? Character.isJavaIdentifierStart(ch) : Character.isJavaIdentifierPart(ch)) { sb.append(ch); } - if (i > 40) { + if (sb.length() > 40) { break; } } diff -r bdb0f6cd71ec -r 2b309a744129 refactoring.java/src/org/netbeans/modules/refactoring/java/plugins/RenamePropertyRefactoringPlugin.java --- a/refactoring.java/src/org/netbeans/modules/refactoring/java/plugins/RenamePropertyRefactoringPlugin.java Tue Apr 23 01:17:09 2013 +0200 +++ b/refactoring.java/src/org/netbeans/modules/refactoring/java/plugins/RenamePropertyRefactoringPlugin.java Tue Apr 23 01:23:45 2013 +0200 @@ -44,8 +44,13 @@ import java.io.IOException; import javax.lang.model.element.Element; import javax.lang.model.element.ExecutableElement; +import javax.lang.model.element.Modifier; +import javax.lang.model.element.Name; import javax.lang.model.element.VariableElement; +import javax.lang.model.type.TypeKind; import javax.lang.model.util.ElementFilter; +import org.netbeans.api.java.source.CodeStyle; +import org.netbeans.api.java.source.CodeStyleUtils; import org.netbeans.api.java.source.CompilationController; import org.netbeans.api.java.source.JavaSource; import org.netbeans.api.java.source.Task; @@ -68,6 +73,9 @@ private RenameRefactoring refactoring; private TreePathHandle property; + private CodeStyle codeStyle; + private boolean isStatic; + private boolean isBoolean; private RenameRefactoring getterDelegate; private RenameRefactoring setterDelegate; private RenameRefactoring parameterDelegate; @@ -122,21 +130,30 @@ Problem p = null; if (getterDelegate != null) { - getterDelegate.setNewName(RefactoringUtils.getGetterName(refactoring.getNewName())); + String gettername = CodeStyleUtils.computeGetterName( + refactoring.getNewName(), isBoolean, isStatic, codeStyle); + getterDelegate.setNewName(gettername); p = JavaPluginUtils.chainProblems(p, getterDelegate.fastCheckParameters()); if (p != null && p.isFatal()) { return p; } } if (setterDelegate != null) { - setterDelegate.setNewName(RefactoringUtils.getSetterName(refactoring.getNewName())); + String settername = CodeStyleUtils.computeSetterName( + refactoring.getNewName(), isStatic, codeStyle); + setterDelegate.setNewName(settername); p = JavaPluginUtils.chainProblems(p, setterDelegate.fastCheckParameters()); if (p != null && p.isFatal()) { return p; } } if (parameterDelegate != null) { - parameterDelegate.setNewName(refactoring.getNewName()); + String newParam = RefactoringUtils.addParamPrefixSuffix( + CodeStyleUtils.removePrefixSuffix( + refactoring.getNewName(), + isStatic ? codeStyle.getStaticFieldNamePrefix() : codeStyle.getFieldNamePrefix(), + isStatic ? codeStyle.getStaticFieldNameSuffix() : codeStyle.getFieldNameSuffix()), codeStyle); + parameterDelegate.setNewName(newParam); p = JavaPluginUtils.chainProblems(p, parameterDelegate.fastCheckParameters()); if (p != null && p.isFatal()) { return p; @@ -224,22 +241,37 @@ } try { getJavaSource(Phase.PREPARE).runUserActionTask(new Task() { + private String propName; @Override public void run(CompilationController p) throws Exception { p.toPhase(JavaSource.Phase.RESOLVED); + codeStyle = RefactoringUtils.getCodeStyle(p); Element propertyElement = property.resolveElement(p); + isStatic = propertyElement.getModifiers().contains(Modifier.STATIC); + isBoolean = propertyElement.asType().getKind() == TypeKind.BOOLEAN; + propName = RefactoringUtils.removeFieldPrefixSuffix(propertyElement, codeStyle); + String paramName = RefactoringUtils.addParamPrefixSuffix(propName, codeStyle); + String newParam = RefactoringUtils.addParamPrefixSuffix( + CodeStyleUtils.removePrefixSuffix( + refactoring.getNewName(), + isStatic ? codeStyle.getStaticFieldNamePrefix() : codeStyle.getFieldNamePrefix(), + isStatic ? codeStyle.getStaticFieldNameSuffix() : codeStyle.getFieldNameSuffix()), codeStyle); for (ExecutableElement el : ElementFilter.methodsIn(propertyElement.getEnclosingElement().getEnclosedElements())) { if (RefactoringUtils.isGetter(p, el, propertyElement)) { getterDelegate = new RenameRefactoring(Lookups.singleton(TreePathHandle.create(el, p))); - getterDelegate.setNewName(RefactoringUtils.getGetterName(refactoring.getNewName())); + String gettername = CodeStyleUtils.computeGetterName( + refactoring.getNewName(), isBoolean, isStatic, codeStyle); + getterDelegate.setNewName(gettername); } else if (RefactoringUtils.isSetter(p, el, propertyElement)) { setterDelegate = new RenameRefactoring(Lookups.singleton(TreePathHandle.create(el, p))); - setterDelegate.setNewName(RefactoringUtils.getSetterName(refactoring.getNewName())); + String settername = CodeStyleUtils.computeSetterName( + refactoring.getNewName(), isStatic, codeStyle); + setterDelegate.setNewName(settername); VariableElement par = el.getParameters().iterator().next(); - if (par.getSimpleName().contentEquals(propertyElement.getSimpleName())) { + if (par.getSimpleName().contentEquals(paramName)) { parameterDelegate = new RenameRefactoring(Lookups.singleton(TreePathHandle.create(p.getTrees().getPath(par), p))); - parameterDelegate.setNewName(refactoring.getNewName()); + parameterDelegate.setNewName(newParam); } } } diff -r bdb0f6cd71ec -r 2b309a744129 refactoring.java/src/org/netbeans/modules/refactoring/java/ui/ChangeParametersPanel.java --- a/refactoring.java/src/org/netbeans/modules/refactoring/java/ui/ChangeParametersPanel.java Tue Apr 23 01:17:09 2013 +0200 +++ b/refactoring.java/src/org/netbeans/modules/refactoring/java/ui/ChangeParametersPanel.java Tue Apr 23 01:23:45 2013 +0200 @@ -118,7 +118,7 @@ private boolean methodNameChanged; private boolean returnTypeChanged; private boolean isConstructor; - + private final String paramname; @Override public Component getComponent() { @@ -142,7 +142,7 @@ private static final String ACTION_INLINE_EDITOR = "invokeInlineEditor"; //NOI18N /** Creates new form ChangeMethodSignature */ - public ChangeParametersPanel(TreePathHandle refactoredObj, ChangeListener parent, ParameterInfo[] preConfiguration) { + public ChangeParametersPanel(TreePathHandle refactoredObj, ChangeListener parent, ParameterInfo[] preConfiguration, CodeStyle cs) { returnTypeDocListener = new ReturnTypeDocListener(); methodNameDocListener = new MethodNameDocListener(); this.refactoredObj = refactoredObj; @@ -151,7 +151,8 @@ model = new ParamTableModel(columnNames, 0); this.returnTypeAction = new ReturnTypeAction(); singleLineEditor = Utilities.createSingleLineEditor(MIME_JAVA); - + paramname = CodeStyleUtils.addPrefixSuffix("par", cs.getParameterNamePrefix(), cs.getParameterNameSuffix()); + initComponents(); InputMap im = paramTable.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); @@ -616,7 +617,7 @@ private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed acceptEditedValue(); int rowCount = model.getRowCount(); - model.addRow(new Object[] { "Object", "par" + rowCount, "null", new Integer(-1), Boolean.TRUE }); // NOI18N + model.addRow(new Object[] { "Object", paramname + rowCount, "null", new Integer(-1), Boolean.TRUE }); // NOI18N paramTable.scrollRectToVisible(paramTable.getCellRect(rowCount, 0, false)); paramTable.changeSelection(rowCount, 0, false, false); autoEdit(paramTable); diff -r bdb0f6cd71ec -r 2b309a744129 refactoring.java/src/org/netbeans/modules/refactoring/java/ui/ChangeParametersUI.java --- a/refactoring.java/src/org/netbeans/modules/refactoring/java/ui/ChangeParametersUI.java Tue Apr 23 01:17:09 2013 +0200 +++ b/refactoring.java/src/org/netbeans/modules/refactoring/java/ui/ChangeParametersUI.java Tue Apr 23 01:23:45 2013 +0200 @@ -57,6 +57,7 @@ import javax.lang.model.type.TypeKind; import javax.swing.event.ChangeListener; import org.netbeans.api.fileinfo.NonRecursiveFolder; +import org.netbeans.api.java.source.CodeStyle; import org.netbeans.api.java.source.CompilationInfo; import org.netbeans.api.java.source.TreePathHandle; import org.netbeans.modules.refactoring.api.AbstractRefactoring; @@ -83,15 +84,17 @@ private boolean isMethod; private ChangeParametersRefactoring.ParameterInfo[] preConfiguration; private Lookup lookup; + private CodeStyle cs; /** Creates a new instance of ChangeMethodSignatureRefactoring */ - private ChangeParametersUI(TreePathHandle refactoredObj, CompilationInfo info, ChangeParametersRefactoring.ParameterInfo[] preConfiguration) { + private ChangeParametersUI(TreePathHandle refactoredObj, CompilationInfo info, ParameterInfo[] preConfiguration, CodeStyle cs) { this.refactoring = new ChangeParametersRefactoring(refactoredObj); this.method = refactoredObj; this.preConfiguration = preConfiguration; Element element = method.resolveElement(info); this.name = element.getSimpleName().toString(); this.isMethod = element.getKind() == ElementKind.METHOD; + this.cs = cs; } private ChangeParametersUI(Lookup lookup) { @@ -126,7 +129,7 @@ } return path != null - ? new ChangeParametersUI(TreePathHandle.create(path, info), info, configuration) + ? new ChangeParametersUI(TreePathHandle.create(path, info), info, configuration, CodeStyle.getDefault(info.getFileObject())) : null; } @@ -148,7 +151,7 @@ @Override public CustomRefactoringPanel getPanel(ChangeListener parent) { if (panel == null) { - panel = new ChangeParametersPanel(method, parent, preConfiguration); + panel = new ChangeParametersPanel(method, parent, preConfiguration, cs); } return panel; } diff -r bdb0f6cd71ec -r 2b309a744129 refactoring.java/src/org/netbeans/modules/refactoring/java/ui/EncapsulateFieldPanel.java --- a/refactoring.java/src/org/netbeans/modules/refactoring/java/ui/EncapsulateFieldPanel.java Tue Apr 23 01:17:09 2013 +0200 +++ b/refactoring.java/src/org/netbeans/modules/refactoring/java/ui/EncapsulateFieldPanel.java Tue Apr 23 01:23:45 2013 +0200 @@ -52,6 +52,7 @@ import java.io.IOException; import java.util.*; import javax.lang.model.element.*; +import javax.lang.model.type.TypeKind; import javax.swing.*; import javax.swing.event.ChangeListener; import javax.swing.event.TableModelEvent; @@ -62,6 +63,7 @@ import org.netbeans.api.java.source.*; import org.netbeans.api.java.source.ui.ElementHeaders; import org.netbeans.modules.refactoring.java.RefactoringModule; +import org.netbeans.modules.refactoring.java.RefactoringUtils; import org.netbeans.modules.refactoring.java.api.MemberInfo; import org.netbeans.modules.refactoring.java.plugins.EncapsulateFieldRefactoringPlugin; import org.netbeans.modules.refactoring.java.ui.EncapsulateFieldsRefactoring.EncapsulateFieldInfo; @@ -188,8 +190,10 @@ TreePath fieldTPath = javac.getTrees().getPath(field); boolean createGetter = fields !=null? fields.contains(TreePathHandle.create(field, javac)) : selectedElm == field ; boolean createSetter = createGetter && !field.getModifiers().contains(Modifier.FINAL); - String getName = EncapsulateFieldRefactoringPlugin.computeGetterName(field); - String setName = EncapsulateFieldRefactoringPlugin.computeSetterName(field); + CodeStyle cs = RefactoringUtils.getCodeStyle(javac); + boolean staticMod = field.getModifiers().contains(Modifier.STATIC); + String getName = CodeStyleUtils.computeGetterName(field.getSimpleName(), field.asType().getKind() == TypeKind.BOOLEAN, staticMod, cs); + String setName = CodeStyleUtils.computeSetterName(field.getSimpleName(), staticMod, cs); model.addRow(new Object[] { MemberInfo.create(fieldTPath, javac), createGetter ? Boolean.TRUE : Boolean.FALSE, diff -r bdb0f6cd71ec -r 2b309a744129 refactoring.java/src/org/netbeans/modules/refactoring/java/ui/IntroduceParameterPanel.java --- a/refactoring.java/src/org/netbeans/modules/refactoring/java/ui/IntroduceParameterPanel.java Tue Apr 23 01:17:09 2013 +0200 +++ b/refactoring.java/src/org/netbeans/modules/refactoring/java/ui/IntroduceParameterPanel.java Tue Apr 23 01:23:45 2013 +0200 @@ -59,6 +59,7 @@ import javax.swing.text.Position; import org.netbeans.api.editor.DialogBinding; import org.netbeans.api.java.source.CancellableTask; +import org.netbeans.api.java.source.CodeStyle; import org.netbeans.api.java.source.CompilationController; import org.netbeans.api.java.source.JavaSource; import org.netbeans.api.java.source.TreePathHandle; @@ -170,7 +171,14 @@ TreePath bodyPath = new TreePath(methodPath, methodTree.getBody()); scope = info.getTrees().getScope(bodyPath); - final String parameterName = JavaPluginUtils.makeNameUnique(info, scope, name); + CodeStyle cs; + Document doc = info.getDocument(); + if(doc != null) { + cs = CodeStyle.getDefault(doc); + } else { + cs = CodeStyle.getDefault(info.getFileObject()); + } + final String parameterName = JavaPluginUtils.makeNameUnique(info, scope, name, cs.getParameterNamePrefix(), cs.getParameterNameSuffix()); SwingUtilities.invokeLater(new Runnable() { @Override diff -r bdb0f6cd71ec -r 2b309a744129 refactoring.java/test/unit/src/org/netbeans/modules/refactoring/java/test/EncapsulateFieldsTest.java --- a/refactoring.java/test/unit/src/org/netbeans/modules/refactoring/java/test/EncapsulateFieldsTest.java Tue Apr 23 01:17:09 2013 +0200 +++ b/refactoring.java/test/unit/src/org/netbeans/modules/refactoring/java/test/EncapsulateFieldsTest.java Tue Apr 23 01:23:45 2013 +0200 @@ -49,14 +49,17 @@ import javax.lang.model.element.Modifier; import javax.lang.model.element.TypeElement; import javax.lang.model.element.VariableElement; +import javax.lang.model.type.TypeKind; import javax.lang.model.util.ElementFilter; +import org.netbeans.api.java.source.CodeStyle; +import org.netbeans.api.java.source.CodeStyleUtils; import org.netbeans.api.java.source.CompilationController; import org.netbeans.api.java.source.JavaSource; import org.netbeans.api.java.source.Task; import org.netbeans.api.java.source.TreePathHandle; +import org.netbeans.modules.java.source.save.DiffContext; import org.netbeans.modules.refactoring.api.Problem; import org.netbeans.modules.refactoring.api.RefactoringSession; -import org.netbeans.modules.refactoring.java.plugins.EncapsulateFieldRefactoringPlugin; import org.netbeans.modules.refactoring.java.ui.EncapsulateFieldsRefactoring; import org.openide.filesystems.FileObject; @@ -511,7 +514,7 @@ public void run(CompilationController info) throws Exception { info.toPhase(JavaSource.Phase.RESOLVED); CompilationUnitTree cut = info.getCompilationUnit(); - + CodeStyle cs = DiffContext.getCodeStyle(info); final ClassTree classTree = (ClassTree) cut.getTypeDecls().get(0); final TreePath classPath = info.getTrees().getPath(cut, classTree); TypeElement classEl = (TypeElement) info.getTrees().getElement(classPath); @@ -520,8 +523,9 @@ LinkedList fields = new LinkedList(); for (int p : position) { VariableElement field = fieldsIn.get(p); - String getName = EncapsulateFieldRefactoringPlugin.computeGetterName(field); - String setName = EncapsulateFieldRefactoringPlugin.computeSetterName(field); + boolean staticMod = field.getModifiers().contains(Modifier.STATIC); + String getName = CodeStyleUtils.computeGetterName(field.getSimpleName(), field.asType().getKind() == TypeKind.BOOLEAN, staticMod, cs); + String setName = CodeStyleUtils.computeSetterName(field.getSimpleName(), staticMod, cs); EncapsulateFieldsRefactoring.EncapsulateFieldInfo encInfo = new EncapsulateFieldsRefactoring.EncapsulateFieldInfo(TreePathHandle.create(field, info), getName, setName); fields.add(encInfo); } diff -r bdb0f6cd71ec -r 2b309a744129 refactoring.java/test/unit/src/org/netbeans/modules/refactoring/java/test/RefactoringTestBase.java --- a/refactoring.java/test/unit/src/org/netbeans/modules/refactoring/java/test/RefactoringTestBase.java Tue Apr 23 01:17:09 2013 +0200 +++ b/refactoring.java/test/unit/src/org/netbeans/modules/refactoring/java/test/RefactoringTestBase.java Tue Apr 23 01:23:45 2013 +0200 @@ -131,7 +131,7 @@ assertNotNull(f); assertNotNull(f.content); assertNotNull("Cannot find " + f.filename + " in map " + content, fileContent); - assertEquals(getName() ,f.content.replaceAll("[ \t\n]+", " "), fileContent.replaceAll("[ \t\n]+", " ")); + assertEquals(getName() ,f.content.replaceAll("[ \t\r\n\n]+", " "), fileContent.replaceAll("[ \t\r\n\n]+", " ")); } assertTrue(content.toString(), content.isEmpty());