static String suggestUniqueParameterName( JavaCodeStyleManager codeStyleManager, PsiExpression expression, PsiType exprType, Set<String> existingNames) { SuggestedNameInfo nameInfo = codeStyleManager.suggestVariableName(VariableKind.PARAMETER, null, expression, exprType); @NonNls String[] names = nameInfo.names; if (expression instanceof PsiReferenceExpression) { final PsiElement resolve = ((PsiReferenceExpression) expression).resolve(); if (resolve instanceof PsiVariable) { final VariableKind variableKind = codeStyleManager.getVariableKind((PsiVariable) resolve); final String propertyName = codeStyleManager.variableNameToPropertyName( ((PsiVariable) resolve).getName(), variableKind); final String parameterName = codeStyleManager.propertyNameToVariableName(propertyName, VariableKind.PARAMETER); names = ArrayUtil.mergeArrays(new String[] {parameterName}, names); } } if (names.length == 0) names = new String[] {"param"}; int suffix = 0; while (true) { for (String name : names) { String suggested = name + (suffix == 0 ? "" : String.valueOf(suffix)); if (existingNames.add(suggested)) { return suggested; } } suffix++; } }
public static PsiMethod generateSetterPrototype( PsiField field, final PsiClass containingClass, boolean returnSelf) { Project project = field.getProject(); JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project); PsiElementFactory factory = JavaPsiFacade.getInstance(field.getProject()).getElementFactory(); String name = field.getName(); boolean isStatic = field.hasModifierProperty(PsiModifier.STATIC); VariableKind kind = codeStyleManager.getVariableKind(field); String propertyName = codeStyleManager.variableNameToPropertyName(name, kind); String setName = suggestSetterName(project, field); try { PsiMethod setMethod = factory.createMethod( setName, returnSelf ? factory.createType(containingClass) : PsiType.VOID); String parameterName = codeStyleManager.propertyNameToVariableName(propertyName, VariableKind.PARAMETER); PsiParameter param = factory.createParameter(parameterName, field.getType()); annotateWithNullableStuff(field, factory, param); setMethod.getParameterList().add(param); PsiUtil.setModifierProperty(setMethod, PsiModifier.PUBLIC, true); PsiUtil.setModifierProperty(setMethod, PsiModifier.STATIC, isStatic); @NonNls StringBuffer buffer = new StringBuffer(); buffer.append("{\n"); if (name.equals(parameterName)) { if (!isStatic) { buffer.append("this."); } else { String className = containingClass.getName(); if (className != null) { buffer.append(className); buffer.append("."); } } } buffer.append(name); buffer.append("="); buffer.append(parameterName); buffer.append(";\n"); if (returnSelf) { buffer.append("return this;\n"); } buffer.append("}"); PsiCodeBlock body = factory.createCodeBlockFromText(buffer.toString(), null); setMethod.getBody().replace(body); setMethod = (PsiMethod) CodeStyleManager.getInstance(project).reformat(setMethod); return setMethod; } catch (IncorrectOperationException e) { LOG.error(e); return null; } }
@Nullable private static String getLookupObjectName(Object o) { if (o instanceof PsiVariable) { final PsiVariable variable = (PsiVariable) o; JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(variable.getProject()); VariableKind variableKind = codeStyleManager.getVariableKind(variable); return codeStyleManager.variableNameToPropertyName(variable.getName(), variableKind); } if (o instanceof PsiMethod) { return ((PsiMethod) o).getName(); } return null; }
private PsiMethod generateMethodPrototype(PsiField field) { Project project = field.getProject(); JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project); PsiElementFactory elementFactory = JavaPsiFacade.getInstance(project).getElementFactory(); String propertyName = codeStyleManager.variableNameToPropertyName( field.getName(), codeStyleManager.getVariableKind(field)); String methodName = methodNameGenerator.generateMethodNameFor(propertyName); String parameterName = codeStyleManager.propertyNameToVariableName(propertyName, VariableKind.PARAMETER); PsiMethod withMethod = generateMethodFor(field, methodName, parameterName, elementFactory); generateMethodBodyFor(withMethod, propertyName, parameterName, elementFactory); return withMethod; }
public static String suggestPropertyName(Project project, PsiField field) { JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project); VariableKind kind = codeStyleManager.getVariableKind(field); return codeStyleManager.variableNameToPropertyName(field.getName(), kind); }