1 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 50 public final class NamingConventions { 51 private static final char[] GETTER_BOOL_NAME = "is".toCharArray(); private static final char[] GETTER_NAME = "get".toCharArray(); private static final char[] SETTER_NAME = "set".toCharArray(); 55 static class NamingRequestor implements INamingRequestor { 56 private final static int SIZE = 10; 57 58 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 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 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 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 } 236 237 private static char[] removePrefixAndSuffix(char[] name, char[][] prefixes, char[][] suffixes) { 238 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 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 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 331 public static String removePrefixAndSuffixForArgumentName(IJavaProject javaProject, String argumentName) { 332 return String.valueOf(removePrefixAndSuffixForArgumentName(javaProject, argumentName.toCharArray())); 333 } 334 335 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 398 public static String removePrefixAndSuffixForFieldName(IJavaProject javaProject, String fieldName, int modifiers) { 399 return String.valueOf(removePrefixAndSuffixForFieldName(javaProject, fieldName.toCharArray(), modifiers)); 400 } 401 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 455 public static String removePrefixAndSuffixForLocalVariableName(IJavaProject javaProject, String localName) { 456 return String.valueOf(removePrefixAndSuffixForLocalVariableName(javaProject, localName.toCharArray())); 457 } 458 459 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 529 public static String [] suggestArgumentNames(IJavaProject javaProject, String packageName, String qualifiedTypeName, int dim, String [] excludedNames) { 530 return convertCharsToString( 531 suggestArgumentNames( 532 javaProject, 533 packageName.toCharArray(), 534 qualifiedTypeName.toCharArray(), 535 dim, 536 convertStringToChars(excludedNames))); 537 } 538 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 617 public static String [] suggestFieldNames(IJavaProject javaProject, String packageName, String qualifiedTypeName, int dim, int modifiers, String [] excludedNames) { 618 return convertCharsToString( 619 suggestFieldNames( 620 javaProject, 621 packageName.toCharArray(), 622 qualifiedTypeName.toCharArray(), 623 dim, 624 modifiers, 625 convertStringToChars(excludedNames))); 626 } 627 628 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 698 public static String [] suggestLocalVariableNames(IJavaProject javaProject, String packageName, String qualifiedTypeName, int dim, String [] excludedNames) { 699 return convertCharsToString( 700 suggestLocalVariableNames( 701 javaProject, 702 packageName.toCharArray(), 703 qualifiedTypeName.toCharArray(), 704 dim, 705 convertStringToChars(excludedNames))); 706 } 707 708 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 791 public static String suggestGetterName(IJavaProject project, String fieldName, int modifiers, boolean isBoolean, String [] excludedNames) { 792 return String.valueOf( 793 suggestGetterName( 794 project, 795 fieldName.toCharArray(), 796 modifiers, 797 isBoolean, 798 convertStringToChars(excludedNames))); 799 } 800 801 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 887 public static String suggestSetterName(IJavaProject project, String fieldName, int modifiers, boolean isBoolean, String [] 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 [] convertCharsToString(char[][] c) { 925 int length = c == null ? 0 : c.length; 926 String [] s = new String [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 [] 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 |