private String[] suggestVariableName(String type, String[] excludes) throws IllegalArgumentException { int dim = 0; while (type.endsWith("[]")) { // $NON-NLS-1$ dim++; type = type.substring(0, type.length() - 2); } IJavaProject project = getJavaProject(); if (project != null) return StubUtility.getVariableNameSuggestions( NamingConventions.VK_LOCAL, project, type, dim, Arrays.asList(excludes), true); // fallback if we lack proper context: roll-our own lowercasing return new String[] {Signature.getSimpleName(type).toLowerCase()}; }
/** * @return proposed variable names (may be empty, but not null). The first proposal should be used * as "best guess" (if it exists). */ public String[] guessTempNames() { if (fGuessedTempNames == null) { try { Expression expression = getSelectedExpression().getAssociatedExpression(); if (expression != null) { ITypeBinding binding = guessBindingForReference(expression); fGuessedTempNames = StubUtility.getVariableNameSuggestions( NamingConventions.VK_LOCAL, fCu.getJavaProject(), binding, expression, Arrays.asList(getExcludedVariableNames())); } } catch (JavaModelException e) { } if (fGuessedTempNames == null) fGuessedTempNames = new String[0]; } return fGuessedTempNames; }
/** * @return proposed variable names (may be empty, but not null). The first proposal should be used * as "best guess" (if it exists). */ public String[] guessConstantNames() { if (fGuessedConstNames == null) { try { Expression expression = getSelectedExpression().getAssociatedExpression(); if (expression != null) { ITypeBinding binding = guessBindingForReference(expression); fGuessedConstNames = StubUtility.getVariableNameSuggestions( NamingConventions.VK_STATIC_FINAL_FIELD, fCu.getJavaProject(), binding, expression, Arrays.asList(getExcludedVariableNames())); } } catch (JavaModelException e) { } if (fGuessedConstNames == null) fGuessedConstNames = new String[0]; } return fGuessedConstNames; }
public ExtractClassRefactoring(ExtractClassDescriptor descriptor) { fDescriptor= descriptor; IType type= fDescriptor.getType(); if (fDescriptor.getPackage() == null) { fDescriptor.setPackage(type.getPackageFragment().getElementName()); } if (fDescriptor.getClassName() == null) { fDescriptor.setClassName(type.getElementName() + "Data"); //$NON-NLS-1$ } if (fDescriptor.getFieldName() == null) { fDescriptor.setFieldName(StubUtility.getVariableNameSuggestions(NamingConventions.VK_INSTANCE_FIELD, type.getJavaProject(), "data", 0, null, true)[0]); //$NON-NLS-1$ } if (fDescriptor.getFields() == null) { try { fDescriptor.setFields(ExtractClassDescriptor.getFields(type)); } catch (JavaModelException e) { JavaPlugin.log(e); } } fVerification= new ExtractClassDescriptorVerification(descriptor); }
/** * @return proposed variable names (may be empty, but not null). The first proposal should be used as "best guess" (if it exists). */ public String[] guessTempNames() { if (fGuessedTempNames == null) { try { Expression expression= getSelectedExpression().getAssociatedExpression(); if (expression != null) { ITypeBinding binding= guessBindingForReference(expression); fGuessedTempNames= StubUtility.getVariableNameSuggestions(NamingConventions.VK_LOCAL, fCu.getJavaProject(), binding, expression, Arrays.asList(getExcludedVariableNames())); } } catch (JavaModelException e) { } if (fGuessedTempNames == null) fGuessedTempNames= new String[0]; } return fGuessedTempNames; }
/** * @return proposed variable names (may be empty, but not null). * The first proposal should be used as "best guess" (if it exists). */ public String[] guessConstantNames() { if (fGuessedConstNames == null) { try { Expression expression= getSelectedExpression().getAssociatedExpression(); if (expression != null) { ITypeBinding binding= guessBindingForReference(expression); fGuessedConstNames= StubUtility.getVariableNameSuggestions(NamingConventions.VK_STATIC_FINAL_FIELD, fCu.getJavaProject(), binding, expression, Arrays.asList(getExcludedVariableNames())); } } catch (JavaModelException e) { } if (fGuessedConstNames == null) fGuessedConstNames= new String[0]; } return fGuessedConstNames; }
@Override protected void initialize() { super.initialize(); fBuilderVariableName= createNameSuggestion(getContext().is50orHigher() ? "builder" : "buffer", NamingConventions.VK_LOCAL); //$NON-NLS-1$ //$NON-NLS-2$ fBuffer= new StringBuffer(); VariableDeclarationFragment fragment= fAst.newVariableDeclarationFragment(); fragment.setName(fAst.newSimpleName(fBuilderVariableName)); ClassInstanceCreation classInstance= fAst.newClassInstanceCreation(); Name typeName= addImport(getContext().is50orHigher() ? "java.lang.StringBuilder" : "java.lang.StringBuffer"); //$NON-NLS-1$ //$NON-NLS-2$ classInstance.setType(fAst.newSimpleType(typeName)); fragment.setInitializer(classInstance); VariableDeclarationStatement vStatement= fAst.newVariableDeclarationStatement(fragment); vStatement.setType(fAst.newSimpleType((Name)ASTNode.copySubtree(fAst, typeName))); toStringMethod.getBody().statements().add(vStatement); }
/** * This method initializes all variables used in the process of generating <code>toString</code> * method. */ protected void initialize() { needMaxLenVariable= false; needCollectionToStringMethod= false; typesThatNeedArrayToStringMethod= new ArrayList<ITypeBinding>(); checkNeedForHelperMethods(); toStringMethod= fAst.newMethodDeclaration(); toStringMethod.modifiers().addAll(ASTNodeFactory.newModifiers(fAst, Modifier.PUBLIC)); toStringMethod.setName(fAst.newSimpleName(METHODNAME_TO_STRING)); toStringMethod.setConstructor(false); toStringMethod.setReturnType2(fAst.newSimpleType(fAst.newName(TYPENAME_STRING))); Block body= fAst.newBlock(); toStringMethod.setBody(body); fMaxLenVariableName= createNameSuggestion(MAX_LEN_VARIABLE_NAME, NamingConventions.VK_LOCAL); }
public static String[][] suggestArgumentNamesWithProposals(IJavaProject project, String[] paramNames) { String[][] newNames= new String[paramNames.length][]; ArrayList<String> takenNames= new ArrayList<String>(); // Ensure that the code generation preferences are respected for (int i= 0; i < paramNames.length; i++) { String curr= paramNames[i]; String baseName= NamingConventions.getBaseName(NamingConventions.VK_PARAMETER, curr, project); String[] proposedNames= getVariableNameSuggestions(NamingConventions.VK_PARAMETER, project, curr, 0, takenNames, true); if (!curr.equals(baseName)) { // make the existing name to favorite LinkedHashSet<String> updatedNames= new LinkedHashSet<String>(); updatedNames.add(curr); for (int k= 0; k < proposedNames.length; k++) { updatedNames.add(proposedNames[k]); } proposedNames= updatedNames.toArray(new String[updatedNames.size()]); } newNames[i]= proposedNames; takenNames.add(proposedNames[0]); } return newNames; }
public String[] suggestFieldName(String type, String[] excludes, boolean staticField, boolean finalField) throws IllegalArgumentException { int dim = 0; while (type.endsWith("[]")) { dim++; type = type.substring(0, type.length() - 2); } IJavaProject project = getJavaProject(); int namingConventions = 0; if (staticField && finalField) { namingConventions = NamingConventions.VK_STATIC_FINAL_FIELD; } else if (staticField && !finalField) { namingConventions = NamingConventions.VK_STATIC_FIELD; } else { namingConventions = NamingConventions.VK_INSTANCE_FIELD; } if (project != null) return StubUtility.getVariableNameSuggestions(namingConventions, project, type, dim, Arrays.asList(excludes), true); return new String[] {Signature.getSimpleName(type).toLowerCase()}; }
private static String[] getDefaultVariableNameSuggestions(int variableKind, Collection<String> excluded) { String prop= variableKind == NamingConventions.VK_STATIC_FINAL_FIELD ? "X" : "x"; //$NON-NLS-1$//$NON-NLS-2$ String name= prop; int i= 1; while (excluded.contains(name)) { name= prop + i++; } return new String[] { name }; }
private static String getBaseNameFromLocationInParent(Expression assignedExpression, List<Expression> arguments, IMethodBinding binding) { if (binding == null) { return null; } ITypeBinding[] parameterTypes= binding.getParameterTypes(); if (parameterTypes.length != arguments.size()) { return null; } int index= arguments.indexOf(assignedExpression); if (index == -1) { return null; } ITypeBinding expressionBinding= assignedExpression.resolveTypeBinding(); if (expressionBinding != null && !expressionBinding.isAssignmentCompatible(parameterTypes[index])) { return null; } try { IJavaElement javaElement= binding.getJavaElement(); if (javaElement instanceof IMethod) { IMethod method= (IMethod)javaElement; if (method.getOpenable().getBuffer() != null) { // avoid dummy names and lookup from Javadoc String[] parameterNames= method.getParameterNames(); if (index < parameterNames.length) { return NamingConventions.getBaseName(NamingConventions.VK_PARAMETER, parameterNames[index], method.getJavaProject()); } } } } catch (JavaModelException e) { // ignore } return null; }
public static String[] getFieldNameSuggestions(IJavaProject project, String baseName, int dimensions, int modifiers, String[] excluded) { if (Flags.isFinal(modifiers) && Flags.isStatic(modifiers)) { return getVariableNameSuggestions(NamingConventions.VK_STATIC_FINAL_FIELD, project, baseName, dimensions, new ExcludedCollection(excluded), true); } else if (Flags.isStatic(modifiers)) { return getVariableNameSuggestions(NamingConventions.VK_STATIC_FIELD, project, baseName, dimensions, new ExcludedCollection(excluded), true); } return getVariableNameSuggestions(NamingConventions.VK_INSTANCE_FIELD, project, baseName, dimensions, new ExcludedCollection(excluded), true); }
public static String[] suggestArgumentNames(IJavaProject project, IMethodBinding binding) { int nParams = binding.getParameterTypes().length; if (nParams > 0) { try { IMethod method = (IMethod) binding.getMethodDeclaration().getJavaElement(); if (method != null) { String[] paramNames = method.getParameterNames(); if (paramNames.length == nParams) { String[] namesArray = EMPTY; ArrayList<String> newNames = new ArrayList<>(paramNames.length); // Ensure that the code generation preferences are respected for (int i = 0; i < paramNames.length; i++) { String curr = paramNames[i]; String baseName = NamingConventions.getBaseName(NamingConventions.VK_PARAMETER, curr, method.getJavaProject()); if (!curr.equals(baseName)) { // make the existing name the favorite newNames.add(curr); } else { newNames.add(suggestArgumentName(project, curr, namesArray)); } namesArray = newNames.toArray(new String[newNames.size()]); } return namesArray; } } } catch (JavaModelException e) { // ignore } } String[] names = new String[nParams]; for (int i = 0; i < names.length; i++) { names[i] = "arg" + i; //$NON-NLS-1$ } return names; }
/** * Returns the kind of the given binding. * * @param binding * variable binding * @return one of the <code>NamingConventions.VK_*</code> constants * @since 3.5 */ private static int getKind(IVariableBinding binding) { if (binding.isField()) { return getFieldKind(binding.getModifiers()); } if (binding.isParameter()) { return NamingConventions.VK_PARAMETER; } return NamingConventions.VK_LOCAL; }
private static int getFieldKind(int modifiers) { if (!Modifier.isStatic(modifiers)) { return NamingConventions.VK_INSTANCE_FIELD; } if (!Modifier.isFinal(modifiers)) { return NamingConventions.VK_STATIC_FIELD; } return NamingConventions.VK_STATIC_FINAL_FIELD; }
private String evaluateParameterName(List<String> takenNames, Expression argNode, Type type, String key) { IJavaProject project= getCompilationUnit().getJavaProject(); String[] names = StubUtility.getVariableNameSuggestions(NamingConventions.VK_PARAMETER, project, type, argNode, takenNames); String favourite= names[0]; takenNames.add(favourite); return favourite; }
private static String[] getDefaultVariableNameSuggestions( int variableKind, Collection<String> excluded) { String prop = variableKind == NamingConventions.VK_STATIC_FINAL_FIELD ? "X" : "x"; // $NON-NLS-1$//$NON-NLS-2$ String name = prop; int i = 1; while (excluded.contains(name)) { name = prop + i++; } return new String[] {name}; }
private static String getBaseNameFromLocationInParent( Expression assignedExpression, List<Expression> arguments, IMethodBinding binding) { if (binding == null) return null; ITypeBinding[] parameterTypes = binding.getParameterTypes(); if (parameterTypes.length != arguments.size()) // beware of guessed method bindings return null; int index = arguments.indexOf(assignedExpression); if (index == -1) return null; ITypeBinding expressionBinding = assignedExpression.resolveTypeBinding(); if (expressionBinding != null && !expressionBinding.isAssignmentCompatible(parameterTypes[index])) return null; try { IJavaElement javaElement = binding.getJavaElement(); if (javaElement instanceof IMethod) { IMethod method = (IMethod) javaElement; if (method.getOpenable().getBuffer() != null) { // avoid dummy names and lookup from Javadoc String[] parameterNames = method.getParameterNames(); if (index < parameterNames.length) { return NamingConventions.getBaseName( NamingConventions.VK_PARAMETER, parameterNames[index], method.getJavaProject()); } } } } catch (JavaModelException e) { // ignore } return null; }
public static String[] getArgumentNameSuggestions(IType type, String[] excluded) { return getVariableNameSuggestions( NamingConventions.VK_PARAMETER, type.getJavaProject(), type.getElementName(), 0, new ExcludedCollection(excluded), true); }
public static String[] getArgumentNameSuggestions( IJavaProject project, String baseName, int dimensions, String[] excluded) { return getVariableNameSuggestions( NamingConventions.VK_PARAMETER, project, baseName, dimensions, new ExcludedCollection(excluded), true); }
public static String[] getFieldNameSuggestions( IJavaProject project, String baseName, int dimensions, int modifiers, String[] excluded) { if (Flags.isFinal(modifiers) && Flags.isStatic(modifiers)) { return getVariableNameSuggestions( NamingConventions.VK_STATIC_FINAL_FIELD, project, baseName, dimensions, new ExcludedCollection(excluded), true); } else if (Flags.isStatic(modifiers)) { return getVariableNameSuggestions( NamingConventions.VK_STATIC_FIELD, project, baseName, dimensions, new ExcludedCollection(excluded), true); } return getVariableNameSuggestions( NamingConventions.VK_INSTANCE_FIELD, project, baseName, dimensions, new ExcludedCollection(excluded), true); }
public static String[] getLocalNameSuggestions( IJavaProject project, String baseName, int dimensions, String[] excluded) { return getVariableNameSuggestions( NamingConventions.VK_LOCAL, project, baseName, dimensions, new ExcludedCollection(excluded), true); }
public static String[][] suggestArgumentNamesWithProposals( IJavaProject project, String[] paramNames) { String[][] newNames = new String[paramNames.length][]; ArrayList<String> takenNames = new ArrayList<String>(); // Ensure that the code generation preferences are respected for (int i = 0; i < paramNames.length; i++) { String curr = paramNames[i]; String baseName = NamingConventions.getBaseName(NamingConventions.VK_PARAMETER, curr, project); String[] proposedNames = getVariableNameSuggestions( NamingConventions.VK_PARAMETER, project, curr, 0, takenNames, true); if (!curr.equals(baseName)) { // make the existing name to favorite LinkedHashSet<String> updatedNames = new LinkedHashSet<String>(); updatedNames.add(curr); for (int k = 0; k < proposedNames.length; k++) { updatedNames.add(proposedNames[k]); } proposedNames = updatedNames.toArray(new String[updatedNames.size()]); } newNames[i] = proposedNames; takenNames.add(proposedNames[0]); } return newNames; }
public static String[] suggestArgumentNames(IJavaProject project, IMethodBinding binding) { int nParams = binding.getParameterTypes().length; if (nParams > 0) { try { IMethod method = (IMethod) binding.getMethodDeclaration().getJavaElement(); if (method != null) { String[] paramNames = method.getParameterNames(); if (paramNames.length == nParams) { String[] namesArray = EMPTY; ArrayList<String> newNames = new ArrayList<String>(paramNames.length); // Ensure that the code generation preferences are respected for (int i = 0; i < paramNames.length; i++) { String curr = paramNames[i]; String baseName = NamingConventions.getBaseName( NamingConventions.VK_PARAMETER, curr, method.getJavaProject()); if (!curr.equals(baseName)) { // make the existing name the favorite newNames.add(curr); } else { newNames.add(suggestArgumentName(project, curr, namesArray)); } namesArray = newNames.toArray(new String[newNames.size()]); } return namesArray; } } } catch (JavaModelException e) { // ignore e.printStackTrace(); } } String[] names = new String[nParams]; for (int i = 0; i < names.length; i++) { names[i] = "arg" + i; // $NON-NLS-1$ } return names; }
/** * Returns the kind of the given binding. * * @param binding variable binding * @return one of the <code>NamingConventions.VK_*</code> constants * @since 3.5 */ private static int getKind(IVariableBinding binding) { if (binding.isField()) return getFieldKind(binding.getModifiers()); if (binding.isParameter()) return NamingConventions.VK_PARAMETER; return NamingConventions.VK_LOCAL; }
public static String getGetterName( IJavaProject project, String fieldName, int flags, boolean isBoolean, String[] excludedNames) { return NamingConventions.suggestGetterName(project, fieldName, flags, isBoolean, excludedNames); }
public static String getSetterName( IJavaProject project, String fieldName, int flags, boolean isBoolean, String[] excludedNames) { boolean useIs = StubUtility.useIsForBooleanGetters(project); return NamingConventions.suggestSetterName( project, fieldName, flags, useIs && isBoolean, excludedNames); }
private String[] suggestFieldNames(ITypeBinding binding, Expression expression, int modifiers) { IJavaProject project = getCompilationUnit().getJavaProject(); int varKind = Modifier.isStatic(modifiers) ? NamingConventions.VK_STATIC_FIELD : NamingConventions.VK_INSTANCE_FIELD; return StubUtility.getVariableNameSuggestions( varKind, project, binding, expression, getUsedVariableNames()); }
private String evaluateParameterName( List<String> takenNames, Expression argNode, Type type, String key) { IJavaProject project = getCompilationUnit().getJavaProject(); String[] names = StubUtility.getVariableNameSuggestions( NamingConventions.VK_PARAMETER, project, type, argNode, takenNames); for (int i = 0; i < names.length; i++) { addLinkedPositionProposal(key, names[i], null); } String favourite = names[0]; takenNames.add(favourite); return favourite; }
private MethodDeclaration createGetter(ParameterInfo pi, String declaringType, CompilationUnitRewrite cuRewrite) throws CoreException { AST ast= cuRewrite.getAST(); ICompilationUnit cu= cuRewrite.getCu(); IJavaProject project= cu.getJavaProject(); MethodDeclaration methodDeclaration= ast.newMethodDeclaration(); String fieldName= pi.getNewName(); String getterName= getGetterName(pi, ast, project); String lineDelim= StubUtility.getLineDelimiterUsed(cu); String bareFieldname= NamingConventions.getBaseName(NamingConventions.VK_INSTANCE_FIELD, fieldName, project); if (createComments(project)) { String comment= CodeGeneration.getGetterComment(cu, declaringType, getterName, fieldName, pi.getNewTypeName(), bareFieldname, lineDelim); if (comment != null) methodDeclaration.setJavadoc((Javadoc) cuRewrite.getASTRewrite().createStringPlaceholder(comment, ASTNode.JAVADOC)); } methodDeclaration.setName(ast.newSimpleName(getterName)); methodDeclaration.setReturnType2(importBinding(pi.getNewTypeBinding(), cuRewrite)); methodDeclaration.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD)); Block block= ast.newBlock(); methodDeclaration.setBody(block); boolean useThis= StubUtility.useThisForFieldAccess(project); if (useThis) { fieldName= "this." + fieldName; //$NON-NLS-1$ } String bodyContent= CodeGeneration.getGetterMethodBodyContent(cu, declaringType, getterName, fieldName, lineDelim); ASTNode getterBody= cuRewrite.getASTRewrite().createStringPlaceholder(bodyContent, ASTNode.EXPRESSION_STATEMENT); block.statements().add(getterBody); return methodDeclaration; }
private MethodDeclaration createSetter(ParameterInfo pi, String declaringType, CompilationUnitRewrite cuRewrite) throws CoreException { AST ast= cuRewrite.getAST(); ICompilationUnit cu= cuRewrite.getCu(); IJavaProject project= cu.getJavaProject(); MethodDeclaration methodDeclaration= ast.newMethodDeclaration(); String fieldName= pi.getNewName(); String setterName= getSetterName(pi, ast, project); String lineDelim= StubUtility.getLineDelimiterUsed(cu); String bareFieldname= NamingConventions.getBaseName(NamingConventions.VK_INSTANCE_FIELD, fieldName, project); String paramName= StubUtility.suggestArgumentName(project, bareFieldname, null); if (createComments(project)) { String comment= CodeGeneration.getSetterComment(cu, declaringType, setterName, fieldName, pi.getNewTypeName(), paramName, bareFieldname, lineDelim); if (comment != null) methodDeclaration.setJavadoc((Javadoc) cuRewrite.getASTRewrite().createStringPlaceholder(comment, ASTNode.JAVADOC)); } methodDeclaration.setName(ast.newSimpleName(setterName)); methodDeclaration.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD)); SingleVariableDeclaration variable= ast.newSingleVariableDeclaration(); variable.setType(importBinding(pi.getNewTypeBinding(), cuRewrite)); variable.setName(ast.newSimpleName(paramName)); methodDeclaration.parameters().add(variable); Block block= ast.newBlock(); methodDeclaration.setBody(block); boolean useThis= StubUtility.useThisForFieldAccess(project); if (useThis || fieldName.equals(paramName)) { fieldName= "this." + fieldName; //$NON-NLS-1$ } String bodyContent= CodeGeneration.getSetterMethodBodyContent(cu, declaringType, setterName, fieldName, paramName, lineDelim); ASTNode setterBody= cuRewrite.getASTRewrite().createStringPlaceholder(bodyContent, ASTNode.EXPRESSION_STATEMENT); block.statements().add(setterBody); return methodDeclaration; }
private String[] suggestVariableName(String type, String[] excludes) throws IllegalArgumentException { int dim=0; while (type.endsWith("[]")) {//$NON-NLS-1$ dim++; type= type.substring(0, type.length() - 2); } IJavaProject project= getJavaProject(); if (project != null) return StubUtility.getVariableNameSuggestions(NamingConventions.VK_LOCAL, project, type, dim, Arrays.asList(excludes), true); // fallback if we lack proper context: roll-our own lowercasing return new String[] {Signature.getSimpleName(type).toLowerCase()}; }