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.internal.codeassist.impl.AssistOptions;
20 import net.sourceforge.phpdt.internal.compiler.impl.CompilerOptions;
21 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
23 public class InternalNamingConventions {
24 private static final char[] DEFAULT_NAME = "name".toCharArray(); //$NON-NLS-1$
26 private static Scanner getNameScanner(CompilerOptions compilerOptions) {
32 // compilerOptions.sourceLevel /*sourceLevel*/,
36 null/*taskPriorities*/, false);
39 public static void suggestArgumentNames(IJavaProject javaProject, char[] packageName, char[] qualifiedTypeName, int dim, char[][] excludedNames, INamingRequestor requestor) {
40 Map options = javaProject.getOptions(true);
41 CompilerOptions compilerOptions = new CompilerOptions(options);
42 AssistOptions assistOptions = new AssistOptions(options);
48 assistOptions.argumentPrefixes,
49 assistOptions.argumentSuffixes,
51 getNameScanner(compilerOptions),
54 public static void suggestFieldNames(IJavaProject javaProject, char[] packageName, char[] qualifiedTypeName, int dim, int modifiers, char[][] excludedNames, INamingRequestor requestor) {
55 boolean isStatic = Flags.isStatic(modifiers);
57 Map options = javaProject.getOptions(true);
58 CompilerOptions compilerOptions = new CompilerOptions(options);
59 AssistOptions assistOptions = new AssistOptions(options);
65 isStatic ? assistOptions.staticFieldPrefixes : assistOptions.fieldPrefixes,
66 isStatic ? assistOptions.staticFieldSuffixes : assistOptions.fieldSuffixes,
68 getNameScanner(compilerOptions),
71 public static void suggestLocalVariableNames(IJavaProject javaProject, char[] packageName, char[] qualifiedTypeName, int dim, char[][] excludedNames, INamingRequestor requestor) {
72 Map options = javaProject.getOptions(true);
73 CompilerOptions compilerOptions = new CompilerOptions(options);
74 AssistOptions assistOptions = new AssistOptions(options);
80 assistOptions.localPrefixes,
81 assistOptions.localSuffixes,
83 getNameScanner(compilerOptions),
87 private static void suggestNames(
89 char[] qualifiedTypeName,
93 char[][] excludedNames,
95 INamingRequestor requestor){
97 if(qualifiedTypeName == null || qualifiedTypeName.length == 0)
100 char[] typeName = CharOperation.lastSegment(qualifiedTypeName, '.');
102 if(prefixes == null || prefixes.length == 0) {
103 prefixes = new char[1][0];
105 int length = prefixes.length;
106 System.arraycopy(prefixes, 0, prefixes = new char[length+1][], 0, length);
107 prefixes[length] = CharOperation.NO_CHAR;
110 if(suffixes == null || suffixes.length == 0) {
111 suffixes = new char[1][0];
113 int length = suffixes.length;
114 System.arraycopy(suffixes, 0, suffixes = new char[length+1][], 0, length);
115 suffixes[length] = CharOperation.NO_CHAR;
118 char[][] tempNames = null;
120 // compute variable name for base type
122 // nameScanner.setSource(typeName);
123 // switch (nameScanner.getNextToken()) {
124 // case TerminalTokens.TokenNameint :
125 // case TerminalTokens.TokenNamebyte :
126 // case TerminalTokens.TokenNameshort :
127 // case TerminalTokens.TokenNamechar :
128 // case TerminalTokens.TokenNamelong :
129 // case TerminalTokens.TokenNamefloat :
130 // case TerminalTokens.TokenNamedouble :
131 // case TerminalTokens.TokenNameboolean :
132 // char[] name = computeBaseTypeNames(typeName[0], excludedNames);
133 // if(name != null) {
134 // tempNames = new char[][]{name};
138 // } catch(InvalidInputException e){
142 // compute variable name for non base type
143 if(tempNames == null) {
144 tempNames = computeNames(typeName);
147 boolean acceptDefaultName = true;
149 for (int i = 0; i < tempNames.length; i++) {
150 char[] tempName = tempNames[i];
152 int length = tempName.length;
153 if (tempName[length-1] == 's'){
154 if(tempName.length > 1 && tempName[length-2] == 's') {
155 System.arraycopy(tempName, 0, tempName = new char[length + 2], 0, length);
156 tempName[length] = 'e';
157 tempName[length+1] = 's';
159 } else if(tempName[length-1] == 'y') {
160 System.arraycopy(tempName, 0, tempName = new char[length + 2], 0, length);
161 tempName[length-1] = 'i';
162 tempName[length] = 'e';
163 tempName[length+1] = 's';
165 System.arraycopy(tempName, 0, tempName = new char[length + 1], 0, length);
166 tempName[length] = 's';
170 for (int j = 0; j < prefixes.length; j++) {
171 if(prefixes[j].length > 0
172 && Character.isLetterOrDigit(prefixes[j][prefixes[j].length - 1])) {
173 tempName[0] = Character.toUpperCase(tempName[0]);
175 tempName[0] = Character.toLowerCase(tempName[0]);
177 char[] prefixName = CharOperation.concat(prefixes[j], tempName);
178 for (int k = 0; k < suffixes.length; k++) {
179 char[] suffixName = CharOperation.concat(prefixName, suffixes[k]);
186 if(JavaConventions.validateFieldName(new String(suffixName)).isOK()) {
187 acceptName(suffixName, prefixes[j], suffixes[k], j == 0, k == 0, requestor);
188 acceptDefaultName = false;
190 suffixName = CharOperation.concat(
192 String.valueOf(1).toCharArray(),
201 if(JavaConventions.validateFieldName(new String(suffixName)).isOK()) {
202 acceptName(suffixName, prefixes[j], suffixes[k], j == 0, k == 0, requestor);
203 acceptDefaultName = false;
210 // if no names were found
211 if(acceptDefaultName) {
212 char[] name = excludeNames(DEFAULT_NAME, DEFAULT_NAME, CharOperation.NO_CHAR, excludedNames);
213 requestor.acceptNameWithoutPrefixAndSuffix(name);
217 private static void acceptName(
221 boolean isFirstPrefix,
222 boolean isFirstSuffix,
223 INamingRequestor requestor) {
224 if(prefix.length > 0 && suffix.length > 0) {
225 requestor.acceptNameWithPrefixAndSuffix(name, isFirstPrefix, isFirstSuffix);
226 } else if(prefix.length > 0){
227 requestor.acceptNameWithPrefix(name, isFirstPrefix);
228 } else if(suffix.length > 0){
229 requestor.acceptNameWithSuffix(name, isFirstSuffix);
231 requestor.acceptNameWithoutPrefixAndSuffix(name);
235 private static char[] computeBaseTypeNames(char firstName, char[][] excludedNames){
236 char[] name = new char[]{firstName};
238 for(int i = 0 ; i < excludedNames.length ; i++){
239 if(CharOperation.equals(name, excludedNames[i], false)) {
243 if(name[0] == firstName)
252 private static char[][] computeNames(char[] sourceName){
253 char[][] names = new char[5][];
255 boolean previousIsUpperCase = false;
256 boolean previousIsLetter = true;
257 for(int i = sourceName.length - 1 ; i >= 0 ; i--){
258 boolean isUpperCase = Character.isUpperCase(sourceName[i]);
259 boolean isLetter = Character.isLetter(sourceName[i]);
260 if(isUpperCase && !previousIsUpperCase && previousIsLetter){
261 char[] name = CharOperation.subarray(sourceName,i,sourceName.length);
263 if(nameCount == names.length) {
264 System.arraycopy(names, 0, names = new char[nameCount * 2][], 0, nameCount);
266 name[0] = Character.toLowerCase(name[0]);
267 names[nameCount++] = name;
270 previousIsUpperCase = isUpperCase;
271 previousIsLetter = isLetter;
274 names[nameCount++] = CharOperation.toLowerCase(sourceName);
276 System.arraycopy(names, 0, names = new char[nameCount][], 0, nameCount);
280 private static char[] excludeNames(
284 char[][] excludedNames) {
287 while (m < excludedNames.length) {
288 if(CharOperation.equals(suffixName, excludedNames[m], false)) {
289 suffixName = CharOperation.concat(
291 String.valueOf(count++).toCharArray(),