}
@NotNull
@Override
public Collection<LombokProblem> verifyAnnotation(@NotNull PsiAnnotation psiAnnotation) {
final ProblemNewBuilder problemNewBuilder = new ProblemNewBuilder(2);
PsiMethod psiMethod = PsiTreeUtil.getParentOfType(psiAnnotation, PsiMethod.class);
if (null != psiMethod) {
if (psiMethod.hasModifierProperty(PsiModifier.ABSTRACT)) {
problemNewBuilder.addError("'@Synchronized' is legal only on concrete methods.",
PsiQuickFixFactory.createModifierListFix(psiMethod, PsiModifier.ABSTRACT, false, false)
);
}
final String lockFieldName = PsiAnnotationUtil.getAnnotationValue(psiAnnotation, "value", String.class);
if (StringUtil.isNotEmpty(lockFieldName)) {
final PsiClass containingClass = psiMethod.getContainingClass();
if (null != containingClass) {
final PsiField lockField = containingClass.findFieldByName(lockFieldName, true);
if (null != lockField) {
if (!lockField.hasModifierProperty(PsiModifier.FINAL)) {
problemNewBuilder.addWarning(String.format("Synchronization on a non-final field %s.", lockFieldName),
PsiQuickFixFactory.createModifierListFix(lockField, PsiModifier.FINAL, true, false));
}
UserMapKeys.addReadUsageFor(lockField);
} else {
final PsiClassType javaLangObjectType = PsiType.getJavaLangObject(containingClass.getManager(), GlobalSearchScope.allScope(containingClass.getProject()));
problemNewBuilder.addError(String.format("The field %s does not exist.", lockFieldName),
PsiQuickFixFactory.createNewFieldFix(containingClass, lockFieldName, javaLangObjectType, "new Object()", PsiModifier.PRIVATE, PsiModifier.FINAL));
}
}
}
} else {
problemNewBuilder.addError("'@Synchronized' is legal only on methods.");
}
return problemNewBuilder.getProblems();
}