return generateSetterPrototype(field, field.getContainingClass());
}
public static PsiMethod generateSetterPrototype(PsiField field, final PsiClass containingClass) {
Project project = field.getProject();
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
PsiElementFactory factory = field.getManager().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, PsiType.VOID);
String parameterName = codeStyleManager.propertyNameToVariableName(propertyName, VariableKind.PARAMETER);
PsiParameter param = factory.createParameter(parameterName, field.getType());
annotateWithNullableStuff(field, factory, param);
setMethod.getParameterList().add(param);
setMethod.getModifierList().setModifierProperty(PsiModifier.PUBLIC, true);
setMethod.getModifierList().setModifierProperty(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}");
PsiCodeBlock body = factory.createCodeBlockFromText(buffer.toString(), null);
setMethod.getBody().replace(body);
setMethod = (PsiMethod)codeStyleManager.reformat(setMethod);
return setMethod;
}
catch (IncorrectOperationException e) {
LOG.error(e);
return null;