perform infer nullable/notnull under progress (IDEA-64086)

This commit is contained in:
anna
2012-04-16 12:23:06 +02:00
parent dadce1b2ef
commit 9dfeb9598e
3 changed files with 118 additions and 16 deletions
@@ -0,0 +1,64 @@
/*
* Copyright 2000-2012 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.
*/
package com.intellij.codeInspection.inferNullity;
import com.intellij.codeInsight.NullableNotNullManager;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.project.Project;
import com.intellij.util.SequentialModalProgressTask;
import com.intellij.util.SequentialTask;
class AnnotateTask implements SequentialTask {
private final Project myProject;
private final NullityInferrer myInferrer;
private final SequentialModalProgressTask myTask;
private int myCount = 0;
private final int myTotal;
private final NullableNotNullManager myNotNullManager;
public AnnotateTask(Project project, NullityInferrer inferrer, SequentialModalProgressTask progressTask) {
myProject = project;
myNotNullManager = NullableNotNullManager.getInstance(myProject);
myInferrer = inferrer;
myTask = progressTask;
myTotal = myInferrer.getCount();
}
@Override
public void prepare() {
}
@Override
public boolean isDone() {
return myCount > myTotal - 1;
}
@Override
public boolean iteration() {
final ProgressIndicator indicator = myTask.getIndicator();
if (indicator != null) {
indicator.setFraction(((double)myCount) / myTotal);
}
myInferrer.apply(myCount++, myProject, myNotNullManager);
return isDone();
}
@Override
public void stop() {
}
}
@@ -49,6 +49,7 @@ import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.PsiUtil;
import com.intellij.ui.TitledSeparator;
import com.intellij.util.Function;
import com.intellij.util.SequentialModalProgressTask;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@@ -196,7 +197,12 @@ public class InferNullityAnnotationsAction extends BaseAnalysisAction {
new WriteCommandAction(project, INFER_NULLITY_ANNOTATIONS) {
@Override
protected void run(Result result) throws Throwable {
inferrer.apply(project);
if (!inferrer.nothingFoundMessage(project)) {
final SequentialModalProgressTask progressTask = new SequentialModalProgressTask(project, INFER_NULLITY_ANNOTATIONS, false);
progressTask.setMinIterationTime(200);
progressTask.setTask(new AnnotateTask(project, inferrer, progressTask));
ProgressManager.getInstance().run(progressTask);
}
}
}.execute();
}
@@ -31,14 +31,15 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
public class NullityInferrer {
private static final int MAX_PASSES = 10;
private int numAnnotationsAdded = 0;
private final HashSet<SmartPsiElementPointer<? extends PsiModifierListOwner>> myNotNullSet = new HashSet<SmartPsiElementPointer<? extends PsiModifierListOwner>>();
private final HashSet<SmartPsiElementPointer<? extends PsiModifierListOwner>> myNullableSet = new HashSet<SmartPsiElementPointer<? extends PsiModifierListOwner>>();
private final List<SmartPsiElementPointer<? extends PsiModifierListOwner>> myNotNullSet = new ArrayList<SmartPsiElementPointer<? extends PsiModifierListOwner>>();
private final List<SmartPsiElementPointer<? extends PsiModifierListOwner>> myNullableSet = new ArrayList<SmartPsiElementPointer<? extends PsiModifierListOwner>>();
private final boolean myAnnotateLocalVariables;
private final SmartPointerManager myPointerManager;
@@ -139,29 +140,60 @@ public class NullityInferrer {
public void apply(final Project project) {
final NullableNotNullManager manager = NullableNotNullManager.getInstance(project);
for (SmartPsiElementPointer<? extends PsiModifierListOwner> pointer : myNullableSet) {
final PsiModifierListOwner element = pointer.getElement();
if (element != null) {
if (shouldIgnore(element)) continue;
new AddAnnotationFix(manager.getDefaultNullable(), element, manager.getDefaultNotNull()).invoke(project, null, element.getContainingFile());
}
annotateNullable(project, manager, pointer);
}
for (SmartPsiElementPointer<? extends PsiModifierListOwner> pointer : myNotNullSet) {
final PsiModifierListOwner element = pointer.getElement();
if (element != null) {
if (shouldIgnore(element)) continue;
if (element instanceof PsiField && ((PsiField)element).hasInitializer() && element.hasModifierProperty(PsiModifier.FINAL)) continue;
new AddAnnotationFix(manager.getDefaultNotNull(), element, manager.getDefaultNullable()).invoke(project, null,
element.getContainingFile());
}
annotateNotNull(project, manager, pointer);
}
nothingFoundMessage(project);
}
public boolean nothingFoundMessage(final Project project) {
if (myNullableSet.isEmpty() && myNotNullSet.isEmpty()) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Messages.showInfoMessage(project, "No places found to infer @Nullable/@NotNull", "Infer Nullity Results");
}
});
return true;
}
return false;
}
private void annotateNotNull(Project project,
NullableNotNullManager manager,
SmartPsiElementPointer<? extends PsiModifierListOwner> pointer) {
final PsiModifierListOwner element = pointer.getElement();
if (element != null) {
if (shouldIgnore(element)) return;
if (element instanceof PsiField && ((PsiField)element).hasInitializer() && element.hasModifierProperty(PsiModifier.FINAL)) return;
new AddAnnotationFix(manager.getDefaultNotNull(), element, manager.getDefaultNullable()).invoke(project, null,
element.getContainingFile());
}
}
private void annotateNullable(Project project,
NullableNotNullManager manager,
SmartPsiElementPointer<? extends PsiModifierListOwner> pointer) {
final PsiModifierListOwner element = pointer.getElement();
if (element != null) {
if (shouldIgnore(element)) return;
new AddAnnotationFix(manager.getDefaultNullable(), element, manager.getDefaultNotNull()).invoke(project, null, element.getContainingFile());
}
}
public int getCount() {
return myNotNullSet.size() + myNullableSet.size();
}
public void apply(int i, Project project, NullableNotNullManager manager) {
if (i < myNullableSet.size()) {
annotateNullable(project, manager, myNullableSet.get(i));
} else {
i -= myNullableSet.size();
annotateNotNull(project, manager, myNotNullSet.get(i));
}
}