inconsisten language level settings: flag if module depends on other nodule with higher language level

This commit is contained in:
anna
2009-11-05 12:55:06 +03:00
parent 88972ed88e
commit 059da422ed
5 changed files with 197 additions and 25 deletions
@@ -29,6 +29,7 @@ import com.intellij.codeInspection.deprecation.DeprecationInspection;
import com.intellij.codeInspection.duplicateThrows.DuplicateThrowsInspection;
import com.intellij.codeInspection.emptyMethod.EmptyMethodInspection;
import com.intellij.codeInspection.equalsAndHashcode.EqualsAndHashcode;
import com.intellij.codeInspection.inconsistentLanguageLevel.InconsistentLanguageLevelInspection;
import com.intellij.codeInspection.java15api.Java15APIUsageInspection;
import com.intellij.codeInspection.javaDoc.JavaDocLocalInspection;
import com.intellij.codeInspection.javaDoc.JavaDocReferenceInspection;
@@ -65,6 +66,7 @@ public class StandardInspectionToolsProvider implements InspectionToolProvider {
return new Class[] {
DeadCodeInspection.class,
UnusedLibrariesInspection.class,
InconsistentLanguageLevelInspection.class,
VisibilityInspection.class,
CanBeFinalInspection.class,
UnusedParametersInspection.class,
@@ -0,0 +1,150 @@
/*
* Copyright 2000-2009 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.
*/
/*
* User: anna
* Date: 03-Nov-2009
*/
package com.intellij.codeInspection.inconsistentLanguageLevel;
import com.intellij.analysis.AnalysisScope;
import com.intellij.codeInsight.daemon.GroupNames;
import com.intellij.codeInspection.*;
import com.intellij.codeInspection.ex.DescriptorProviderInspection;
import com.intellij.codeInspection.ex.JobDescriptor;
import com.intellij.codeInspection.reference.RefModule;
import com.intellij.codeInspection.unnecessaryModuleDependency.UnnecessaryModuleDependencyInspection;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleUtil;
import com.intellij.openapi.options.ShowSettingsUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectBundle;
import com.intellij.openapi.roots.*;
import com.intellij.openapi.roots.ui.configuration.ProjectSettingsService;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import java.util.HashSet;
import java.util.Set;
public class InconsistentLanguageLevelInspection extends DescriptorProviderInspection{
private static final Logger LOGGER = Logger.getInstance("#" + InconsistentLanguageLevelInspection.class.getName());
public void runInspection(AnalysisScope scope, InspectionManager manager) {
final Set<Module> modules = new HashSet<Module>();
scope.accept(new PsiElementVisitor(){
public void visitElement(PsiElement element) {
final Module module = ModuleUtil.findModuleForPsiElement(element);
if (module != null) {
modules.add(module);
}
}
});
if (!modules.isEmpty()) {
final LanguageLevel projectLanguageLevel =
LanguageLevelProjectExtension.getInstance(modules.iterator().next().getProject()).getLanguageLevel();
for (Module module : modules) {
LanguageLevel languageLevel = LanguageLevelModuleExtension.getInstance(module).getLanguageLevel();
if (languageLevel == null) {
languageLevel = projectLanguageLevel;
}
LOGGER.assertTrue(languageLevel != null);
final RefModule refModule = getRefManager().getRefModule(module);
for (OrderEntry entry : ModuleRootManager.getInstance(module).getOrderEntries()) {
if (entry instanceof ModuleOrderEntry) {
final Module dependantModule = ((ModuleOrderEntry)entry).getModule();
if (dependantModule != null) {
LanguageLevel dependantLanguageLevel = LanguageLevelModuleExtension.getInstance(dependantModule).getLanguageLevel();
if (dependantLanguageLevel == null) {
dependantLanguageLevel = projectLanguageLevel;
}
LOGGER.assertTrue(dependantLanguageLevel != null);
if (languageLevel.compareTo(dependantLanguageLevel) < 0) {
final CommonProblemDescriptor problemDescriptor = manager.createProblemDescriptor(
"Inconsistent language level settings: module " + module.getName() + " with language level " + languageLevel +
" depends on module " + dependantModule.getName() +" with language level " + dependantLanguageLevel,
new UnnecessaryModuleDependencyInspection.RemoveModuleDependencyFix(module, dependantModule),
new OpenModuleSettingsFix(module));
addProblemElement(refModule, problemDescriptor);
}
}
}
}
}
}
}
public boolean isGraphNeeded() {
return false;
}
@NotNull
public JobDescriptor[] getJobDescriptors() {
return JobDescriptor.EMPTY_ARRAY;
}
public boolean isEnabledByDefault() {
return false;
}
@Nls
@NotNull
public String getGroupDisplayName() {
return GroupNames.MODULARIZATION_GROUP_NAME;
}
@NotNull
public String getDisplayName() {
return "Inconsistent language level settings";
}
@NonNls
@NotNull
public String getShortName() {
return "InconsistentLanguageLevel";
}
private static class OpenModuleSettingsFix implements QuickFix {
private Module myModule;
private OpenModuleSettingsFix(Module module) {
myModule = module;
}
@NotNull
public String getName() {
return "Open module " + myModule.getName() + " settings";
}
@NotNull
public String getFamilyName() {
return getName();
}
public void applyFix(@NotNull Project project, @NotNull CommonProblemDescriptor descriptor) {
if (!myModule.isDisposed()) {
ProjectSettingsService.getInstance(project).showModuleConfigurationDialog(myModule.getName(), ProjectBundle.message("modules.classpath.title"), false);
}
}
}
}
@@ -44,31 +44,8 @@ public class UnnecessaryModuleDependencyInspection extends GlobalInspectionTool
for (final Module dependency : declaredDependencies) {
if (scope.contains(dependency.getModuleFile())) { //external references are rejected -> annotator doesn't provide any information on them -> false positives
if (modules == null || !modules.contains(dependency)) {
descriptors.add(manager.createProblemDescriptor(InspectionsBundle.message("unnecessary.module.dependency.problem.descriptor", module.getName(), dependency.getName()), new QuickFix(){
@NotNull
public String getName() {
return "Remove dependency";
}
@NotNull
public String getFamilyName() {
return getName();
}
public void applyFix(@NotNull Project project, @NotNull CommonProblemDescriptor descriptor) {
final ModifiableRootModel model = ModuleRootManager.getInstance(module).getModifiableModel();
for (OrderEntry entry : model.getOrderEntries()) {
if (entry instanceof ModuleOrderEntry) {
final Module mDependency = ((ModuleOrderEntry)entry).getModule();
if (Comparing.equal(mDependency, dependency)) {
model.removeOrderEntry(entry);
break;
}
}
}
model.commit();
}
}));
descriptors.add(manager.createProblemDescriptor(InspectionsBundle.message("unnecessary.module.dependency.problem.descriptor", module.getName(), dependency.getName()),
new RemoveModuleDependencyFix(module, dependency)));
}
}
}
@@ -92,4 +69,38 @@ public class UnnecessaryModuleDependencyInspection extends GlobalInspectionTool
public String getShortName() {
return "UnnecessaryModuleDependencyInspection";
}
public static class RemoveModuleDependencyFix implements QuickFix {
private final Module myModule;
private final Module myDependency;
public RemoveModuleDependencyFix(Module module, Module dependency) {
myModule = module;
myDependency = dependency;
}
@NotNull
public String getName() {
return "Remove dependency";
}
@NotNull
public String getFamilyName() {
return getName();
}
public void applyFix(@NotNull Project project, @NotNull CommonProblemDescriptor descriptor) {
final ModifiableRootModel model = ModuleRootManager.getInstance(myModule).getModifiableModel();
for (OrderEntry entry : model.getOrderEntries()) {
if (entry instanceof ModuleOrderEntry) {
final Module mDependency = ((ModuleOrderEntry)entry).getModule();
if (Comparing.equal(mDependency, myDependency)) {
model.removeOrderEntry(entry);
break;
}
}
}
model.commit();
}
}
}