All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.jdt.core.NamingConventions Maven / Gradle / Ivy

/*******************************************************************************
 * Copyright (c) 2000, 2009 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jdt.core;

import org.eclipse.jdt.core.compiler.CharOperation;

import org.eclipse.jdt.internal.compiler.parser.ScannerHelper;
import org.eclipse.jdt.internal.core.INamingRequestor;
import org.eclipse.jdt.internal.core.InternalNamingConventions;


/**
 * Provides methods for computing Java-specific names.
 * 

* The behavior of the methods is dependent of several JavaCore options. *

* The possible options are : *

    *
  • {@link JavaCore#CODEASSIST_FIELD_PREFIXES} : Define the Prefixes for Field Name.
  • *
  • {@link JavaCore#CODEASSIST_FIELD_SUFFIXES} : Define the Suffixes for Field Name.
  • * *
  • {@link JavaCore#CODEASSIST_STATIC_FIELD_PREFIXES} : Define the Prefixes for Static Field Name.
  • *
  • {@link JavaCore#CODEASSIST_STATIC_FIELD_SUFFIXES} : Define the Suffixes for Static Field Name.
  • * *
  • {@link JavaCore#CODEASSIST_STATIC_FINAL_FIELD_PREFIXES} : Define the Prefixes for Static Final Field Name.
  • *
  • {@link JavaCore#CODEASSIST_STATIC_FINAL_FIELD_SUFFIXES} : Define the Suffixes for Static Final Field Name.
  • * *
  • {@link JavaCore#CODEASSIST_LOCAL_PREFIXES} : Define the Prefixes for Local Variable Name.
  • *
  • {@link JavaCore#CODEASSIST_LOCAL_SUFFIXES} : Define the Suffixes for Local Variable Name.
  • * *
  • {@link JavaCore#CODEASSIST_ARGUMENT_PREFIXES} : Define the Prefixes for Argument Name.
  • *
  • {@link JavaCore#CODEASSIST_ARGUMENT_SUFFIXES} : Define the Suffixes for Argument Name.
  • *
*

*

* For a complete description of the configurable options, see {@link JavaCore#getDefaultOptions()}. * To programmatically change these options, see {@link JavaCore#setOptions(java.util.Hashtable)}. *

*

* This class provides static methods and constants only. *

* * @see JavaCore#setOptions(java.util.Hashtable) * @see JavaCore#getDefaultOptions() * @since 2.1 * @noinstantiate This class is not intended to be instantiated by clients. */ public final class NamingConventions { static class NamingRequestor implements INamingRequestor { private final static int SIZE = 10; // for acceptNameWithPrefixAndSuffix private char[][] firstPrefixAndFirstSuffixResults = new char[SIZE][]; private int firstPrefixAndFirstSuffixResultsCount = 0; private char[][] firstPrefixAndSuffixResults = new char[SIZE][]; private int firstPrefixAndSuffixResultsCount = 0; private char[][] prefixAndFirstSuffixResults = new char[SIZE][]; private int prefixAndFirstSuffixResultsCount = 0; private char[][] prefixAndSuffixResults = new char[SIZE][]; private int prefixAndSuffixResultsCount = 0; // for acceptNameWithPrefix private char[][] firstPrefixResults = new char[SIZE][]; private int firstPrefixResultsCount = 0; private char[][] prefixResults = new char[SIZE][]; private int prefixResultsCount = 0; // for acceptNameWithSuffix private char[][] firstSuffixResults = new char[SIZE][]; private int firstSuffixResultsCount = 0; private char[][] suffixResults = new char[SIZE][]; private int suffixResultsCount = 0; // 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; if(length == this.firstPrefixAndFirstSuffixResultsCount) { System.arraycopy( this.firstPrefixAndFirstSuffixResults, 0, this.firstPrefixAndFirstSuffixResults = new char[length * 2][], 0, length); } this.firstPrefixAndFirstSuffixResults[this.firstPrefixAndFirstSuffixResultsCount++] = name; } else if (isFirstPrefix) { int length = this.firstPrefixAndSuffixResults.length; if(length == this.firstPrefixAndSuffixResultsCount) { System.arraycopy( this.firstPrefixAndSuffixResults, 0, this.firstPrefixAndSuffixResults = new char[length * 2][], 0, length); } this.firstPrefixAndSuffixResults[this.firstPrefixAndSuffixResultsCount++] = name; } else if(isFirstSuffix) { int length = this.prefixAndFirstSuffixResults.length; if(length == this.prefixAndFirstSuffixResultsCount) { System.arraycopy( this.prefixAndFirstSuffixResults, 0, this.prefixAndFirstSuffixResults = new char[length * 2][], 0, length); } this.prefixAndFirstSuffixResults[this.prefixAndFirstSuffixResultsCount++] = name; } else { int length = this.prefixAndSuffixResults.length; if(length == this.prefixAndSuffixResultsCount) { System.arraycopy( this.prefixAndSuffixResults, 0, this.prefixAndSuffixResults = new char[length * 2][], 0, length); } this.prefixAndSuffixResults[this.prefixAndSuffixResultsCount++] = name; } } public void acceptNameWithSuffix(char[] name, boolean isFirstSuffix, int reusedCharacters) { if(isFirstSuffix) { int length = this.firstSuffixResults.length; if(length == this.firstSuffixResultsCount) { System.arraycopy( this.firstSuffixResults, 0, this.firstSuffixResults = new char[length * 2][], 0, length); } this.firstSuffixResults[this.firstSuffixResultsCount++] = name; } else { int length = this.suffixResults.length; if(length == this.suffixResultsCount) { System.arraycopy( this.suffixResults, 0, this.suffixResults = new char[length * 2][], 0, length); } this.suffixResults[this.suffixResultsCount++] = name; } } public char[][] getResults(){ int count = this.firstPrefixAndFirstSuffixResultsCount + this.firstPrefixAndSuffixResultsCount + this.prefixAndFirstSuffixResultsCount + this.prefixAndSuffixResultsCount + this.firstPrefixResultsCount + this.prefixResultsCount + this.firstSuffixResultsCount + this.suffixResultsCount + this.otherResultsCount; char[][] results = new char[count][]; int index = 0; System.arraycopy(this.firstPrefixAndFirstSuffixResults, 0, results, index, this.firstPrefixAndFirstSuffixResultsCount); index += this.firstPrefixAndFirstSuffixResultsCount; System.arraycopy(this.firstPrefixAndSuffixResults, 0, results, index, this.firstPrefixAndSuffixResultsCount); index += this.firstPrefixAndSuffixResultsCount; System.arraycopy(this.prefixAndFirstSuffixResults, 0, results, index, this.prefixAndFirstSuffixResultsCount); index += this.prefixAndFirstSuffixResultsCount; System.arraycopy(this.prefixAndSuffixResults, 0, results, index, this.prefixAndSuffixResultsCount); index += this.prefixAndSuffixResultsCount; System.arraycopy(this.firstPrefixResults, 0, results, index, this.firstPrefixResultsCount); index += this.firstPrefixResultsCount; System.arraycopy(this.prefixResults, 0, results, index, this.prefixResultsCount); index += this.prefixResultsCount; System.arraycopy(this.firstSuffixResults, 0, results, index, this.firstSuffixResultsCount); index += this.firstSuffixResultsCount; System.arraycopy(this.suffixResults, 0, results, index, this.suffixResultsCount); index += this.suffixResultsCount; System.arraycopy(this.otherResults, 0, results, index, this.otherResultsCount); 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$ /** * 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 static final field. * * @since 3.5 */ public static final int VK_STATIC_FINAL_FIELD = InternalNamingConventions.VK_STATIC_FINAL_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 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; 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(); } } return c; } /** * Remove prefix and suffix from an argument name. *

* If argument name prefix is pre and argument name suffix is suf * then for an argument named preArgsuf the result of this method is arg. * If there is no prefix or suffix defined in JavaCore options the result is the unchanged * name preArgsuf. *

*

* This method is affected by the following JavaCore options : {@link JavaCore#CODEASSIST_ARGUMENT_PREFIXES} and * {@link JavaCore#CODEASSIST_ARGUMENT_SUFFIXES}. *

*

* For a complete description of these configurable options, see getDefaultOptions. * For programmaticaly change these options, see JavaCore#setOptions(). *

* * @param javaProject project which contains the argument. * @param argumentName argument's name. * @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) { return InternalNamingConventions.removeVariablePrefixAndSuffix(VK_PARAMETER, javaProject, argumentName); } /** * Remove prefix and suffix from an argument name. *

* If argument name prefix is pre and argument name suffix is suf * then for an argument named preArgsuf the result of this method is arg. * If there is no prefix or suffix defined in JavaCore options the result is the unchanged * name preArgsuf. *

*

* This method is affected by the following JavaCore options : {@link JavaCore#CODEASSIST_ARGUMENT_PREFIXES} and * {@link JavaCore#CODEASSIST_ARGUMENT_SUFFIXES}. *

*

* For a complete description of these configurable options, see getDefaultOptions. * For programmaticaly change these options, see JavaCore#setOptions(). *

* * @param javaProject project which contains the argument. * @param argumentName argument's name. * @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. *

* If field name prefix is pre and field name suffix is suf * then for a field named preFieldsuf the result of this method is field. * If there is no prefix or suffix defined in JavaCore options the result is the unchanged * name preFieldsuf. *

*

* This method is affected by the following JavaCore options : {@link JavaCore#CODEASSIST_FIELD_PREFIXES} } , * {@link JavaCore#CODEASSIST_FIELD_SUFFIXES} for instance field and {@link JavaCore#CODEASSIST_STATIC_FIELD_PREFIXES}, * {@link JavaCore#CODEASSIST_STATIC_FIELD_SUFFIXES} for static field. *

*

* For a complete description of these configurable options, see getDefaultOptions. * For programmaticaly change these options, see JavaCore#setOptions(). *

* * @param javaProject project which contains the field. * @param fieldName field's name. * @param modifiers field's modifiers as defined by the class * Flags. * @return char[] the name without prefix and suffix. * @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) { return InternalNamingConventions.removeVariablePrefixAndSuffix( Flags.isStatic(modifiers) ? VK_STATIC_FIELD : VK_INSTANCE_FIELD, javaProject, fieldName); } /** * Remove prefix and suffix from a field name. *

* If field name prefix is pre and field name suffix is suf * then for a field named preFieldsuf the result of this method is field. * If there is no prefix or suffix defined in JavaCore options the result is the unchanged * name preFieldsuf. *

*

* This method is affected by the following JavaCore options : {@link JavaCore#CODEASSIST_FIELD_PREFIXES}, * {@link JavaCore#CODEASSIST_FIELD_SUFFIXES} for instance field and {@link JavaCore#CODEASSIST_STATIC_FIELD_PREFIXES}, * {@link JavaCore#CODEASSIST_STATIC_FIELD_SUFFIXES} for static field. *

*

* For a complete description of these configurable options, see getDefaultOptions. * For programmaticaly change these options, see JavaCore#setOptions(). *

* * @param javaProject project which contains the field. * @param fieldName field's name. * @param modifiers field's modifiers as defined by the class * Flags. * @return char[] the name without prefix and suffix. * @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. *

* If local variable name prefix is pre and local variable name suffix is suf * then for a local variable named preLocalsuf the result of this method is local. * If there is no prefix or suffix defined in JavaCore options the result is the unchanged * name preLocalsuf. *

*

* 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(). *

* * @param javaProject project which contains the variable. * @param localName variable's name. * @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) { return InternalNamingConventions.removeVariablePrefixAndSuffix(VK_LOCAL, javaProject, localName); } /** * Remove prefix and suffix from a local variable name. *

* If local variable name prefix is pre and local variable name suffix is suf * then for a local variable named preLocalsuf the result of this method is local. * If there is no prefix or suffix defined in JavaCore options the result is the unchanged * name preLocalsuf. *

*

* 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(). *

* * @param javaProject project which contains the variable. * @param localName variable's name. * @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 the given 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_STATIC_FINAL_FIELD} and the variable name is VARIABLE_NAME then the base name will be variableName.
*

*

* Prefixes and suffixes defined in JavaCore options will be also removed from the variable name.
* Each variable kind is affected by the following JavaCore options: *

    *
  • {@link #VK_PARAMETER}: {@link JavaCore#CODEASSIST_ARGUMENT_PREFIXES} and {@link JavaCore#CODEASSIST_ARGUMENT_SUFFIXES}
  • *
  • {@link #VK_LOCAL}: {@link JavaCore#CODEASSIST_LOCAL_PREFIXES} and {@link JavaCore#CODEASSIST_LOCAL_SUFFIXES}
  • *
  • {@link #VK_INSTANCE_FIELD}: {@link JavaCore#CODEASSIST_FIELD_PREFIXES} and {@link JavaCore#CODEASSIST_FIELD_SUFFIXES}
  • *
  • {@link #VK_STATIC_FIELD}: {@link JavaCore#CODEASSIST_STATIC_FIELD_PREFIXES} and {@link JavaCore#CODEASSIST_STATIC_FIELD_SUFFIXES}
  • *
  • {@link #VK_STATIC_FINAL_FIELD}: {@link JavaCore#CODEASSIST_STATIC_FINAL_FIELD_PREFIXES} and {@link JavaCore#CODEASSIST_STATIC_FINAL_FIELD_SUFFIXES}
  • *
*

*

* e.g.
* If the variable is a {@link #VK_LOCAL}, the variable name is preVariableNamesuf, a possible prefix is pre and a possible suffix is suf * then the base name will be variableName.
*

* * @param variableKind specifies what type the variable is: {@link #VK_LOCAL}, {@link #VK_PARAMETER}, {@link #VK_STATIC_FIELD}, * {@link #VK_INSTANCE_FIELD} or {@link #VK_STATIC_FINAL_FIELD}. * @param variableName a variable name * @param javaProject project which contains the variable or 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(), true)); } private static int getFieldVariableKind(int modifiers) { if (Flags.isStatic(modifiers)) { if (Flags.isFinal(modifiers)) { return VK_STATIC_FINAL_FIELD; } return VK_STATIC_FIELD; } return VK_INSTANCE_FIELD; } private static char[] suggestAccessorName(IJavaProject project, char[] fieldName, int modifiers) { char[] name = InternalNamingConventions.getBaseName(getFieldVariableKind(modifiers), project, fieldName, false); if (name.length > 0 && ScannerHelper.isLowerCase(name[0])) { if (name.length == 1 || !ScannerHelper.isUpperCase(name[1])) { name[0] = ScannerHelper.toUpperCase(name[0]); } } return name; } /** * Suggest names for an argument. The name is computed from argument's type * and possible prefixes or suffixes are added. *

* If the type of the argument is TypeName, the prefix for argument is pre * and the suffix for argument 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_ARGUMENT_PREFIXES} and * {@link JavaCore#CODEASSIST_ARGUMENT_SUFFIXES}. *

*

* For a complete description of these configurable options, see getDefaultOptions. * For programmaticaly change these options, see JavaCore#setOptions(). *

* * @param javaProject project which contains the argument. * @param packageName package of the argument's type. * @param qualifiedTypeName argument's type. * @param dim argument's dimension (0 if the argument is not an array). * @param excludedNames a list of names which cannot be suggested (already used names). * Can be 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_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.suggestVariableNames( VK_PARAMETER, BK_TYPE_NAME, typeName, javaProject, dim, null, excludedNames, true, requestor); return requestor.getResults(); } /** * Suggest names for an argument. The name is computed from argument's type * and possible prefixes or suffixes are added. *

* If the type of the argument is TypeName, the prefix for argument is pre * and the suffix for argument 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_ARGUMENT_PREFIXES} and * {@link JavaCore#CODEASSIST_ARGUMENT_SUFFIXES}. *

*

* For a complete description of these configurable options, see getDefaultOptions. * For programmaticaly change these options, see JavaCore#setOptions(). *

* * @param javaProject project which contains the argument. * @param packageName package of the argument's type. * @param qualifiedTypeName argument's type. * @param dim argument's dimension (0 if the argument is not an array). * @param excludedNames a list of names which cannot be suggested (already used names). * Can be 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_PARAMETER} as variable kind. */ public static String[] suggestArgumentNames(IJavaProject javaProject, String packageName, String qualifiedTypeName, int dim, String[] excludedNames) { return convertCharsToString( suggestArgumentNames( javaProject, packageName.toCharArray(), qualifiedTypeName.toCharArray(), dim, convertStringToChars(excludedNames))); } /** * Suggest names for a field. The name is computed from field's type * and possible prefixes or suffixes are added. *

* If the type of the field is TypeName, the prefix for field is pre * and the suffix for field 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_FIELD_PREFIXES}, * {@link JavaCore#CODEASSIST_FIELD_SUFFIXES} and for instance field and {@link JavaCore#CODEASSIST_STATIC_FIELD_PREFIXES}, * {@link JavaCore#CODEASSIST_STATIC_FIELD_SUFFIXES} for static field. *

*

* For a complete description of these configurable options, see getDefaultOptions. * For programmaticaly change these options, see JavaCore#setOptions(). *

* * @param javaProject project which contains the field. * @param packageName package of the field's type. * @param qualifiedTypeName field's type. * @param dim field's dimension (0 if the field is not an array). * @param modifiers field's modifiers as defined by the class * Flags. * @param excludedNames a list of names which cannot be suggested (already used names). * Can be null if there is no excluded names. * @return char[][] an array of names. * @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.suggestVariableNames( Flags.isStatic(modifiers) ? VK_STATIC_FIELD : VK_INSTANCE_FIELD, BK_TYPE_NAME, typeName, javaProject, dim, null, excludedNames, true, requestor); return requestor.getResults(); } /** * Suggest names for a field. The name is computed from field's type * and possible prefixes or suffixes are added. *

* If the type of the field is TypeName, the prefix for field is pre * and the suffix for field 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_FIELD_PREFIXES}, * {@link JavaCore#CODEASSIST_FIELD_SUFFIXES} and for instance field and {@link JavaCore#CODEASSIST_STATIC_FIELD_PREFIXES}, * {@link JavaCore#CODEASSIST_STATIC_FIELD_SUFFIXES} for static field. *

*

* For a complete description of these configurable options, see getDefaultOptions. * For programmaticaly change these options, see JavaCore#setOptions(). *

* * @param javaProject project which contains the field. * @param packageName package of the field's type. * @param qualifiedTypeName field's type. * @param dim field's dimension (0 if the field is not an array). * @param modifiers field's modifiers as defined by the class * Flags. * @param excludedNames a list of names which cannot be suggested (already used names). * Can be null if there is no excluded names. * @return char[][] an array of names. * @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( suggestFieldNames( javaProject, packageName.toCharArray(), qualifiedTypeName.toCharArray(), dim, modifiers, convertStringToChars(excludedNames))); } /** * Suggest name for a getter method. The name is computed from field's name * and possible prefixes or suffixes are removed. *

* If the field name is preFieldNamesuf and the prefix for field is pre and * the suffix for field is suf then the prosposed name is isFieldName for boolean field or * getFieldName for others. If there is no prefix and suffix the proposal is isPreFieldNamesuf * for boolean field or getPreFieldNamesuf for others. *

*

* This method is affected by the following JavaCore options : {@link JavaCore#CODEASSIST_FIELD_PREFIXES}, * {@link JavaCore#CODEASSIST_FIELD_SUFFIXES} for instance field and {@link JavaCore#CODEASSIST_STATIC_FIELD_PREFIXES}, * {@link JavaCore#CODEASSIST_STATIC_FIELD_SUFFIXES} for static field. *

*

* For a complete description of these configurable options, see getDefaultOptions. * For programmaticaly change these options, see JavaCore#setOptions(). *

* * @param project project which contains the field. * @param fieldName field's name's. * @param modifiers field's modifiers as defined by the class * Flags. * @param isBoolean true if the field's type is boolean * @param excludedNames a list of names which cannot be suggested (already used names). * Can be null if there is no excluded names. * @return char[] a name. * @see Flags * @see JavaCore#setOptions(java.util.Hashtable) * @see JavaCore#getDefaultOptions() */ public static char[] suggestGetterName(IJavaProject project, char[] fieldName, int modifiers, boolean isBoolean, char[][] excludedNames) { if (isBoolean) { char[] name = InternalNamingConventions.getBaseName(getFieldVariableKind(modifiers), project, fieldName, false); int prefixLen = GETTER_BOOL_NAME.length; if (CharOperation.prefixEquals(GETTER_BOOL_NAME, name) && name.length > prefixLen && ScannerHelper.isUpperCase(name[prefixLen])) { return suggestNewName(name, excludedNames); } else { return suggestNewName( CharOperation.concat(GETTER_BOOL_NAME, suggestAccessorName(project, fieldName, modifiers)), excludedNames ); } } else { return suggestNewName( CharOperation.concat(GETTER_NAME, suggestAccessorName(project, fieldName, modifiers)), excludedNames ); } } /** * Suggest name for a getter method. The name is computed from field's name * and possible prefixes or suffixes are removed. *

* If the field name is preFieldNamesuf and the prefix for field is pre and * the suffix for field is suf then the prosposed name is isFieldName for boolean field or * getFieldName for others. If there is no prefix and suffix the proposal is isPreFieldNamesuf * for boolean field or getPreFieldNamesuf for others. *

*

* This method is affected by the following JavaCore options : {@link JavaCore#CODEASSIST_FIELD_PREFIXES}, * {@link JavaCore#CODEASSIST_FIELD_SUFFIXES} for instance field and {@link JavaCore#CODEASSIST_STATIC_FIELD_PREFIXES}, * {@link JavaCore#CODEASSIST_STATIC_FIELD_SUFFIXES} for static field. *

*

* For a complete description of these configurable options, see getDefaultOptions. * For programmaticaly change these options, see JavaCore#setOptions(). *

* * @param project project which contains the field. * @param fieldName field's name's. * @param modifiers field's modifiers as defined by the class * Flags. * @param isBoolean true if the field's type is boolean * @param excludedNames a list of names which cannot be suggested (already used names). * Can be null if there is no excluded names. * @return char[] a name. * @see Flags * @see JavaCore#setOptions(java.util.Hashtable) * @see JavaCore#getDefaultOptions() */ public static String suggestGetterName(IJavaProject project, String fieldName, int modifiers, boolean isBoolean, String[] excludedNames) { return String.valueOf( suggestGetterName( project, fieldName.toCharArray(), modifiers, 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(). *

* * @param javaProject project which contains the variable. * @param packageName package of the variable's type. * @param qualifiedTypeName variable's type. * @param dim variable's dimension (0 if the variable is not an array). * @param excludedNames a list of names which cannot be suggested (already used names). * Can be 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(). *

* * @param javaProject project which contains the variable. * @param packageName package of the variable's type. * @param qualifiedTypeName variable's type. * @param dim variable's dimension (0 if the variable is not an array). * @param excludedNames a list of names which cannot be suggested (already used names). * Can be 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. *

* If the field name is preFieldNamesuf and the prefix for field is pre and * the suffix for field is suf then the proposed name is setFieldName. * If there is no prefix and suffix the proposal is setPreFieldNamesuf. *

*

* This method is affected by the following JavaCore options : {@link JavaCore#CODEASSIST_FIELD_PREFIXES}, * {@link JavaCore#CODEASSIST_FIELD_SUFFIXES} for instance field and {@link JavaCore#CODEASSIST_STATIC_FIELD_PREFIXES}, * {@link JavaCore#CODEASSIST_STATIC_FIELD_SUFFIXES} for static field. *

*

* For a complete description of these configurable options, see getDefaultOptions. * For programmaticaly change these options, see JavaCore#setOptions(). *

* * @param project project which contains the field. * @param fieldName field's name's. * @param modifiers field's modifiers as defined by the class * Flags. * @param isBoolean true if the field's type is boolean * @param excludedNames a list of names which cannot be suggested (already used names). * Can be null if there is no excluded names. * @return char[] a name. * @see Flags * @see JavaCore#setOptions(java.util.Hashtable) * @see JavaCore#getDefaultOptions() */ public static char[] suggestSetterName(IJavaProject project, char[] fieldName, int modifiers, boolean isBoolean, char[][] excludedNames) { if (isBoolean) { char[] name = InternalNamingConventions.getBaseName(getFieldVariableKind(modifiers), project, fieldName, false); int prefixLen = GETTER_BOOL_NAME.length; if (CharOperation.prefixEquals(GETTER_BOOL_NAME, name) && name.length > prefixLen && ScannerHelper.isUpperCase(name[prefixLen])) { name = CharOperation.subarray(name, prefixLen, name.length); return suggestNewName( CharOperation.concat(SETTER_NAME, suggestAccessorName(project, name, modifiers)), excludedNames ); } else { return suggestNewName( CharOperation.concat(SETTER_NAME, suggestAccessorName(project, fieldName, modifiers)), excludedNames ); } } else { return suggestNewName( CharOperation.concat(SETTER_NAME, suggestAccessorName(project, fieldName, modifiers)), excludedNames ); } } /** * Suggest name for a setter method. The name is computed from field's name * and possible prefixes or suffixes are removed. *

* If the field name is preFieldNamesuf and the prefix for field is pre and * the suffix for field is suf then the proposed name is setFieldName. * If there is no prefix and suffix the proposal is setPreFieldNamesuf. *

*

* This method is affected by the following JavaCore options : {@link JavaCore#CODEASSIST_FIELD_PREFIXES}, * {@link JavaCore#CODEASSIST_FIELD_SUFFIXES} for instance field and {@link JavaCore#CODEASSIST_STATIC_FIELD_PREFIXES}, * {@link JavaCore#CODEASSIST_STATIC_FIELD_SUFFIXES} for static field. *

*

* For a complete description of these configurable options, see getDefaultOptions. * For programmaticaly change these options, see JavaCore#setOptions(). *

* * @param project project which contains the field. * @param fieldName field's name's. * @param modifiers field's modifiers as defined by the class * Flags. * @param isBoolean true if the field's type is boolean * @param excludedNames a list of names which cannot be suggested (already used names). * Can be null if there is no excluded names. * @return char[] a name. * @see Flags * @see JavaCore#setOptions(java.util.Hashtable) * @see JavaCore#getDefaultOptions() */ public static String suggestSetterName(IJavaProject project, String fieldName, int modifiers, boolean isBoolean, String[] excludedNames) { return String.valueOf( suggestSetterName( project, fieldName.toCharArray(), modifiers, 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 names are possible and each kind is associated to a different heuristic to compute variable names.
* The heuristic depends also on 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 names are: *

    *
  • {@link #BK_NAME}: the base name is a Java name and the whole base name is considered to compute the variable names. A prefix and a suffix can be added.
    * There is a heuristic by variable kind. *
      *
    • {@link #VK_PARAMETER}, {@link #VK_LOCAL}, {@link #VK_INSTANCE_FIELD} and {@link #VK_STATIC_FIELD}:
      * In this case the first word will be converted to lower case and the other characters won't be changed.
      * If the base name is SimpleName then the suggested name will be simpleName.
    • *
    • {@link #VK_STATIC_FINAL_FIELD} :
      * In this case all letters of the name will be converted to upper case and words will be separated by an underscore ("_").
      * If the base name is SimpleName then the suggested name will be SIMPLE_NAME.
    • *
  • *
  • {@link #BK_TYPE_NAME}: the base name is a Java simple type name (e.g. 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.
    * There is a heuristic by variable kind. *
      *
    • {@link #VK_PARAMETER}, {@link #VK_LOCAL}, {@link #VK_INSTANCE_FIELD} and {@link #VK_STATIC_FIELD}:
      * In this case a variable name will contain some words of the base name and the first word will be converted to lower case.
      * If the type is TypeName then the suggested names will be typeName and name.
    • *
    • {@link #VK_STATIC_FINAL_FIELD} :
      * In this case a variable name will contain some words of the base name, all letters of the name will be converted to upper case and segments will be separated by a underscore ("_").
      * If the base name is TypeName then the suggested name will be TYPE_NAME and NAME.
    • *
  • *
* Some other kinds could be added in the future. *

*

* Each variable kind is affected by the following JavaCore options: *

    *
  • {@link #VK_PARAMETER}: {@link JavaCore#CODEASSIST_ARGUMENT_PREFIXES} and {@link JavaCore#CODEASSIST_ARGUMENT_SUFFIXES}
  • *
  • {@link #VK_LOCAL}: {@link JavaCore#CODEASSIST_LOCAL_PREFIXES} and {@link JavaCore#CODEASSIST_LOCAL_SUFFIXES}
  • *
  • {@link #VK_INSTANCE_FIELD}: {@link JavaCore#CODEASSIST_FIELD_PREFIXES} and {@link JavaCore#CODEASSIST_FIELD_SUFFIXES}
  • *
  • {@link #VK_STATIC_FIELD}: {@link JavaCore#CODEASSIST_STATIC_FIELD_PREFIXES} and {@link JavaCore#CODEASSIST_STATIC_FIELD_SUFFIXES}
  • *
  • {@link #VK_STATIC_FINAL_FIELD}: {@link JavaCore#CODEASSIST_STATIC_FINAL_FIELD_PREFIXES} and {@link JavaCore#CODEASSIST_STATIC_FINAL_FIELD_SUFFIXES}
  • *
*

*

* For a complete description of these configurable options, see {@link JavaCore#getDefaultOptions()}. * To programmatically change these options, see {@link JavaCore#setOptions(java.util.Hashtable)} and {@link IJavaProject#setOptions(java.util.Map)} *

*

* Proposed names are sorted by relevance (best proposal first).
* The names are proposed in the following order: *

    *
  1. Names with prefix and suffix. Longer names are proposed first
  2. *
  3. Names with prefix. Longer names are proposed first
  4. *
  5. Names with suffix. Longer names are proposed first
  6. *
  7. Names without prefix and suffix. Longer names are proposed first
  8. *
*

* * @param variableKind specifies what type the variable is: {@link #VK_LOCAL}, {@link #VK_PARAMETER}, {@link #VK_STATIC_FIELD}, * {@link #VK_INSTANCE_FIELD} or {@link #VK_STATIC_FINAL_FIELD}. * @param baseNameKind specifies what type the base name is: {@link #BK_NAME} or {@link #BK_TYPE_NAME} * @param baseName name used to compute the suggested names. * @param javaProject project which contains the variable or 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 are no excluded names. * @param evaluateDefault if true, the result is guaranteed to contain at least one result. If false, 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); return convertCharsToString(requestor.getResults()); } private NamingConventions() { // Not instantiable } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy