assert psiClass != null;
UserMapKeys.addWriteUsageFor(psiField);
PsiType returnType = getReturnType(psiField);
LombokLightMethodBuilder method = new LombokLightMethodBuilder(psiField.getManager(), methodName)
.withMethodReturnType(returnType)
.withContainingClass(psiClass)
.withParameter(fieldName, psiFieldType)
.withNavigationElement(psiField);
if (StringUtil.isNotEmpty(methodModifier)) {
method.withModifier(methodModifier);
}
boolean isStatic = psiField.hasModifierProperty(PsiModifier.STATIC);
if (isStatic) {
method.withModifier(PsiModifier.STATIC);
}
PsiParameter methodParameter = method.getParameterList().getParameters()[0];
PsiModifierList methodParameterModifierList = methodParameter.getModifierList();
if (null != methodParameterModifierList) {
final Collection<String> annotationsToCopy = PsiAnnotationUtil.collectAnnotationsToCopy(psiField,
LombokUtils.NON_NULL_PATTERN, LombokUtils.NULLABLE_PATTERN);
for (String annotationFQN : annotationsToCopy) {
methodParameterModifierList.addAnnotation(annotationFQN);
}
}
final String thisOrClass = isStatic ? psiClass.getName() : "this";
String blockText = String.format("%s.%s = %s;", thisOrClass, psiField.getName(), methodParameter.getName());
if (!isStatic && !PsiType.VOID.equals(returnType)) {
blockText += "return this;";
}
method.withBody(PsiMethodUtil.createCodeBlockFromText(blockText, psiClass));
copyAnnotations(psiField, method.getModifierList(), LombokUtils.DEPRECATED_PATTERN);
return method;
}