KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > InternalNamingConventions


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.internal.core;
12
13 import java.util.Map JavaDoc;
14
15 import org.eclipse.jdt.core.Flags;
16 import org.eclipse.jdt.core.IJavaProject;
17 import org.eclipse.jdt.core.compiler.CharOperation;
18 import org.eclipse.jdt.core.compiler.InvalidInputException;
19 import org.eclipse.jdt.internal.codeassist.impl.AssistOptions;
20 import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
21 import org.eclipse.jdt.internal.compiler.parser.Scanner;
22 import org.eclipse.jdt.internal.compiler.parser.ScannerHelper;
23 import org.eclipse.jdt.internal.compiler.parser.TerminalTokens;
24
25 public class InternalNamingConventions {
26     private static final char[] DEFAULT_NAME = "name".toCharArray(); //$NON-NLS-1$
27

28     private static Scanner getNameScanner(CompilerOptions compilerOptions) {
29         return
30             new Scanner(
31                 false /*comment*/,
32                 false /*whitespace*/,
33                 false /*nls*/,
34                 compilerOptions.sourceLevel /*sourceLevel*/,
35                 null /*taskTags*/,
36                 null/*taskPriorities*/,
37                 true/*taskCaseSensitive*/);
38     }
39     public static void suggestArgumentNames(IJavaProject javaProject, char[] packageName, char[] qualifiedTypeName, int dim, char[] internalPrefix, char[][] excludedNames, INamingRequestor requestor) {
40         Map JavaDoc options = javaProject.getOptions(true);
41         CompilerOptions compilerOptions = new CompilerOptions(options);
42         AssistOptions assistOptions = new AssistOptions(options);
43
44         suggestNames(
45             packageName,
46             qualifiedTypeName,
47             dim,
48             internalPrefix,
49             assistOptions.argumentPrefixes,
50             assistOptions.argumentSuffixes,
51             excludedNames,
52             getNameScanner(compilerOptions),
53             requestor);
54     }
55     public static void suggestFieldNames(IJavaProject javaProject, char[] packageName, char[] qualifiedTypeName, int dim, int modifiers, char[] internalPrefix, char[][] excludedNames, INamingRequestor requestor) {
56         boolean isStatic = Flags.isStatic(modifiers);
57         
58         Map JavaDoc options = javaProject.getOptions(true);
59         CompilerOptions compilerOptions = new CompilerOptions(options);
60         AssistOptions assistOptions = new AssistOptions(options);
61
62         suggestNames(
63             packageName,
64             qualifiedTypeName,
65             dim,
66             internalPrefix,
67             isStatic ? assistOptions.staticFieldPrefixes : assistOptions.fieldPrefixes,
68             isStatic ? assistOptions.staticFieldSuffixes : assistOptions.fieldSuffixes,
69             excludedNames,
70             getNameScanner(compilerOptions),
71             requestor);
72     }
73     public static void suggestLocalVariableNames(IJavaProject javaProject, char[] packageName, char[] qualifiedTypeName, int dim, char[] internalPrefix, char[][] excludedNames, INamingRequestor requestor) {
74         Map JavaDoc options = javaProject.getOptions(true);
75         CompilerOptions compilerOptions = new CompilerOptions(options);
76         AssistOptions assistOptions = new AssistOptions(options);
77
78         suggestNames(
79             packageName,
80             qualifiedTypeName,
81             dim,
82             internalPrefix,
83             assistOptions.localPrefixes,
84             assistOptions.localSuffixes,
85             excludedNames,
86             getNameScanner(compilerOptions),
87             requestor);
88     }
89     
90     private static void suggestNames(
91         char[] packageName,
92         char[] qualifiedTypeName,
93         int dim,
94         char[] internalPrefix,
95         char[][] prefixes,
96         char[][] suffixes,
97         char[][] excludedNames,
98         Scanner nameScanner,
99         INamingRequestor requestor){
100         
101         if(qualifiedTypeName == null || qualifiedTypeName.length == 0)
102             return;
103         
104         if(internalPrefix == null) {
105             internalPrefix = CharOperation.NO_CHAR;
106         } else {
107             internalPrefix = removePrefix(internalPrefix, prefixes);
108         }
109         
110         char[] typeName = CharOperation.lastSegment(qualifiedTypeName, '.');
111     
112         if(prefixes == null || prefixes.length == 0) {
113             prefixes = new char[1][0];
114         } else {
115             int length = prefixes.length;
116             System.arraycopy(prefixes, 0, prefixes = new char[length+1][], 0, length);
117             prefixes[length] = CharOperation.NO_CHAR;
118         }
119     
120         if(suffixes == null || suffixes.length == 0) {
121             suffixes = new char[1][0];
122         } else {
123             int length = suffixes.length;
124             System.arraycopy(suffixes, 0, suffixes = new char[length+1][], 0, length);
125             suffixes[length] = CharOperation.NO_CHAR;
126         }
127     
128         char[][] tempNames = null;
129     
130         // compute variable name for base type
131
try{
132             nameScanner.setSource(typeName);
133             switch (nameScanner.getNextToken()) {
134                 case TerminalTokens.TokenNameint :
135                 case TerminalTokens.TokenNamebyte :
136                 case TerminalTokens.TokenNameshort :
137                 case TerminalTokens.TokenNamechar :
138                 case TerminalTokens.TokenNamelong :
139                 case TerminalTokens.TokenNamefloat :
140                 case TerminalTokens.TokenNamedouble :
141                 case TerminalTokens.TokenNameboolean :
142                     
143                     if (internalPrefix != null && internalPrefix.length > 0) return;
144                     
145                     char[] name = computeBaseTypeNames(typeName[0], excludedNames);
146                     if(name != null) {
147                         tempNames = new char[][]{name};
148                     }
149                     break;
150             }
151         } catch(InvalidInputException e){
152             // ignore
153
}
154
155         // compute variable name for non base type
156
if(tempNames == null) {
157             tempNames = computeNames(typeName);
158         }
159     
160         boolean acceptDefaultName = true;
161         
162         next : for (int i = 0; i < tempNames.length; i++) {
163             char[] tempName = tempNames[i];
164             if(dim > 0) {
165                 int length = tempName.length;
166                 if (tempName[length-1] == 's'){
167                     if(tempName.length > 1 && tempName[length-2] == 's') {
168                         System.arraycopy(tempName, 0, tempName = new char[length + 2], 0, length);
169                         tempName[length] = 'e';
170                         tempName[length+1] = 's';
171                     }
172                 } else if(tempName[length-1] == 'y') {
173                     System.arraycopy(tempName, 0, tempName = new char[length + 2], 0, length);
174                     tempName[length-1] = 'i';
175                     tempName[length] = 'e';
176                     tempName[length+1] = 's';
177                 } else {
178                     System.arraycopy(tempName, 0, tempName = new char[length + 1], 0, length);
179                     tempName[length] = 's';
180                 }
181             }
182         
183             char[] unprefixedName = tempName;
184             for (int j = 0; j <= internalPrefix.length; j++) {
185                 if(j == internalPrefix.length || CharOperation.prefixEquals(CharOperation.subarray(internalPrefix, j, -1), unprefixedName, false)) {
186                     tempName = CharOperation.concat(CharOperation.subarray(internalPrefix, 0, j), unprefixedName);
187                     if(j != 0) tempName[j] = ScannerHelper.toUpperCase(tempName[j]);
188                     for (int k = 0; k < prefixes.length; k++) {
189                         if(prefixes[k].length > 0
190                             && ScannerHelper.isLetterOrDigit(prefixes[k][prefixes[k].length - 1])) {
191                             tempName[0] = ScannerHelper.toUpperCase(tempName[0]);
192                         } else {
193                             tempName[0] = ScannerHelper.toLowerCase(tempName[0]);
194                         }
195                         char[] prefixName = CharOperation.concat(prefixes[k], tempName);
196                         for (int l = 0; l < suffixes.length; l++) {
197                             char[] suffixName = CharOperation.concat(prefixName, suffixes[l]);
198                             suffixName =
199                                 excludeNames(
200                                     suffixName,
201                                     prefixName,
202                                     suffixes[l],
203                                     excludedNames);
204                             try{
205                                 nameScanner.setSource(suffixName);
206                                 switch (nameScanner.getNextToken()) {
207                                     case TerminalTokens.TokenNameIdentifier :
208                                         int token = nameScanner.getNextToken();
209                                         if (token == TerminalTokens.TokenNameEOF && nameScanner.startPosition == suffixName.length) {
210                                             acceptName(suffixName, prefixes[k], suffixes[l], k == 0, l == 0, internalPrefix.length - j, requestor);
211                                             acceptDefaultName = false;
212                                         }
213                                         break;
214                                     default:
215                                         suffixName = CharOperation.concat(
216                                             prefixName,
217                                             String.valueOf(1).toCharArray(),
218                                             suffixes[l]
219                                         );
220                                         suffixName =
221                                             excludeNames(
222                                                 suffixName,
223                                                 prefixName,
224                                                 suffixes[l],
225                                                 excludedNames);
226                                         nameScanner.setSource(suffixName);
227                                         switch (nameScanner.getNextToken()) {
228                                             case TerminalTokens.TokenNameIdentifier :
229                                                 token = nameScanner.getNextToken();
230                                                 if (token == TerminalTokens.TokenNameEOF && nameScanner.startPosition == suffixName.length) {
231                                                     acceptName(suffixName, prefixes[k], suffixes[l], k == 0, l == 0, internalPrefix.length - j, requestor);
232                                                     acceptDefaultName = false;
233                                                 }
234                                         }
235                                 }
236                             } catch(InvalidInputException e){
237                                 // ignore
238
}
239                         }
240                     }
241                     continue next;
242                 }
243             }
244         }
245         // if no names were found
246
if(acceptDefaultName) {
247             char[] name = excludeNames(DEFAULT_NAME, DEFAULT_NAME, CharOperation.NO_CHAR, excludedNames);
248             requestor.acceptNameWithoutPrefixAndSuffix(name, 0);
249         }
250     }
251     
252     private static void acceptName(
253         char[] name,
254         char[] prefix,
255         char[] suffix,
256         boolean isFirstPrefix,
257         boolean isFirstSuffix,
258         int reusedCharacters,
259         INamingRequestor requestor) {
260         if(prefix.length > 0 && suffix.length > 0) {
261             requestor.acceptNameWithPrefixAndSuffix(name, isFirstPrefix, isFirstSuffix, reusedCharacters);
262         } else if(prefix.length > 0){
263             requestor.acceptNameWithPrefix(name, isFirstPrefix, reusedCharacters);
264         } else if(suffix.length > 0){
265             requestor.acceptNameWithSuffix(name, isFirstSuffix, reusedCharacters);
266         } else {
267             requestor.acceptNameWithoutPrefixAndSuffix(name, reusedCharacters);
268         }
269     }
270     
271     private static char[] computeBaseTypeNames(char firstName, char[][] excludedNames){
272         char[] name = new char[]{firstName};
273         
274         for(int i = 0 ; i < excludedNames.length ; i++){
275             if(CharOperation.equals(name, excludedNames[i], false)) {
276                 name[0]++;
277                 if(name[0] > 'z')
278                     name[0] = 'a';
279                 if(name[0] == firstName)
280                     return null;
281                 i = 0;
282             }
283         }
284         
285         return name;
286     }
287     
288     private static char[][] computeNames(char[] sourceName){
289         char[][] names = new char[5][];
290         int nameCount = 0;
291         boolean previousIsUpperCase = false;
292         boolean previousIsLetter = true;
293         for(int i = sourceName.length - 1 ; i >= 0 ; i--){
294             boolean isUpperCase = ScannerHelper.isUpperCase(sourceName[i]);
295             boolean isLetter = ScannerHelper.isLetter(sourceName[i]);
296             if(isUpperCase && !previousIsUpperCase && previousIsLetter){
297                 char[] name = CharOperation.subarray(sourceName,i,sourceName.length);
298                 if(name.length > 1){
299                     if(nameCount == names.length) {
300                         System.arraycopy(names, 0, names = new char[nameCount * 2][], 0, nameCount);
301                     }
302                     name[0] = ScannerHelper.toLowerCase(name[0]);
303                     names[nameCount++] = name;
304                 }
305             }
306             previousIsUpperCase = isUpperCase;
307             previousIsLetter = isLetter;
308         }
309         if(nameCount == 0){
310             names[nameCount++] = CharOperation.toLowerCase(sourceName);
311         }
312         System.arraycopy(names, 0, names = new char[nameCount][], 0, nameCount);
313         return names;
314     }
315
316     private static char[] excludeNames(
317         char[] suffixName,
318         char[] prefixName,
319         char[] suffix,
320         char[][] excludedNames) {
321         int count = 2;
322         int m = 0;
323         while (m < excludedNames.length) {
324             if(CharOperation.equals(suffixName, excludedNames[m], false)) {
325                 suffixName = CharOperation.concat(
326                     prefixName,
327                     String.valueOf(count++).toCharArray(),
328                     suffix
329                 );
330                 m = 0;
331             } else {
332                 m++;
333             }
334         }
335         return suffixName;
336     }
337     
338     private static char[] removePrefix(char[] name, char[][] prefixes) {
339         // remove longer prefix
340
char[] withoutPrefixName = name;
341         if (prefixes != null) {
342             int bestLength = 0;
343             int nameLength = name.length;
344             for (int i= 0; i < prefixes.length; i++) {
345                 char[] prefix = prefixes[i];
346                 
347                 int prefixLength = prefix.length;
348                 if(prefixLength <= nameLength) {
349                     if(CharOperation.prefixEquals(prefix, name, false)) {
350                         if (prefixLength > bestLength) {
351                             bestLength = prefixLength;
352                         }
353                     }
354                 } else {
355                     int currLen = 0;
356                     for (; currLen < nameLength; currLen++) {
357                         if(ScannerHelper.toLowerCase(prefix[currLen]) != ScannerHelper.toLowerCase(name[currLen])) {
358                             if (currLen > bestLength) {
359                                 bestLength = currLen;
360                             }
361                             break;
362                         }
363                     }
364                     if(currLen == nameLength && currLen > bestLength) {
365                         bestLength = currLen;
366                     }
367                 }
368             }
369             if(bestLength > 0) {
370                 if(bestLength == nameLength) {
371                     withoutPrefixName = CharOperation.NO_CHAR;
372                 } else {
373                     withoutPrefixName = CharOperation.subarray(name, bestLength, nameLength);
374                 }
375             }
376         }
377 //
378
//
379
// // remove longer prefix
380
// char[] withoutPrefixName = name;
381
// if (prefixes != null) {
382
// int bestLength = 0;
383
// for (int i= 0; i < prefixes.length; i++) {
384
// char[] prefix = prefixes[i];
385
// int max = prefix.length < name.length ? prefix.length : name.length;
386
// int currLen = 0;
387
// for (; currLen < max; currLen++) {
388
// if(Character.toLowerCase(prefix[currLen]) != Character.toLowerCase(name[currLen])) {
389
// if (currLen > bestLength) {
390
// bestLength = currLen;
391
// }
392
// break;
393
// }
394
// }
395
// if(currLen == max && currLen > bestLength) {
396
// bestLength = max;
397
// }
398
// }
399
// if(bestLength > 0) {
400
// if(bestLength == name.length) {
401
// withoutPrefixName = CharOperation.NO_CHAR;
402
// } else {
403
// withoutPrefixName = CharOperation.subarray(name, bestLength, name.length);
404
// }
405
// }
406
// }
407

408         return withoutPrefixName;
409     }
410     
411     public static final boolean prefixEquals(char[] prefix, char[] name) {
412
413         int max = prefix.length;
414         if (name.length < max)
415             return false;
416         for (int i = max;
417             --i >= 0;
418             ) // assumes the prefix is not larger than the name
419
if (prefix[i] != name[i])
420                     return false;
421             return true;
422     }
423 }
424
Popular Tags