leaks in AddAnnotationFix

This commit is contained in:
Alexey Kudravtsev
2011-04-15 15:46:29 +04:00
parent 891dea62bd
commit a1aeacd499
20 changed files with 389 additions and 226 deletions
@@ -140,7 +140,8 @@ public class SuppressFix extends SuppressIntentionAction {
if (newAnnotation != null) {
if (annotation != null && annotation.isPhysical()) {
annotation.replace(newAnnotation);
} else {
}
else {
final PsiNameValuePair[] attributes = newAnnotation.getParameterList().getAttributes();
new AddAnnotationFix(SuppressManager.SUPPRESS_INSPECTIONS_ANNOTATION_NAME, modifierOwner, attributes).invoke(project, editor, container.getContainingFile());
}
@@ -20,71 +20,59 @@ import com.intellij.codeInsight.AnnotationUtil;
import com.intellij.codeInsight.CodeInsightBundle;
import com.intellij.codeInsight.CodeInsightUtilBase;
import com.intellij.codeInsight.ExternalAnnotationsManager;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement;
import com.intellij.lang.findUsages.FindUsagesProvider;
import com.intellij.lang.findUsages.LanguageFindUsages;
import com.intellij.openapi.command.undo.UndoUtil;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.CaretModel;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author ven
*/
public class AddAnnotationFix extends PsiElementBaseIntentionAction implements LocalQuickFix {
private final String myAnnotation;
private final PsiModifierListOwner myModifierListOwner;
public class AddAnnotationFix extends LocalQuickFixAndIntentionActionOnPsiElement {
protected final String myAnnotation;
private final String[] myAnnotationsToRemove;
private final PsiNameValuePair[] myPairs;
private final PsiNameValuePair[] myPairs; // not used when registering local quick fix
private static final Logger LOG = Logger.getInstance("#" + AddAnnotationFix.class.getName());
private final String myText;
public AddAnnotationFix(String fqn, PsiModifierListOwner modifierListOwner, String... annotationsToRemove) {
myAnnotation = fqn;
myModifierListOwner = modifierListOwner;
myAnnotationsToRemove = annotationsToRemove;
myPairs = null;
public AddAnnotationFix(@NotNull String fqn, @NotNull PsiModifierListOwner modifierListOwner, @NotNull String... annotationsToRemove) {
this(fqn, modifierListOwner, PsiNameValuePair.EMPTY_ARRAY, annotationsToRemove);
}
public AddAnnotationFix(String fqn, PsiModifierListOwner modifierListOwner, PsiNameValuePair[] values, String... annotationsToRemove) {
public AddAnnotationFix(@NotNull String fqn, @NotNull PsiModifierListOwner modifierListOwner, @NotNull PsiNameValuePair[] values, @NotNull String... annotationsToRemove) {
super(modifierListOwner);
myAnnotation = fqn;
myModifierListOwner = modifierListOwner;
myAnnotationsToRemove = annotationsToRemove;
myPairs = values;
myText = calcText(modifierListOwner, myAnnotation);
}
public AddAnnotationFix(@NonNls final String fqn, @NonNls String... annotationsToRemove) {
this(fqn, null,annotationsToRemove);
public static String calcText(PsiModifierListOwner modifierListOwner, @NotNull String annotation) {
final String shortName = annotation.substring(annotation.lastIndexOf('.') + 1);
if (modifierListOwner instanceof PsiNamedElement) {
final String name = ((PsiNamedElement)modifierListOwner).getName();
if (name != null) {
FindUsagesProvider provider = LanguageFindUsages.INSTANCE.forLanguage(modifierListOwner.getLanguage());
return CodeInsightBundle
.message("inspection.i18n.quickfix.annotate.element.as", provider.getType(modifierListOwner), name, shortName);
}
}
return CodeInsightBundle.message("inspection.i18n.quickfix.annotate.as", shortName);
}
@NotNull
public String getText() {
final String shortName = myAnnotation.substring(myAnnotation.lastIndexOf('.') + 1);
if (myModifierListOwner instanceof PsiNamedElement) {
final String name = ((PsiNamedElement)myModifierListOwner).getName();
if (name != null) {
FindUsagesProvider provider = LanguageFindUsages.INSTANCE.forLanguage(myModifierListOwner.getLanguage());
return CodeInsightBundle.message("inspection.i18n.quickfix.annotate.element.as", provider.getType(myModifierListOwner), name, shortName);
}
}
return myModifierListOwner != null ?
CodeInsightBundle.message("inspection.i18n.quickfix.annotate.as", shortName) :
CodeInsightBundle.message("add.external.annotation.test", shortName);
}
@NotNull
public String getName() {
return getText();
return myText;
}
@NotNull
@@ -92,17 +80,13 @@ public class AddAnnotationFix extends PsiElementBaseIntentionAction implements L
return CodeInsightBundle.message("intention.add.annotation.family");
}
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
try {
invoke(project, null, descriptor.getPsiElement().getContainingFile());
}
catch (IncorrectOperationException e) {
LOG.error(e);
}
@Override
public boolean startInWriteAction() {
return true;
}
@Nullable
protected static PsiModifierListOwner getContainer(final PsiElement element) {
public static PsiModifierListOwner getContainer(final PsiElement element) {
PsiModifierListOwner listOwner = PsiTreeUtil.getParentOfType(element, PsiParameter.class, false);
if (listOwner == null) {
final PsiIdentifier psiIdentifier = PsiTreeUtil.getParentOfType(element, PsiIdentifier.class, false);
@@ -113,86 +97,60 @@ public class AddAnnotationFix extends PsiElementBaseIntentionAction implements L
return listOwner;
}
public boolean isAvailable(@NotNull final Project project, final Editor editor, @NotNull final PsiElement element) {
if (!element.isValid()) return false;
if (!PsiUtil.isLanguageLevel5OrHigher(element)) return false;
final PsiModifierListOwner owner;
if (myModifierListOwner != null) {
if (!myModifierListOwner.isValid()) return false;
//if (!PsiManager.getInstance(project).isInProject(myModifierListOwner)
// || myModifierListOwner.getModifierList() == null) {
// if (!myModifierListOwner.isPhysical()) { //we might want to apply fix to just created method
// return true;
// }
//}
owner = myModifierListOwner;
}
else if (!element.getManager().isInProject(element) || CodeStyleSettingsManager.getSettings(project).USE_EXTERNAL_ANNOTATIONS) {
owner = getContainer(element);
}
else {
owner = null;
}
return owner != null && !AnnotationUtil.isAnnotated(owner, myAnnotation, false);
@Override
public boolean isAvailable(@NotNull Project project,
@NotNull PsiFile file,
@NotNull PsiElement startElement,
@NotNull PsiElement endElement) {
if (!startElement.isValid()) return false;
if (!PsiUtil.isLanguageLevel5OrHigher(startElement)) return false;
final PsiModifierListOwner myModifierListOwner = (PsiModifierListOwner)startElement;
return !AnnotationUtil.isAnnotated(myModifierListOwner, myAnnotation, false);
}
@Override
public boolean isAvailable(@NotNull final Project project, final Editor editor, final PsiFile file) {
final PsiElement element;
if (myModifierListOwner != null) {
element = myModifierListOwner;
}
else {
final CaretModel caretModel = editor.getCaretModel();
final int position = caretModel.getOffset();
element = file.findElementAt(position);
}
return element != null && isAvailable(project, editor, element);
}
public void invoke(@NotNull Project project,
@NotNull PsiFile file,
@Nullable("is null when called from inspection") Editor editor,
@NotNull PsiElement startElement,
@NotNull PsiElement endElement) {
final PsiModifierListOwner myModifierListOwner = (PsiModifierListOwner)startElement;
public void invoke(@NotNull final Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
final ExternalAnnotationsManager annotationsManager = ExternalAnnotationsManager.getInstance(project);
if (myModifierListOwner != null) {
final PsiModifierList modifierList = myModifierListOwner.getModifierList();
LOG.assertTrue(modifierList != null);
if (modifierList.findAnnotation(myAnnotation) != null) return;
final ExternalAnnotationsManager.AnnotationPlace annotationAnnotationPlace = annotationsManager.chooseAnnotationsPlace(myModifierListOwner);
if (annotationAnnotationPlace == ExternalAnnotationsManager.AnnotationPlace.NOWHERE) return;
if (annotationAnnotationPlace == ExternalAnnotationsManager.AnnotationPlace.EXTERNAL) {
for (String fqn : myAnnotationsToRemove) {
annotationsManager.deannotate(myModifierListOwner, fqn);
}
annotationsManager.annotateExternally(myModifierListOwner, myAnnotation, file, myPairs);
}
else {
final PsiFile containingFile = myModifierListOwner.getContainingFile();
if (!CodeInsightUtilBase.preparePsiElementForWrite(containingFile)) return;
for (String fqn : myAnnotationsToRemove) {
PsiAnnotation annotation = AnnotationUtil.findAnnotation(myModifierListOwner, fqn);
if (annotation != null) {
annotation.delete();
}
}
final @NotNull PsiAnnotation inserted = modifierList.addAnnotation(myAnnotation);
if (myPairs != null) {
for (PsiNameValuePair pair : myPairs) {
inserted.setDeclaredAttributeValue(pair.getName(), pair.getValue());
}
}
JavaCodeStyleManager.getInstance(project).shortenClassReferences(inserted);
if (containingFile != file) {
UndoUtil.markPsiFileForUndo(file);
}
final PsiModifierList modifierList = myModifierListOwner.getModifierList();
LOG.assertTrue(modifierList != null);
if (modifierList.findAnnotation(myAnnotation) != null) return;
final ExternalAnnotationsManager.AnnotationPlace annotationAnnotationPlace = annotationsManager.chooseAnnotationsPlace(myModifierListOwner);
if (annotationAnnotationPlace == ExternalAnnotationsManager.AnnotationPlace.NOWHERE) return;
if (annotationAnnotationPlace == ExternalAnnotationsManager.AnnotationPlace.EXTERNAL) {
for (String fqn : myAnnotationsToRemove) {
annotationsManager.deannotate(myModifierListOwner, fqn);
}
annotationsManager.annotateExternally(myModifierListOwner, myAnnotation, file, myPairs);
}
else {
final PsiElement element = file.findElementAt(editor.getCaretModel().getOffset());
annotationsManager.annotateExternally(PsiTreeUtil.getParentOfType(element, PsiModifierListOwner.class, false), myAnnotation, file, null);
final PsiFile containingFile = myModifierListOwner.getContainingFile();
if (!CodeInsightUtilBase.preparePsiElementForWrite(containingFile)) return;
for (String fqn : myAnnotationsToRemove) {
PsiAnnotation annotation = AnnotationUtil.findAnnotation(myModifierListOwner, fqn);
if (annotation != null) {
annotation.delete();
}
}
PsiAnnotation inserted = modifierList.addAnnotation(myAnnotation);
for (PsiNameValuePair pair : myPairs) {
inserted.setDeclaredAttributeValue(pair.getName(), pair.getValue());
}
JavaCodeStyleManager.getInstance(project).shortenClassReferences(inserted);
if (containingFile != file) {
UndoUtil.markPsiFileForUndo(file);
}
}
}
@NotNull
public String[] getAnnotationsToRemove() {
return myAnnotationsToRemove;
}
@@ -0,0 +1,96 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Created by IntelliJ IDEA.
* User: cdr
* Date: Jul 20, 2007
* Time: 2:57:38 PM
*/
package com.intellij.codeInsight.intention.impl;
import com.intellij.codeInsight.AnnotationUtil;
import com.intellij.codeInsight.CodeInsightBundle;
import com.intellij.codeInsight.intention.AddAnnotationFix;
import com.intellij.openapi.editor.CaretModel;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pair;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
public abstract class AddAnnotationIntention extends BaseIntentionAction {
@NotNull
@Override
public String getFamilyName() {
return CodeInsightBundle.message("intention.add.annotation.family");
}
@NotNull
public abstract Pair<String, String[]> getAnnotations(@NotNull Project project);
// include not in project files
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
CaretModel caretModel = editor.getCaretModel();
int position = caretModel.getOffset();
PsiElement element = file.findElementAt(position);
return element != null && isAvailable(project, element);
}
public boolean isAvailable(@NotNull final Project project, @NotNull final PsiElement element) {
if (!element.isValid()) return false;
if (!PsiUtil.isLanguageLevel5OrHigher(element)) return false;
final PsiModifierListOwner owner;
if (!element.getManager().isInProject(element) || CodeStyleSettingsManager.getSettings(project).USE_EXTERNAL_ANNOTATIONS) {
owner = AddAnnotationFix.getContainer(element);
}
else {
return false;
}
if (owner == null) return false;
Pair<String, String[]> annotations = getAnnotations(project);
String toAdd = annotations.first;
String[] toRemove = annotations.second;
if (toRemove.length > 0 && AnnotationUtil.isAnnotated(owner, toRemove[0], false)) return false;
setText(AddAnnotationFix.calcText(owner, toAdd));
if (AnnotationUtil.isAnnotated(owner, toAdd, false)) return false;
if (owner instanceof PsiMethod) {
PsiType returnType = ((PsiMethod)owner).getReturnType();
return returnType != null && !(returnType instanceof PsiPrimitiveType);
}
return true;
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
CaretModel caretModel = editor.getCaretModel();
int position = caretModel.getOffset();
PsiElement element = file.findElementAt(position);
PsiModifierListOwner owner = AddAnnotationFix.getContainer(element);
if (owner == null || !owner.isValid()) return;
Pair<String, String[]> annotations = getAnnotations(project);
String toAdd = annotations.first;
String[] toRemove = annotations.second;
AddAnnotationFix fix = new AddAnnotationFix(toAdd, owner, toRemove);
fix.invoke(project, editor, file);
}
}
@@ -22,32 +22,15 @@
*/
package com.intellij.codeInsight.intention.impl;
import com.intellij.codeInsight.intention.AddAnnotationFix;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.openapi.util.Pair;
import com.intellij.util.ArrayUtil;
import org.jetbrains.annotations.NotNull;
public class AddDeprecationAnnotationFix extends AddAnnotationFix {
public AddDeprecationAnnotationFix() {
super("java.lang.annotation.Deprecated");
}
public class AddDeprecationAnnotationIntention extends AddAnnotationIntention {
@NotNull
@Override
public boolean isAvailable(@NotNull final Project project, final Editor editor, @NotNull final PsiElement element) {
if (!super.isAvailable(project, editor, element)) {
return false;
}
PsiModifierListOwner owner = getContainer(element);
if (owner == null) {
return false;
}
if (owner instanceof PsiMethod) {
PsiType returnType = ((PsiMethod)owner).getReturnType();
return returnType != null && !(returnType instanceof PsiPrimitiveType);
}
return true;
public Pair<String, String[]> getAnnotations(@NotNull Project project) {
return new Pair<String, String[]>("java.lang.annotation.Deprecated", ArrayUtil.EMPTY_STRING_ARRAY);
}
}
@@ -22,26 +22,23 @@
*/
package com.intellij.codeInsight.intention.impl;
import com.intellij.codeInsight.AnnotationUtil;
import com.intellij.codeInsight.NullableNotNullManager;
import com.intellij.psi.PsiModifierListOwner;
import com.intellij.util.ArrayUtil;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public class AddNotNullAnnotationFix extends AddNullableNotNullAnnotationFix {
@Deprecated
public AddNotNullAnnotationFix() {
super(AnnotationUtil.NOT_NULL, AnnotationUtil.NULLABLE);
}
public AddNotNullAnnotationFix(PsiModifierListOwner owner) {
public AddNotNullAnnotationFix(@NotNull PsiModifierListOwner owner) {
super(NullableNotNullManager.getInstance(owner.getProject()).getDefaultNotNull(),
owner,
getNullables(owner));
}
private static String[] getNullables(PsiModifierListOwner owner) {
@NotNull
private static String[] getNullables(@NotNull PsiModifierListOwner owner) {
final List<String> nullables = NullableNotNullManager.getInstance(owner.getProject()).getNullables();
return ArrayUtil.toStringArray(nullables);
}
}
}
@@ -0,0 +1,45 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Created by IntelliJ IDEA.
* User: cdr
* Date: Jul 20, 2007
* Time: 2:57:38 PM
*/
package com.intellij.codeInsight.intention.impl;
import com.intellij.codeInsight.NullableNotNullManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pair;
import com.intellij.util.ArrayUtil;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public class AddNotNullAnnotationIntention extends AddAnnotationIntention {
@NotNull
@Override
public Pair<String, String[]> getAnnotations(@NotNull Project project) {
return new Pair<String, String[]>(NullableNotNullManager.getInstance(project).getDefaultNotNull(), getNullables(project));
}
@NotNull
private static String[] getNullables(@NotNull Project project) {
final List<String> nullables = NullableNotNullManager.getInstance(project).getNullables();
return ArrayUtil.toStringArray(nullables);
}
}
@@ -22,26 +22,22 @@
*/
package com.intellij.codeInsight.intention.impl;
import com.intellij.codeInsight.AnnotationUtil;
import com.intellij.codeInsight.NullableNotNullManager;
import com.intellij.psi.PsiModifierListOwner;
import com.intellij.util.ArrayUtil;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public class AddNullableAnnotationFix extends AddNullableNotNullAnnotationFix {
@Deprecated
public AddNullableAnnotationFix() {
super(AnnotationUtil.NULLABLE, AnnotationUtil.NOT_NULL);
}
public AddNullableAnnotationFix(PsiModifierListOwner owner) {
public AddNullableAnnotationFix(@NotNull PsiModifierListOwner owner) {
super(NullableNotNullManager.getInstance(owner.getProject()).getDefaultNullable(),
owner,
getNotNulls(owner));
}
private static String[] getNotNulls(PsiModifierListOwner owner) {
@NotNull
private static String[] getNotNulls(@NotNull PsiModifierListOwner owner) {
final List<String> notnulls = NullableNotNullManager.getInstance(owner.getProject()).getNotNulls();
return ArrayUtil.toStringArray(notnulls);
}
@@ -0,0 +1,45 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Created by IntelliJ IDEA.
* User: cdr
* Date: Jul 20, 2007
* Time: 2:57:59 PM
*/
package com.intellij.codeInsight.intention.impl;
import com.intellij.codeInsight.NullableNotNullManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pair;
import com.intellij.util.ArrayUtil;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public class AddNullableAnnotationIntention extends AddAnnotationIntention {
@NotNull
@Override
public Pair<String, String[]> getAnnotations(@NotNull Project project) {
return new Pair<String, String[]>(NullableNotNullManager.getInstance(project).getDefaultNullable(), getNotNulls(project));
}
@NotNull
private static String[] getNotNulls(@NotNull Project project) {
final List<String> notnulls = NullableNotNullManager.getInstance(project).getNotNulls();
return ArrayUtil.toStringArray(notnulls);
}
}
@@ -24,26 +24,24 @@ package com.intellij.codeInsight.intention.impl;
import com.intellij.codeInsight.AnnotationUtil;
import com.intellij.codeInsight.intention.AddAnnotationFix;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import org.jetbrains.annotations.NotNull;
public class AddNullableNotNullAnnotationFix extends AddAnnotationFix {
public AddNullableNotNullAnnotationFix(final String annotation, final String... annotationToRemove) {
super(annotation, annotationToRemove);
}
public AddNullableNotNullAnnotationFix(final String fqn, final PsiModifierListOwner owner, final String... annotationToRemove) {
public AddNullableNotNullAnnotationFix(@NotNull String fqn, @NotNull PsiModifierListOwner owner, @NotNull String... annotationToRemove) {
super(fqn, owner, annotationToRemove);
}
@Override
public boolean isAvailable(@NotNull final Project project, final Editor editor, @NotNull final PsiElement element) {
if (!super.isAvailable(project, editor, element)) {
public boolean isAvailable(@NotNull Project project,
@NotNull PsiFile file,
@NotNull PsiElement startElement,
@NotNull PsiElement endElement) {
if (!super.isAvailable(project, file, startElement, endElement)) {
return false;
}
PsiModifierListOwner owner = getContainer(element);
PsiModifierListOwner owner = getContainer(startElement);
if (owner == null || AnnotationUtil.isAnnotated(owner, getAnnotationsToRemove()[0], false)) {
return false;
}
@@ -61,7 +61,9 @@ public class AddOverrideAnnotationAction implements IntentionAction {
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
PsiMethod method = findMethod(file, editor.getCaretModel().getOffset());
new AddAnnotationFix(JAVA_LANG_OVERRIDE, method).invoke(project, editor, file);
if (method != null) {
new AddAnnotationFix(JAVA_LANG_OVERRIDE, method).invoke(project, editor, file);
}
}
private static PsiMethod findMethod(PsiFile file, int offset) {
@@ -109,7 +109,7 @@ public class AnnotateMethodFix implements LocalQuickFix {
return getName();
}
private void annotateMethod(final PsiMethod method) {
private void annotateMethod(@NotNull PsiMethod method) {
try {
new AddAnnotationFix(myAnnotation, method, myAnnotationsToRemove).invoke(method.getProject(), null, method.getContainingFile());
}
@@ -25,11 +25,9 @@ import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.*;
import com.intellij.psi.search.searches.OverridingMethodsSearch;
import com.intellij.psi.search.searches.ReferencesSearch;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import sun.util.LocaleServiceProviderPool;
/**
* User: anna
@@ -111,7 +109,9 @@ public class PossibleHeapPollutionVarargsInspection extends BaseJavaLocalInspect
final PsiElement psiElement = descriptor.getPsiElement();
if (psiElement instanceof PsiIdentifier) {
final PsiMethod psiMethod = (PsiMethod)psiElement.getParent();
new AddAnnotationFix("java.lang.SafeVarargs", psiMethod).applyFix(project, descriptor);
if (psiMethod != null) {
new AddAnnotationFix("java.lang.SafeVarargs", psiMethod).applyFix(project, descriptor);
}
}
}
}
@@ -80,6 +80,7 @@ public class AnnotateOverriddenMethodParameterFix implements LocalQuickFix {
CodeInsightUtilBase.preparePsiElementsForWrite(toAnnotate);
for (PsiParameter psiParam : toAnnotate) {
try {
assert psiParam != null : toAnnotate;
new AddAnnotationFix(myAnnotation, psiParam, myAnnosToRemove).invoke(project, null, psiParam.getContainingFile());
}
catch (IncorrectOperationException e) {
@@ -92,4 +93,4 @@ public class AnnotateOverriddenMethodParameterFix implements LocalQuickFix {
public String getFamilyName() {
return getName();
}
}
}
@@ -23,11 +23,12 @@ import com.intellij.codeInsight.intention.impl.AddNotNullAnnotationFix;
import com.intellij.codeInsight.intention.impl.AddNullableAnnotationFix;
import com.intellij.codeInspection.*;
import com.intellij.codeInspection.ex.BaseLocalInspectionTool;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.*;
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import com.intellij.psi.codeStyle.VariableKind;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.search.LocalSearchScope;
import com.intellij.psi.search.searches.OverridingMethodsSearch;
@@ -76,13 +77,9 @@ public class NullableStuffInspection extends BaseLocalInspectionTool {
for (int i = 0, expressionsLength = expressions.length; i < Math.min(expressionsLength, parameters.length); i++) {
PsiExpression psiExpression = expressions[i];
if (psiExpression.getType() == PsiType.NULL) {
if (!AnnotationUtil.isNullable(parameters[i]) && !AnnotationUtil.isNotNull(parameters[i])) {
holder.registerProblem(psiExpression, "Null is passed to parameter which is not yet @Nullable", new AddNullableAnnotationFix(parameters[i]){
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) {
return true;
}
});
final PsiParameter parameter = parameters[i];
if (!AnnotationUtil.isNullable(parameter) && !AnnotationUtil.isNotNull(parameter)) {
holder.registerProblem(psiExpression, "Null is passed to parameter which is not yet @Nullable", new MyAddNullableAnnotationFix(parameter));
}
}
}
@@ -107,21 +104,22 @@ public class NullableStuffInspection extends BaseLocalInspectionTool {
final PsiMethod getter = PropertyUtil.findPropertyGetter(field.getContainingClass(), propName, isStatic, false);
final String nullableSimpleName = StringUtil.getShortName(manager.getDefaultNullable());
final String notNullSimpleName = StringUtil.getShortName(manager.getDefaultNotNull());
if (getter != null) {
final PsiIdentifier nameIdentifier = getter == null ? null : getter.getNameIdentifier();
if (nameIdentifier != null) {
if (REPORT_NOT_ANNOTATED_GETTER) {
if (!AnnotationUtil.isAnnotated(getter, manager.getAllAnnotations()) &&
!TypeConversionUtil.isPrimitiveAndNotNull(getter.getReturnType())) {
holder.registerProblem(getter.getNameIdentifier(), InspectionsBundle
holder.registerProblem(nameIdentifier, InspectionsBundle
.message("inspection.nullable.problems.annotated.field.getter.not.annotated", StringUtil.getShortName(anno)),
ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new AnnotateMethodFix(anno, ArrayUtil.toStringArray(annoToRemove)));
}
}
if (annotated.isDeclaredNotNull && manager.isNullable(getter, false)) {
holder.registerProblem(getter.getNameIdentifier(), InspectionsBundle.message(
holder.registerProblem(nameIdentifier, InspectionsBundle.message(
"inspection.nullable.problems.annotated.field.getter.conflict", StringUtil.getShortName(anno), nullableSimpleName),
ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new AnnotateMethodFix(anno, ArrayUtil.toStringArray(annoToRemove)));
} else if (annotated.isDeclaredNullable && manager.isNotNull(getter, false)) {
holder.registerProblem(getter.getNameIdentifier(), InspectionsBundle.message(
holder.registerProblem(nameIdentifier, InspectionsBundle.message(
"inspection.nullable.problems.annotated.field.getter.conflict", StringUtil.getShortName(anno), notNullSimpleName),
ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new AnnotateMethodFix(anno, ArrayUtil.toStringArray(annoToRemove)));
}
@@ -131,23 +129,31 @@ public class NullableStuffInspection extends BaseLocalInspectionTool {
final PsiMethod setter = PropertyUtil.findPropertySetter(containingClass, propName, isStatic, false);
if (setter != null) {
final PsiParameter[] parameters = setter.getParameterList().getParameters();
assert parameters.length == 1;
assert parameters.length == 1 : setter.getText();
final PsiParameter parameter = parameters[0];
assert parameter != null : setter.getText();
if (REPORT_NOT_ANNOTATED_SETTER_PARAMETER && !AnnotationUtil.isAnnotated(parameter, manager.getAllAnnotations()) && !TypeConversionUtil.isPrimitiveAndNotNull(parameter.getType())) {
holder.registerProblem(parameter.getNameIdentifier(),
final PsiIdentifier nameIdentifier1 = parameter.getNameIdentifier();
assert nameIdentifier1 != null : parameter;
holder.registerProblem(nameIdentifier1,
InspectionsBundle.message("inspection.nullable.problems.annotated.field.setter.parameter.not.annotated",
StringUtil.getShortName(anno)),
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
new AddAnnotationFix(anno, parameter, ArrayUtil.toStringArray(annoToRemove)));
}
if (annotated.isDeclaredNotNull && manager.isNullable(parameter, false)) {
holder.registerProblem(parameter.getNameIdentifier(), InspectionsBundle.message(
final PsiIdentifier nameIdentifier1 = parameter.getNameIdentifier();
assert nameIdentifier1 != null : parameter;
holder.registerProblem(nameIdentifier1, InspectionsBundle.message(
"inspection.nullable.problems.annotated.field.setter.parameter.conflict",
StringUtil.getShortName(anno), nullableSimpleName),
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
new AddAnnotationFix(anno, parameter, ArrayUtil.toStringArray(annoToRemove)));
} else if (annotated.isDeclaredNullable && manager.isNotNull(parameter, false)) {
holder.registerProblem(parameter.getNameIdentifier(), InspectionsBundle.message(
}
else if (annotated.isDeclaredNullable && manager.isNotNull(parameter, false)) {
final PsiIdentifier nameIdentifier1 = parameter.getNameIdentifier();
assert nameIdentifier1 != null : parameter;
holder.registerProblem(nameIdentifier1, InspectionsBundle.message(
"inspection.nullable.problems.annotated.field.setter.parameter.conflict", StringUtil.getShortName(anno), notNullSimpleName),
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
new AddAnnotationFix(anno, parameter, ArrayUtil.toStringArray(annoToRemove)));
@@ -187,20 +193,27 @@ public class NullableStuffInspection extends BaseLocalInspectionTool {
return true;
}
if (REPORT_NOT_ANNOTATED_SETTER_PARAMETER && !AnnotationUtil.isAnnotated(parameter, manager.getAllAnnotations())) {
holder.registerProblem(parameter.getNameIdentifier(), InspectionsBundle
final PsiIdentifier nameIdentifier2 = parameter.getNameIdentifier();
assert nameIdentifier2 != null : parameter;
holder.registerProblem(nameIdentifier2, InspectionsBundle
.message("inspection.nullable.problems.annotated.field.constructor.parameter.not.annotated",
StringUtil.getShortName(anno)),
ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new AddAnnotationFix(anno, parameter, ArrayUtil.toStringArray(annoToRemove)));
return true;
}
if (annotated.isDeclaredNotNull && manager.isNullable(parameter, false)) {
holder.registerProblem(parameter.getNameIdentifier(), InspectionsBundle.message(
final PsiIdentifier nameIdentifier2 = parameter.getNameIdentifier();
assert nameIdentifier2 != null : parameter;
holder.registerProblem(nameIdentifier2, InspectionsBundle.message(
"inspection.nullable.problems.annotated.field.constructor.parameter.conflict", StringUtil.getShortName(anno),
nullableSimpleName),
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
new AddAnnotationFix(anno, parameter, ArrayUtil.toStringArray(annoToRemove)));
} else if (annotated.isDeclaredNullable && manager.isNotNull(parameter, false)) {
holder.registerProblem(parameter.getNameIdentifier(), InspectionsBundle.message(
}
else if (annotated.isDeclaredNullable && manager.isNotNull(parameter, false)) {
final PsiIdentifier nameIdentifier2 = parameter.getNameIdentifier();
assert nameIdentifier2 != null : parameter;
holder.registerProblem(nameIdentifier2, InspectionsBundle.message(
"inspection.nullable.problems.annotated.field.constructor.parameter.conflict", StringUtil.getShortName(anno),
notNullSimpleName),
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
@@ -306,14 +319,11 @@ public class NullableStuffInspection extends BaseLocalInspectionTool {
&& !annotated.isDeclaredNotNull
&& AnnotationUtil.isNotNull(superMethod)) {
reported_not_annotated_method_overrides_notnull = true;
final String defaultNotNull = nullableManager.getDefaultNotNull();
final String[] annotationsToRemove = ArrayUtil.toStringArray(nullableManager.getNullables());
holder.registerProblem(method.getNameIdentifier(),
InspectionsBundle.message("inspection.nullable.problems.method.overrides.NotNull"),
ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new AnnotateMethodFix(
nullableManager.getDefaultNotNull(), ArrayUtil.toStringArray(nullableManager.getNullables())) {
public int annotateBaseMethod(final PsiMethod method, final PsiMethod superMethod, final Project project) {
return NullableStuffInspection.this.annotateBaseMethod(method, superMethod, project);
}
});
ProblemHighlightType.GENERIC_ERROR_OR_WARNING, createAnnotateMethodFix(defaultNotNull, annotationsToRemove));
}
if (REPORT_NOTNULL_PARAMETER_OVERRIDES_NULLABLE || REPORT_NOT_ANNOTATED_PARAMETER_OVERRIDES_NOTNULL) {
PsiParameter[] superParameters = superMethod.getParameterList().getParameters();
@@ -366,17 +376,11 @@ public class NullableStuffInspection extends BaseLocalInspectionTool {
&& !nullableManager.isNotNull(overriding, false)) {
method.getNameIdentifier(); //load tree
PsiAnnotation annotation = AnnotationUtil.findAnnotation(method, nullableManager.getNotNulls());
final String defaultNotNull = nullableManager.getDefaultNotNull();
final String[] annotationsToRemove = ArrayUtil.toStringArray(nullableManager.getNullables());
holder.registerProblem(annotation, InspectionsBundle.message("nullable.stuff.problems.overridden.methods.are.not.annotated"),
ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new AnnotateMethodFix(nullableManager.getDefaultNotNull(), ArrayUtil.toStringArray(nullableManager.getNullables())){
protected boolean annotateOverriddenMethods() {
return true;
}
@NotNull
public String getName() {
return InspectionsBundle.message("annotate.overridden.methods.as.notnull");
}
});
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
new MyAnnotateMethodFix(defaultNotNull, annotationsToRemove));
methodQuickFixSuggested = true;
}
if (hasAnnotatedParameter) {
@@ -400,9 +404,8 @@ public class NullableStuffInspection extends BaseLocalInspectionTool {
}
}
protected int annotateBaseMethod(final PsiMethod method, final PsiMethod superMethod, final Project project) {
final NullableNotNullManager manager = NullableNotNullManager.getInstance(project);
return new AnnotateMethodFix(manager.getDefaultNotNull(), ArrayUtil.toStringArray(manager.getNullables())).annotateBaseMethod(method, superMethod, project);
protected AnnotateMethodFix createAnnotateMethodFix(final String defaultNotNull, final String[] annotationsToRemove) {
return new AnnotateMethodFix(defaultNotNull, annotationsToRemove);
}
private static void reportNullableNotNullConflict(final ProblemsHolder holder, final PsiModifierListOwner listOwner, final PsiAnnotation declaredNullable,
@@ -419,6 +422,35 @@ public class NullableStuffInspection extends BaseLocalInspectionTool {
return new OptionsPanel();
}
private static class MyAddNullableAnnotationFix extends AddNullableAnnotationFix {
public MyAddNullableAnnotationFix(PsiParameter parameter) {
super(parameter);
}
@Override
public boolean isAvailable(@NotNull Project project,
@NotNull PsiFile file,
@NotNull PsiElement startElement,
@NotNull PsiElement endElement) {
return true;
}
}
private static class MyAnnotateMethodFix extends AnnotateMethodFix {
public MyAnnotateMethodFix(String defaultNotNull, String[] annotationsToRemove) {
super(defaultNotNull, annotationsToRemove);
}
protected boolean annotateOverriddenMethods() {
return true;
}
@NotNull
public String getName() {
return InspectionsBundle.message("annotate.overridden.methods.as.notnull");
}
}
private class OptionsPanel extends JPanel {
private JCheckBox myNNParameterOverridesN;
private JCheckBox myNAMethodOverridesNN;