Search in sources :

Example 1 with UnusedDeclarationInspectionBase

use of com.intellij.codeInspection.deadCode.UnusedDeclarationInspectionBase in project intellij-community by JetBrains.

the class InspectionProfileTest method testMergeUnusedDeclarationAndUnusedSymbol.

public void testMergeUnusedDeclarationAndUnusedSymbol() throws Exception {
    //no specific settings
    final Element element = JdomKt.loadElement("<profile version=\"1.0\">\n" + "  <option name=\"myName\" value=\"" + PROFILE + "\" />\n" + "</profile>");
    InspectionProfileImpl profile = createProfile(new InspectionProfileImpl("foo"));
    profile.readExternal(element);
    profile.getModifiableModel().commit();
    assertThat(profile.writeScheme()).isEqualTo(element);
    //settings to merge
    final Element unusedProfile = JdomKt.loadElement("<profile version=\"1.0\">\n" + "  <option name=\"myName\" value=\"" + PROFILE + "\" />\n" + "  <inspection_tool class=\"UNUSED_SYMBOL\" enabled=\"true\" level=\"WARNING\" enabled_by_default=\"false\">\n" + "      <option name=\"LOCAL_VARIABLE\" value=\"true\" />\n" + "      <option name=\"FIELD\" value=\"true\" />\n" + "      <option name=\"METHOD\" value=\"true\" />\n" + "      <option name=\"CLASS\" value=\"true\" />\n" + "      <option name=\"PARAMETER\" value=\"true\" />\n" + "      <option name=\"REPORT_PARAMETER_FOR_PUBLIC_METHODS\" value=\"false\" />\n" + "  </inspection_tool>\n" + "  <inspection_tool class=\"UnusedDeclaration\" enabled=\"true\" level=\"WARNING\" enabled_by_default=\"false\">\n" + "      <option name=\"ADD_MAINS_TO_ENTRIES\" value=\"true\" />\n" + "      <option name=\"ADD_APPLET_TO_ENTRIES\" value=\"true\" />\n" + "      <option name=\"ADD_SERVLET_TO_ENTRIES\" value=\"true\" />\n" + "      <option name=\"ADD_NONJAVA_TO_ENTRIES\" value=\"false\" />\n" + "   </inspection_tool>\n" + "</profile>");
    profile.readExternal(unusedProfile);
    profile.getModifiableModel().commit();
    assertEquals("<profile version=\"1.0\">\n" + "  <option name=\"myName\" value=\"ToConvert\" />\n" + "  <inspection_tool class=\"UNUSED_SYMBOL\" enabled=\"true\" level=\"WARNING\" enabled_by_default=\"false\">\n" + "    <option name=\"LOCAL_VARIABLE\" value=\"true\" />\n" + "    <option name=\"FIELD\" value=\"true\" />\n" + "    <option name=\"METHOD\" value=\"true\" />\n" + "    <option name=\"CLASS\" value=\"true\" />\n" + "    <option name=\"PARAMETER\" value=\"true\" />\n" + "    <option name=\"REPORT_PARAMETER_FOR_PUBLIC_METHODS\" value=\"false\" />\n" + "  </inspection_tool>\n" + "  <inspection_tool class=\"UnusedDeclaration\" enabled=\"true\" level=\"WARNING\" enabled_by_default=\"false\">\n" + "    <option name=\"ADD_MAINS_TO_ENTRIES\" value=\"true\" />\n" + "    <option name=\"ADD_APPLET_TO_ENTRIES\" value=\"true\" />\n" + "    <option name=\"ADD_SERVLET_TO_ENTRIES\" value=\"true\" />\n" + "    <option name=\"ADD_NONJAVA_TO_ENTRIES\" value=\"false\" />\n" + "  </inspection_tool>\n" + "</profile>", serialize(profile));
    //make them default
    profile = createProfile(new InspectionProfileImpl("foo"));
    profile.readExternal(unusedProfile);
    profile.modifyProfile(it -> {
        InspectionToolWrapper toolWrapper = it.getInspectionTool("unused", getProject());
        UnusedDeclarationInspectionBase tool = (UnusedDeclarationInspectionBase) toolWrapper.getTool();
        tool.ADD_NONJAVA_TO_ENTRIES = true;
        UnusedSymbolLocalInspectionBase inspectionTool = tool.getSharedLocalInspectionTool();
        inspectionTool.setParameterVisibility(PsiModifier.PUBLIC);
    });
    String mergedText = "<profile version=\"1.0\">\n" + "  <option name=\"myName\" value=\"ToConvert\" />\n" + "  <inspection_tool class=\"UNUSED_SYMBOL\" enabled=\"true\" level=\"WARNING\" enabled_by_default=\"false\">\n" + "    <option name=\"LOCAL_VARIABLE\" value=\"true\" />\n" + "    <option name=\"FIELD\" value=\"true\" />\n" + "    <option name=\"METHOD\" value=\"true\" />\n" + "    <option name=\"CLASS\" value=\"true\" />\n" + "    <option name=\"PARAMETER\" value=\"true\" />\n" + "    <option name=\"REPORT_PARAMETER_FOR_PUBLIC_METHODS\" value=\"false\" />\n" + "  </inspection_tool>\n" + "  <inspection_tool class=\"UnusedDeclaration\" enabled=\"true\" level=\"WARNING\" enabled_by_default=\"false\">\n" + "    <option name=\"ADD_MAINS_TO_ENTRIES\" value=\"true\" />\n" + "    <option name=\"ADD_APPLET_TO_ENTRIES\" value=\"true\" />\n" + "    <option name=\"ADD_SERVLET_TO_ENTRIES\" value=\"true\" />\n" + "    <option name=\"ADD_NONJAVA_TO_ENTRIES\" value=\"false\" />\n" + "  </inspection_tool>\n" + "  <inspection_tool class=\"unusedMerged\" />\n" + "</profile>";
    assertEquals(mergedText, serialize(profile));
    Element toImportElement = profile.writeScheme();
    final InspectionProfileImpl importedProfile = InspectionToolsConfigurable.importInspectionProfile(toImportElement, getApplicationProfileManager(), getProject());
    //check merged
    Element mergedElement = JdomKt.loadElement(mergedText);
    profile = createProfile(new InspectionProfileImpl("foo"));
    profile.readExternal(mergedElement);
    profile.getModifiableModel().commit();
    assertThat(profile.writeScheme()).isEqualTo(mergedElement);
    assertThat(importedProfile.writeScheme()).isEqualTo(mergedElement);
}
Also used : UnusedDeclarationInspectionBase(com.intellij.codeInspection.deadCode.UnusedDeclarationInspectionBase) UnusedSymbolLocalInspectionBase(com.intellij.codeInspection.unusedSymbol.UnusedSymbolLocalInspectionBase) Element(org.jdom.Element)

Example 2 with UnusedDeclarationInspectionBase

use of com.intellij.codeInspection.deadCode.UnusedDeclarationInspectionBase in project android by JetBrains.

the class AndroidJavaHighlightingTest method testInjectResourceAnnotation.

public void testInjectResourceAnnotation() throws Exception {
    myFixture.enableInspections(new UnusedDeclarationInspectionBase());
    myFixture.copyFileToProject(BASE_PATH + "values.xml", "res/values/values.xml");
    myFixture.copyFileToProject(BASE_PATH + "InjectResource.java", "src/p1/p2/InjectResource.java");
    myFixture.copyFileToProject(BASE_PATH + "SomeAnnotation.java", "src/p1/p2/SomeAnnotation.java");
    myFixture.copyFileToProject(BASE_PATH + "R1.java", "src/p1/p2/R1.java");
    myFixture.copyFileToProject("R.java", "src/p1/p2/R.java");
    final VirtualFile f = myFixture.copyFileToProject(BASE_PATH + getTestName(false) + ".java", "src/p1/p2/MyActivity.java");
    myFixture.configureFromExistingVirtualFile(f);
    myFixture.checkHighlighting(true, false, true);
}
Also used : UnusedDeclarationInspectionBase(com.intellij.codeInspection.deadCode.UnusedDeclarationInspectionBase) VirtualFile(com.intellij.openapi.vfs.VirtualFile)

Example 3 with UnusedDeclarationInspectionBase

use of com.intellij.codeInspection.deadCode.UnusedDeclarationInspectionBase in project android by JetBrains.

the class AndroidJavaHighlightingTest method testParcelable.

public void testParcelable() throws Exception {
    myFixture.enableInspections(new UnusedDeclarationInspectionBase(true));
    final VirtualFile f = myFixture.copyFileToProject(BASE_PATH + getTestName(false) + ".java", "src/p1/p2/MyParcelable.java");
    myFixture.configureFromExistingVirtualFile(f);
    myFixture.checkHighlighting(true, false, true);
}
Also used : UnusedDeclarationInspectionBase(com.intellij.codeInspection.deadCode.UnusedDeclarationInspectionBase) VirtualFile(com.intellij.openapi.vfs.VirtualFile)

Example 4 with UnusedDeclarationInspectionBase

use of com.intellij.codeInspection.deadCode.UnusedDeclarationInspectionBase in project intellij-community by JetBrains.

the class RefJavaManagerImpl method getDeadCodeTool.

private UnusedDeclarationInspectionBase getDeadCodeTool(PsiFile file) {
    Tools tools = ((GlobalInspectionContextBase) myRefManager.getContext()).getTools().get(UnusedDeclarationInspectionBase.SHORT_NAME);
    InspectionToolWrapper toolWrapper = tools == null ? null : tools.getEnabledTool(file);
    InspectionProfileEntry tool = toolWrapper == null ? null : toolWrapper.getTool();
    return tool instanceof UnusedDeclarationInspectionBase ? (UnusedDeclarationInspectionBase) tool : null;
}
Also used : UnusedDeclarationInspectionBase(com.intellij.codeInspection.deadCode.UnusedDeclarationInspectionBase) InspectionProfileEntry(com.intellij.codeInspection.InspectionProfileEntry)

Example 5 with UnusedDeclarationInspectionBase

use of com.intellij.codeInspection.deadCode.UnusedDeclarationInspectionBase in project intellij-community by JetBrains.

the class QuickFixFactoryImpl method registerFixesForUnusedParameter.

@Override
public void registerFixesForUnusedParameter(@NotNull PsiParameter parameter, @NotNull Object highlightInfo) {
    Project myProject = parameter.getProject();
    InspectionProfile profile = InspectionProjectProfileManager.getInstance(myProject).getCurrentProfile();
    UnusedDeclarationInspectionBase unusedParametersInspection = (UnusedDeclarationInspectionBase) profile.getUnwrappedTool(UnusedSymbolLocalInspectionBase.SHORT_NAME, parameter);
    LOG.assertTrue(ApplicationManager.getApplication().isUnitTestMode() || unusedParametersInspection != null);
    List<IntentionAction> options = new ArrayList<>();
    HighlightDisplayKey myUnusedSymbolKey = HighlightDisplayKey.find(UnusedSymbolLocalInspectionBase.SHORT_NAME);
    options.addAll(IntentionManager.getInstance().getStandardIntentionOptions(myUnusedSymbolKey, parameter));
    if (unusedParametersInspection != null) {
        SuppressQuickFix[] batchSuppressActions = unusedParametersInspection.getBatchSuppressActions(parameter);
        Collections.addAll(options, SuppressIntentionActionFromFix.convertBatchToSuppressIntentionActions(batchSuppressActions));
    }
    //need suppress from Unused Parameters but settings from Unused Symbol
    QuickFixAction.registerQuickFixAction((HighlightInfo) highlightInfo, new SafeDeleteFix(parameter), options, HighlightDisplayKey.getDisplayNameByKey(myUnusedSymbolKey));
}
Also used : UnusedDeclarationInspectionBase(com.intellij.codeInspection.deadCode.UnusedDeclarationInspectionBase) Project(com.intellij.openapi.project.Project) HighlightDisplayKey(com.intellij.codeInsight.daemon.HighlightDisplayKey) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) ArrayList(java.util.ArrayList)

Aggregations

UnusedDeclarationInspectionBase (com.intellij.codeInspection.deadCode.UnusedDeclarationInspectionBase)13 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 NotNull (org.jetbrains.annotations.NotNull)3 HighlightDisplayKey (com.intellij.codeInsight.daemon.HighlightDisplayKey)2 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)2 InspectionProfile (com.intellij.codeInspection.InspectionProfile)2 EntryPoint (com.intellij.codeInspection.reference.EntryPoint)2 RefElement (com.intellij.codeInspection.reference.RefElement)2 UnusedSymbolLocalInspectionBase (com.intellij.codeInspection.unusedSymbol.UnusedSymbolLocalInspectionBase)2 Project (com.intellij.openapi.project.Project)2 Element (org.jdom.Element)2 com.intellij.codeHighlighting (com.intellij.codeHighlighting)1 EditorInfo (com.intellij.codeInsight.EditorInfo)1 CompletionContributor (com.intellij.codeInsight.completion.CompletionContributor)1 com.intellij.codeInsight.daemon (com.intellij.codeInsight.daemon)1 HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)1 DeleteCatchFix (com.intellij.codeInsight.daemon.impl.quickfix.DeleteCatchFix)1 LightQuickFixTestCase (com.intellij.codeInsight.daemon.quickFix.LightQuickFixTestCase)1 CodeFoldingManager (com.intellij.codeInsight.folding.CodeFoldingManager)1 EditorHintListener (com.intellij.codeInsight.hint.EditorHintListener)1