### Eclipse Workspace Patch 1.0 #P org.eclipse.jdt.core Index: model/org/eclipse/jdt/internal/core/InternalNamingConventions.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/InternalNamingConventions.java,v retrieving revision 1.21 diff -u -r1.21 InternalNamingConventions.java --- model/org/eclipse/jdt/internal/core/InternalNamingConventions.java 27 Jun 2008 16:03:50 -0000 1.21 +++ model/org/eclipse/jdt/internal/core/InternalNamingConventions.java 17 Oct 2008 11:27:18 -0000 @@ -12,8 +12,8 @@ import java.util.Map; -import org.eclipse.jdt.core.Flags; import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.compiler.CharOperation; import org.eclipse.jdt.core.compiler.InvalidInputException; import org.eclipse.jdt.internal.codeassist.impl.AssistOptions; @@ -37,227 +37,6 @@ null/*taskPriorities*/, true/*taskCaseSensitive*/); } - public static void suggestArgumentNames(IJavaProject javaProject, char[] packageName, char[] qualifiedTypeName, int dim, char[] internalPrefix, char[][] excludedNames, INamingRequestor requestor) { - Map options = javaProject.getOptions(true); - CompilerOptions compilerOptions = new CompilerOptions(options); - AssistOptions assistOptions = new AssistOptions(options); - - suggestNames( - packageName, - qualifiedTypeName, - dim, - internalPrefix, - assistOptions.argumentPrefixes, - assistOptions.argumentSuffixes, - excludedNames, - getNameScanner(compilerOptions), - requestor); - } - public static void suggestFieldNames(IJavaProject javaProject, char[] packageName, char[] qualifiedTypeName, int dim, int modifiers, char[] internalPrefix, char[][] excludedNames, INamingRequestor requestor) { - boolean isStatic = Flags.isStatic(modifiers); - - Map options = javaProject.getOptions(true); - CompilerOptions compilerOptions = new CompilerOptions(options); - AssistOptions assistOptions = new AssistOptions(options); - - suggestNames( - packageName, - qualifiedTypeName, - dim, - internalPrefix, - isStatic ? assistOptions.staticFieldPrefixes : assistOptions.fieldPrefixes, - isStatic ? assistOptions.staticFieldSuffixes : assistOptions.fieldSuffixes, - excludedNames, - getNameScanner(compilerOptions), - requestor); - } - public static void suggestLocalVariableNames(IJavaProject javaProject, char[] packageName, char[] qualifiedTypeName, int dim, char[] internalPrefix, char[][] excludedNames, INamingRequestor requestor) { - Map options = javaProject.getOptions(true); - CompilerOptions compilerOptions = new CompilerOptions(options); - AssistOptions assistOptions = new AssistOptions(options); - - suggestNames( - packageName, - qualifiedTypeName, - dim, - internalPrefix, - assistOptions.localPrefixes, - assistOptions.localSuffixes, - excludedNames, - getNameScanner(compilerOptions), - requestor); - } - - private static void suggestNames( - char[] packageName, - char[] qualifiedTypeName, - int dim, - char[] internalPrefix, - char[][] prefixes, - char[][] suffixes, - char[][] excludedNames, - Scanner nameScanner, - INamingRequestor requestor){ - - if(qualifiedTypeName == null || qualifiedTypeName.length == 0) - return; - - if(internalPrefix == null) { - internalPrefix = CharOperation.NO_CHAR; - } else { - internalPrefix = removePrefix(internalPrefix, prefixes); - } - - char[] typeName = CharOperation.lastSegment(qualifiedTypeName, '.'); - - if(prefixes == null || prefixes.length == 0) { - prefixes = new char[1][0]; - } else { - int length = prefixes.length; - System.arraycopy(prefixes, 0, prefixes = new char[length+1][], 0, length); - prefixes[length] = CharOperation.NO_CHAR; - } - - if(suffixes == null || suffixes.length == 0) { - suffixes = new char[1][0]; - } else { - int length = suffixes.length; - System.arraycopy(suffixes, 0, suffixes = new char[length+1][], 0, length); - suffixes[length] = CharOperation.NO_CHAR; - } - - char[][] tempNames = null; - - // compute variable name for base type - try{ - nameScanner.setSource(typeName); - switch (nameScanner.getNextToken()) { - case TerminalTokens.TokenNameint : - case TerminalTokens.TokenNamebyte : - case TerminalTokens.TokenNameshort : - case TerminalTokens.TokenNamechar : - case TerminalTokens.TokenNamelong : - case TerminalTokens.TokenNamefloat : - case TerminalTokens.TokenNamedouble : - case TerminalTokens.TokenNameboolean : - - if (internalPrefix != null && internalPrefix.length > 0) return; - - char[] name = computeBaseTypeNames(typeName[0], excludedNames); - if(name != null) { - tempNames = new char[][]{name}; - } - break; - } - } catch(InvalidInputException e){ - // ignore - } - - // compute variable name for non base type - if(tempNames == null) { - tempNames = computeNames(typeName); - } - - boolean acceptDefaultName = true; - SimpleSetOfCharArray foundNames = new SimpleSetOfCharArray(); - - next : for (int i = 0; i < tempNames.length; i++) { - char[] tempName = tempNames[i]; - if(dim > 0) { - int length = tempName.length; - if (tempName[length-1] == 's'){ - if(tempName.length > 1 && tempName[length-2] == 's') { - System.arraycopy(tempName, 0, tempName = new char[length + 2], 0, length); - tempName[length] = 'e'; - tempName[length+1] = 's'; - } - } else if(tempName[length-1] == 'y') { - System.arraycopy(tempName, 0, tempName = new char[length + 2], 0, length); - tempName[length-1] = 'i'; - tempName[length] = 'e'; - tempName[length+1] = 's'; - } else { - System.arraycopy(tempName, 0, tempName = new char[length + 1], 0, length); - tempName[length] = 's'; - } - } - - char[] unprefixedName = tempName; - unprefixedName[0] = ScannerHelper.toUpperCase(unprefixedName[0]); - for (int j = 0; j <= internalPrefix.length; j++) { - if(j == internalPrefix.length || - CharOperation.prefixEquals(CharOperation.subarray(internalPrefix, j, -1), unprefixedName, j != 0 /*do not check case when there is no prefix*/)) { - tempName = CharOperation.concat(CharOperation.subarray(internalPrefix, 0, j), unprefixedName); - if(j == 0) tempName[0] = ScannerHelper.toLowerCase(tempName[0]); - for (int k = 0; k < prefixes.length; k++) { - if(prefixes[k].length > 0 - && ScannerHelper.isLetterOrDigit(prefixes[k][prefixes[k].length - 1])) { - tempName[0] = ScannerHelper.toUpperCase(tempName[0]); - } else { - tempName[0] = ScannerHelper.toLowerCase(tempName[0]); - } - char[] prefixName = CharOperation.concat(prefixes[k], tempName); - for (int l = 0; l < suffixes.length; l++) { - char[] suffixName = CharOperation.concat(prefixName, suffixes[l]); - suffixName = - excludeNames( - suffixName, - prefixName, - suffixes[l], - excludedNames); - try{ - nameScanner.setSource(suffixName); - switch (nameScanner.getNextToken()) { - case TerminalTokens.TokenNameIdentifier : - int token = nameScanner.getNextToken(); - if (token == TerminalTokens.TokenNameEOF && nameScanner.startPosition == suffixName.length) { - if (!foundNames.includes(suffixName)) { - acceptName(suffixName, prefixes[k], suffixes[l], k == 0, l == 0, internalPrefix.length - j, requestor); - foundNames.add(suffixName); - acceptDefaultName = false; - } - } - break; - default: - suffixName = CharOperation.concat( - prefixName, - String.valueOf(1).toCharArray(), - suffixes[l] - ); - suffixName = - excludeNames( - suffixName, - prefixName, - suffixes[l], - excludedNames); - nameScanner.setSource(suffixName); - switch (nameScanner.getNextToken()) { - case TerminalTokens.TokenNameIdentifier : - token = nameScanner.getNextToken(); - if (token == TerminalTokens.TokenNameEOF && nameScanner.startPosition == suffixName.length) { - if (!foundNames.includes(suffixName)) { - acceptName(suffixName, prefixes[k], suffixes[l], k == 0, l == 0, internalPrefix.length - j, requestor); - foundNames.add(suffixName); - acceptDefaultName = false; - } - } - } - } - } catch(InvalidInputException e){ - // ignore - } - } - } - continue next; - } - } - } - // if no names were found - if(acceptDefaultName) { - char[] name = excludeNames(DEFAULT_NAME, DEFAULT_NAME, CharOperation.NO_CHAR, excludedNames); - requestor.acceptNameWithoutPrefixAndSuffix(name, 0); - } - } private static void acceptName( char[] name, @@ -278,6 +57,15 @@ } } + private static char[][] computeBaseTypeNames(char[] typeName, boolean isConstantField, char[][] excludedNames){ + char[] name = computeBaseTypeNames(typeName[0], excludedNames); + if(name != null) { + return new char[][]{name}; + } else { + // compute variable name like from non base type + return computeNonBaseTypeNames(typeName, isConstantField); + } + } private static char[] computeBaseTypeNames(char firstName, char[][] excludedNames){ char[] name = new char[]{firstName}; @@ -294,34 +82,111 @@ return name; } - - private static char[][] computeNames(char[] sourceName){ - char[][] names = new char[5][]; - int nameCount = 0; - boolean previousIsUpperCase = false; - boolean previousIsLetter = true; - for(int i = sourceName.length - 1 ; i >= 0 ; i--){ - boolean isUpperCase = ScannerHelper.isUpperCase(sourceName[i]); - boolean isLetter = ScannerHelper.isLetter(sourceName[i]); - if(isUpperCase && !previousIsUpperCase && previousIsLetter){ - char[] name = CharOperation.subarray(sourceName,i,sourceName.length); - if(name.length > 1){ - if(nameCount == names.length) { - System.arraycopy(names, 0, names = new char[nameCount * 2][], 0, nameCount); - } - name[0] = ScannerHelper.toLowerCase(name[0]); - names[nameCount++] = name; - } + + private static char[][] computeNonBaseTypeNames(char[] sourceName, boolean isConstantField){ + int length = sourceName.length; + + if (length == 0) { + return CharOperation.NO_CHAR_CHAR; + } + + if (length == 1) { + return new char[][]{CharOperation.toLowerCase(sourceName)}; + } + + char[][] nameParts = new char[length][]; + int namePartsPtr = -1; + + int endIndex = length; + char c = sourceName[length - 1]; + + final int IS_LOWER_CASE = 1; + final int IS_UPPER_CASE = 2; + final int IS_UNDERSCORE = 3; + final int IS_OTHER = 4; + + int previousCharKind = + ScannerHelper.isLowerCase(c) ? IS_LOWER_CASE : + ScannerHelper.isUpperCase(c) ? IS_UPPER_CASE : + c == '_' ? IS_UNDERSCORE : IS_OTHER; + + for(int i = length - 1 ; i >= 0 ; i--){ + c = sourceName[i]; + + int charKind = + ScannerHelper.isLowerCase(c) ? IS_LOWER_CASE : + ScannerHelper.isUpperCase(c) ? IS_UPPER_CASE : + c == '_' ? IS_UNDERSCORE : IS_OTHER; + + switch (charKind) { + case IS_LOWER_CASE: + if (previousCharKind == IS_UPPER_CASE) { + nameParts[++namePartsPtr] = CharOperation.subarray(sourceName, i + 1, endIndex); + endIndex = i + 1; + } + previousCharKind = IS_LOWER_CASE; + break; + case IS_UPPER_CASE: + if (previousCharKind == IS_LOWER_CASE) { + nameParts[++namePartsPtr] = CharOperation.subarray(sourceName, i, endIndex); + if (i > 0) { + char pc = sourceName[i - 1]; + previousCharKind = + ScannerHelper.isLowerCase(pc) ? IS_LOWER_CASE : + ScannerHelper.isUpperCase(pc) ? IS_UPPER_CASE : + pc == '_' ? IS_UNDERSCORE : IS_OTHER; + } + endIndex = i; + } else { + previousCharKind = IS_UPPER_CASE; + } + break; + case IS_UNDERSCORE: + switch (previousCharKind) { + case IS_UNDERSCORE: + if (i > 0) { + char pc = sourceName[i - 1]; + previousCharKind = + ScannerHelper.isLowerCase(pc) ? IS_LOWER_CASE : + ScannerHelper.isUpperCase(pc) ? IS_UPPER_CASE : + pc == '_' ? IS_UNDERSCORE : IS_OTHER; + } + endIndex = i; + break; + case IS_LOWER_CASE: + case IS_UPPER_CASE: + nameParts[++namePartsPtr] = CharOperation.subarray(sourceName, i + 1, endIndex); + if (i > 0) { + char pc = sourceName[i - 1]; + previousCharKind = + ScannerHelper.isLowerCase(pc) ? IS_LOWER_CASE : + ScannerHelper.isUpperCase(pc) ? IS_UPPER_CASE : + pc == '_' ? IS_UNDERSCORE : IS_OTHER; + } + endIndex = i; + break; + default: + previousCharKind = IS_UNDERSCORE; + break; + } + break; + default: + previousCharKind = IS_OTHER; + break; } - previousIsUpperCase = isUpperCase; - previousIsLetter = isLetter; } - if(nameCount == 0){ - names[nameCount++] = CharOperation.toLowerCase(sourceName); + if (endIndex > 0) { + nameParts[++namePartsPtr] = CharOperation.subarray(sourceName, 0, endIndex); + } + + if (isConstantField) { + return generateConstantName(nameParts, namePartsPtr); + } else { + return generateNonConstantName(nameParts, namePartsPtr); } - System.arraycopy(names, 0, names = new char[nameCount][], 0, nameCount); - return names; } + + private static char[] excludeNames( char[] suffixName, @@ -344,6 +209,204 @@ } return suffixName; } + + private static char[][] generateNonConstantName(char[][] nameParts, int namePartsPtr) { + char[][] names = new char[namePartsPtr + 1][]; + + char[] namePart = CharOperation.toLowerCase(nameParts[0]); + int namePartLength = namePart.length; + System.arraycopy(namePart, 0, namePart, 0, namePartLength); + + char[] name = namePart; + + names[namePartsPtr] = name; + + for (int i = 1; i <= namePartsPtr; i++) { + namePart = CharOperation.toLowerCase(nameParts[i]); + namePartLength = namePart.length; + name = CharOperation.concat(namePart, name); + name[namePartLength] = ScannerHelper.toUpperCase(name[namePartLength]); + + names[namePartsPtr - i] = name; + } + return names; + } + + private static char[][] generateConstantName(char[][] nameParts, int namePartsPtr) { + char[][] names = new char[namePartsPtr + 1][]; + + char[] namePart = CharOperation.toUpperCase(nameParts[0]); + int namePartLength = namePart.length; + System.arraycopy(namePart, 0, namePart, 0, namePartLength); + + char[] name = namePart; + + names[namePartsPtr] = name; + + for (int i = 1; i <= namePartsPtr; i++) { + namePart = CharOperation.toUpperCase(nameParts[i]); + namePartLength = namePart.length; + if (namePart[namePartLength - 1] != '_') { + name = CharOperation.concat(namePart, name, '_'); + } else { + name = CharOperation.concat(namePart, name); + } + + names[namePartsPtr - i] = name; + } + return names; + } + + public static char[] getBaseName( + int variableKind, + IJavaProject javaProject, + char[] name) { + + AssistOptions assistOptions; + if (javaProject != null) { + assistOptions = new AssistOptions(javaProject.getOptions(true)); + } else { + assistOptions = new AssistOptions(JavaCore.getOptions()); + } + + char[][] prefixes = null; + char[][] suffixes = null; + switch (variableKind) { + case VK_INSTANCE_FIELD: + prefixes = assistOptions.fieldPrefixes; + suffixes = assistOptions.fieldSuffixes; + break; + case VK_STATIC_FIELD: + prefixes = assistOptions.staticFieldPrefixes; + suffixes = assistOptions.staticFieldSuffixes; + break; + case VK_CONSTANT_FIELD: + prefixes = assistOptions.staticFinalFieldPrefixes; + suffixes = assistOptions.staticFinalFieldSuffixes; + break; + case VK_LOCAL: + prefixes = assistOptions.localPrefixes; + suffixes = assistOptions.localSuffixes; + break; + case VK_PARAMETER: + prefixes = assistOptions.argumentPrefixes; + suffixes = assistOptions.argumentSuffixes; + break; + } + + + return getBaseName(name, prefixes, suffixes, variableKind == VK_CONSTANT_FIELD); + } + + private static char[] getBaseName(char[] name, char[][] prefixes, char[][] suffixes, boolean isConstant) { + char[] nameWithoutPrefixAndSiffix = removeVariablePrefixAndSuffix(name, prefixes, suffixes, false); + + char[] baseName; + if (isConstant) { + int length = nameWithoutPrefixAndSiffix.length; + baseName = new char[length]; + int baseNamePtr = -1; + + boolean previousIsUnderscore = false; + for (int i = 0; i < length; i++) { + char c = nameWithoutPrefixAndSiffix[i]; + if (c != '_') { + if (previousIsUnderscore || i == 0) { + baseName[++baseNamePtr] = ScannerHelper.toUpperCase(c); + previousIsUnderscore = false; + } else { + baseName[++baseNamePtr] = ScannerHelper.toLowerCase(c); + } + } else { + previousIsUnderscore = true; + } + } + System.arraycopy(baseName, 0, baseName = new char[baseNamePtr + 1], 0, baseNamePtr + 1); + } else { + baseName = nameWithoutPrefixAndSiffix; + } + + return baseName; + } + + public static char[] removeVariablePrefixAndSuffix( + int variableKind, + IJavaProject javaProject, + char[] name) { + AssistOptions assistOptions; + if (javaProject != null) { + assistOptions = new AssistOptions(javaProject.getOptions(true)); + } else { + assistOptions = new AssistOptions(JavaCore.getOptions()); + } + + char[][] prefixes = null; + char[][] suffixes = null; + switch (variableKind) { + case VK_INSTANCE_FIELD: + prefixes = assistOptions.fieldPrefixes; + suffixes = assistOptions.fieldSuffixes; + break; + case VK_STATIC_FIELD: + prefixes = assistOptions.staticFieldPrefixes; + suffixes = assistOptions.staticFieldSuffixes; + break; + case VK_CONSTANT_FIELD: + prefixes = assistOptions.staticFinalFieldPrefixes; + suffixes = assistOptions.staticFinalFieldSuffixes; + break; + case VK_LOCAL: + prefixes = assistOptions.localPrefixes; + suffixes = assistOptions.localSuffixes; + break; + case VK_PARAMETER: + prefixes = assistOptions.argumentPrefixes; + suffixes = assistOptions.argumentSuffixes; + break; + } + + return InternalNamingConventions.removeVariablePrefixAndSuffix(name, prefixes, suffixes, true); + } + + private static char[] removeVariablePrefixAndSuffix(char[] name, char[][] prefixes, char[][] suffixes, boolean updateFirstCharacter) { + // remove longer prefix + char[] withoutPrefixName = name; + if (prefixes != null) { + int bestLength = 0; + for (int i= 0; i < prefixes.length; i++) { + char[] prefix = prefixes[i]; + if (CharOperation.prefixEquals(prefix, name)) { + int currLen = prefix.length; + boolean lastCharIsLetter = ScannerHelper.isLetter(prefix[currLen - 1]); + if(!lastCharIsLetter || (lastCharIsLetter && name.length > currLen && ScannerHelper.isUpperCase(name[currLen]))) { + if (bestLength < currLen && name.length != currLen) { + withoutPrefixName = CharOperation.subarray(name, currLen, name.length); + bestLength = currLen; + } + } + } + } + } + + // remove longer suffix + char[] withoutSuffixName = withoutPrefixName; + if(suffixes != null) { + int bestLength = 0; + for (int i = 0; i < suffixes.length; i++) { + char[] suffix = suffixes[i]; + if(CharOperation.endsWith(withoutPrefixName, suffix)) { + int currLen = suffix.length; + if(bestLength < currLen && withoutPrefixName.length != currLen) { + withoutSuffixName = CharOperation.subarray(withoutPrefixName, 0, withoutPrefixName.length - currLen); + bestLength = currLen; + } + } + } + } + + if (updateFirstCharacter) withoutSuffixName[0] = ScannerHelper.toLowerCase(withoutSuffixName[0]); + return withoutSuffixName; + } private static char[] removePrefix(char[] name, char[][] prefixes) { // remove longer prefix @@ -430,4 +493,277 @@ return false; return true; } + + public static final int VK_STATIC_FIELD = 1; + public static final int VK_INSTANCE_FIELD = 2; + public static final int VK_CONSTANT_FIELD = 3; + public static final int VK_PARAMETER = 4; + public static final int VK_LOCAL = 5; + + public static final int BK_SIMPLE_NAME = 1; + public static final int BK_SIMPLE_TYPE_NAME = 2; + + public static void suggestVariableNames( + int variableKind, + int baseNameKind, + char[] baseName, + IJavaProject javaProject, + int dim, + char[] internalPrefix, + char[][] excluded, + boolean evaluateDefault, + INamingRequestor requestor) { + + if(baseName == null || baseName.length == 0) + return; + + Map options; + if (javaProject != null) { + options = javaProject.getOptions(true); + } else { + options = JavaCore.getOptions(); + } + CompilerOptions compilerOptions = new CompilerOptions(options); + AssistOptions assistOptions = new AssistOptions(options); + + boolean isConstantField = false; + + char[][] prefixes = null; + char[][] suffixes = null; + switch (variableKind) { + case VK_INSTANCE_FIELD: + prefixes = assistOptions.fieldPrefixes; + suffixes = assistOptions.fieldSuffixes; + break; + case VK_STATIC_FIELD: + prefixes = assistOptions.staticFieldPrefixes; + suffixes = assistOptions.staticFieldSuffixes; + break; + case VK_CONSTANT_FIELD: + isConstantField = true; + prefixes = assistOptions.staticFinalFieldPrefixes; + suffixes = assistOptions.staticFinalFieldSuffixes; + break; + case VK_LOCAL: + prefixes = assistOptions.localPrefixes; + suffixes = assistOptions.localSuffixes; + break; + case VK_PARAMETER: + prefixes = assistOptions.argumentPrefixes; + suffixes = assistOptions.argumentSuffixes; + break; + } + + if(prefixes == null || prefixes.length == 0) { + prefixes = new char[1][0]; + } else { + int length = prefixes.length; + System.arraycopy(prefixes, 0, prefixes = new char[length+1][], 0, length); + prefixes[length] = CharOperation.NO_CHAR; + } + + if(suffixes == null || suffixes.length == 0) { + suffixes = new char[1][0]; + } else { + int length = suffixes.length; + System.arraycopy(suffixes, 0, suffixes = new char[length+1][], 0, length); + suffixes[length] = CharOperation.NO_CHAR; + } + + if(internalPrefix == null) { + internalPrefix = CharOperation.NO_CHAR; + } else { + internalPrefix = removePrefix(internalPrefix, prefixes); + } + + char[][] tempNames = null; + + Scanner nameScanner = getNameScanner(compilerOptions); + if (baseNameKind == BK_SIMPLE_TYPE_NAME) { + boolean isBaseType = false; + + try{ + nameScanner.setSource(baseName); + switch (nameScanner.getNextToken()) { + case TerminalTokens.TokenNameint : + case TerminalTokens.TokenNamebyte : + case TerminalTokens.TokenNameshort : + case TerminalTokens.TokenNamechar : + case TerminalTokens.TokenNamelong : + case TerminalTokens.TokenNamefloat : + case TerminalTokens.TokenNamedouble : + case TerminalTokens.TokenNameboolean : + isBaseType = true; + break; + } + } catch(InvalidInputException e){ + // ignore + } + if (isBaseType) { + // compute variable name from base type + if (internalPrefix.length > 0) return; + + tempNames = computeBaseTypeNames(baseName, isConstantField, excluded); + } else { + // compute variable name for non base type + tempNames = computeNonBaseTypeNames(baseName, isConstantField); + } + } else { + tempNames = new char[][]{baseName}; + } + + boolean acceptDefaultName = true; + SimpleSetOfCharArray foundNames = new SimpleSetOfCharArray(); + + for (int i = 0; i < tempNames.length; i++) { + char[] tempName = tempNames[i]; + + // add English plural form is necessary + if(dim > 0) { + int length = tempName.length; + + if (isConstantField) { + if (tempName[length-1] == 'S'){ + if(tempName.length > 1 && tempName[length-2] == 'S') { + System.arraycopy(tempName, 0, tempName = new char[length + 2], 0, length); + tempName[length] = 'E'; + tempName[length+1] = 'S'; + } + } else if(tempName[length-1] == 'Y') { + System.arraycopy(tempName, 0, tempName = new char[length + 2], 0, length); + tempName[length-1] = 'I'; + tempName[length] = 'E'; + tempName[length+1] = 'S'; + } else { + System.arraycopy(tempName, 0, tempName = new char[length + 1], 0, length); + tempName[length] = 'S'; + } + } else { + if (tempName[length-1] == 's'){ + if(tempName.length > 1 && tempName[length-2] == 's') { + System.arraycopy(tempName, 0, tempName = new char[length + 2], 0, length); + tempName[length] = 'e'; + tempName[length+1] = 's'; + } + } else if(tempName[length-1] == 'y') { + System.arraycopy(tempName, 0, tempName = new char[length + 2], 0, length); + tempName[length-1] = 'i'; + tempName[length] = 'e'; + tempName[length+1] = 's'; + } else { + System.arraycopy(tempName, 0, tempName = new char[length + 1], 0, length); + tempName[length] = 's'; + } + } + } + + char[] unprefixedName = tempName; + + int matchingIndex = -1; + if (!isConstantField) { + unprefixedName[0] = ScannerHelper.toUpperCase(unprefixedName[0]); + + done : for (int j = 0; j <= internalPrefix.length; j++) { + if(j == internalPrefix.length || + CharOperation.prefixEquals(CharOperation.subarray(internalPrefix, j, -1), unprefixedName, j != 0 /*do not check case when there is no prefix*/)) { + matchingIndex = j; + break done; + } + } + } else { + done : for (int j = 0; j <= internalPrefix.length; j++) { + if(j == internalPrefix.length) { + matchingIndex = j; + break done; + } else if(CharOperation.prefixEquals(CharOperation.subarray(internalPrefix, j, -1), unprefixedName, j != 0 /*do not check case when there is no prefix*/)) { + if (j == 0 || internalPrefix[j - 1] == '_') { + matchingIndex = j; + break done; + } + + } + } + } + + if(matchingIndex > -1) { + if (!isConstantField) { + tempName = CharOperation.concat(CharOperation.subarray(internalPrefix, 0, matchingIndex), unprefixedName); + if(matchingIndex == 0) tempName[0] = ScannerHelper.toLowerCase(tempName[0]); + } else { + if(matchingIndex != 0 && tempName[0] != '_' && internalPrefix[matchingIndex - 1] != '_') { + tempName = CharOperation.concat(CharOperation.subarray(CharOperation.toUpperCase(internalPrefix), 0, matchingIndex), unprefixedName, '_'); + } else { + tempName = CharOperation.concat(CharOperation.subarray(CharOperation.toUpperCase(internalPrefix), 0, matchingIndex), unprefixedName); + } + } + + for (int k = 0; k < prefixes.length; k++) { + if (!isConstantField) { + if(prefixes[k].length > 0 + && ScannerHelper.isLetterOrDigit(prefixes[k][prefixes[k].length - 1])) { + tempName[0] = ScannerHelper.toUpperCase(tempName[0]); + } else { + tempName[0] = ScannerHelper.toLowerCase(tempName[0]); + } + } + char[] prefixName = CharOperation.concat(prefixes[k], tempName); + for (int l = 0; l < suffixes.length; l++) { + char[] suffixName = CharOperation.concat(prefixName, suffixes[l]); + suffixName = + excludeNames( + suffixName, + prefixName, + suffixes[l], + excluded); + try{ + nameScanner.setSource(suffixName); + switch (nameScanner.getNextToken()) { + case TerminalTokens.TokenNameIdentifier : + int token = nameScanner.getNextToken(); + if (token == TerminalTokens.TokenNameEOF && nameScanner.startPosition == suffixName.length) { + if (!foundNames.includes(suffixName)) { + acceptName(suffixName, prefixes[k], suffixes[l], k == 0, l == 0, internalPrefix.length - matchingIndex, requestor); + foundNames.add(suffixName); + acceptDefaultName = false; + } + } + break; + default: + suffixName = CharOperation.concat( + prefixName, + String.valueOf(1).toCharArray(), + suffixes[l] + ); + suffixName = + excludeNames( + suffixName, + prefixName, + suffixes[l], + excluded); + nameScanner.setSource(suffixName); + switch (nameScanner.getNextToken()) { + case TerminalTokens.TokenNameIdentifier : + token = nameScanner.getNextToken(); + if (token == TerminalTokens.TokenNameEOF && nameScanner.startPosition == suffixName.length) { + if (!foundNames.includes(suffixName)) { + acceptName(suffixName, prefixes[k], suffixes[l], k == 0, l == 0, internalPrefix.length - matchingIndex, requestor); + foundNames.add(suffixName); + acceptDefaultName = false; + } + } + } + } + } catch(InvalidInputException e){ + // ignore + } + } + } + } + } + // if no names were found + if(evaluateDefault && acceptDefaultName) { + char[] name = excludeNames(DEFAULT_NAME, DEFAULT_NAME, CharOperation.NO_CHAR, excluded); + requestor.acceptNameWithoutPrefixAndSuffix(name, 0); + } + } } Index: model/org/eclipse/jdt/internal/core/JavaCorePreferenceInitializer.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/JavaCorePreferenceInitializer.java,v retrieving revision 1.41 diff -u -r1.41 JavaCorePreferenceInitializer.java --- model/org/eclipse/jdt/internal/core/JavaCorePreferenceInitializer.java 8 Oct 2008 11:58:47 -0000 1.41 +++ model/org/eclipse/jdt/internal/core/JavaCorePreferenceInitializer.java 17 Oct 2008 11:27:18 -0000 @@ -77,10 +77,12 @@ defaultOptionsMap.put(JavaCore.CODEASSIST_IMPLICIT_QUALIFICATION, JavaCore.DISABLED); defaultOptionsMap.put(JavaCore.CODEASSIST_FIELD_PREFIXES, ""); //$NON-NLS-1$ defaultOptionsMap.put(JavaCore.CODEASSIST_STATIC_FIELD_PREFIXES, ""); //$NON-NLS-1$ + defaultOptionsMap.put(JavaCore.CODEASSIST_STATIC_FINAL_FIELD_PREFIXES, ""); //$NON-NLS-1$ defaultOptionsMap.put(JavaCore.CODEASSIST_LOCAL_PREFIXES, ""); //$NON-NLS-1$ defaultOptionsMap.put(JavaCore.CODEASSIST_ARGUMENT_PREFIXES, ""); //$NON-NLS-1$ defaultOptionsMap.put(JavaCore.CODEASSIST_FIELD_SUFFIXES, ""); //$NON-NLS-1$ defaultOptionsMap.put(JavaCore.CODEASSIST_STATIC_FIELD_SUFFIXES, ""); //$NON-NLS-1$ + defaultOptionsMap.put(JavaCore.CODEASSIST_STATIC_FINAL_FIELD_SUFFIXES, ""); //$NON-NLS-1$ defaultOptionsMap.put(JavaCore.CODEASSIST_LOCAL_SUFFIXES, ""); //$NON-NLS-1$ defaultOptionsMap.put(JavaCore.CODEASSIST_ARGUMENT_SUFFIXES, ""); //$NON-NLS-1$ defaultOptionsMap.put(JavaCore.CODEASSIST_FORBIDDEN_REFERENCE_CHECK, JavaCore.ENABLED); Index: model/org/eclipse/jdt/internal/core/JavaModelManager.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/JavaModelManager.java,v retrieving revision 1.416 diff -u -r1.416 JavaModelManager.java --- model/org/eclipse/jdt/internal/core/JavaModelManager.java 13 Oct 2008 13:35:17 -0000 1.416 +++ model/org/eclipse/jdt/internal/core/JavaModelManager.java 17 Oct 2008 11:27:18 -0000 @@ -1965,10 +1965,12 @@ defaultOptionsMap.put(JavaCore.CODEASSIST_IMPLICIT_QUALIFICATION, JavaCore.DISABLED); defaultOptionsMap.put(JavaCore.CODEASSIST_FIELD_PREFIXES, ""); //$NON-NLS-1$ defaultOptionsMap.put(JavaCore.CODEASSIST_STATIC_FIELD_PREFIXES, ""); //$NON-NLS-1$ + defaultOptionsMap.put(JavaCore.CODEASSIST_STATIC_FINAL_FIELD_PREFIXES, ""); //$NON-NLS-1$ defaultOptionsMap.put(JavaCore.CODEASSIST_LOCAL_PREFIXES, ""); //$NON-NLS-1$ defaultOptionsMap.put(JavaCore.CODEASSIST_ARGUMENT_PREFIXES, ""); //$NON-NLS-1$ defaultOptionsMap.put(JavaCore.CODEASSIST_FIELD_SUFFIXES, ""); //$NON-NLS-1$ defaultOptionsMap.put(JavaCore.CODEASSIST_STATIC_FIELD_SUFFIXES, ""); //$NON-NLS-1$ + defaultOptionsMap.put(JavaCore.CODEASSIST_STATIC_FINAL_FIELD_SUFFIXES, ""); //$NON-NLS-1$ defaultOptionsMap.put(JavaCore.CODEASSIST_LOCAL_SUFFIXES, ""); //$NON-NLS-1$ defaultOptionsMap.put(JavaCore.CODEASSIST_ARGUMENT_SUFFIXES, ""); //$NON-NLS-1$ defaultOptionsMap.put(JavaCore.CODEASSIST_FORBIDDEN_REFERENCE_CHECK, JavaCore.ENABLED); Index: model/org/eclipse/jdt/core/JavaCore.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/core/JavaCore.java,v retrieving revision 1.629 diff -u -r1.629 JavaCore.java --- model/org/eclipse/jdt/core/JavaCore.java 10 Oct 2008 17:24:31 -0000 1.629 +++ model/org/eclipse/jdt/core/JavaCore.java 17 Oct 2008 11:27:17 -0000 @@ -1812,6 +1812,19 @@ */ public static final String CODEASSIST_STATIC_FIELD_PREFIXES = PLUGIN_ID + ".codeComplete.staticFieldPrefixes"; //$NON-NLS-1$ /** + * Code assist option ID: Define the Prefixes for Static Final Field Name. + *
When the prefixes is non empty, completion for static final field name will begin with + * one of the proposed prefixes. + *
"org.eclipse.jdt.core.codeComplete.staticFinalFieldPrefixes"
{ "<prefix>[,<prefix>]*" }
where <prefix>
is a String without any wild-card""
When the prefixes is non empty, completion for local variable name will begin with * one of the proposed prefixes. @@ -1864,6 +1877,19 @@ */ public static final String CODEASSIST_STATIC_FIELD_SUFFIXES = PLUGIN_ID + ".codeComplete.staticFieldSuffixes"; //$NON-NLS-1$ /** + * Code assist option ID: Define the Suffixes for Static Final Field Name. + *
When the suffixes is non empty, completion for static final field name will end with + * one of the proposed suffixes. + *
"org.eclipse.jdt.core.codeComplete.staticFinalFieldSuffixes"
{ "<suffix>[,<suffix>]*" }
< where <suffix>
is a String without any wild-card""
When the suffixes is non empty, completion for local variable name will end with * one of the proposed suffixes. Index: model/org/eclipse/jdt/core/NamingConventions.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/core/NamingConventions.java,v retrieving revision 1.37 diff -u -r1.37 NamingConventions.java --- model/org/eclipse/jdt/core/NamingConventions.java 27 Jun 2008 16:04:00 -0000 1.37 +++ model/org/eclipse/jdt/core/NamingConventions.java 17 Oct 2008 11:27:18 -0000 @@ -11,7 +11,6 @@ package org.eclipse.jdt.core; import org.eclipse.jdt.core.compiler.CharOperation; -import org.eclipse.jdt.internal.codeassist.impl.AssistOptions; import org.eclipse.jdt.internal.compiler.parser.ScannerHelper; import org.eclipse.jdt.internal.core.INamingRequestor; import org.eclipse.jdt.internal.core.InternalNamingConventions; @@ -48,10 +47,6 @@ * @noinstantiate This class is not intended to be instantiated by clients. */ public final class NamingConventions { - private static final char[] GETTER_BOOL_NAME = "is".toCharArray(); //$NON-NLS-1$ - private static final char[] GETTER_NAME = "get".toCharArray(); //$NON-NLS-1$ - private static final char[] SETTER_NAME = "set".toCharArray(); //$NON-NLS-1$ - static class NamingRequestor implements INamingRequestor { private final static int SIZE = 10; @@ -80,6 +75,45 @@ // for acceptNameWithoutPrefixAndSuffix private char[][] otherResults = new char[SIZE][]; private int otherResultsCount = 0; + public void acceptNameWithoutPrefixAndSuffix(char[] name, int reusedCharacters) { + int length = this.otherResults.length; + if(length == this.otherResultsCount) { + System.arraycopy( + this.otherResults, + 0, + this.otherResults = new char[length * 2][], + 0, + length); + } + this.otherResults[this.otherResultsCount++] = name; + } + + public void acceptNameWithPrefix(char[] name, boolean isFirstPrefix, int reusedCharacters) { + if(isFirstPrefix) { + int length = this.firstPrefixResults.length; + if(length == this.firstPrefixResultsCount) { + System.arraycopy( + this.firstPrefixResults, + 0, + this.firstPrefixResults = new char[length * 2][], + 0, + length); + } + this.firstPrefixResults[this.firstPrefixResultsCount++] = name; + } else{ + int length = this.prefixResults.length; + if(length == this.prefixResultsCount) { + System.arraycopy( + this.prefixResults, + 0, + this.prefixResults = new char[length * 2][], + 0, + length); + } + this.prefixResults[this.prefixResultsCount++] = name; + } + } + public void acceptNameWithPrefixAndSuffix(char[] name, boolean isFirstPrefix, boolean isFirstSuffix, int reusedCharacters) { if(isFirstPrefix && isFirstSuffix) { int length = this.firstPrefixAndFirstSuffixResults.length; @@ -128,32 +162,6 @@ } } - public void acceptNameWithPrefix(char[] name, boolean isFirstPrefix, int reusedCharacters) { - if(isFirstPrefix) { - int length = this.firstPrefixResults.length; - if(length == this.firstPrefixResultsCount) { - System.arraycopy( - this.firstPrefixResults, - 0, - this.firstPrefixResults = new char[length * 2][], - 0, - length); - } - this.firstPrefixResults[this.firstPrefixResultsCount++] = name; - } else{ - int length = this.prefixResults.length; - if(length == this.prefixResultsCount) { - System.arraycopy( - this.prefixResults, - 0, - this.prefixResults = new char[length * 2][], - 0, - length); - } - this.prefixResults[this.prefixResultsCount++] = name; - } - } - public void acceptNameWithSuffix(char[] name, boolean isFirstSuffix, int reusedCharacters) { if(isFirstSuffix) { int length = this.firstSuffixResults.length; @@ -179,19 +187,6 @@ this.suffixResults[this.suffixResultsCount++] = name; } } - - public void acceptNameWithoutPrefixAndSuffix(char[] name, int reusedCharacters) { - int length = this.otherResults.length; - if(length == this.otherResultsCount) { - System.arraycopy( - this.otherResults, - 0, - this.otherResults = new char[length * 2][], - 0, - length); - } - this.otherResults[this.otherResultsCount++] = name; - } public char[][] getResults(){ int count = this.firstPrefixAndFirstSuffixResultsCount @@ -228,50 +223,82 @@ return results; } } + private static final char[] GETTER_BOOL_NAME = "is".toCharArray(); //$NON-NLS-1$ + private static final char[] GETTER_NAME = "get".toCharArray(); //$NON-NLS-1$ + private static final char[] SETTER_NAME = "set".toCharArray(); //$NON-NLS-1$ - private NamingConventions() { - // Not instantiable - } - private static char[] removePrefixAndSuffix(char[] name, char[][] prefixes, char[][] suffixes) { - // remove longer prefix - char[] withoutPrefixName = name; - if (prefixes != null) { - int bestLength = 0; - for (int i= 0; i < prefixes.length; i++) { - char[] prefix = prefixes[i]; - if (CharOperation.prefixEquals(prefix, name)) { - int currLen = prefix.length; - boolean lastCharIsLetter = ScannerHelper.isLetter(prefix[currLen - 1]); - if(!lastCharIsLetter || (lastCharIsLetter && name.length > currLen && ScannerHelper.isUpperCase(name[currLen]))) { - if (bestLength < currLen && name.length != currLen) { - withoutPrefixName = CharOperation.subarray(name, currLen, name.length); - bestLength = currLen; - } - } - } - } - } + /** + * Variable kind which represents a static field. + * + * @since 3.5 + */ + public static final int VK_STATIC_FIELD = InternalNamingConventions.VK_STATIC_FIELD; + /** + * Variable kind which represents an instance field. + * + * @since 3.5 + */ + public static final int VK_INSTANCE_FIELD = InternalNamingConventions.VK_INSTANCE_FIELD; + /** + * Variable kind which represents a constant field (static final). + * + * @since 3.5 + */ + public static final int VK_CONSTANT_FIELD = InternalNamingConventions.VK_CONSTANT_FIELD; + /** + * Variable kind which represents an argument. + * + * @since 3.5 + */ + public static final int VK_PARAMETER = InternalNamingConventions.VK_PARAMETER; + /** + * Variable kind which represents a local variable. + * + * @since 3.5 + */ + public static final int VK_LOCAL = InternalNamingConventions.VK_LOCAL; + + /** + * The base name associated to this base name kind is a simple name. + * When this base name is used the whole name is considered. + * + * @see #suggestVariableNames(int, int, String, IJavaProject, int, String[], boolean) + * + * @since 3.5 + */ + public static final int BK_NAME = InternalNamingConventions.BK_SIMPLE_NAME; + + /** + * The base name associated to this base name kind is a simple type name. + * When this base name is used to all the words of the name are considered. + * + * @see #suggestVariableNames(int, int, String, IJavaProject, int, String[], boolean) + * + * @since 3.5 + */ + public static final int BK_TYPE_NAME = InternalNamingConventions.BK_SIMPLE_TYPE_NAME; - // remove longer suffix - char[] withoutSuffixName = withoutPrefixName; - if(suffixes != null) { - int bestLength = 0; - for (int i = 0; i < suffixes.length; i++) { - char[] suffix = suffixes[i]; - if(CharOperation.endsWith(withoutPrefixName, suffix)) { - int currLen = suffix.length; - if(bestLength < currLen && withoutPrefixName.length != currLen) { - withoutSuffixName = CharOperation.subarray(withoutPrefixName, 0, withoutPrefixName.length - currLen); - bestLength = currLen; - } - } + private static String[] convertCharsToString(char[][] c) { + int length = c == null ? 0 : c.length; + String[] s = new String[length]; + for (int i = 0; i < length; i++) { + s[i] = String.valueOf(c[i]); + } + return s; + } + private static char[][] convertStringToChars(String[] s) { + int length = s == null ? 0 : s.length; + char[][] c = new char[length][]; + for (int i = 0; i < length; i++) { + if(s[i] == null) { + c[i] = CharOperation.NO_CHAR; + } else { + c[i] = s[i].toCharArray(); } } - - withoutSuffixName[0] = ScannerHelper.toLowerCase(withoutSuffixName[0]); - return withoutSuffixName; + return c; } /** @@ -296,13 +323,11 @@ * @return char[] the name without prefix and suffix. * @see JavaCore#setOptions(java.util.Hashtable) * @see JavaCore#getDefaultOptions() + * + * @deprecated Use {@link #getBaseName(int, String, IJavaProject)} instead with {@link #VK_PARAMETER} as variable kind. */ public static char[] removePrefixAndSuffixForArgumentName(IJavaProject javaProject, char[] argumentName) { - AssistOptions assistOptions = new AssistOptions(javaProject.getOptions(true)); - return removePrefixAndSuffix( - argumentName, - assistOptions.argumentPrefixes, - assistOptions.argumentSuffixes); + return InternalNamingConventions.removeVariablePrefixAndSuffix(VK_PARAMETER, javaProject, argumentName); } /** @@ -327,11 +352,12 @@ * @return char[] the name without prefix and suffix. * @see JavaCore#setOptions(java.util.Hashtable) * @see JavaCore#getDefaultOptions() + * + * @deprecated Use {@link #getBaseName(int, String, IJavaProject)} instead with {@link #VK_PARAMETER} as variable kind. */ public static String removePrefixAndSuffixForArgumentName(IJavaProject javaProject, String argumentName) { return String.valueOf(removePrefixAndSuffixForArgumentName(javaProject, argumentName.toCharArray())); } - /** * Remove prefix and suffix from a field name. *
@@ -358,14 +384,13 @@ * @see Flags * @see JavaCore#setOptions(java.util.Hashtable) * @see JavaCore#getDefaultOptions() + * + * @deprecated Use {@link #getBaseName(int, String, IJavaProject)} instead + * with {@link #VK_INSTANCE_FIELD} or {@link #VK_STATIC_FIELD} as variable kind. */ public static char[] removePrefixAndSuffixForFieldName(IJavaProject javaProject, char[] fieldName, int modifiers) { boolean isStatic = Flags.isStatic(modifiers); - AssistOptions assistOptions = new AssistOptions(javaProject.getOptions(true)); - return removePrefixAndSuffix( - fieldName, - isStatic ? assistOptions.staticFieldPrefixes : assistOptions.fieldPrefixes, - isStatic ? assistOptions.staticFieldSuffixes : assistOptions.fieldSuffixes); + return InternalNamingConventions.removeVariablePrefixAndSuffix(isStatic ? VK_STATIC_FIELD : VK_INSTANCE_FIELD, javaProject, fieldName); } /** @@ -394,10 +419,14 @@ * @see Flags * @see JavaCore#setOptions(java.util.Hashtable) * @see JavaCore#getDefaultOptions() + * + * @deprecated Use {@link #getBaseName(int, String, IJavaProject)} instead + * with {@link #VK_INSTANCE_FIELD} or {@link #VK_STATIC_FIELD} as variable kind. */ public static String removePrefixAndSuffixForFieldName(IJavaProject javaProject, String fieldName, int modifiers) { return String.valueOf(removePrefixAndSuffixForFieldName(javaProject, fieldName.toCharArray(), modifiers)); } + /** * Remove prefix and suffix from a local variable name. *
@@ -420,13 +449,11 @@ * @return char[] the name without prefix and suffix. * @see JavaCore#setOptions(java.util.Hashtable) * @see JavaCore#getDefaultOptions() + * + * @deprecated Use {@link #getBaseName(int, String, IJavaProject)} instead with {@link #VK_LOCAL} as variable kind. */ public static char[] removePrefixAndSuffixForLocalVariableName(IJavaProject javaProject, char[] localName) { - AssistOptions assistOptions = new AssistOptions(javaProject.getOptions(true)); - return removePrefixAndSuffix( - localName, - assistOptions.localPrefixes, - assistOptions.localSuffixes); + return InternalNamingConventions.removeVariablePrefixAndSuffix(VK_LOCAL, javaProject, localName); } /** @@ -451,10 +478,43 @@ * @return char[] the name without prefix and suffix. * @see JavaCore#setOptions(java.util.Hashtable) * @see JavaCore#getDefaultOptions() + * + * @deprecated Use {@link #getBaseName(int, String, IJavaProject)} instead with {@link #VK_LOCAL} as variable kind. */ public static String removePrefixAndSuffixForLocalVariableName(IJavaProject javaProject, String localName) { return String.valueOf(removePrefixAndSuffixForLocalVariableName(javaProject, localName.toCharArray())); } + + /** + * Returns a base name which could be used to generate this variable name with {@link #suggestVariableNames(int, int, String, IJavaProject, int, String[], boolean)}. + *
+ * e.g.
+ * If the variable is a {@link #VK_LOCAL} and the variable name is variableName
then the base name will be VariableName
.
+ * If the variable is a {@link #VK_CONSTANT_FIELD} and the variable name is VARIABLE_NAME
then the base name will be VariableName
.
+ *
null
to take into account only workspace settings.
+ *
+ * @see #suggestVariableNames(int, int, String, IJavaProject, int, String[], boolean)
+ * @since 3.5
+ */
+ public static String getBaseName(
+ int variableKind,
+ String variableName,
+ IJavaProject javaProject) {
+ return String.valueOf(InternalNamingConventions.getBaseName(variableKind, javaProject, variableName.toCharArray()));
+ }
+
+ private static char[] suggestAccessorName(IJavaProject project, char[] fieldName, int modifiers) {
+ char[] name = removePrefixAndSuffixForFieldName(project, fieldName, modifiers);
+ if (name.length > 0 && ScannerHelper.isLowerCase(name[0])) {
+ name[0] = ScannerHelper.toUpperCase(name[0]);
+ }
+ return name;
+ }
/**
* Suggest names for an argument. The name is computed from argument's type
@@ -483,17 +543,26 @@
* @return char[][] an array of names.
* @see JavaCore#setOptions(java.util.Hashtable)
* @see JavaCore#getDefaultOptions()
+ *
+ * @deprecated Use {@link #suggestVariableNames(int, int, String, IJavaProject, int, String[], boolean)} instead with {@link #VK_PARAMETER} as variable kind.
*/
public static char[][] suggestArgumentNames(IJavaProject javaProject, char[] packageName, char[] qualifiedTypeName, int dim, char[][] excludedNames) {
+ if(qualifiedTypeName == null || qualifiedTypeName.length == 0)
+ return CharOperation.NO_CHAR_CHAR;
+
+ char[] typeName = CharOperation.lastSegment(qualifiedTypeName, '.');
+
NamingRequestor requestor = new NamingRequestor();
- InternalNamingConventions.suggestArgumentNames(
- javaProject,
- packageName,
- qualifiedTypeName,
- dim,
- null,
- excludedNames,
- requestor);
+ InternalNamingConventions.suggestVariableNames(
+ VK_PARAMETER,
+ BK_TYPE_NAME,
+ typeName,
+ javaProject,
+ dim,
+ null,
+ excludedNames,
+ true,
+ requestor);
return requestor.getResults();
}
@@ -525,6 +594,8 @@
* @return char[][] an array of names.
* @see JavaCore#setOptions(java.util.Hashtable)
* @see JavaCore#getDefaultOptions()
+ *
+ * @deprecated Use {@link #suggestVariableNames(int, int, String, IJavaProject, int, String[], boolean)} instead with {@link #VK_PARAMETER} as variable kind.
*/
public static String[] suggestArgumentNames(IJavaProject javaProject, String packageName, String qualifiedTypeName, int dim, String[] excludedNames) {
return convertCharsToString(
@@ -535,6 +606,7 @@
dim,
convertStringToChars(excludedNames)));
}
+
/**
* Suggest names for a field. The name is computed from field's type
* and possible prefixes or suffixes are added.
@@ -566,18 +638,27 @@
* @see Flags
* @see JavaCore#setOptions(java.util.Hashtable)
* @see JavaCore#getDefaultOptions()
+ *
+ * @deprecated Use {@link #suggestVariableNames(int, int, String, IJavaProject, int, String[], boolean)} instead
+ * with {@link #VK_INSTANCE_FIELD} or {@link #VK_STATIC_FIELD} as variable kind.
*/
public static char[][] suggestFieldNames(IJavaProject javaProject, char[] packageName, char[] qualifiedTypeName, int dim, int modifiers, char[][] excludedNames) {
+ if(qualifiedTypeName == null || qualifiedTypeName.length == 0)
+ return CharOperation.NO_CHAR_CHAR;
+
+ char[] typeName = CharOperation.lastSegment(qualifiedTypeName, '.');
+
NamingRequestor requestor = new NamingRequestor();
- InternalNamingConventions.suggestFieldNames(
- javaProject,
- packageName,
- qualifiedTypeName,
- dim,
- modifiers,
- null,
- excludedNames,
- requestor);
+ InternalNamingConventions.suggestVariableNames(
+ Flags.isStatic(modifiers) ? VK_STATIC_FIELD : VK_INSTANCE_FIELD,
+ BK_TYPE_NAME,
+ typeName,
+ javaProject,
+ dim,
+ null,
+ excludedNames,
+ true,
+ requestor);
return requestor.getResults();
}
@@ -613,6 +694,9 @@
* @see Flags
* @see JavaCore#setOptions(java.util.Hashtable)
* @see JavaCore#getDefaultOptions()
+ *
+ * @deprecated Use {@link #suggestVariableNames(int, int, String, IJavaProject, int, String[], boolean)} instead
+ * with {@link #VK_INSTANCE_FIELD} or {@link #VK_STATIC_FIELD} as variable kind.
*/
public static String[] suggestFieldNames(IJavaProject javaProject, String packageName, String qualifiedTypeName, int dim, int modifiers, String[] excludedNames) {
return convertCharsToString(
@@ -624,87 +708,7 @@
modifiers,
convertStringToChars(excludedNames)));
}
-
- /**
- * Suggest names for a local variable. The name is computed from variable's type
- * and possible prefixes or suffixes are added.
- *
- * If the type of the local variable is TypeName
, the prefix for local variable is pre
- * and the suffix for local variable is suf
then the proposed names are preTypeNamesuf
- * and preNamesuf
. If there is no prefix or suffix the proposals are typeName
- * and name
.
- *
- * This method is affected by the following JavaCore options : {@link JavaCore#CODEASSIST_LOCAL_PREFIXES} and - * {@link JavaCore#CODEASSIST_LOCAL_SUFFIXES}. - *
- *
- * For a complete description of these configurable options, see getDefaultOptions
.
- * For programmaticaly change these options, see JavaCore#setOptions()
.
- *
null
if there is no excluded names.
- * @return char[][] an array of names.
- * @see JavaCore#setOptions(java.util.Hashtable)
- * @see JavaCore#getDefaultOptions()
- */
- public static char[][] suggestLocalVariableNames(IJavaProject javaProject, char[] packageName, char[] qualifiedTypeName, int dim, char[][] excludedNames) {
- NamingRequestor requestor = new NamingRequestor();
- InternalNamingConventions.suggestLocalVariableNames(
- javaProject,
- packageName,
- qualifiedTypeName,
- dim,
- null,
- excludedNames,
- requestor);
-
- return requestor.getResults();
- }
-
- /**
- * Suggest names for a local variable. The name is computed from variable's type
- * and possible prefixes or suffixes are added.
- *
- * If the type of the local variable is TypeName
, the prefix for local variable is pre
- * and the suffix for local variable is suf
then the proposed names are preTypeNamesuf
- * and preNamesuf
. If there is no prefix or suffix the proposals are typeName
- * and name
.
- *
- * This method is affected by the following JavaCore options : {@link JavaCore#CODEASSIST_LOCAL_PREFIXES} and - * {@link JavaCore#CODEASSIST_LOCAL_SUFFIXES}. - *
- *
- * For a complete description of these configurable options, see getDefaultOptions
.
- * For programmaticaly change these options, see JavaCore#setOptions()
.
- *
null
if there is no excluded names.
- * @return char[][] an array of names.
- * @see JavaCore#setOptions(java.util.Hashtable)
- * @see JavaCore#getDefaultOptions()
- */
- public static String[] suggestLocalVariableNames(IJavaProject javaProject, String packageName, String qualifiedTypeName, int dim, String[] excludedNames) {
- return convertCharsToString(
- suggestLocalVariableNames(
- javaProject,
- packageName.toCharArray(),
- qualifiedTypeName.toCharArray(),
- dim,
- convertStringToChars(excludedNames)));
- }
-
+
/**
* Suggest name for a getter method. The name is computed from field's name
* and possible prefixes or suffixes are removed.
@@ -756,7 +760,6 @@
);
}
}
-
/**
* Suggest name for a getter method. The name is computed from field's name
* and possible prefixes or suffixes are removed.
@@ -797,7 +800,114 @@
isBoolean,
convertStringToChars(excludedNames)));
}
+ /**
+ * Suggest names for a local variable. The name is computed from variable's type
+ * and possible prefixes or suffixes are added.
+ *
+ * If the type of the local variable is TypeName
, the prefix for local variable is pre
+ * and the suffix for local variable is suf
then the proposed names are preTypeNamesuf
+ * and preNamesuf
. If there is no prefix or suffix the proposals are typeName
+ * and name
.
+ *
+ * This method is affected by the following JavaCore options : {@link JavaCore#CODEASSIST_LOCAL_PREFIXES} and + * {@link JavaCore#CODEASSIST_LOCAL_SUFFIXES}. + *
+ *
+ * For a complete description of these configurable options, see getDefaultOptions
.
+ * For programmaticaly change these options, see JavaCore#setOptions()
.
+ *
null
if there is no excluded names.
+ * @return char[][] an array of names.
+ * @see JavaCore#setOptions(java.util.Hashtable)
+ * @see JavaCore#getDefaultOptions()
+ *
+ * @deprecated Use {@link #suggestVariableNames(int, int, String, IJavaProject, int, String[], boolean)} instead with {@link #VK_LOCAL} as variable kind.
+ */
+ public static char[][] suggestLocalVariableNames(IJavaProject javaProject, char[] packageName, char[] qualifiedTypeName, int dim, char[][] excludedNames) {
+ if(qualifiedTypeName == null || qualifiedTypeName.length == 0)
+ return CharOperation.NO_CHAR_CHAR;
+
+ char[] typeName = CharOperation.lastSegment(qualifiedTypeName, '.');
+
+ NamingRequestor requestor = new NamingRequestor();
+ InternalNamingConventions.suggestVariableNames(
+ VK_LOCAL,
+ BK_TYPE_NAME,
+ typeName,
+ javaProject,
+ dim,
+ null,
+ excludedNames,
+ true,
+ requestor);
+ return requestor.getResults();
+ }
+ /**
+ * Suggest names for a local variable. The name is computed from variable's type
+ * and possible prefixes or suffixes are added.
+ *
+ * If the type of the local variable is TypeName
, the prefix for local variable is pre
+ * and the suffix for local variable is suf
then the proposed names are preTypeNamesuf
+ * and preNamesuf
. If there is no prefix or suffix the proposals are typeName
+ * and name
.
+ *
+ * This method is affected by the following JavaCore options : {@link JavaCore#CODEASSIST_LOCAL_PREFIXES} and + * {@link JavaCore#CODEASSIST_LOCAL_SUFFIXES}. + *
+ *
+ * For a complete description of these configurable options, see getDefaultOptions
.
+ * For programmaticaly change these options, see JavaCore#setOptions()
.
+ *
null
if there is no excluded names.
+ * @return char[][] an array of names.
+ * @see JavaCore#setOptions(java.util.Hashtable)
+ * @see JavaCore#getDefaultOptions()
+ *
+ * @deprecated Use {@link #suggestVariableNames(int, int, String, IJavaProject, int, String[], boolean)} instead with {@link #VK_LOCAL} as variable kind.
+ */
+ public static String[] suggestLocalVariableNames(IJavaProject javaProject, String packageName, String qualifiedTypeName, int dim, String[] excludedNames) {
+ return convertCharsToString(
+ suggestLocalVariableNames(
+ javaProject,
+ packageName.toCharArray(),
+ qualifiedTypeName.toCharArray(),
+ dim,
+ convertStringToChars(excludedNames)));
+ }
+ private static char[] suggestNewName(char[] name, char[][] excludedNames){
+ if(excludedNames == null) {
+ return name;
+ }
+
+ char[] newName = name;
+ int count = 2;
+ int i = 0;
+ while (i < excludedNames.length) {
+ if(CharOperation.equals(newName, excludedNames[i], false)) {
+ newName = CharOperation.concat(name, String.valueOf(count++).toCharArray());
+ i = 0;
+ } else {
+ i++;
+ }
+ }
+ return newName;
+ }
+
/**
* Suggest name for a setter method. The name is computed from field's name
* and possible prefixes or suffixes are removed.
@@ -853,7 +963,6 @@
);
}
}
-
/**
* Suggest name for a setter method. The name is computed from field's name
* and possible prefixes or suffixes are removed.
@@ -893,53 +1002,108 @@
isBoolean,
convertStringToChars(excludedNames)));
}
+
+ /**
+ * Suggests names for a variable. The name is computed from a base name and possible prefixes or suffixes are added.
+ *
+ *
+ * The base name is used to compute the variable name.
+ * Some different kinds of base name are possible and each kind is associated to a different heuristic to compute variable names.
+ * The heuristic depends also of the kind of the variable. Each kind of variable is identified by a constant starting with VK_
.
+ * When a prefix and a suffix can be added then all combinations of prefix and suffix are suggested.
+ * If the name is name
, the prefix is pre
and the suffix is suf
then the suggested names will be
+ * prenamesuf
, prename
, namesuf
and name
.
+ *
+ * The different kinds of base name are:
+ *
SimpleName
then the suggested name will be simpleName
."_"
).SimpleName
then the suggested name will be SIMPLE_NAME
.HashMap
) and all the words of the base name are considered to compute the variable names. A prefix and a suffix can be added to these names.TypeName
then the suggested names will be typeName
and name
."_"
).TypeName
then the suggested name will be TYPE_NAME
and NAME
.+ * Each variable kind is affected by the following JavaCore options: + *
+ * For a complete description of these configurable options, see getDefaultOptions
.
+ * For programmaticaly change these options, see JavaCore#setOptions()
.
+ *
+ * Proposed names are sorted by relevance (best proposal first).
+ * The names are proposed in the following order:
+ *
null
to take into account only workspace settings.
+ * @param dim variable dimension (0 if the field is not an array).
+ * @param excluded a list of names which cannot be suggested (already used names).
+ * Can be null
if there is no excluded names.
+ * @param evaluateDefault if set, the result is guaranteed to contain at least one result. If not, the result can be an empty array.
+ * @return String[] an array of names.
+ * @see JavaCore#setOptions(java.util.Hashtable)
+ * @see JavaCore#getDefaultOptions()
+ *
+ * @since 3.5
+ */
+ public static String[] suggestVariableNames(
+ int variableKind,
+ int baseNameKind,
+ String baseName,
+ IJavaProject javaProject,
+ int dim,
+ String[] excluded,
+ boolean evaluateDefault) {
+
+ NamingRequestor requestor = new NamingRequestor();
+ InternalNamingConventions.suggestVariableNames(
+ variableKind,
+ baseNameKind,
+ baseName.toCharArray(),
+ javaProject,
+ dim,
+ null,
+ convertStringToChars(excluded),
+ evaluateDefault,
+ requestor);
- private static char[] suggestAccessorName(IJavaProject project, char[] fieldName, int modifiers) {
- char[] name = removePrefixAndSuffixForFieldName(project, fieldName, modifiers);
- if (name.length > 0 && ScannerHelper.isLowerCase(name[0])) {
- name[0] = ScannerHelper.toUpperCase(name[0]);
- }
- return name;
- }
-
- private static char[] suggestNewName(char[] name, char[][] excludedNames){
- if(excludedNames == null) {
- return name;
- }
-
- char[] newName = name;
- int count = 2;
- int i = 0;
- while (i < excludedNames.length) {
- if(CharOperation.equals(newName, excludedNames[i], false)) {
- newName = CharOperation.concat(name, String.valueOf(count++).toCharArray());
- i = 0;
- } else {
- i++;
- }
- }
- return newName;
- }
-
- private static String[] convertCharsToString(char[][] c) {
- int length = c == null ? 0 : c.length;
- String[] s = new String[length];
- for (int i = 0; i < length; i++) {
- s[i] = String.valueOf(c[i]);
- }
- return s;
+ return convertCharsToString(requestor.getResults());
}
-
- private static char[][] convertStringToChars(String[] s) {
- int length = s == null ? 0 : s.length;
- char[][] c = new char[length][];
- for (int i = 0; i < length; i++) {
- if(s[i] == null) {
- c[i] = CharOperation.NO_CHAR;
- } else {
- c[i] = s[i].toCharArray();
- }
- }
- return c;
+
+ private NamingConventions() {
+ // Not instantiable
}
}
Index: compiler/org/eclipse/jdt/core/compiler/CharOperation.java
===================================================================
RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/core/compiler/CharOperation.java,v
retrieving revision 1.85
diff -u -r1.85 CharOperation.java
--- compiler/org/eclipse/jdt/core/compiler/CharOperation.java 27 Jun 2008 16:04:14 -0000 1.85
+++ compiler/org/eclipse/jdt/core/compiler/CharOperation.java 17 Oct 2008 11:27:16 -0000
@@ -3582,6 +3582,54 @@
}
/**
+ * Answers the result of a char[] conversion to uppercase. Answers null if the given chars array is null.
+ * + * chars = { 'A' , 'B' } + * result => { 'A' , 'B' } + *+ *
+ * array = { 'a', 'B' } + * result => { 'A' , 'B' } + *+ *