remove settings for since API usage inspection: use language level instead

This commit is contained in:
anna
2010-01-21 18:06:07 +03:00
parent c7f59356f6
commit a170a20bbd
4 changed files with 35 additions and 111 deletions
@@ -1,32 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.intellij.codeInspection.java15api.Java15APIUsageInspection">
<grid id="27dc6" binding="myWholePanel" layout-manager="FormLayout">
<rowspec value="center:max(d;4px):noGrow"/>
<colspec value="fill:193px:noGrow"/>
<colspec value="fill:max(d;4px):noGrow"/>
<constraints>
<xy x="20" y="20" width="226" height="22"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="f6746" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<forms top="5" left="5" bottom="0" right="0"/>
</constraints>
<properties>
<preferredSize width="200" height="14"/>
<text value="Forbid api usages documented @since"/>
</properties>
</component>
<component id="2ca21" class="javax.swing.JComboBox" binding="myApiComboBox" default-binding="true">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
<forms top="5" left="0" bottom="0" right="0"/>
</constraints>
<properties/>
</component>
</children>
</grid>
</form>
@@ -21,23 +21,20 @@ import com.intellij.codeInsight.daemon.GroupNames;
import com.intellij.codeInspection.*;
import com.intellij.openapi.extensions.ExtensionPoint;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.util.WriteExternalException;
import com.intellij.openapi.module.LanguageLevelUtil;
import com.intellij.openapi.module.ModuleUtil;
import com.intellij.openapi.vfs.CharsetToolkit;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.*;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.containers.HashMap;
import gnu.trove.THashSet;
import gnu.trove.TIntObjectHashMap;
import org.jdom.Element;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
/**
@@ -46,32 +43,23 @@ import java.io.*;
public class Java15APIUsageInspection extends BaseJavaLocalInspectionTool {
@NonNls public static final String SHORT_NAME = "Since15";
private static final TIntObjectHashMap<THashSet<String>> ourForbiddenAPI = new TIntObjectHashMap<THashSet<String>>(5);
private static final HashMap<LanguageLevel, THashSet<String>> ourForbiddenAPI = new HashMap<LanguageLevel, THashSet<String>>(5);
private static final THashSet<String> ourIgnored16ClassesAPI = new THashSet<String>(10);
private static final int API_16 = 2;
private static final TIntObjectHashMap<String> ourAPIPresentationMap = new TIntObjectHashMap<String>(5);
public boolean FORBID_15_API = false;
public boolean FORBID_16_API = false;
private JPanel myWholePanel;
private JComboBox myApiComboBox;
public int API = 1;
private static final HashMap<LanguageLevel, String> ourAPIPresentationMap = new HashMap<LanguageLevel, String>(5);
static {
final THashSet<String> ourForbidden14API = new THashSet<String>(1000);
initForbiddenApi("api14List.txt", ourForbidden14API);
ourForbiddenAPI.put(0, ourForbidden14API);
ourAPIPresentationMap.put(0, "1.4");
ourForbiddenAPI.put(LanguageLevel.JDK_1_4, ourForbidden14API);
ourAPIPresentationMap.put(LanguageLevel.JDK_1_4, "1.4");
final THashSet<String> ourForbidden15API = new THashSet<String>(1000);
initForbiddenApi("apiList.txt", ourForbidden15API);
ourForbiddenAPI.put(1, ourForbidden15API);
ourAPIPresentationMap.put(1, "1.5");
ourForbiddenAPI.put(LanguageLevel.JDK_1_5, ourForbidden15API);
ourAPIPresentationMap.put(LanguageLevel.JDK_1_5, "1.5");
final THashSet<String> ourForbidden16API = new THashSet<String>(1000);
initForbiddenApi("api16List.txt", ourForbidden16API);
ourForbiddenAPI.put(2, ourForbidden16API);
ourAPIPresentationMap.put(2, "1.6");
ourForbiddenAPI.put(LanguageLevel.JDK_1_6, ourForbidden16API);
ourAPIPresentationMap.put(LanguageLevel.JDK_1_6, "1.6");
initForbiddenApi("ignore16List.txt", ourIgnored16ClassesAPI);
}
@@ -106,22 +94,6 @@ public class Java15APIUsageInspection extends BaseJavaLocalInspectionTool {
}
}
@Nullable
public JComponent createOptionsPanel() {
final DefaultComboBoxModel model = (DefaultComboBoxModel)myApiComboBox.getModel();
model.removeAllElements();
for (int idx = 0; idx < ourAPIPresentationMap.size(); idx++) {
model.addElement(ourAPIPresentationMap.get(idx));
}
myApiComboBox.setSelectedIndex(API);
myApiComboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
API = myApiComboBox.getSelectedIndex();
}
});
return myWholePanel;
}
@NotNull
public String getGroupDisplayName() {
return GroupNames.JDK15_SPECIFIC_GROUP_NAME;
@@ -138,27 +110,6 @@ public class Java15APIUsageInspection extends BaseJavaLocalInspectionTool {
}
@Override
public void readSettings(Element node) throws InvalidDataException {
super.readSettings(node);
if (FORBID_15_API) {
API = API_16 - 1;
}
else if (FORBID_16_API) {
API = API_16;
}
}
@Override
public void writeSettings(Element node) throws WriteExternalException {
if (API != API_16 - 1) {
final Element element = new Element("option");
element.setAttribute("name", "API");
element.setAttribute("value", String.valueOf(API));
node.addContent(element);
}
}
@NotNull
@Override
public HighlightDisplayLevel getDefaultLevel() {
@@ -193,8 +144,8 @@ public class Java15APIUsageInspection extends BaseJavaLocalInspectionTool {
return null;
}
public String getApiPresentable() {
return ourAPIPresentationMap.get(API);
public static String getPresentable(LanguageLevel languageLevel) {
return ourAPIPresentationMap.get(languageLevel);
}
private class MyVisitor extends JavaElementVisitor {
@@ -221,7 +172,8 @@ public class Java15APIUsageInspection extends BaseJavaLocalInspectionTool {
final PsiElement resolved = reference.resolve();
if (resolved instanceof PsiCompiledElement && resolved instanceof PsiMember) {
if (isForbiddenApiUsage((PsiMember)resolved, API)) {
final LanguageLevel languageLevel = LanguageLevelUtil.getEffectiveLanguageLevel(ModuleUtil.findModuleForPsiElement(reference.getElement()));
if (isForbiddenApiUsage((PsiMember)resolved, languageLevel)) {
PsiClass psiClass = null;
final PsiElement qualifier = reference.getQualifier();
if (qualifier != null) {
@@ -238,7 +190,7 @@ public class Java15APIUsageInspection extends BaseJavaLocalInspectionTool {
if (isIgnored(superClass)) return;
}
}
registerError(reference, API);
registerError(reference, languageLevel);
}
}
}
@@ -251,22 +203,22 @@ public class Java15APIUsageInspection extends BaseJavaLocalInspectionTool {
@Override public void visitNewExpression(final PsiNewExpression expression) {
super.visitNewExpression(expression);
final PsiMethod constructor = expression.resolveConstructor();
final LanguageLevel languageLevel = LanguageLevelUtil.getEffectiveLanguageLevel(ModuleUtil.findModuleForPsiElement(expression));
if (constructor instanceof PsiCompiledElement) {
if (isForbiddenApiUsage(constructor, API)) {
registerError(expression.getClassReference(), API);
if (isForbiddenApiUsage(constructor, languageLevel)) {
registerError(expression.getClassReference(), languageLevel);
}
}
}
private void registerError(PsiJavaCodeReferenceElement reference, int api) {
private void registerError(PsiJavaCodeReferenceElement reference, LanguageLevel api) {
if (isInProject(reference)) {
myHolder.registerProblem(reference, InspectionsBundle.message("inspection.1.5.problem.descriptor", ourAPIPresentationMap.get(api)));
myHolder.registerProblem(reference, InspectionsBundle.message("inspection.1.5.problem.descriptor", getPresentable(api)));
}
}
}
public static boolean isForbiddenApiUsage(final PsiMember member, int api) {
if (api == -1) return false;
public static boolean isForbiddenApiUsage(final PsiMember member, LanguageLevel languageLevel) {
if (member == null) return false;
// Annotations caught by special inspection if necessary
@@ -275,16 +227,18 @@ public class Java15APIUsageInspection extends BaseJavaLocalInspectionTool {
if (member instanceof PsiAnonymousClass) return false;
if (member.getContainingClass() instanceof PsiAnonymousClass) return false;
if (member instanceof PsiClass && !(member.getParent() instanceof PsiClass || member.getParent() instanceof PsiFile)) return false;
return isForbiddenSignature(member, api) ||
isForbiddenApiUsage(member.getContainingClass(), api);
return isForbiddenSignature(member, languageLevel) ||
isForbiddenApiUsage(member.getContainingClass(), languageLevel);
}
private static boolean isForbiddenSignature(PsiMember member, int api) {
final THashSet<String> forbiddenApi = ourForbiddenAPI.get(api);
private static boolean isForbiddenSignature(PsiMember member, LanguageLevel languageLevel) {
final THashSet<String> forbiddenApi = ourForbiddenAPI.get(languageLevel);
if (forbiddenApi == null) return false;
return forbiddenApi.contains(getSignature(member)) ||
isForbiddenSignature(member, api + 1);
(languageLevel.compareTo(LanguageLevel.HIGHEST) != 0 && isForbiddenSignature(member, LanguageLevel.values()[languageLevel.ordinal() + 1]));
}
public static String getSignature(PsiMember member) {
@@ -27,7 +27,6 @@ public class JavaAPIUsagesInspectionTest extends InspectionTestCase {
private void doTest() throws Exception {
final Java15APIUsageInspection usageInspection = new Java15APIUsageInspection();
usageInspection.API = 1;
doTest("usage1.5/" + getTestName(true), new LocalInspectionToolWrapper(usageInspection), "java 1.5");
}
@@ -19,7 +19,9 @@ import com.intellij.codeInspection.InspectionProfileEntry;
import com.intellij.codeInspection.InspectionsBundle;
import com.intellij.codeInspection.ex.LocalInspectionToolWrapper;
import com.intellij.codeInspection.java15api.Java15APIUsageInspection;
import com.intellij.openapi.module.LanguageLevelUtil;
import com.intellij.openapi.module.Module;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.profile.codeInspection.InspectionProjectProfileManager;
import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.PsiClass;
@@ -64,8 +66,9 @@ public class Java15FormInspection extends BaseFormInspection {
profileEntry = ((LocalInspectionToolWrapper) profileEntry).getTool();
}
final Java15APIUsageInspection tool = (Java15APIUsageInspection)profileEntry;
if (Java15APIUsageInspection.isForbiddenApiUsage(getter, tool.API)) {
registerError(component, collector, prop, "@since " + tool.getApiPresentable());
final LanguageLevel languageLevel = LanguageLevelUtil.getEffectiveLanguageLevel(module);
if (Java15APIUsageInspection.isForbiddenApiUsage(getter, languageLevel)) {
registerError(component, collector, prop, "@since " + tool.getPresentable(languageLevel));
}
}
}