use of com.intellij.codeInsight.NullableNotNullManager in project intellij-community by JetBrains.
the class AnnotateOverriddenMethodsIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element) {
final PsiAnnotation annotation = (PsiAnnotation) element;
final String annotationName = annotation.getQualifiedName();
if (annotationName == null) {
return;
}
final Project project = element.getProject();
final NullableNotNullManager notNullManager = NullableNotNullManager.getInstance(project);
final List<String> notNulls = notNullManager.getNotNulls();
final List<String> nullables = notNullManager.getNullables();
final List<String> annotationsToRemove;
if (notNulls.contains(annotationName)) {
annotationsToRemove = nullables;
} else if (nullables.contains(annotationName)) {
annotationsToRemove = notNulls;
} else {
annotationsToRemove = Collections.emptyList();
}
final PsiElement parent = annotation.getParent();
final PsiElement grandParent = parent.getParent();
final PsiMethod method;
final int parameterIndex;
if (!(grandParent instanceof PsiMethod)) {
if (!(grandParent instanceof PsiParameter)) {
return;
}
final PsiParameter parameter = (PsiParameter) grandParent;
final PsiElement greatGrandParent = grandParent.getParent();
if (!(greatGrandParent instanceof PsiParameterList)) {
return;
}
final PsiParameterList parameterList = (PsiParameterList) greatGrandParent;
parameterIndex = parameterList.getParameterIndex(parameter);
final PsiElement greatGreatGrandParent = greatGrandParent.getParent();
if (!(greatGreatGrandParent instanceof PsiMethod)) {
return;
}
method = (PsiMethod) greatGreatGrandParent;
} else {
parameterIndex = -1;
method = (PsiMethod) grandParent;
}
final Collection<PsiMethod> overridingMethods = OverridingMethodsSearch.search(method).findAll();
final List<PsiMethod> prepare = new ArrayList<>();
final ExternalAnnotationsManager annotationsManager = ExternalAnnotationsManager.getInstance(project);
for (PsiMethod overridingMethod : overridingMethods) {
if (annotationsManager.chooseAnnotationsPlace(overridingMethod) == ExternalAnnotationsManager.AnnotationPlace.IN_CODE) {
prepare.add(overridingMethod);
}
}
if (!FileModificationService.getInstance().preparePsiElementsForWrite(prepare)) {
return;
}
final PsiNameValuePair[] attributes = annotation.getParameterList().getAttributes();
try {
for (PsiMethod overridingMethod : overridingMethods) {
if (parameterIndex == -1) {
annotate(overridingMethod, annotationName, attributes, annotationsToRemove, annotationsManager);
} else {
final PsiParameterList parameterList = overridingMethod.getParameterList();
final PsiParameter[] parameters = parameterList.getParameters();
final PsiParameter parameter = parameters[parameterIndex];
annotate(parameter, annotationName, attributes, annotationsToRemove, annotationsManager);
}
}
} catch (ExternalAnnotationsManager.CanceledConfigurationException ignored) {
//escape on configuring root cancel further annotations
}
if (!prepare.isEmpty()) {
UndoUtil.markPsiFileForUndo(annotation.getContainingFile());
}
}
use of com.intellij.codeInsight.NullableNotNullManager in project intellij-community by JetBrains.
the class ReturnNullInspectionBase method buildFix.
@Override
@Nullable
protected InspectionGadgetsFix buildFix(Object... infos) {
final PsiElement elt = (PsiElement) infos[0];
if (!AnnotationUtil.isAnnotatingApplicable(elt)) {
return null;
}
final PsiElement element = PsiTreeUtil.getParentOfType(elt, PsiMethod.class, PsiLambdaExpression.class);
if (element instanceof PsiLambdaExpression) {
return null;
}
if (element instanceof PsiMethod) {
final PsiMethod method = (PsiMethod) element;
final PsiType type = method.getReturnType();
if (TypeUtils.isOptional(type)) {
// don't suggest to annotate Optional methods as Nullable
return new ReplaceWithEmptyOptionalFix(((PsiClassType) type).rawType().getCanonicalText());
}
}
final NullableNotNullManager manager = NullableNotNullManager.getInstance(elt.getProject());
return new DelegatingFix(new AnnotateMethodFix(manager.getDefaultNullable(), ArrayUtil.toStringArray(manager.getNotNulls())) {
@Override
public int shouldAnnotateBaseMethod(PsiMethod method, PsiMethod superMethod, Project project) {
return ReturnNullInspectionBase.this.shouldAnnotateBaseMethod(method, superMethod);
}
});
}
Aggregations