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.internal.core;
15 import net.sourceforge.phpdt.core.Flags;
16 import net.sourceforge.phpdt.core.IJavaProject;
17 import net.sourceforge.phpdt.core.JavaConventions;
18 import net.sourceforge.phpdt.core.compiler.CharOperation;
19 import net.sourceforge.phpdt.core.compiler.InvalidInputException;
20 import net.sourceforge.phpdt.internal.codeassist.impl.AssistOptions;
21 import net.sourceforge.phpdt.internal.compiler.impl.CompilerOptions;
22 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
24 public class InternalNamingConventions {
25 private static final char[] DEFAULT_NAME = "name".toCharArray(); //$NON-NLS-1$
27 private static Scanner getNameScanner(CompilerOptions compilerOptions) {
33 // compilerOptions.sourceLevel /*sourceLevel*/,
37 null/*taskPriorities*/, false);
40 public static void suggestArgumentNames(IJavaProject javaProject, char[] packageName, char[] qualifiedTypeName, int dim, char[][] excludedNames, INamingRequestor requestor) {
41 Map options = javaProject.getOptions(true);
42 CompilerOptions compilerOptions = new CompilerOptions(options);
43 AssistOptions assistOptions = new AssistOptions(options);
49 assistOptions.argumentPrefixes,
50 assistOptions.argumentSuffixes,
52 getNameScanner(compilerOptions),
55 public static void suggestFieldNames(IJavaProject javaProject, char[] packageName, char[] qualifiedTypeName, int dim, int modifiers, char[][] excludedNames, INamingRequestor requestor) {
56 boolean isStatic = Flags.isStatic(modifiers);
58 Map options = javaProject.getOptions(true);
59 CompilerOptions compilerOptions = new CompilerOptions(options);
60 AssistOptions assistOptions = new AssistOptions(options);
66 isStatic ? assistOptions.staticFieldPrefixes : assistOptions.fieldPrefixes,
67 isStatic ? assistOptions.staticFieldSuffixes : assistOptions.fieldSuffixes,
69 getNameScanner(compilerOptions),
72 public static void suggestLocalVariableNames(IJavaProject javaProject, char[] packageName, char[] qualifiedTypeName, int dim, char[][] excludedNames, INamingRequestor requestor) {
73 Map options = javaProject.getOptions(true);
74 CompilerOptions compilerOptions = new CompilerOptions(options);
75 AssistOptions assistOptions = new AssistOptions(options);
81 assistOptions.localPrefixes,
82 assistOptions.localSuffixes,
84 getNameScanner(compilerOptions),
88 private static void suggestNames(
90 char[] qualifiedTypeName,
94 char[][] excludedNames,
96 INamingRequestor requestor){
98 if(qualifiedTypeName == null || qualifiedTypeName.length == 0)
101 char[] typeName = CharOperation.lastSegment(qualifiedTypeName, '.');
103 if(prefixes == null || prefixes.length == 0) {
104 prefixes = new char[1][0];
106 int length = prefixes.length;
107 System.arraycopy(prefixes, 0, prefixes = new char[length+1][], 0, length);
108 prefixes[length] = CharOperation.NO_CHAR;
111 if(suffixes == null || suffixes.length == 0) {
112 suffixes = new char[1][0];
114 int length = suffixes.length;
115 System.arraycopy(suffixes, 0, suffixes = new char[length+1][], 0, length);
116 suffixes[length] = CharOperation.NO_CHAR;
119 char[][] tempNames = null;
121 // compute variable name for base type
123 // nameScanner.setSource(typeName);
124 // switch (nameScanner.getNextToken()) {
125 // case TerminalTokens.TokenNameint :
126 // case TerminalTokens.TokenNamebyte :
127 // case TerminalTokens.TokenNameshort :
128 // case TerminalTokens.TokenNamechar :
129 // case TerminalTokens.TokenNamelong :
130 // case TerminalTokens.TokenNamefloat :
131 // case TerminalTokens.TokenNamedouble :
132 // case TerminalTokens.TokenNameboolean :
133 // char[] name = computeBaseTypeNames(typeName[0], excludedNames);
134 // if(name != null) {
135 // tempNames = new char[][]{name};
139 // } catch(InvalidInputException e){
143 // compute variable name for non base type
144 if(tempNames == null) {
145 tempNames = computeNames(typeName);
148 boolean acceptDefaultName = true;
150 for (int i = 0; i < tempNames.length; i++) {
151 char[] tempName = tempNames[i];
153 int length = tempName.length;
154 if (tempName[length-1] == 's'){
155 if(tempName.length > 1 && tempName[length-2] == 's') {
156 System.arraycopy(tempName, 0, tempName = new char[length + 2], 0, length);
157 tempName[length] = 'e';
158 tempName[length+1] = 's';
160 } else if(tempName[length-1] == 'y') {
161 System.arraycopy(tempName, 0, tempName = new char[length + 2], 0, length);
162 tempName[length-1] = 'i';
163 tempName[length] = 'e';
164 tempName[length+1] = 's';
166 System.arraycopy(tempName, 0, tempName = new char[length + 1], 0, length);
167 tempName[length] = 's';
171 for (int j = 0; j < prefixes.length; j++) {
172 if(prefixes[j].length > 0
173 && Character.isLetterOrDigit(prefixes[j][prefixes[j].length - 1])) {
174 tempName[0] = Character.toUpperCase(tempName[0]);
176 tempName[0] = Character.toLowerCase(tempName[0]);
178 char[] prefixName = CharOperation.concat(prefixes[j], tempName);
179 for (int k = 0; k < suffixes.length; k++) {
180 char[] suffixName = CharOperation.concat(prefixName, suffixes[k]);
187 if(JavaConventions.validateFieldName(new String(suffixName)).isOK()) {
188 acceptName(suffixName, prefixes[j], suffixes[k], j == 0, k == 0, requestor);
189 acceptDefaultName = false;
191 suffixName = CharOperation.concat(
193 String.valueOf(1).toCharArray(),
202 if(JavaConventions.validateFieldName(new String(suffixName)).isOK()) {
203 acceptName(suffixName, prefixes[j], suffixes[k], j == 0, k == 0, requestor);
204 acceptDefaultName = false;
211 // if no names were found
212 if(acceptDefaultName) {
213 char[] name = excludeNames(DEFAULT_NAME, DEFAULT_NAME, CharOperation.NO_CHAR, excludedNames);
214 requestor.acceptNameWithoutPrefixAndSuffix(name);
218 private static void acceptName(
222 boolean isFirstPrefix,
223 boolean isFirstSuffix,
224 INamingRequestor requestor) {
225 if(prefix.length > 0 && suffix.length > 0) {
226 requestor.acceptNameWithPrefixAndSuffix(name, isFirstPrefix, isFirstSuffix);
227 } else if(prefix.length > 0){
228 requestor.acceptNameWithPrefix(name, isFirstPrefix);
229 } else if(suffix.length > 0){
230 requestor.acceptNameWithSuffix(name, isFirstSuffix);
232 requestor.acceptNameWithoutPrefixAndSuffix(name);
236 private static char[] computeBaseTypeNames(char firstName, char[][] excludedNames){
237 char[] name = new char[]{firstName};
239 for(int i = 0 ; i < excludedNames.length ; i++){
240 if(CharOperation.equals(name, excludedNames[i], false)) {
244 if(name[0] == firstName)
253 private static char[][] computeNames(char[] sourceName){
254 char[][] names = new char[5][];
256 boolean previousIsUpperCase = false;
257 boolean previousIsLetter = true;
258 for(int i = sourceName.length - 1 ; i >= 0 ; i--){
259 boolean isUpperCase = Character.isUpperCase(sourceName[i]);
260 boolean isLetter = Character.isLetter(sourceName[i]);
261 if(isUpperCase && !previousIsUpperCase && previousIsLetter){
262 char[] name = CharOperation.subarray(sourceName,i,sourceName.length);
264 if(nameCount == names.length) {
265 System.arraycopy(names, 0, names = new char[nameCount * 2][], 0, nameCount);
267 name[0] = Character.toLowerCase(name[0]);
268 names[nameCount++] = name;
271 previousIsUpperCase = isUpperCase;
272 previousIsLetter = isLetter;
275 names[nameCount++] = CharOperation.toLowerCase(sourceName);
277 System.arraycopy(names, 0, names = new char[nameCount][], 0, nameCount);
281 private static char[] excludeNames(
285 char[][] excludedNames) {
288 while (m < excludedNames.length) {
289 if(CharOperation.equals(suffixName, excludedNames[m], false)) {
290 suffixName = CharOperation.concat(
292 String.valueOf(count++).toCharArray(),