KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > core > NamingConventions


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.core;
12
13 import org.eclipse.jdt.core.compiler.CharOperation;
14 import org.eclipse.jdt.internal.codeassist.impl.AssistOptions;
15 import org.eclipse.jdt.internal.compiler.parser.ScannerHelper;
16 import org.eclipse.jdt.internal.core.INamingRequestor;
17 import org.eclipse.jdt.internal.core.InternalNamingConventions;
18
19
20 /**
21  * Provides methods for computing Java-specific names.
22  * <p>
23  * The behavior of the methods is dependent of several JavaCore options.
24  * <p>
25  * The possible options are :
26  * <ul>
27  * <li> {@link JavaCore#CODEASSIST_FIELD_PREFIXES} : Define the Prefixes for Field Name.</li>
28  * <li> {@link JavaCore#CODEASSIST_STATIC_FIELD_PREFIXES} : Define the Prefixes for Static Field Name.</li>
29  * <li> {@link JavaCore#CODEASSIST_LOCAL_PREFIXES} : Define the Prefixes for Local Variable Name.</li>
30  * <li> {@link JavaCore#CODEASSIST_ARGUMENT_PREFIXES} : Define the Prefixes for Argument Name.</li>
31  * <li> {@link JavaCore#CODEASSIST_FIELD_SUFFIXES} : Define the Suffixes for Field Name.</li>
32  * <li> {@link JavaCore#CODEASSIST_STATIC_FIELD_SUFFIXES} : Define the Suffixes for Static Field Name.</li>
33  * <li> {@link JavaCore#CODEASSIST_LOCAL_SUFFIXES} : Define the Suffixes for Local Variable Name.</li>
34  * <li> {@link JavaCore#CODEASSIST_ARGUMENT_SUFFIXES} : Define the Suffixes for Argument Name.</li>
35  * </ul>
36  * </p>
37  * <p>
38  * For a complete description of the configurable options, see <code>getDefaultOptions</code>.
39  * For programmaticaly change these options, see <code>JavaCore#setOptions()</code>.
40  * </p>
41  * <p>
42  * This class provides static methods and constants only; it is not intended to be
43  * instantiated or subclassed by clients.
44  * </p>
45  *
46  * @see JavaCore#setOptions(java.util.Hashtable)
47  * @see JavaCore#getDefaultOptions()
48  * @since 2.1
49  */

50 public final class NamingConventions {
51     private static final char[] GETTER_BOOL_NAME = "is".toCharArray(); //$NON-NLS-1$
52
private static final char[] GETTER_NAME = "get".toCharArray(); //$NON-NLS-1$
53
private static final char[] SETTER_NAME = "set".toCharArray(); //$NON-NLS-1$
54

55     static class NamingRequestor implements INamingRequestor {
56         private final static int SIZE = 10;
57         
58         // for acceptNameWithPrefixAndSuffix
59
private char[][] firstPrefixAndFirstSuffixResults = new char[SIZE][];
60         private int firstPrefixAndFirstSuffixResultsCount = 0;
61         private char[][] firstPrefixAndSuffixResults = new char[SIZE][];
62         private int firstPrefixAndSuffixResultsCount = 0;
63         private char[][] prefixAndFirstSuffixResults = new char[SIZE][];
64         private int prefixAndFirstSuffixResultsCount = 0;
65         private char[][] prefixAndSuffixResults = new char[SIZE][];
66         private int prefixAndSuffixResultsCount = 0;
67         
68         // for acceptNameWithPrefix
69
private char[][] firstPrefixResults = new char[SIZE][];
70         private int firstPrefixResultsCount = 0;
71         private char[][] prefixResults = new char[SIZE][];
72         private int prefixResultsCount = 0;
73         
74         // for acceptNameWithSuffix
75
private char[][] firstSuffixResults = new char[SIZE][];
76         private int firstSuffixResultsCount = 0;
77         private char[][] suffixResults = new char[SIZE][];
78         private int suffixResultsCount = 0;
79         
80         // for acceptNameWithoutPrefixAndSuffix
81
private char[][] otherResults = new char[SIZE][];
82         private int otherResultsCount = 0;
83         public void acceptNameWithPrefixAndSuffix(char[] name, boolean isFirstPrefix, boolean isFirstSuffix, int reusedCharacters) {
84             if(isFirstPrefix && isFirstSuffix) {
85                 int length = this.firstPrefixAndFirstSuffixResults.length;
86                 if(length == this.firstPrefixAndFirstSuffixResultsCount) {
87                     System.arraycopy(
88                         this.firstPrefixAndFirstSuffixResults,
89                         0,
90                         this.firstPrefixAndFirstSuffixResults = new char[length * 2][],
91                         0,
92                         length);
93                 }
94                 this.firstPrefixAndFirstSuffixResults[this.firstPrefixAndFirstSuffixResultsCount++] = name;
95             } else if (isFirstPrefix) {
96                 int length = this.firstPrefixAndSuffixResults.length;
97                 if(length == this.firstPrefixAndSuffixResultsCount) {
98                     System.arraycopy(
99                         this.firstPrefixAndSuffixResults,
100                         0,
101                         this.firstPrefixAndSuffixResults = new char[length * 2][],
102                         0,
103                         length);
104                 }
105                 this.firstPrefixAndSuffixResults[this.firstPrefixAndSuffixResultsCount++] = name;
106             } else if(isFirstSuffix) {
107                 int length = this.prefixAndFirstSuffixResults.length;
108                 if(length == this.prefixAndFirstSuffixResultsCount) {
109                     System.arraycopy(
110                         this.prefixAndFirstSuffixResults,
111                         0,
112                         this.prefixAndFirstSuffixResults = new char[length * 2][],
113                         0,
114                         length);
115                 }
116                 this.prefixAndFirstSuffixResults[this.prefixAndFirstSuffixResultsCount++] = name;
117             } else {
118                 int length = this.prefixAndSuffixResults.length;
119                 if(length == this.prefixAndSuffixResultsCount) {
120                     System.arraycopy(
121                         this.prefixAndSuffixResults,
122                         0,
123                         this.prefixAndSuffixResults = new char[length * 2][],
124                         0,
125                         length);
126                 }
127                 this.prefixAndSuffixResults[this.prefixAndSuffixResultsCount++] = name;
128             }
129         }
130
131         public void acceptNameWithPrefix(char[] name, boolean isFirstPrefix, int reusedCharacters) {
132             if(isFirstPrefix) {
133                 int length = this.firstPrefixResults.length;
134                 if(length == this.firstPrefixResultsCount) {
135                     System.arraycopy(
136                         this.firstPrefixResults,
137                         0,
138                         this.firstPrefixResults = new char[length * 2][],
139                         0,
140                         length);
141                 }
142                 this.firstPrefixResults[this.firstPrefixResultsCount++] = name;
143             } else{
144                 int length = this.prefixResults.length;
145                 if(length == this.prefixResultsCount) {
146                     System.arraycopy(
147                         this.prefixResults,
148                         0,
149                         this.prefixResults = new char[length * 2][],
150                         0,
151                         length);
152                 }
153                 this.prefixResults[this.prefixResultsCount++] = name;
154             }
155         }
156
157         public void acceptNameWithSuffix(char[] name, boolean isFirstSuffix, int reusedCharacters) {
158             if(isFirstSuffix) {
159                 int length = this.firstSuffixResults.length;
160                 if(length == this.firstSuffixResultsCount) {
161                     System.arraycopy(
162                         this.firstSuffixResults,
163                         0,
164                         this.firstSuffixResults = new char[length * 2][],
165                         0,
166                         length);
167                 }
168                 this.firstSuffixResults[this.firstSuffixResultsCount++] = name;
169             } else {
170                 int length = this.suffixResults.length;
171                 if(length == this.suffixResultsCount) {
172                     System.arraycopy(
173                         this.suffixResults,
174                         0,
175                         this.suffixResults = new char[length * 2][],
176                         0,
177                         length);
178                 }
179                 this.suffixResults[this.suffixResultsCount++] = name;
180             }
181         }
182
183         public void acceptNameWithoutPrefixAndSuffix(char[] name, int reusedCharacters) {
184             int length = this.otherResults.length;
185             if(length == this.otherResultsCount) {
186                 System.arraycopy(
187                     this.otherResults,
188                     0,
189                     this.otherResults = new char[length * 2][],
190                     0,
191                     length);
192             }
193             this.otherResults[this.otherResultsCount++] = name;
194         }
195         public char[][] getResults(){
196             int count =
197                 this.firstPrefixAndFirstSuffixResultsCount
198                 + this.firstPrefixAndSuffixResultsCount
199                 + this.prefixAndFirstSuffixResultsCount
200                 + this.prefixAndSuffixResultsCount
201                 + this.firstPrefixResultsCount
202                 + this.prefixResultsCount
203                 + this.firstSuffixResultsCount
204                 + this.suffixResultsCount
205                 + this.otherResultsCount;
206                 
207             char[][] results = new char[count][];
208             
209             int index = 0;
210             System.arraycopy(this.firstPrefixAndFirstSuffixResults, 0, results, index, this.firstPrefixAndFirstSuffixResultsCount);
211             index += this.firstPrefixAndFirstSuffixResultsCount;
212             System.arraycopy(this.firstPrefixAndSuffixResults, 0, results, index, this.firstPrefixAndSuffixResultsCount);
213             index += this.firstPrefixAndSuffixResultsCount;
214             System.arraycopy(this.prefixAndFirstSuffixResults, 0, results, index, this.prefixAndFirstSuffixResultsCount);
215             index += this.prefixAndFirstSuffixResultsCount;
216             System.arraycopy(this.prefixAndSuffixResults, 0, results, index, this.prefixAndSuffixResultsCount);
217             index += this.prefixAndSuffixResultsCount;
218             System.arraycopy(this.firstPrefixResults, 0, results, index, this.firstPrefixResultsCount);
219             index += this.firstPrefixResultsCount;
220             System.arraycopy(this.prefixResults, 0, results, index, this.prefixResultsCount);
221             index += this.prefixResultsCount;
222             System.arraycopy(this.firstSuffixResults, 0, results, index, this.firstSuffixResultsCount);
223             index += this.firstSuffixResultsCount;
224             System.arraycopy(this.suffixResults, 0, results, index, this.suffixResultsCount);
225             index += this.suffixResultsCount;
226             System.arraycopy(this.otherResults, 0, results, index, this.otherResultsCount);
227             
228             return results;
229         }
230     }
231
232     
233     private NamingConventions() {
234         // Not instantiable
235
}
236
237     private static char[] removePrefixAndSuffix(char[] name, char[][] prefixes, char[][] suffixes) {
238         // remove longer prefix
239
char[] withoutPrefixName = name;
240         if (prefixes != null) {
241             int bestLength = 0;
242             for (int i= 0; i < prefixes.length; i++) {
243                 char[] prefix = prefixes[i];
244                 if (CharOperation.prefixEquals(prefix, name)) {
245                     int currLen = prefix.length;
246                     boolean lastCharIsLetter = ScannerHelper.isLetter(prefix[currLen - 1]);
247                     if(!lastCharIsLetter || (lastCharIsLetter && name.length > currLen && ScannerHelper.isUpperCase(name[currLen]))) {
248                         if (bestLength < currLen && name.length != currLen) {
249                             withoutPrefixName = CharOperation.subarray(name, currLen, name.length);
250                             bestLength = currLen;
251                         }
252                     }
253                 }
254             }
255         }
256         
257         // remove longer suffix
258
char[] withoutSuffixName = withoutPrefixName;
259         if(suffixes != null) {
260             int bestLength = 0;
261             for (int i = 0; i < suffixes.length; i++) {
262                 char[] suffix = suffixes[i];
263                 if(CharOperation.endsWith(withoutPrefixName, suffix)) {
264                     int currLen = suffix.length;
265                     if(bestLength < currLen && withoutPrefixName.length != currLen) {
266                         withoutSuffixName = CharOperation.subarray(withoutPrefixName, 0, withoutPrefixName.length - currLen);
267                         bestLength = currLen;
268                     }
269                 }
270             }
271         }
272         
273         withoutSuffixName[0] = ScannerHelper.toLowerCase(withoutSuffixName[0]);
274         return withoutSuffixName;
275     }
276
277     /**
278      * Remove prefix and suffix from an argument name.
279      * <p>
280      * If argument name prefix is <code>pre</code> and argument name suffix is <code>suf</code>
281      * then for an argument named <code>preArgsuf</code> the result of this method is <code>arg</code>.
282      * If there is no prefix or suffix defined in JavaCore options the result is the unchanged
283      * name <code>preArgsuf</code>.
284      * </p>
285      * <p>
286      * This method is affected by the following JavaCore options : {@link JavaCore#CODEASSIST_ARGUMENT_PREFIXES} and
287      * {@link JavaCore#CODEASSIST_ARGUMENT_SUFFIXES}.
288      * </p>
289      * <p>
290      * For a complete description of these configurable options, see <code>getDefaultOptions</code>.
291      * For programmaticaly change these options, see <code>JavaCore#setOptions()</code>.
292      * </p>
293      *
294      * @param javaProject project which contains the argument.
295      * @param argumentName argument's name.
296      * @return char[] the name without prefix and suffix.
297      * @see JavaCore#setOptions(java.util.Hashtable)
298      * @see JavaCore#getDefaultOptions()
299      */

300     public static char[] removePrefixAndSuffixForArgumentName(IJavaProject javaProject, char[] argumentName) {
301         AssistOptions assistOptions = new AssistOptions(javaProject.getOptions(true));
302         return removePrefixAndSuffix(
303             argumentName,
304             assistOptions.argumentPrefixes,
305             assistOptions.argumentSuffixes);
306     }
307     
308     /**
309      * Remove prefix and suffix from an argument name.
310      * <p>
311      * If argument name prefix is <code>pre</code> and argument name suffix is <code>suf</code>
312      * then for an argument named <code>preArgsuf</code> the result of this method is <code>arg</code>.
313      * If there is no prefix or suffix defined in JavaCore options the result is the unchanged
314      * name <code>preArgsuf</code>.
315      * </p>
316      * <p>
317      * This method is affected by the following JavaCore options : {@link JavaCore#CODEASSIST_ARGUMENT_PREFIXES} and
318      * {@link JavaCore#CODEASSIST_ARGUMENT_SUFFIXES}.
319      * </p>
320      * <p>
321      * For a complete description of these configurable options, see <code>getDefaultOptions</code>.
322      * For programmaticaly change these options, see <code>JavaCore#setOptions()</code>.
323      * </p>
324      *
325      * @param javaProject project which contains the argument.
326      * @param argumentName argument's name.
327      * @return char[] the name without prefix and suffix.
328      * @see JavaCore#setOptions(java.util.Hashtable)
329      * @see JavaCore#getDefaultOptions()
330      */

331     public static String JavaDoc removePrefixAndSuffixForArgumentName(IJavaProject javaProject, String JavaDoc argumentName) {
332         return String.valueOf(removePrefixAndSuffixForArgumentName(javaProject, argumentName.toCharArray()));
333     }
334
335     /**
336      * Remove prefix and suffix from a field name.
337      * <p>
338      * If field name prefix is <code>pre</code> and field name suffix is <code>suf</code>
339      * then for a field named <code>preFieldsuf</code> the result of this method is <code>field</code>.
340      * If there is no prefix or suffix defined in JavaCore options the result is the unchanged
341      * name <code>preFieldsuf</code>.
342      * </p>
343      * <p>
344      * This method is affected by the following JavaCore options : {@link JavaCore#CODEASSIST_FIELD_PREFIXES} } ,
345      * {@link JavaCore#CODEASSIST_FIELD_SUFFIXES} for instance field and {@link JavaCore#CODEASSIST_STATIC_FIELD_PREFIXES},
346      * {@link JavaCore#CODEASSIST_STATIC_FIELD_SUFFIXES} for static field.
347      * </p>
348      * <p>
349      * For a complete description of these configurable options, see <code>getDefaultOptions</code>.
350      * For programmaticaly change these options, see <code>JavaCore#setOptions()</code>.
351      * </p>
352      *
353      * @param javaProject project which contains the field.
354      * @param fieldName field's name.
355      * @param modifiers field's modifiers as defined by the class
356      * <code>Flags</code>.
357      * @return char[] the name without prefix and suffix.
358      * @see Flags
359      * @see JavaCore#setOptions(java.util.Hashtable)
360      * @see JavaCore#getDefaultOptions()
361      */

362     public static char[] removePrefixAndSuffixForFieldName(IJavaProject javaProject, char[] fieldName, int modifiers) {
363         boolean isStatic = Flags.isStatic(modifiers);
364         AssistOptions assistOptions = new AssistOptions(javaProject.getOptions(true));
365         return removePrefixAndSuffix(
366             fieldName,
367             isStatic ? assistOptions.staticFieldPrefixes : assistOptions.fieldPrefixes,
368             isStatic ? assistOptions.staticFieldSuffixes : assistOptions.fieldSuffixes);
369     }
370
371     /**
372      * Remove prefix and suffix from a field name.
373      * <p>
374      * If field name prefix is <code>pre</code> and field name suffix is <code>suf</code>
375      * then for a field named <code>preFieldsuf</code> the result of this method is <code>field</code>.
376      * If there is no prefix or suffix defined in JavaCore options the result is the unchanged
377      * name <code>preFieldsuf</code>.
378      * </p>
379      * <p>
380      * This method is affected by the following JavaCore options : {@link JavaCore#CODEASSIST_FIELD_PREFIXES},
381      * {@link JavaCore#CODEASSIST_FIELD_SUFFIXES} for instance field and {@link JavaCore#CODEASSIST_STATIC_FIELD_PREFIXES},
382      * {@link JavaCore#CODEASSIST_STATIC_FIELD_SUFFIXES} for static field.
383      * </p>
384      * <p>
385      * For a complete description of these configurable options, see <code>getDefaultOptions</code>.
386      * For programmaticaly change these options, see <code>JavaCore#setOptions()</code>.
387      * </p>
388      *
389      * @param javaProject project which contains the field.
390      * @param fieldName field's name.
391      * @param modifiers field's modifiers as defined by the class
392      * <code>Flags</code>.
393      * @return char[] the name without prefix and suffix.
394      * @see Flags
395      * @see JavaCore#setOptions(java.util.Hashtable)
396      * @see JavaCore#getDefaultOptions()
397      */

398     public static String JavaDoc removePrefixAndSuffixForFieldName(IJavaProject javaProject, String JavaDoc fieldName, int modifiers) {
399         return String.valueOf(removePrefixAndSuffixForFieldName(javaProject, fieldName.toCharArray(), modifiers));
400     }
401     /**
402      * Remove prefix and suffix from a local variable name.
403      * <p>
404      * If local variable name prefix is <code>pre</code> and local variable name suffix is <code>suf</code>
405      * then for a local variable named <code>preLocalsuf</code> the result of this method is <code>local</code>.
406      * If there is no prefix or suffix defined in JavaCore options the result is the unchanged
407      * name <code>preLocalsuf</code>.
408      * </p>
409      * <p>
410      * This method is affected by the following JavaCore options : {@link JavaCore#CODEASSIST_LOCAL_PREFIXES} and
411      * {@link JavaCore#CODEASSIST_LOCAL_SUFFIXES}.
412      * </p>
413      * <p>
414      * For a complete description of these configurable options, see <code>getDefaultOptions</code>.
415      * For programmaticaly change these options, see <code>JavaCore#setOptions()</code>.
416      * </p>
417      *
418      * @param javaProject project which contains the variable.
419      * @param localName variable's name.
420      * @return char[] the name without prefix and suffix.
421      * @see JavaCore#setOptions(java.util.Hashtable)
422      * @see JavaCore#getDefaultOptions()
423      */

424     public static char[] removePrefixAndSuffixForLocalVariableName(IJavaProject javaProject, char[] localName) {
425         AssistOptions assistOptions = new AssistOptions(javaProject.getOptions(true));
426         return removePrefixAndSuffix(
427             localName,
428             assistOptions.localPrefixes,
429             assistOptions.localSuffixes);
430     }
431     
432     /**
433      * Remove prefix and suffix from a local variable name.
434      * <p>
435      * If local variable name prefix is <code>pre</code> and local variable name suffix is <code>suf</code>
436      * then for a local variable named <code>preLocalsuf</code> the result of this method is <code>local</code>.
437      * If there is no prefix or suffix defined in JavaCore options the result is the unchanged
438      * name <code>preLocalsuf</code>.
439      * </p>
440      * <p>
441      * This method is affected by the following JavaCore options : {@link JavaCore#CODEASSIST_LOCAL_PREFIXES} and
442      * {@link JavaCore#CODEASSIST_LOCAL_SUFFIXES}.
443      * </p>
444      * <p>
445      * For a complete description of these configurable options, see <code>getDefaultOptions</code>.
446      * For programmaticaly change these options, see <code>JavaCore#setOptions()</code>.
447      * </p>
448      *
449      * @param javaProject project which contains the variable.
450      * @param localName variable's name.
451      * @return char[] the name without prefix and suffix.
452      * @see JavaCore#setOptions(java.util.Hashtable)
453      * @see JavaCore#getDefaultOptions()
454      */

455     public static String JavaDoc removePrefixAndSuffixForLocalVariableName(IJavaProject javaProject, String JavaDoc localName) {
456         return String.valueOf(removePrefixAndSuffixForLocalVariableName(javaProject, localName.toCharArray()));
457     }
458
459     /**
460      * Suggest names for an argument. The name is computed from argument's type
461      * and possible prefixes or suffixes are added.
462      * <p>
463      * If the type of the argument is <code>TypeName</code>, the prefix for argument is <code>pre</code>
464      * and the suffix for argument is <code>suf</code> then the proposed names are <code>preTypeNamesuf</code>
465      * and <code>preNamesuf</code>. If there is no prefix or suffix the proposals are <code>typeName</code>
466      * and <code>name</code>.
467      * </p>
468      * <p>
469      * This method is affected by the following JavaCore options : {@link JavaCore#CODEASSIST_ARGUMENT_PREFIXES} and
470      * {@link JavaCore#CODEASSIST_ARGUMENT_SUFFIXES}.
471      * </p>
472      * <p>
473      * For a complete description of these configurable options, see <code>getDefaultOptions</code>.
474      * For programmaticaly change these options, see <code>JavaCore#setOptions()</code>.
475      * </p>
476      *
477      * @param javaProject project which contains the argument.
478      * @param packageName package of the argument's type.
479      * @param qualifiedTypeName argument's type.
480      * @param dim argument's dimension (0 if the argument is not an array).
481      * @param excludedNames a list of names which cannot be suggested (already used names).
482      * Can be <code>null</code> if there is no excluded names.
483      * @return char[][] an array of names.
484      * @see JavaCore#setOptions(java.util.Hashtable)
485      * @see JavaCore#getDefaultOptions()
486      */

487     public static char[][] suggestArgumentNames(IJavaProject javaProject, char[] packageName, char[] qualifiedTypeName, int dim, char[][] excludedNames) {
488         NamingRequestor requestor = new NamingRequestor();
489         InternalNamingConventions.suggestArgumentNames(
490             javaProject,
491             packageName,
492             qualifiedTypeName,
493             dim,
494             null,
495             excludedNames,
496             requestor);
497
498         return requestor.getResults();
499     }
500     
501     /**
502      * Suggest names for an argument. The name is computed from argument's type
503      * and possible prefixes or suffixes are added.
504      * <p>
505      * If the type of the argument is <code>TypeName</code>, the prefix for argument is <code>pre</code>
506      * and the suffix for argument is <code>suf</code> then the proposed names are <code>preTypeNamesuf</code>
507      * and <code>preNamesuf</code>. If there is no prefix or suffix the proposals are <code>typeName</code>
508      * and <code>name</code>.
509      * </p>
510      * <p>
511      * This method is affected by the following JavaCore options : {@link JavaCore#CODEASSIST_ARGUMENT_PREFIXES} and
512      * {@link JavaCore#CODEASSIST_ARGUMENT_SUFFIXES}.
513      * </p>
514      * <p>
515      * For a complete description of these configurable options, see <code>getDefaultOptions</code>.
516      * For programmaticaly change these options, see <code>JavaCore#setOptions()</code>.
517      * </p>
518      *
519      * @param javaProject project which contains the argument.
520      * @param packageName package of the argument's type.
521      * @param qualifiedTypeName argument's type.
522      * @param dim argument's dimension (0 if the argument is not an array).
523      * @param excludedNames a list of names which cannot be suggested (already used names).
524      * Can be <code>null</code> if there is no excluded names.
525      * @return char[][] an array of names.
526      * @see JavaCore#setOptions(java.util.Hashtable)
527      * @see JavaCore#getDefaultOptions()
528      */

529     public static String JavaDoc[] suggestArgumentNames(IJavaProject javaProject, String JavaDoc packageName, String JavaDoc qualifiedTypeName, int dim, String JavaDoc[] excludedNames) {
530         return convertCharsToString(
531             suggestArgumentNames(
532                 javaProject,
533                 packageName.toCharArray(),
534                 qualifiedTypeName.toCharArray(),
535                 dim,
536                 convertStringToChars(excludedNames)));
537     }
538     /**
539      * Suggest names for a field. The name is computed from field's type
540      * and possible prefixes or suffixes are added.
541      * <p>
542      * If the type of the field is <code>TypeName</code>, the prefix for field is <code>pre</code>
543      * and the suffix for field is <code>suf</code> then the proposed names are <code>preTypeNamesuf</code>
544      * and <code>preNamesuf</code>. If there is no prefix or suffix the proposals are <code>typeName</code>
545      * and <code>name</code>.
546      * </p>
547      * <p>
548      * This method is affected by the following JavaCore options : {@link JavaCore#CODEASSIST_FIELD_PREFIXES},
549      * {@link JavaCore#CODEASSIST_FIELD_SUFFIXES} and for instance field and {@link JavaCore#CODEASSIST_STATIC_FIELD_PREFIXES},
550      * {@link JavaCore#CODEASSIST_STATIC_FIELD_SUFFIXES} for static field.
551      * </p>
552      * <p>
553      * For a complete description of these configurable options, see <code>getDefaultOptions</code>.
554      * For programmaticaly change these options, see <code>JavaCore#setOptions()</code>.
555      * </p>
556      *
557      * @param javaProject project which contains the field.
558      * @param packageName package of the field's type.
559      * @param qualifiedTypeName field's type.
560      * @param dim field's dimension (0 if the field is not an array).
561      * @param modifiers field's modifiers as defined by the class
562      * <code>Flags</code>.
563      * @param excludedNames a list of names which cannot be suggested (already used names).
564      * Can be <code>null</code> if there is no excluded names.
565      * @return char[][] an array of names.
566      * @see Flags
567      * @see JavaCore#setOptions(java.util.Hashtable)
568      * @see JavaCore#getDefaultOptions()
569      */

570     public static char[][] suggestFieldNames(IJavaProject javaProject, char[] packageName, char[] qualifiedTypeName, int dim, int modifiers, char[][] excludedNames) {
571         NamingRequestor requestor = new NamingRequestor();
572         InternalNamingConventions.suggestFieldNames(
573             javaProject,
574             packageName,
575             qualifiedTypeName,
576             dim,
577             modifiers,
578             null,
579             excludedNames,
580             requestor);
581
582         return requestor.getResults();
583     }
584     
585     /**
586      * Suggest names for a field. The name is computed from field's type
587      * and possible prefixes or suffixes are added.
588      * <p>
589      * If the type of the field is <code>TypeName</code>, the prefix for field is <code>pre</code>
590      * and the suffix for field is <code>suf</code> then the proposed names are <code>preTypeNamesuf</code>
591      * and <code>preNamesuf</code>. If there is no prefix or suffix the proposals are <code>typeName</code>
592      * and <code>name</code>.
593      * </p>
594      * <p>
595      * This method is affected by the following JavaCore options : {@link JavaCore#CODEASSIST_FIELD_PREFIXES},
596      * {@link JavaCore#CODEASSIST_FIELD_SUFFIXES} and for instance field and {@link JavaCore#CODEASSIST_STATIC_FIELD_PREFIXES},
597      * {@link JavaCore#CODEASSIST_STATIC_FIELD_SUFFIXES} for static field.
598      * </p>
599      * <p>
600      * For a complete description of these configurable options, see <code>getDefaultOptions</code>.
601      * For programmaticaly change these options, see <code>JavaCore#setOptions()</code>.
602      * </p>
603      *
604      * @param javaProject project which contains the field.
605      * @param packageName package of the field's type.
606      * @param qualifiedTypeName field's type.
607      * @param dim field's dimension (0 if the field is not an array).
608      * @param modifiers field's modifiers as defined by the class
609      * <code>Flags</code>.
610      * @param excludedNames a list of names which cannot be suggested (already used names).
611      * Can be <code>null</code> if there is no excluded names.
612      * @return char[][] an array of names.
613      * @see Flags
614      * @see JavaCore#setOptions(java.util.Hashtable)
615      * @see JavaCore#getDefaultOptions()
616      */

617     public static String JavaDoc[] suggestFieldNames(IJavaProject javaProject, String JavaDoc packageName, String JavaDoc qualifiedTypeName, int dim, int modifiers, String JavaDoc[] excludedNames) {
618         return convertCharsToString(
619             suggestFieldNames(
620                 javaProject,
621                 packageName.toCharArray(),
622                 qualifiedTypeName.toCharArray(),
623                 dim,
624                 modifiers,
625                 convertStringToChars(excludedNames)));
626     }
627     
628     /**
629      * Suggest names for a local variable. The name is computed from variable's type
630      * and possible prefixes or suffixes are added.
631      * <p>
632      * If the type of the local variable is <code>TypeName</code>, the prefix for local variable is <code>pre</code>
633      * and the suffix for local variable is <code>suf</code> then the proposed names are <code>preTypeNamesuf</code>
634      * and <code>preNamesuf</code>. If there is no prefix or suffix the proposals are <code>typeName</code>
635      * and <code>name</code>.
636      * </p>
637      * <p>
638      * This method is affected by the following JavaCore options : {@link JavaCore#CODEASSIST_LOCAL_PREFIXES} and
639      * {@link JavaCore#CODEASSIST_LOCAL_SUFFIXES}.
640      * </p>
641      * <p>
642      * For a complete description of these configurable options, see <code>getDefaultOptions</code>.
643      * For programmaticaly change these options, see <code>JavaCore#setOptions()</code>.
644      * </p>
645      *
646      * @param javaProject project which contains the variable.
647      * @param packageName package of the variable's type.
648      * @param qualifiedTypeName variable's type.
649      * @param dim variable's dimension (0 if the variable is not an array).
650      * @param excludedNames a list of names which cannot be suggested (already used names).
651      * Can be <code>null</code> if there is no excluded names.
652      * @return char[][] an array of names.
653      * @see JavaCore#setOptions(java.util.Hashtable)
654      * @see JavaCore#getDefaultOptions()
655      */

656     public static char[][] suggestLocalVariableNames(IJavaProject javaProject, char[] packageName, char[] qualifiedTypeName, int dim, char[][] excludedNames) {
657         NamingRequestor requestor = new NamingRequestor();
658         InternalNamingConventions.suggestLocalVariableNames(
659             javaProject,
660             packageName,
661             qualifiedTypeName,
662             dim,
663             null,
664             excludedNames,
665             requestor);
666
667         return requestor.getResults();
668     }
669     
670     /**
671      * Suggest names for a local variable. The name is computed from variable's type
672      * and possible prefixes or suffixes are added.
673      * <p>
674      * If the type of the local variable is <code>TypeName</code>, the prefix for local variable is <code>pre</code>
675      * and the suffix for local variable is <code>suf</code> then the proposed names are <code>preTypeNamesuf</code>
676      * and <code>preNamesuf</code>. If there is no prefix or suffix the proposals are <code>typeName</code>
677      * and <code>name</code>.
678      * </p>
679      * <p>
680      * This method is affected by the following JavaCore options : {@link JavaCore#CODEASSIST_LOCAL_PREFIXES} and
681      * {@link JavaCore#CODEASSIST_LOCAL_SUFFIXES}.
682      * </p>
683      * <p>
684      * For a complete description of these configurable options, see <code>getDefaultOptions</code>.
685      * For programmaticaly change these options, see <code>JavaCore#setOptions()</code>.
686      * </p>
687      *
688      * @param javaProject project which contains the variable.
689      * @param packageName package of the variable's type.
690      * @param qualifiedTypeName variable's type.
691      * @param dim variable's dimension (0 if the variable is not an array).
692      * @param excludedNames a list of names which cannot be suggested (already used names).
693      * Can be <code>null</code> if there is no excluded names.
694      * @return char[][] an array of names.
695      * @see JavaCore#setOptions(java.util.Hashtable)
696      * @see JavaCore#getDefaultOptions()
697      */

698     public static String JavaDoc[] suggestLocalVariableNames(IJavaProject javaProject, String JavaDoc packageName, String JavaDoc qualifiedTypeName, int dim, String JavaDoc[] excludedNames) {
699         return convertCharsToString(
700             suggestLocalVariableNames(
701                 javaProject,
702                 packageName.toCharArray(),
703                 qualifiedTypeName.toCharArray(),
704                 dim,
705                 convertStringToChars(excludedNames)));
706     }
707     
708     /**
709      * Suggest name for a getter method. The name is computed from field's name
710      * and possible prefixes or suffixes are removed.
711      * <p>
712      * If the field name is <code>preFieldNamesuf</code> and the prefix for field is <code>pre</code> and
713      * the suffix for field is <code>suf</code> then the prosposed name is <code>isFieldName</code> for boolean field or
714      * <code>getFieldName</code> for others. If there is no prefix and suffix the proposal is <code>isPreFieldNamesuf</code>
715      * for boolean field or <code>getPreFieldNamesuf</code> for others.
716      * </p>
717      * <p>
718      * This method is affected by the following JavaCore options : {@link JavaCore#CODEASSIST_FIELD_PREFIXES},
719      * {@link JavaCore#CODEASSIST_FIELD_SUFFIXES} for instance field and {@link JavaCore#CODEASSIST_STATIC_FIELD_PREFIXES},
720      * {@link JavaCore#CODEASSIST_STATIC_FIELD_SUFFIXES} for static field.
721      * </p>
722      * <p>
723      * For a complete description of these configurable options, see <code>getDefaultOptions</code>.
724      * For programmaticaly change these options, see <code>JavaCore#setOptions()</code>.
725      * </p>
726      *
727      * @param project project which contains the field.
728      * @param fieldName field's name's.
729      * @param modifiers field's modifiers as defined by the class
730      * <code>Flags</code>.
731      * @param isBoolean <code>true</code> if the field's type is boolean
732      * @param excludedNames a list of names which cannot be suggested (already used names).
733      * Can be <code>null</code> if there is no excluded names.
734      * @return char[] a name.
735      * @see Flags
736      * @see JavaCore#setOptions(java.util.Hashtable)
737      * @see JavaCore#getDefaultOptions()
738      */

739     public static char[] suggestGetterName(IJavaProject project, char[] fieldName, int modifiers, boolean isBoolean, char[][] excludedNames) {
740         if (isBoolean) {
741             char[] name = removePrefixAndSuffixForFieldName(project, fieldName, modifiers);
742             int prefixLen = GETTER_BOOL_NAME.length;
743             if (CharOperation.prefixEquals(GETTER_BOOL_NAME, name)
744                 && name.length > prefixLen && ScannerHelper.isUpperCase(name[prefixLen])) {
745                 return suggestNewName(name, excludedNames);
746             } else {
747                 return suggestNewName(
748                     CharOperation.concat(GETTER_BOOL_NAME, suggestAccessorName(project, fieldName, modifiers)),
749                     excludedNames
750                 );
751             }
752         } else {
753             return suggestNewName(
754                 CharOperation.concat(GETTER_NAME, suggestAccessorName(project, fieldName, modifiers)),
755                 excludedNames
756             );
757         }
758     }
759     
760     /**
761      * Suggest name for a getter method. The name is computed from field's name
762      * and possible prefixes or suffixes are removed.
763      * <p>
764      * If the field name is <code>preFieldNamesuf</code> and the prefix for field is <code>pre</code> and
765      * the suffix for field is <code>suf</code> then the prosposed name is <code>isFieldName</code> for boolean field or
766      * <code>getFieldName</code> for others. If there is no prefix and suffix the proposal is <code>isPreFieldNamesuf</code>
767      * for boolean field or <code>getPreFieldNamesuf</code> for others.
768      * </p>
769      * <p>
770      * This method is affected by the following JavaCore options : {@link JavaCore#CODEASSIST_FIELD_PREFIXES},
771      * {@link JavaCore#CODEASSIST_FIELD_SUFFIXES} for instance field and {@link JavaCore#CODEASSIST_STATIC_FIELD_PREFIXES},
772      * {@link JavaCore#CODEASSIST_STATIC_FIELD_SUFFIXES} for static field.
773      * </p>
774      * <p>
775      * For a complete description of these configurable options, see <code>getDefaultOptions</code>.
776      * For programmaticaly change these options, see <code>JavaCore#setOptions()</code>.
777      * </p>
778      *
779      * @param project project which contains the field.
780      * @param fieldName field's name's.
781      * @param modifiers field's modifiers as defined by the class
782      * <code>Flags</code>.
783      * @param isBoolean <code>true</code> if the field's type is boolean
784      * @param excludedNames a list of names which cannot be suggested (already used names).
785      * Can be <code>null</code> if there is no excluded names.
786      * @return char[] a name.
787      * @see Flags
788      * @see JavaCore#setOptions(java.util.Hashtable)
789      * @see JavaCore#getDefaultOptions()
790      */

791     public static String JavaDoc suggestGetterName(IJavaProject project, String JavaDoc fieldName, int modifiers, boolean isBoolean, String JavaDoc[] excludedNames) {
792         return String.valueOf(
793             suggestGetterName(
794                 project,
795                 fieldName.toCharArray(),
796                 modifiers,
797                 isBoolean,
798                 convertStringToChars(excludedNames)));
799     }
800
801     /**
802      * Suggest name for a setter method. The name is computed from field's name
803      * and possible prefixes or suffixes are removed.
804      * <p>
805      * If the field name is <code>preFieldNamesuf</code> and the prefix for field is <code>pre</code> and
806      * the suffix for field is <code>suf</code> then the proposed name is <code>setFieldName</code>.
807      * If there is no prefix and suffix the proposal is <code>setPreFieldNamesuf</code>.
808      * </p>
809      * <p>
810      * This method is affected by the following JavaCore options : {@link JavaCore#CODEASSIST_FIELD_PREFIXES},
811      * {@link JavaCore#CODEASSIST_FIELD_SUFFIXES} for instance field and {@link JavaCore#CODEASSIST_STATIC_FIELD_PREFIXES},
812      * {@link JavaCore#CODEASSIST_STATIC_FIELD_SUFFIXES} for static field.
813      * </p>
814      * <p>
815      * For a complete description of these configurable options, see <code>getDefaultOptions</code>.
816      * For programmaticaly change these options, see <code>JavaCore#setOptions()</code>.
817      * </p>
818      *
819      * @param project project which contains the field.
820      * @param fieldName field's name's.
821      * @param modifiers field's modifiers as defined by the class
822      * <code>Flags</code>.
823      * @param isBoolean <code>true</code> if the field's type is boolean
824      * @param excludedNames a list of names which cannot be suggested (already used names).
825      * Can be <code>null</code> if there is no excluded names.
826      * @return char[] a name.
827      * @see Flags
828      * @see JavaCore#setOptions(java.util.Hashtable)
829      * @see JavaCore#getDefaultOptions()
830      */

831     public static char[] suggestSetterName(IJavaProject project, char[] fieldName, int modifiers, boolean isBoolean, char[][] excludedNames) {
832
833         if (isBoolean) {
834             char[] name = removePrefixAndSuffixForFieldName(project, fieldName, modifiers);
835             int prefixLen = GETTER_BOOL_NAME.length;
836             if (CharOperation.prefixEquals(GETTER_BOOL_NAME, name)
837                 && name.length > prefixLen && ScannerHelper.isUpperCase(name[prefixLen])) {
838                 name = CharOperation.subarray(name, prefixLen, name.length);
839                 return suggestNewName(
840                     CharOperation.concat(SETTER_NAME, suggestAccessorName(project, name, modifiers)),
841                     excludedNames
842                 );
843             } else {
844                 return suggestNewName(
845                     CharOperation.concat(SETTER_NAME, suggestAccessorName(project, fieldName, modifiers)),
846                     excludedNames
847                 );
848             }
849         } else {
850             return suggestNewName(
851                 CharOperation.concat(SETTER_NAME, suggestAccessorName(project, fieldName, modifiers)),
852                 excludedNames
853             );
854         }
855     }
856     
857     /**
858      * Suggest name for a setter method. The name is computed from field's name
859      * and possible prefixes or suffixes are removed.
860      * <p>
861      * If the field name is <code>preFieldNamesuf</code> and the prefix for field is <code>pre</code> and
862      * the suffix for field is <code>suf</code> then the proposed name is <code>setFieldName</code>.
863      * If there is no prefix and suffix the proposal is <code>setPreFieldNamesuf</code>.
864      * </p>
865      * <p>
866      * This method is affected by the following JavaCore options : {@link JavaCore#CODEASSIST_FIELD_PREFIXES},
867      * {@link JavaCore#CODEASSIST_FIELD_SUFFIXES} for instance field and {@link JavaCore#CODEASSIST_STATIC_FIELD_PREFIXES},
868      * {@link JavaCore#CODEASSIST_STATIC_FIELD_SUFFIXES} for static field.
869      * </p>
870      * <p>
871      * For a complete description of these configurable options, see <code>getDefaultOptions</code>.
872      * For programmaticaly change these options, see <code>JavaCore#setOptions()</code>.
873      * </p>
874      *
875      * @param project project which contains the field.
876      * @param fieldName field's name's.
877      * @param modifiers field's modifiers as defined by the class
878      * <code>Flags</code>.
879      * @param isBoolean <code>true</code> if the field's type is boolean
880      * @param excludedNames a list of names which cannot be suggested (already used names).
881      * Can be <code>null</code> if there is no excluded names.
882      * @return char[] a name.
883      * @see Flags
884      * @see JavaCore#setOptions(java.util.Hashtable)
885      * @see JavaCore#getDefaultOptions()
886      */

887     public static String JavaDoc suggestSetterName(IJavaProject project, String JavaDoc fieldName, int modifiers, boolean isBoolean, String JavaDoc[] excludedNames) {
888         return String.valueOf(
889             suggestSetterName(
890                 project,
891                 fieldName.toCharArray(),
892                 modifiers,
893                 isBoolean,
894                 convertStringToChars(excludedNames)));
895     }
896     
897     private static char[] suggestAccessorName(IJavaProject project, char[] fieldName, int modifiers) {
898         char[] name = removePrefixAndSuffixForFieldName(project, fieldName, modifiers);
899         if (name.length > 0 && ScannerHelper.isLowerCase(name[0])) {
900             name[0] = ScannerHelper.toUpperCase(name[0]);
901         }
902         return name;
903     }
904     
905     private static char[] suggestNewName(char[] name, char[][] excludedNames){
906         if(excludedNames == null) {
907             return name;
908         }
909         
910         char[] newName = name;
911         int count = 2;
912         int i = 0;
913         while (i < excludedNames.length) {
914             if(CharOperation.equals(newName, excludedNames[i], false)) {
915                 newName = CharOperation.concat(name, String.valueOf(count++).toCharArray());
916                 i = 0;
917             } else {
918                 i++;
919             }
920         }
921         return newName;
922     }
923     
924     private static String JavaDoc[] convertCharsToString(char[][] c) {
925         int length = c == null ? 0 : c.length;
926         String JavaDoc[] s = new String JavaDoc[length];
927         for (int i = 0; i < length; i++) {
928             s[i] = String.valueOf(c[i]);
929         }
930         return s;
931     }
932     
933     private static char[][] convertStringToChars(String JavaDoc[] s) {
934         int length = s == null ? 0 : s.length;
935         char[][] c = new char[length][];
936         for (int i = 0; i < length; i++) {
937             if(s[i] == null) {
938                 c[i] = CharOperation.NO_CHAR;
939             } else {
940                 c[i] = s[i].toCharArray();
941             }
942         }
943         return c;
944     }
945 }
946
Popular Tags