1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.core;
13 import net.sourceforge.phpdt.core.compiler.CharOperation;
14 import net.sourceforge.phpdt.internal.codeassist.impl.AssistOptions;
15 import net.sourceforge.phpdt.internal.core.INamingRequestor;
16 import net.sourceforge.phpdt.internal.core.InternalNamingConventions;
20 * Provides methods for computing Java-specific names.
22 * The behavior of the methods is dependent of several JavaCore options.
24 * The possible options are :
26 * <li>CODEASSIST_FIELD_PREFIXES : Define the Prefixes for Field Name.</li>
27 * <li>CODEASSIST_STATIC_FIELD_PREFIXES : Define the Prefixes for Static Field Name.</li>
28 * <li>CODEASSIST_LOCAL_PREFIXES : Define the Prefixes for Local Variable Name.</li>
29 * <li>CODEASSIST_ARGUMENT_PREFIXES : Define the Prefixes for Argument Name.</li>
30 * <li>CODEASSIST_FIELD_SUFFIXES : Define the Suffixes for Field Name.</li>
31 * <li>CODEASSIST_STATIC_FIELD_SUFFIXES : Define the Suffixes for Static Field Name.</li>
32 * <li>CODEASSIST_LOCAL_SUFFIXES : Define the Suffixes for Local Variable Name.</li>
33 * <li>CODEASSIST_ARGUMENT_SUFFIXES : Define the Suffixes for Argument Name.</li>
37 * For a complete description of the configurable options, see <code>getDefaultOptions</code>.
38 * For programmaticaly change these options, see <code>JavaCore#setOptions()</code>.
41 * This class provides static methods and constants only; it is not intended to be
42 * instantiated or subclassed by clients.
45 * @see JavaCore#setOptions(java.util.Hashtable)
46 * @see JavaCore#getDefaultOptions()
49 public final class NamingConventions {
50 private static final char[] GETTER_BOOL_NAME = "is".toCharArray(); //$NON-NLS-1$
51 private static final char[] GETTER_NAME = "get".toCharArray(); //$NON-NLS-1$
52 private static final char[] SETTER_NAME = "set".toCharArray(); //$NON-NLS-1$
54 private static class NamingRequestor implements INamingRequestor {
55 private final static int SIZE = 10;
57 // for acceptNameWithPrefixAndSuffix
58 private char[][] firstPrefixAndFirstSuffixResults = new char[SIZE][];
59 private int firstPrefixAndFirstSuffixResultsCount = 0;
60 private char[][] firstPrefixAndSuffixResults = new char[SIZE][];
61 private int firstPrefixAndSuffixResultsCount = 0;
62 private char[][] prefixAndFirstSuffixResults = new char[SIZE][];
63 private int prefixAndFirstSuffixResultsCount = 0;
64 private char[][] prefixAndSuffixResults = new char[SIZE][];
65 private int prefixAndSuffixResultsCount = 0;
67 // for acceptNameWithPrefix
68 private char[][] firstPrefixResults = new char[SIZE][];
69 private int firstPrefixResultsCount = 0;
70 private char[][] prefixResults = new char[SIZE][];
71 private int prefixResultsCount = 0;
73 // for acceptNameWithSuffix
74 private char[][] firstSuffixResults = new char[SIZE][];
75 private int firstSuffixResultsCount = 0;
76 private char[][] suffixResults = new char[SIZE][];
77 private int suffixResultsCount = 0;
79 // for acceptNameWithoutPrefixAndSuffix
80 private char[][] otherResults = new char[SIZE][];
81 private int otherResultsCount = 0;
82 public void acceptNameWithPrefixAndSuffix(char[] name, boolean isFirstPrefix, boolean isFirstSuffix) {
83 if(isFirstPrefix && isFirstSuffix) {
84 int length = this.firstPrefixAndFirstSuffixResults.length;
85 if(length == this.firstPrefixAndFirstSuffixResultsCount) {
87 this.firstPrefixAndFirstSuffixResults,
89 this.firstPrefixAndFirstSuffixResults = new char[length * 2][],
93 this.firstPrefixAndFirstSuffixResults[this.firstPrefixAndFirstSuffixResultsCount++] = name;
94 } else if (isFirstPrefix) {
95 int length = this.firstPrefixAndSuffixResults.length;
96 if(length == this.firstPrefixAndSuffixResultsCount) {
98 this.firstPrefixAndSuffixResults,
100 this.firstPrefixAndSuffixResults = new char[length * 2][],
104 this.firstPrefixAndSuffixResults[this.firstPrefixAndSuffixResultsCount++] = name;
105 } else if(isFirstSuffix) {
106 int length = this.prefixAndFirstSuffixResults.length;
107 if(length == this.prefixAndFirstSuffixResultsCount) {
109 this.prefixAndFirstSuffixResults,
111 this.prefixAndFirstSuffixResults = new char[length * 2][],
115 this.prefixAndFirstSuffixResults[this.prefixAndFirstSuffixResultsCount++] = name;
117 int length = this.prefixAndSuffixResults.length;
118 if(length == this.prefixAndSuffixResultsCount) {
120 this.prefixAndSuffixResults,
122 this.prefixAndSuffixResults = new char[length * 2][],
126 this.prefixAndSuffixResults[this.prefixAndSuffixResultsCount++] = name;
130 public void acceptNameWithPrefix(char[] name, boolean isFirstPrefix) {
132 int length = this.firstPrefixResults.length;
133 if(length == this.firstPrefixResultsCount) {
135 this.firstPrefixResults,
137 this.firstPrefixResults = new char[length * 2][],
141 this.firstPrefixResults[this.firstPrefixResultsCount++] = name;
143 int length = this.prefixResults.length;
144 if(length == this.prefixResultsCount) {
148 this.prefixResults = new char[length * 2][],
152 this.prefixResults[this.prefixResultsCount++] = name;
156 public void acceptNameWithSuffix(char[] name, boolean isFirstSuffix) {
158 int length = this.firstSuffixResults.length;
159 if(length == this.firstSuffixResultsCount) {
161 this.firstSuffixResults,
163 this.firstSuffixResults = new char[length * 2][],
167 this.firstSuffixResults[this.firstSuffixResultsCount++] = name;
169 int length = this.suffixResults.length;
170 if(length == this.suffixResultsCount) {
174 this.suffixResults = new char[length * 2][],
178 this.suffixResults[this.suffixResultsCount++] = name;
182 public void acceptNameWithoutPrefixAndSuffix(char[] name) {
183 int length = this.otherResults.length;
184 if(length == this.otherResultsCount) {
188 this.otherResults = new char[length * 2][],
192 this.otherResults[this.otherResultsCount++] = name;
194 public char[][] getResults(){
196 this.firstPrefixAndFirstSuffixResultsCount
197 + this.firstPrefixAndSuffixResultsCount
198 + this.prefixAndFirstSuffixResultsCount
199 + this.prefixAndSuffixResultsCount
200 + this.firstPrefixResultsCount
201 + this.prefixResultsCount
202 + this.firstSuffixResultsCount
203 + this.suffixResultsCount
204 + this.otherResultsCount;
206 char[][] results = new char[count][];
209 System.arraycopy(this.firstPrefixAndFirstSuffixResults, 0, results, index, this.firstPrefixAndFirstSuffixResultsCount);
210 index += this.firstPrefixAndFirstSuffixResultsCount;
211 System.arraycopy(this.firstPrefixAndSuffixResults, 0, results, index, this.firstPrefixAndSuffixResultsCount);
212 index += this.firstPrefixAndSuffixResultsCount;
213 System.arraycopy(this.prefixAndFirstSuffixResults, 0, results, index, this.prefixAndFirstSuffixResultsCount);
214 index += this.prefixAndFirstSuffixResultsCount;
215 System.arraycopy(this.prefixAndSuffixResults, 0, results, index, this.prefixAndSuffixResultsCount);
216 index += this.prefixAndSuffixResultsCount;
217 System.arraycopy(this.firstPrefixResults, 0, results, index, this.firstPrefixResultsCount);
218 index += this.firstPrefixResultsCount;
219 System.arraycopy(this.prefixResults, 0, results, index, this.prefixResultsCount);
220 index += this.prefixResultsCount;
221 System.arraycopy(this.firstSuffixResults, 0, results, index, this.firstSuffixResultsCount);
222 index += this.firstSuffixResultsCount;
223 System.arraycopy(this.suffixResults, 0, results, index, this.suffixResultsCount);
224 index += this.suffixResultsCount;
225 System.arraycopy(this.otherResults, 0, results, index, this.otherResultsCount);
232 private NamingConventions() {
236 private static char[] removePrefixAndSuffix(char[] name, char[][] prefixes, char[][] suffixes) {
237 // remove longer prefix
238 char[] withoutPrefixName = name;
239 if (prefixes != null) {
241 for (int i= 0; i < prefixes.length; i++) {
242 char[] prefix = prefixes[i];
243 if (CharOperation.prefixEquals(prefix, name)) {
244 int currLen = prefix.length;
245 boolean lastCharIsLetter = Character.isLetter(prefix[currLen - 1]);
246 if(!lastCharIsLetter || (lastCharIsLetter && name.length > currLen && Character.isUpperCase(name[currLen]))) {
247 if (bestLength < currLen && name.length != currLen) {
248 withoutPrefixName = CharOperation.subarray(name, currLen, name.length);
249 bestLength = currLen;
256 // remove longer suffix
257 char[] withoutSuffixName = withoutPrefixName;
258 if(suffixes != null) {
260 for (int i = 0; i < suffixes.length; i++) {
261 char[] suffix = suffixes[i];
262 if(CharOperation.endsWith(withoutPrefixName, suffix)) {
263 int currLen = suffix.length;
264 if(bestLength < currLen && withoutPrefixName.length != currLen) {
265 withoutSuffixName = CharOperation.subarray(withoutPrefixName, 0, withoutPrefixName.length - currLen);
266 bestLength = currLen;
272 withoutSuffixName[0] = Character.toLowerCase(withoutSuffixName[0]);
273 return withoutSuffixName;
277 * Remove prefix and suffix from an argument name.
279 * If argument name prefix is <code>pre</code> and argument name suffix is <code>suf</code>
280 * then for an argument named <code>preArgsuf</code> the result of this method is <code>arg</code>.
281 * If there is no prefix or suffix defined in JavaCore options the result is the unchanged
282 * name <code>preArgsuf</code>.
285 * This method is affected by the following JavaCore options : CODEASSIST_ARGUMENT_PREFIXES and
286 * CODEASSIST_ARGUMENT_SUFFIXES.
289 * For a complete description of these configurable options, see <code>getDefaultOptions</code>.
290 * For programmaticaly change these options, see <code>JavaCore#setOptions()</code>.
293 * @param javaProject project which contains the argument.
294 * @param argumentName argument's name.
295 * @return char[] the name without prefix and suffix.
296 * @see JavaCore#setOptions(java.util.Hashtable)
297 * @see JavaCore#getDefaultOptions()
299 public static char[] removePrefixAndSuffixForArgumentName(IJavaProject javaProject, char[] argumentName) {
300 AssistOptions assistOptions = new AssistOptions(javaProject.getOptions(true));
301 return removePrefixAndSuffix(
303 assistOptions.argumentPrefixes,
304 assistOptions.argumentSuffixes);
308 * Remove prefix and suffix from an argument name.
310 * If argument name prefix is <code>pre</code> and argument name suffix is <code>suf</code>
311 * then for an argument named <code>preArgsuf</code> the result of this method is <code>arg</code>.
312 * If there is no prefix or suffix defined in JavaCore options the result is the unchanged
313 * name <code>preArgsuf</code>.
316 * This method is affected by the following JavaCore options : CODEASSIST_ARGUMENT_PREFIXES and
317 * CODEASSIST_ARGUMENT_SUFFIXES.
320 * For a complete description of these configurable options, see <code>getDefaultOptions</code>.
321 * For programmaticaly change these options, see <code>JavaCore#setOptions()</code>.
324 * @param javaProject project which contains the argument.
325 * @param argumentName argument's name.
326 * @return char[] the name without prefix and suffix.
327 * @see JavaCore#setOptions(java.util.Hashtable)
328 * @see JavaCore#getDefaultOptions()
330 public static String removePrefixAndSuffixForArgumentName(IJavaProject javaProject, String argumentName) {
331 return String.valueOf(removePrefixAndSuffixForArgumentName(javaProject, argumentName.toCharArray()));
335 * Remove prefix and suffix from a field name.
337 * If field name prefix is <code>pre</code> and field name suffix is <code>suf</code>
338 * then for a field named <code>preFieldsuf</code> the result of this method is <code>field</code>.
339 * If there is no prefix or suffix defined in JavaCore options the result is the unchanged
340 * name <code>preFieldsuf</code>.
343 * This method is affected by the following JavaCore options : CODEASSIST_FIELD_PREFIXES,
344 * CODEASSIST_FIELD_SUFFIXES for instance field and CODEASSIST_STATIC_FIELD_PREFIXES,
345 * CODEASSIST_STATIC_FIELD_SUFFIXES for static field.
348 * For a complete description of these configurable options, see <code>getDefaultOptions</code>.
349 * For programmaticaly change these options, see <code>JavaCore#setOptions()</code>.
352 * @param javaProject project which contains the field.
353 * @param fieldName field's name.
354 * @param modifiers field's modifiers as defined by the class
355 * <code>Flags</code>.
356 * @return char[] the name without prefix and suffix.
358 * @see JavaCore#setOptions(java.util.Hashtable)
359 * @see JavaCore#getDefaultOptions()
361 public static char[] removePrefixAndSuffixForFieldName(IJavaProject javaProject, char[] fieldName, int modifiers) {
362 boolean isStatic = Flags.isStatic(modifiers);
363 AssistOptions assistOptions = new AssistOptions(javaProject.getOptions(true));
364 return removePrefixAndSuffix(
366 isStatic ? assistOptions.staticFieldPrefixes : assistOptions.fieldPrefixes,
367 isStatic ? assistOptions.staticFieldSuffixes : assistOptions.fieldSuffixes);
371 * Remove prefix and suffix from a field name.
373 * If field name prefix is <code>pre</code> and field name suffix is <code>suf</code>
374 * then for a field named <code>preFieldsuf</code> the result of this method is <code>field</code>.
375 * If there is no prefix or suffix defined in JavaCore options the result is the unchanged
376 * name <code>preFieldsuf</code>.
379 * This method is affected by the following JavaCore options : CODEASSIST_FIELD_PREFIXES,
380 * CODEASSIST_FIELD_SUFFIXES for instance field and CODEASSIST_STATIC_FIELD_PREFIXES,
381 * CODEASSIST_STATIC_FIELD_SUFFIXES for static field.
384 * For a complete description of these configurable options, see <code>getDefaultOptions</code>.
385 * For programmaticaly change these options, see <code>JavaCore#setOptions()</code>.
388 * @param javaProject project which contains the field.
389 * @param fieldName field's name.
390 * @param modifiers field's modifiers as defined by the class
391 * <code>Flags</code>.
392 * @return char[] the name without prefix and suffix.
394 * @see JavaCore#setOptions(java.util.Hashtable)
395 * @see JavaCore#getDefaultOptions()
397 public static String removePrefixAndSuffixForFieldName(IJavaProject javaProject, String fieldName, int modifiers) {
398 return String.valueOf(removePrefixAndSuffixForFieldName(javaProject, fieldName.toCharArray(), modifiers));
401 * Remove prefix and suffix from a local variable name.
403 * If local variable name prefix is <code>pre</code> and local variable name suffix is <code>suf</code>
404 * then for a local variable named <code>preLocalsuf</code> the result of this method is <code>local</code>.
405 * If there is no prefix or suffix defined in JavaCore options the result is the unchanged
406 * name <code>preLocalsuf</code>.
409 * This method is affected by the following JavaCore options : CODEASSIST_LOCAL_PREFIXES and
410 * CODEASSIST_LOCAL_SUFFIXES.
413 * For a complete description of these configurable options, see <code>getDefaultOptions</code>.
414 * For programmaticaly change these options, see <code>JavaCore#setOptions()</code>.
417 * @param javaProject project which contains the variable.
418 * @param localName variable's name.
419 * @return char[] the name without prefix and suffix.
420 * @see JavaCore#setOptions(java.util.Hashtable)
421 * @see JavaCore#getDefaultOptions()
423 public static char[] removePrefixAndSuffixForLocalVariableName(IJavaProject javaProject, char[] localName) {
424 AssistOptions assistOptions = new AssistOptions(javaProject.getOptions(true));
425 return removePrefixAndSuffix(
427 assistOptions.argumentPrefixes,
428 assistOptions.argumentSuffixes);
432 * Remove prefix and suffix from a local variable name.
434 * If local variable name prefix is <code>pre</code> and local variable name suffix is <code>suf</code>
435 * then for a local variable named <code>preLocalsuf</code> the result of this method is <code>local</code>.
436 * If there is no prefix or suffix defined in JavaCore options the result is the unchanged
437 * name <code>preLocalsuf</code>.
440 * This method is affected by the following JavaCore options : CODEASSIST_LOCAL_PREFIXES and
441 * CODEASSIST_LOCAL_SUFFIXES.
444 * For a complete description of these configurable options, see <code>getDefaultOptions</code>.
445 * For programmaticaly change these options, see <code>JavaCore#setOptions()</code>.
448 * @param javaProject project which contains the variable.
449 * @param localName variable's name.
450 * @return char[] the name without prefix and suffix.
451 * @see JavaCore#setOptions(java.util.Hashtable)
452 * @see JavaCore#getDefaultOptions()
454 public static String removePrefixAndSuffixForLocalVariableName(IJavaProject javaProject, String localName) {
455 return String.valueOf(removePrefixAndSuffixForLocalVariableName(javaProject, localName.toCharArray()));
459 * Suggest names for an argument. The name is computed from argument's type
460 * and possible prefixes or suffixes are added.
462 * If the type of the argument is <code>TypeName</code>, the prefix for argument is <code>pre</code>
463 * and the suffix for argument is <code>suf</code> then the proposed names are <code>preTypeNamesuf</code>
464 * and <code>preNamesuf</code>. If there is no prefix or suffix the proposals are <code>typeName</code>
465 * and <code>name</code>.
468 * This method is affected by the following JavaCore options : CODEASSIST_ARGUMENT_PREFIXES and
469 * CODEASSIST_ARGUMENT_SUFFIXES.
472 * For a complete description of these configurable options, see <code>getDefaultOptions</code>.
473 * For programmaticaly change these options, see <code>JavaCore#setOptions()</code>.
476 * @param javaProject project which contains the argument.
477 * @param packageName package of the argument's type.
478 * @param qualifiedTypeName argument's type.
479 * @param dim argument's dimension (0 if the argument is not an array).
480 * @param excludedNames a list of names which cannot be suggested (already used names).
481 * Can be <code>null</code> if there is no excluded names.
482 * @return char[][] an array of names.
483 * @see JavaCore#setOptions(java.util.Hashtable)
484 * @see JavaCore#getDefaultOptions()
486 public static char[][] suggestArgumentNames(IJavaProject javaProject, char[] packageName, char[] qualifiedTypeName, int dim, char[][] excludedNames) {
487 NamingRequestor requestor = new NamingRequestor();
488 InternalNamingConventions.suggestArgumentNames(
496 return requestor.getResults();
500 * Suggest names for an argument. The name is computed from argument's type
501 * and possible prefixes or suffixes are added.
503 * If the type of the argument is <code>TypeName</code>, the prefix for argument is <code>pre</code>
504 * and the suffix for argument is <code>suf</code> then the proposed names are <code>preTypeNamesuf</code>
505 * and <code>preNamesuf</code>. If there is no prefix or suffix the proposals are <code>typeName</code>
506 * and <code>name</code>.
509 * This method is affected by the following JavaCore options : CODEASSIST_ARGUMENT_PREFIXES and
510 * CODEASSIST_ARGUMENT_SUFFIXES.
513 * For a complete description of these configurable options, see <code>getDefaultOptions</code>.
514 * For programmaticaly change these options, see <code>JavaCore#setOptions()</code>.
517 * @param javaProject project which contains the argument.
518 * @param packageName package of the argument's type.
519 * @param qualifiedTypeName argument's type.
520 * @param dim argument's dimension (0 if the argument is not an array).
521 * @param excludedNames a list of names which cannot be suggested (already used names).
522 * Can be <code>null</code> if there is no excluded names.
523 * @return char[][] an array of names.
524 * @see JavaCore#setOptions(java.util.Hashtable)
525 * @see JavaCore#getDefaultOptions()
527 public static String[] suggestArgumentNames(IJavaProject javaProject, String packageName, String qualifiedTypeName, int dim, String[] excludedNames) {
528 return convertCharsToString(
529 suggestArgumentNames(
531 packageName.toCharArray(),
532 qualifiedTypeName.toCharArray(),
534 convertStringToChars(excludedNames)));
537 * Suggest names for a field. The name is computed from field's type
538 * and possible prefixes or suffixes are added.
540 * If the type of the field is <code>TypeName</code>, the prefix for field is <code>pre</code>
541 * and the suffix for field is <code>suf</code> then the proposed names are <code>preTypeNamesuf</code>
542 * and <code>preNamesuf</code>. If there is no prefix or suffix the proposals are <code>typeName</code>
543 * and <code>name</code>.
546 * This method is affected by the following JavaCore options : CODEASSIST_FIELD_PREFIXES,
547 * CODEASSIST_FIELD_SUFFIXES and for instance field and CODEASSIST_STATIC_FIELD_PREFIXES,
548 * CODEASSIST_STATIC_FIELD_SUFFIXES for static field.
551 * For a complete description of these configurable options, see <code>getDefaultOptions</code>.
552 * For programmaticaly change these options, see <code>JavaCore#setOptions()</code>.
555 * @param javaProject project which contains the field.
556 * @param packageName package of the field's type.
557 * @param qualifiedTypeName field's type.
558 * @param dim field's dimension (0 if the field is not an array).
559 * @param modifiers field's modifiers as defined by the class
560 * <code>Flags</code>.
561 * @param excludedNames a list of names which cannot be suggested (already used names).
562 * Can be <code>null</code> if there is no excluded names.
563 * @return char[][] an array of names.
565 * @see JavaCore#setOptions(java.util.Hashtable)
566 * @see JavaCore#getDefaultOptions()
568 public static char[][] suggestFieldNames(IJavaProject javaProject, char[] packageName, char[] qualifiedTypeName, int dim, int modifiers, char[][] excludedNames) {
569 NamingRequestor requestor = new NamingRequestor();
570 InternalNamingConventions.suggestFieldNames(
579 return requestor.getResults();
583 * Suggest names for a field. The name is computed from field's type
584 * and possible prefixes or suffixes are added.
586 * If the type of the field is <code>TypeName</code>, the prefix for field is <code>pre</code>
587 * and the suffix for field is <code>suf</code> then the proposed names are <code>preTypeNamesuf</code>
588 * and <code>preNamesuf</code>. If there is no prefix or suffix the proposals are <code>typeName</code>
589 * and <code>name</code>.
592 * This method is affected by the following JavaCore options : CODEASSIST_FIELD_PREFIXES,
593 * CODEASSIST_FIELD_SUFFIXES and for instance field and CODEASSIST_STATIC_FIELD_PREFIXES,
594 * CODEASSIST_STATIC_FIELD_SUFFIXES for static field.
597 * For a complete description of these configurable options, see <code>getDefaultOptions</code>.
598 * For programmaticaly change these options, see <code>JavaCore#setOptions()</code>.
601 * @param javaProject project which contains the field.
602 * @param packageName package of the field's type.
603 * @param qualifiedTypeName field's type.
604 * @param dim field's dimension (0 if the field is not an array).
605 * @param modifiers field's modifiers as defined by the class
606 * <code>Flags</code>.
607 * @param excludedNames a list of names which cannot be suggested (already used names).
608 * Can be <code>null</code> if there is no excluded names.
609 * @return char[][] an array of names.
611 * @see JavaCore#setOptions(java.util.Hashtable)
612 * @see JavaCore#getDefaultOptions()
614 public static String[] suggestFieldNames(IJavaProject javaProject, String packageName, String qualifiedTypeName, int dim, int modifiers, String[] excludedNames) {
615 return convertCharsToString(
618 packageName.toCharArray(),
619 qualifiedTypeName.toCharArray(),
622 convertStringToChars(excludedNames)));
626 * Suggest names for a local variable. The name is computed from variable's type
627 * and possible prefixes or suffixes are added.
629 * If the type of the local variable is <code>TypeName</code>, the prefix for local variable is <code>pre</code>
630 * and the suffix for local variable is <code>suf</code> then the proposed names are <code>preTypeNamesuf</code>
631 * and <code>preNamesuf</code>. If there is no prefix or suffix the proposals are <code>typeName</code>
632 * and <code>name</code>.
635 * This method is affected by the following JavaCore options : CODEASSIST_LOCAL_PREFIXES and
636 * CODEASSIST_LOCAL_SUFFIXES.
639 * For a complete description of these configurable options, see <code>getDefaultOptions</code>.
640 * For programmaticaly change these options, see <code>JavaCore#setOptions()</code>.
643 * @param javaProject project which contains the variable.
644 * @param packageName package of the variable's type.
645 * @param qualifiedTypeName variable's type.
646 * @param dim variable's dimension (0 if the variable is not an array).
647 * @param excludedNames a list of names which cannot be suggested (already used names).
648 * Can be <code>null</code> if there is no excluded names.
649 * @return char[][] an array of names.
650 * @see JavaCore#setOptions(java.util.Hashtable)
651 * @see JavaCore#getDefaultOptions()
653 public static char[][] suggestLocalVariableNames(IJavaProject javaProject, char[] packageName, char[] qualifiedTypeName, int dim, char[][] excludedNames) {
654 NamingRequestor requestor = new NamingRequestor();
655 InternalNamingConventions.suggestLocalVariableNames(
663 return requestor.getResults();
667 * Suggest names for a local variable. The name is computed from variable's type
668 * and possible prefixes or suffixes are added.
670 * If the type of the local variable is <code>TypeName</code>, the prefix for local variable is <code>pre</code>
671 * and the suffix for local variable is <code>suf</code> then the proposed names are <code>preTypeNamesuf</code>
672 * and <code>preNamesuf</code>. If there is no prefix or suffix the proposals are <code>typeName</code>
673 * and <code>name</code>.
676 * This method is affected by the following JavaCore options : CODEASSIST_LOCAL_PREFIXES and
677 * CODEASSIST_LOCAL_SUFFIXES.
680 * For a complete description of these configurable options, see <code>getDefaultOptions</code>.
681 * For programmaticaly change these options, see <code>JavaCore#setOptions()</code>.
684 * @param javaProject project which contains the variable.
685 * @param packageName package of the variable's type.
686 * @param qualifiedTypeName variable's type.
687 * @param dim variable's dimension (0 if the variable is not an array).
688 * @param excludedNames a list of names which cannot be suggested (already used names).
689 * Can be <code>null</code> if there is no excluded names.
690 * @return char[][] an array of names.
691 * @see JavaCore#setOptions(java.util.Hashtable)
692 * @see JavaCore#getDefaultOptions()
694 public static String[] suggestLocalVariableNames(IJavaProject javaProject, String packageName, String qualifiedTypeName, int dim, String[] excludedNames) {
695 return convertCharsToString(
696 suggestLocalVariableNames(
698 packageName.toCharArray(),
699 qualifiedTypeName.toCharArray(),
701 convertStringToChars(excludedNames)));
705 * Suggest name for a getter method. The name is computed from field's name
706 * and possible prefixes or suffixes are removed.
708 * If the field name is <code>preFieldNamesuf</code> and the prefix for field is <code>pre</code> and
709 * the suffix for field is <code>suf</code> then the prosposed name is <code>isFieldName</code> for boolean field or
710 * <code>getFieldName</code> for others. If there is no prefix and suffix the proposal is <code>isPreFieldNamesuf</code>
711 * for boolean field or <code>getPreFieldNamesuf</code> for others.
714 * This method is affected by the following JavaCore options : CODEASSIST_FIELD_PREFIXES,
715 * CODEASSIST_FIELD_SUFFIXES for instance field and CODEASSIST_STATIC_FIELD_PREFIXES,
716 * CODEASSIST_STATIC_FIELD_SUFFIXES for static field.
719 * For a complete description of these configurable options, see <code>getDefaultOptions</code>.
720 * For programmaticaly change these options, see <code>JavaCore#setOptions()</code>.
723 * @param project project which contains the field.
724 * @param fieldName field's name's.
725 * @param modifiers field's modifiers as defined by the class
726 * <code>Flags</code>.
727 * @param isBoolean <code>true</code> if the field's type is boolean
728 * @param excludedNames a list of names which cannot be suggested (already used names).
729 * Can be <code>null</code> if there is no excluded names.
730 * @return char[] a name.
732 * @see JavaCore#setOptions(java.util.Hashtable)
733 * @see JavaCore#getDefaultOptions()
735 public static char[] suggestGetterName(IJavaProject project, char[] fieldName, int modifiers, boolean isBoolean, char[][] excludedNames) {
737 char[] name = removePrefixAndSuffixForFieldName(project, fieldName, modifiers);
738 int prefixLen = GETTER_BOOL_NAME.length;
739 if (CharOperation.prefixEquals(GETTER_BOOL_NAME, name)
740 && name.length > prefixLen && Character.isUpperCase(name[prefixLen])) {
741 return suggestNewName(name, excludedNames);
743 return suggestNewName(
744 CharOperation.concat(GETTER_BOOL_NAME, suggestAccessorName(project, fieldName, modifiers)),
749 return suggestNewName(
750 CharOperation.concat(GETTER_NAME, suggestAccessorName(project, fieldName, modifiers)),
757 * Suggest name for a getter method. The name is computed from field's name
758 * and possible prefixes or suffixes are removed.
760 * If the field name is <code>preFieldNamesuf</code> and the prefix for field is <code>pre</code> and
761 * the suffix for field is <code>suf</code> then the prosposed name is <code>isFieldName</code> for boolean field or
762 * <code>getFieldName</code> for others. If there is no prefix and suffix the proposal is <code>isPreFieldNamesuf</code>
763 * for boolean field or <code>getPreFieldNamesuf</code> for others.
766 * This method is affected by the following JavaCore options : CODEASSIST_FIELD_PREFIXES,
767 * CODEASSIST_FIELD_SUFFIXES for instance field and CODEASSIST_STATIC_FIELD_PREFIXES,
768 * CODEASSIST_STATIC_FIELD_SUFFIXES for static field.
771 * For a complete description of these configurable options, see <code>getDefaultOptions</code>.
772 * For programmaticaly change these options, see <code>JavaCore#setOptions()</code>.
775 * @param project project which contains the field.
776 * @param fieldName field's name's.
777 * @param modifiers field's modifiers as defined by the class
778 * <code>Flags</code>.
779 * @param isBoolean <code>true</code> if the field's type is boolean
780 * @param excludedNames a list of names which cannot be suggested (already used names).
781 * Can be <code>null</code> if there is no excluded names.
782 * @return char[] a name.
784 * @see JavaCore#setOptions(java.util.Hashtable)
785 * @see JavaCore#getDefaultOptions()
787 public static String suggestGetterName(IJavaProject project, String fieldName, int modifiers, boolean isBoolean, String[] excludedNames) {
788 return String.valueOf(
791 fieldName.toCharArray(),
794 convertStringToChars(excludedNames)));
798 * Suggest name for a setter method. The name is computed from field's name
799 * and possible prefixes or suffixes are removed.
801 * If the field name is <code>preFieldNamesuf</code> and the prefix for field is <code>pre</code> and
802 * the suffix for field is <code>suf</code> then the proposed name is <code>setFieldName</code>.
803 * If there is no prefix and suffix the proposal is <code>setPreFieldNamesuf</code>.
806 * This method is affected by the following JavaCore options : CODEASSIST_FIELD_PREFIXES,
807 * CODEASSIST_FIELD_SUFFIXES for instance field and CODEASSIST_STATIC_FIELD_PREFIXES,
808 * CODEASSIST_STATIC_FIELD_SUFFIXES for static field.
811 * For a complete description of these configurable options, see <code>getDefaultOptions</code>.
812 * For programmaticaly change these options, see <code>JavaCore#setOptions()</code>.
815 * @param project project which contains the field.
816 * @param fieldName field's name's.
817 * @param modifiers field's modifiers as defined by the class
818 * <code>Flags</code>.
819 * @param isBoolean <code>true</code> if the field's type is boolean
820 * @param excludedNames a list of names which cannot be suggested (already used names).
821 * Can be <code>null</code> if there is no excluded names.
822 * @return char[] a name.
824 * @see JavaCore#setOptions(java.util.Hashtable)
825 * @see JavaCore#getDefaultOptions()
827 public static char[] suggestSetterName(IJavaProject project, char[] fieldName, int modifiers, boolean isBoolean, char[][] excludedNames) {
830 char[] name = removePrefixAndSuffixForFieldName(project, fieldName, modifiers);
831 int prefixLen = GETTER_BOOL_NAME.length;
832 if (CharOperation.prefixEquals(GETTER_BOOL_NAME, name)
833 && name.length > prefixLen && Character.isUpperCase(name[prefixLen])) {
834 name = CharOperation.subarray(name, prefixLen, name.length);
835 return suggestNewName(
836 CharOperation.concat(SETTER_NAME, suggestAccessorName(project, name, modifiers)),
840 return suggestNewName(
841 CharOperation.concat(SETTER_NAME, suggestAccessorName(project, fieldName, modifiers)),
846 return suggestNewName(
847 CharOperation.concat(SETTER_NAME, suggestAccessorName(project, fieldName, modifiers)),
854 * Suggest name for a setter method. The name is computed from field's name
855 * and possible prefixes or suffixes are removed.
857 * If the field name is <code>preFieldNamesuf</code> and the prefix for field is <code>pre</code> and
858 * the suffix for field is <code>suf</code> then the proposed name is <code>setFieldName</code>.
859 * If there is no prefix and suffix the proposal is <code>setPreFieldNamesuf</code>.
862 * This method is affected by the following JavaCore options : CODEASSIST_FIELD_PREFIXES,
863 * CODEASSIST_FIELD_SUFFIXES for instance field and CODEASSIST_STATIC_FIELD_PREFIXES,
864 * CODEASSIST_STATIC_FIELD_SUFFIXES for static field.
867 * For a complete description of these configurable options, see <code>getDefaultOptions</code>.
868 * For programmaticaly change these options, see <code>JavaCore#setOptions()</code>.
871 * @param project project which contains the field.
872 * @param fieldName field's name's.
873 * @param modifiers field's modifiers as defined by the class
874 * <code>Flags</code>.
875 * @param isBoolean <code>true</code> if the field's type is boolean
876 * @param excludedNames a list of names which cannot be suggested (already used names).
877 * Can be <code>null</code> if there is no excluded names.
878 * @return char[] a name.
880 * @see JavaCore#setOptions(java.util.Hashtable)
881 * @see JavaCore#getDefaultOptions()
883 public static String suggestSetterName(IJavaProject project, String fieldName, int modifiers, boolean isBoolean, String[] excludedNames) {
884 return String.valueOf(
887 fieldName.toCharArray(),
890 convertStringToChars(excludedNames)));
893 private static char[] suggestAccessorName(IJavaProject project, char[] fieldName, int modifiers) {
894 char[] name = removePrefixAndSuffixForFieldName(project, fieldName, modifiers);
895 if (name.length > 0 && Character.isLowerCase(name[0])) {
896 name[0] = Character.toUpperCase(name[0]);
901 private static char[] suggestNewName(char[] name, char[][] excludedNames){
902 if(excludedNames == null) {
906 char[] newName = name;
909 while (i < excludedNames.length) {
910 if(CharOperation.equals(newName, excludedNames[i], false)) {
911 newName = CharOperation.concat(name, String.valueOf(count++).toCharArray());
920 private static String[] convertCharsToString(char[][] c) {
921 int length = c == null ? 0 : c.length;
922 String[] s = new String[length];
923 for (int i = 0; i < length; i++) {
924 s[i] = String.valueOf(c[i]);
929 private static char[][] convertStringToChars(String[] s) {
930 int length = s == null ? 0 : s.length;
931 char[][] c = new char[length][];
932 for (int i = 0; i < length; i++) {
934 c[i] = CharOperation.NO_CHAR;
936 c[i] = s[i].toCharArray();