This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 228870
Collapse All | Expand All

(-)a/java.source/apichanges.xml (-1 / +16 lines)
Lines 108-118 Link Here
108
    <!-- ACTUAL CHANGES BEGIN HERE: -->
108
    <!-- ACTUAL CHANGES BEGIN HERE: -->
109
109
110
    <changes>
110
    <changes>
111
        <change id="CodeStyle-Naming">
112
             <api name="general"/>
113
             <summary>Added <code>CodeStyleUtils</code>.</summary>
114
             <version major="0" minor="122"/>
115
             <date day="29" month="4" year="2013"/>
116
             <author login="ralphbenjamin"/>
117
             <compatibility addition="yes" binary="compatible" deletion="no" deprecation="no" modification="no" semantic="compatible" source="compatible"/>
118
             <description>
119
                 Added class <code>CodeStyleUtils</code>, which helps with
120
                 conforming java names to the naming codestyle. Used for
121
                 variable naming and getter/setter names.
122
             </description>
123
             <class package="org.netbeans.api.java.source" name="CodeStyleUtils"/>
124
             <issue number="228870"/>
125
        </change>
111
         <change id="getSymbols">
126
         <change id="getSymbols">
112
             <api name="general"/>
127
             <api name="general"/>
113
             <summary>Added several methods to support Java 8 features.</summary>
128
             <summary>Added several methods to support Java 8 features.</summary>
114
             <version major="0" minor="117"/>
129
             <version major="0" minor="117"/>
115
             <date day="28" month="2" year="2013"/>
130
             <date day="28" month="3" year="2013"/>
116
             <author login="jlahoda"/>
131
             <author login="jlahoda"/>
117
            <compatibility addition="yes" binary="compatible" deletion="no" deprecation="no" modification="no" semantic="compatible" source="compatible"/>
132
            <compatibility addition="yes" binary="compatible" deletion="no" deprecation="no" modification="no" semantic="compatible" source="compatible"/>
118
             <description>
133
             <description>
(-)a/java.source/nbproject/project.properties (-1 / +1 lines)
Lines 46-52 Link Here
46
javadoc.title=Java Source
46
javadoc.title=Java Source
47
javadoc.arch=${basedir}/arch.xml
47
javadoc.arch=${basedir}/arch.xml
48
javadoc.apichanges=${basedir}/apichanges.xml
48
javadoc.apichanges=${basedir}/apichanges.xml
49
spec.version.base=0.121.0
49
spec.version.base=0.122.0
50
test.qa-functional.cp.extra=${refactoring.java.dir}/modules/ext/nb-javac-api.jar
50
test.qa-functional.cp.extra=${refactoring.java.dir}/modules/ext/nb-javac-api.jar
51
test.unit.run.cp.extra=${o.n.core.dir}/core/core.jar:\
51
test.unit.run.cp.extra=${o.n.core.dir}/core/core.jar:\
52
    ${o.n.core.dir}/lib/boot.jar:\
52
    ${o.n.core.dir}/lib/boot.jar:\
(-)ee9bfd7b8f58 (+226 lines)
Added Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2013 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2013 Sun Microsystems, Inc.
41
 */
42
43
package org.netbeans.api.java.source;
44
45
import org.netbeans.api.annotations.common.NonNull;
46
import org.netbeans.api.annotations.common.NullAllowed;
47
import org.netbeans.lib.editor.util.CharSequenceUtilities;
48
49
/**
50
 *
51
 * @author Ralph Benjamin Ruijs <ralphbenjamin@netbeans.org>
52
 * @since 0.122
53
 */
54
public final class CodeStyleUtils {
55
56
    private CodeStyleUtils() {
57
    }
58
    
59
    /**
60
     * Add a prefix and suffix to a name. If the prefix or suffix are null, then
61
     * the name without prefixes or suffixes will be returned. When you add a
62
     * prefix value ending with an alphabetic character, the name will be
63
     * capitalized. For example, if the prefix for a name is defined as s, and
64
     * the name is "i", then the suggested name will be "sI".
65
     *
66
     *
67
     * @param name the name to add the Prefix and Suffix to.
68
     * @param prefix the prefix to add, or null if no prefix should be added.
69
     * @param suffix the suffix to add, or null if no suffix should be added.
70
     * @return the name with added prefix and suffix
71
     * @see #getCapitalizedName(java.lang.CharSequence)
72
     */
73
    @NonNull
74
    public static String addPrefixSuffix(@NullAllowed CharSequence name,
75
                                         @NullAllowed String prefix,
76
                                         @NullAllowed String suffix) {
77
        StringBuilder sb = new StringBuilder();
78
        boolean capitalize = false;
79
        if (prefix != null && prefix.length() > 0) {
80
            if (Character.isAlphabetic(prefix.charAt(prefix.length() - 1))) {
81
                capitalize = true;
82
            }
83
            sb.insert(0, prefix);
84
        }
85
        if (name != null) {
86
            sb.append(capitalize ? getCapitalizedName(name) : name);
87
        }
88
        if (suffix != null) {
89
            sb.append(suffix);
90
        }
91
        return sb.toString();
92
    }
93
94
    /**
95
     * Removes a prefix and suffix from a name. When you remove a prefix value
96
     * ending with an alphabetic character, the name will be decapitalized. If
97
     * the name originally started with _ , they will not be added.
98
     *
99
     * @param name the name to remove the Prefix and Suffix from.
100
     * @param prefix the prefix to remove, or null if no prefix should be
101
     * removed.
102
     * @param suffix the suffix to remove, or null if no suffix should be
103
     * removed.
104
     * @return the name without the prefix and suffix
105
     * @see #addPrefixSuffix(java.lang.CharSequence, java.lang.String,
106
     * java.lang.String)
107
     * @see #getDecapitalizedName(java.lang.CharSequence) 
108
     */
109
    @NonNull
110
    public static String removePrefixSuffix(@NonNull CharSequence name,
111
                                            @NullAllowed String prefix,
112
                                            @NullAllowed String suffix) {
113
        StringBuilder sb = new StringBuilder(name);
114
        int start = 0;
115
        int end = name.length();
116
        boolean decapitalize = false;
117
        if (prefix != null && CharSequenceUtilities.startsWith(name, prefix)) {
118
            start = prefix.length();
119
            if (Character.isAlphabetic(prefix.charAt(prefix.length() - 1))) {
120
                decapitalize = true;
121
            }
122
        }
123
        if (suffix != null && CharSequenceUtilities.endsWith(name, suffix)) {
124
            end = end - suffix.length();
125
        }
126
        String result = sb.substring(start, end);
127
        if(decapitalize) {
128
            return getDecapitalizedName(result);
129
        } else {
130
            return result;
131
        }
132
    }
133
134
    /**
135
     * Capitalize a name following the javabeans specification. This normally
136
     * means converting the first character from lower case to upper case, but
137
     * in the (unusual) special case when there is more than one character and
138
     * the second character is upper case, we leave it alone. Thus "fooBah"
139
     * becomes "FooBah" and "x" becomes "X", but "eMail" stays as "eMail".
140
     * <p>To stay backwards compatible, if the name starts with _ they will be
141
     * removed.
142
     *
143
     * @param name the name to capitalize
144
     * @return the capizalized name
145
     */
146
    @NonNull
147
    public static String getCapitalizedName(CharSequence name) {
148
        StringBuilder sb = new StringBuilder(name);
149
        while (sb.length() > 1 && sb.charAt(0) == '_') { //NOI18N
150
            sb.deleteCharAt(0);
151
        }
152
153
        //Beans naming convention, #165241
154
        if (sb.length() > 1 && Character.isUpperCase(sb.charAt(1))) {
155
            return sb.toString();
156
        }
157
158
        if (sb.length() > 0) {
159
            sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
160
        }
161
        return sb.toString();
162
    }
163
    
164
    /**
165
     * Decapitalize a name to its normal java form. This normally
166
     * means converting the first character from upper case to lower case, but
167
     * in the (unusual) special case when there is more than one character and
168
     * the second character is upper case, we leave it alone. Thus "FooBah"
169
     * becomes "fooBah" and "X" becomes "x", but "URL" stays as "URL".
170
     *
171
     * @param name the name to decapitalize
172
     * @return the decapizalized name
173
     */
174
    @NonNull
175
    public static String getDecapitalizedName(@NonNull CharSequence name) {
176
        //Beans naming convention, #165241
177
        if (name.length() > 1 && (Character.isUpperCase(name.charAt(1)) ||
178
                                  Character.isLowerCase(name.charAt(0)))) {
179
            return name.toString();
180
        }
181
        
182
        StringBuilder sb = new StringBuilder(name);
183
        if (sb.length() > 0) {
184
            sb.setCharAt(0, Character.toLowerCase(sb.charAt(0)));
185
        }
186
        return sb.toString();
187
    }
188
189
    /**
190
     * Computes the method name that should be used to read the property value.
191
     * 
192
     * @param fieldName the name of the property
193
     * @param isBoolean true if the property is of boolean type
194
     * @param isStatic true if the property is static
195
     * @param cs the CodeSyle to use
196
     * @return the getter name
197
     */
198
    @NonNull
199
    public static String computeGetterName(CharSequence fieldName, boolean isBoolean, boolean isStatic, CodeStyle cs) {
200
        StringBuilder sb = new StringBuilder(getCapitalizedName(removeFieldPrefixSuffix(fieldName, isStatic, cs)));
201
        sb.insert(0, isBoolean && cs.useIsForBooleanGetters() ? "is" : "get"); //NOI18N
202
        String getterName = sb.toString();
203
        return getterName;
204
    }
205
206
    /**
207
     * Computes the method name that should be used to write the property value.
208
     * 
209
     * @param fieldName the name of the property
210
     * @param isStatic true if the property is static
211
     * @param cs the CodeSyle to use
212
     * @return the setter name
213
     */
214
    @NonNull
215
    public static String computeSetterName(CharSequence fieldName, boolean isStatic, CodeStyle cs) {
216
        StringBuilder name = new StringBuilder(getCapitalizedName(removeFieldPrefixSuffix(fieldName, isStatic, cs)));
217
        name.insert(0, "set"); //NOI18N
218
        return name.toString();
219
    }
220
    
221
    private static String removeFieldPrefixSuffix(CharSequence fieldName, boolean isStatic, CodeStyle cs) {
222
        return removePrefixSuffix(fieldName,
223
                                  isStatic ? cs.getStaticFieldNamePrefix() : cs.getFieldNamePrefix(),
224
                                  isStatic ? cs.getStaticFieldNameSuffix() : cs.getFieldNameSuffix());
225
    }
226
}
(-)ee9bfd7b8f58 (+168 lines)
Added Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2013 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2013 Sun Microsystems, Inc.
41
 */
42
43
package org.netbeans.api.java.source;
44
45
import org.junit.Test;
46
import static org.junit.Assert.*;
47
48
/**
49
 *
50
 * @author Ralph Benjamin Ruijs <ralphbenjamin@netbeans.org>
51
 */
52
public class CodeStyleUtilsTest {
53
54
    @Test
55
    public void testAddPrefixSuffix() {
56
        CharSequence name = null;
57
        String prefix = null;
58
        String suffix = null;
59
        String result = CodeStyleUtils.addPrefixSuffix(name, prefix, suffix);
60
        assertEquals("", result);
61
62
        name = "name";
63
        result = CodeStyleUtils.addPrefixSuffix(name, prefix, suffix);
64
        assertEquals(name, result);
65
        
66
        suffix = "S";
67
        result = CodeStyleUtils.addPrefixSuffix(name, prefix, suffix);
68
        assertEquals("nameS", result);
69
        
70
        prefix = "$";
71
        result = CodeStyleUtils.addPrefixSuffix(name, prefix, suffix);
72
        assertEquals("$nameS", result);
73
        
74
        prefix = "s";
75
        result = CodeStyleUtils.addPrefixSuffix(name, prefix, suffix);
76
        assertEquals("sNameS", result);
77
        
78
        name = "__name";
79
        result = CodeStyleUtils.addPrefixSuffix(name, prefix, suffix);
80
        assertEquals("sNameS", result);
81
        
82
        name = null;
83
        result = CodeStyleUtils.addPrefixSuffix(name, prefix, suffix);
84
        assertEquals("sS", result);
85
    }
86
87
    @Test
88
    public void testRemovePrefixSuffix() {
89
        String prefix = null;
90
        String suffix = null;
91
92
        CharSequence name = "name";
93
        String result = CodeStyleUtils.removePrefixSuffix(name, prefix, suffix);
94
        assertEquals(name, result);
95
        
96
        suffix = "S";
97
        name = "nameS";
98
        result = CodeStyleUtils.removePrefixSuffix(name, prefix, suffix);
99
        assertEquals("name", result);
100
        
101
        prefix = "$";
102
        name = "$nameS";
103
        result = CodeStyleUtils.removePrefixSuffix(name, prefix, suffix);
104
        assertEquals("name", result);
105
        
106
        prefix = "s";
107
        name = "sNameS";
108
        result = CodeStyleUtils.removePrefixSuffix(name, prefix, suffix);
109
        assertEquals("name", result);
110
        
111
        prefix = "_";
112
        name = "__nameS";
113
        result = CodeStyleUtils.removePrefixSuffix(name, prefix, suffix);
114
        assertEquals("_name", result);
115
        
116
        prefix = "S";
117
        suffix = "s";
118
        name = "sNameS";
119
        result = CodeStyleUtils.removePrefixSuffix(name, prefix, suffix);
120
        assertEquals("sNameS", result);
121
    }
122
123
    @Test
124
    public void testGetCapitalizedName() {
125
        CharSequence name = "name";
126
        String result = CodeStyleUtils.getCapitalizedName(name);
127
        assertEquals("Name", result);
128
        
129
        name = "Name";
130
        result = CodeStyleUtils.getCapitalizedName(name);
131
        assertEquals("Name", result);
132
        
133
        name = "NAME";
134
        result = CodeStyleUtils.getCapitalizedName(name);
135
        assertEquals("NAME", result);
136
        
137
        name = "nAme";
138
        result = CodeStyleUtils.getCapitalizedName(name);
139
        assertEquals("nAme", result);
140
        
141
        name = "naMe";
142
        result = CodeStyleUtils.getCapitalizedName(name);
143
        assertEquals("NaMe", result);
144
    }
145
146
    @Test
147
    public void testGetDecapitalizedName() {
148
        CharSequence name = "name";
149
        String result = CodeStyleUtils.getDecapitalizedName(name);
150
        assertEquals("name", result);
151
        
152
        name = "Name";
153
        result = CodeStyleUtils.getDecapitalizedName(name);
154
        assertEquals("name", result);
155
        
156
        name = "NAME";
157
        result = CodeStyleUtils.getDecapitalizedName(name);
158
        assertEquals("NAME", result);
159
        
160
        name = "nAme";
161
        result = CodeStyleUtils.getDecapitalizedName(name);
162
        assertEquals("nAme", result);
163
        
164
        name = "NaMe";
165
        result = CodeStyleUtils.getDecapitalizedName(name);
166
        assertEquals("naMe", result);
167
    }
168
}

Return to bug 228870