added utility for common ModuleRootModel modifications

This commit is contained in:
nik
2012-06-18 10:33:53 +04:00
parent 5f277b6472
commit f4e5a9dd15
41 changed files with 571 additions and 589 deletions
@@ -158,7 +158,7 @@ public abstract class OrderEntryFix implements IntentionAction, LocalQuickFix {
@Override
public void run() {
final LocateLibraryDialog dialog = new LocateLibraryDialog(currentModule, PathManager.getLibPath(), "annotations.jar",
QuickFixBundle.message("add.library.annotations.description"));
QuickFixBundle.message("add.library.annotations.description"));
dialog.show();
if (dialog.isOK()) {
new WriteCommandAction(project) {
@@ -212,12 +212,9 @@ public abstract class OrderEntryFix implements IntentionAction, LocalQuickFix {
final Runnable doit = new Runnable() {
@Override
public void run() {
ModifiableRootModel model = ModuleRootManager.getInstance(currentModule).getModifiableModel();
final ModuleOrderEntry entry = model.addModuleOrderEntry(classModule);
if (ModuleRootManager.getInstance(currentModule).getFileIndex().isInTestSourceContent(classVFile)) {
entry.setScope(DependencyScope.TEST);
}
model.commit();
final boolean test = ModuleRootManager.getInstance(currentModule).getFileIndex().isInTestSourceContent(classVFile);
ModuleRootModificationUtil.addDependency(currentModule, classModule,
test ? DependencyScope.TEST : DependencyScope.COMPILE, false);
if (editor != null) {
final List<PsiClass> targetClasses = new ArrayList<PsiClass>();
for (PsiClass psiClass : classes) {
@@ -257,7 +254,8 @@ public abstract class OrderEntryFix implements IntentionAction, LocalQuickFix {
if (entryForFile instanceof ExportableOrderEntry &&
((ExportableOrderEntry)entryForFile).getScope() == DependencyScope.TEST &&
!ModuleRootManager.getInstance(currentModule).getFileIndex().isInTestSourceContent(classVFile)) {
} else {
}
else {
continue;
}
}
@@ -361,9 +359,9 @@ public abstract class OrderEntryFix implements IntentionAction, LocalQuickFix {
final Module classModule,
final Runnable doit) {
final String message = QuickFixBundle.message("orderEntry.fix.circular.dependency.warning", classModule.getName(),
circularModules.getFirst().getName(), circularModules.getSecond().getName());
circularModules.getFirst().getName(), circularModules.getSecond().getName());
if (ApplicationManager.getApplication().isUnitTestMode()) throw new RuntimeException(message);
ApplicationManager.getApplication().invokeLater(new Runnable(){
ApplicationManager.getApplication().invokeLater(new Runnable() {
@Override
public void run() {
if (!project.isOpen()) return;
@@ -24,8 +24,7 @@ import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.roots.ModuleRootModificationUtil;
import com.intellij.openapi.roots.ui.configuration.ProjectSettingsService;
import com.intellij.psi.CommonClassNames;
import com.intellij.psi.JavaPsiFacade;
@@ -34,15 +33,17 @@ import org.jetbrains.annotations.NotNull;
/**
* @author mike
* Date: Aug 20, 2002
* Date: Aug 20, 2002
*/
public class SetupJDKFix implements IntentionAction, HighPriorityAction {
private static final SetupJDKFix ourInstance = new SetupJDKFix();
public static SetupJDKFix getInstance() {
return ourInstance;
}
private SetupJDKFix() { }
private SetupJDKFix() {
}
@Override
@NotNull
@@ -70,9 +71,7 @@ public class SetupJDKFix implements IntentionAction, HighPriorityAction {
public void run() {
Module module = ModuleUtil.findModuleForPsiElement(file);
if (module != null) {
ModifiableRootModel modifiableModel = ModuleRootManager.getInstance(module).getModifiableModel();
modifiableModel.inheritSdk();
modifiableModel.commit();
ModuleRootModificationUtil.setSdkInherited(module);
}
}
});
@@ -34,8 +34,7 @@ import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectUtil;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.roots.ModuleRootModificationUtil;
import com.intellij.openapi.roots.libraries.Library;
import com.intellij.openapi.roots.libraries.LibraryUtil;
import com.intellij.openapi.ui.DialogWrapper;
@@ -79,6 +78,7 @@ public class InferNullityAnnotationsAction extends BaseAnalysisAction {
scope.accept(new PsiElementVisitor() {
private int myFileCount = 0;
final private Set<Module> processed = new HashSet<Module>();
@Override
public void visitFile(PsiFile file) {
myFileCount++;
@@ -94,7 +94,8 @@ public class InferNullityAnnotationsAction extends BaseAnalysisAction {
if (module != null && !processed.contains(module)) {
processed.add(module);
if (JavaPsiFacade.getInstance(project)
.findClass(NullableNotNullManager.getInstance(project).getDefaultNullable(), GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module)) == null) {
.findClass(NullableNotNullManager.getInstance(project).getDefaultNullable(),
GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module)) == null) {
modulesWithoutAnnotations.add(module);
}
if (PsiUtil.getLanguageLevel(file).compareTo(LanguageLevel.JDK_1_5) < 0) {
@@ -104,13 +105,17 @@ public class InferNullityAnnotationsAction extends BaseAnalysisAction {
}
});
}
}, "Check applicability...", true, project)) return;
}, "Check applicability...", true, project)) {
return;
}
if (!modulesWithLL.isEmpty()) {
Messages.showErrorDialog(project, "Infer Nullity Annotations requires the project language level be set to 1.5 or greater.", INFER_NULLITY_ANNOTATIONS);
Messages.showErrorDialog(project, "Infer Nullity Annotations requires the project language level be set to 1.5 or greater.",
INFER_NULLITY_ANNOTATIONS);
return;
}
if (!modulesWithoutAnnotations.isEmpty()) {
final Library annotationsLib = LibraryUtil.findLibraryByClass(NullableNotNullManager.getInstance(project).getDefaultNullable(), project);
final Library annotationsLib =
LibraryUtil.findLibraryByClass(NullableNotNullManager.getInstance(project).getDefaultNullable(), project);
if (annotationsLib != null) {
String message = "Module" + (modulesWithoutAnnotations.size() == 1 ? " " : "s ");
message += StringUtil.join(modulesWithoutAnnotations, new Function<Module, String>() {
@@ -120,25 +125,27 @@ public class InferNullityAnnotationsAction extends BaseAnalysisAction {
}
}, ", ");
message += (modulesWithoutAnnotations.size() == 1 ? " doesn't" : " don't");
message += " refer to the existing '" + annotationsLib.getName() + "' library with IDEA nullity annotations. Would you like to add the dependenc";
message += (modulesWithoutAnnotations.size() == 1 ? "y" : "ies")+ " now?";
if (Messages.showOkCancelDialog(project, message, INFER_NULLITY_ANNOTATIONS, Messages.getErrorIcon()) == DialogWrapper.OK_EXIT_CODE) {
message += " refer to the existing '" +
annotationsLib.getName() +
"' library with IDEA nullity annotations. Would you like to add the dependenc";
message += (modulesWithoutAnnotations.size() == 1 ? "y" : "ies") + " now?";
if (Messages.showOkCancelDialog(project, message, INFER_NULLITY_ANNOTATIONS, Messages.getErrorIcon()) ==
DialogWrapper.OK_EXIT_CODE) {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
for (Module module : modulesWithoutAnnotations) {
final ModifiableRootModel modifiableModel = ModuleRootManager.getInstance(module).getModifiableModel();
modifiableModel.addLibraryEntry(annotationsLib);
modifiableModel.commit();
ModuleRootModificationUtil.addDependency(module, annotationsLib);
}
}
});
}
} else if (Messages.showOkCancelDialog(project, "Infer Nullity Annotations requires that the nullity annotations" +
" be available in all your project sources.\n\nYou will need to add annotations.jar as a library. " +
"It is possible to configure custom jar in e.g. Constant Conditions & Exceptions inspection or use JetBrains annotations available in installation. " +
" The IDEA nullity annotations are freely usable and redistributable under the Apache 2.0 license. Would you like to do it now?",
INFER_NULLITY_ANNOTATIONS, Messages.getErrorIcon()) == DialogWrapper.OK_EXIT_CODE) {
}
else if (Messages.showOkCancelDialog(project, "Infer Nullity Annotations requires that the nullity annotations" +
" be available in all your project sources.\n\nYou will need to add annotations.jar as a library. " +
"It is possible to configure custom jar in e.g. Constant Conditions & Exceptions inspection or use JetBrains annotations available in installation. " +
" The IDEA nullity annotations are freely usable and redistributable under the Apache 2.0 license. Would you like to do it now?",
INFER_NULLITY_ANNOTATIONS, Messages.getErrorIcon()) == DialogWrapper.OK_EXIT_CODE) {
ApplicationManager.getApplication().invokeLater(new Runnable() {
public void run() {
final LocateLibraryDialog dialog =
@@ -169,6 +176,7 @@ public class InferNullityAnnotationsAction extends BaseAnalysisAction {
public void run() {
scope.accept(new PsiElementVisitor() {
int myFileCount = 0;
@Override
public void visitFile(final PsiFile file) {
myFileCount++;
@@ -189,7 +197,9 @@ public class InferNullityAnnotationsAction extends BaseAnalysisAction {
}
});
}
}, INFER_NULLITY_ANNOTATIONS, true, project)) return;
}, INFER_NULLITY_ANNOTATIONS, true, project)) {
return;
}
final Runnable applyRunnable = new Runnable() {
@Override