Search in sources :

Example 1 with Matcher

use of edu.umd.cs.findbugs.filter.Matcher in project spotbugs by spotbugs.

the class FilterFromBugPicker method makeMatcherFromSelection.

public Matcher makeMatcherFromSelection() {
    HashSet<Sortables> set = new HashSet<>();
    for (Map.Entry<JCheckBox, Sortables> e : map.entrySet()) {
        if (e.getKey().isSelected()) {
            set.add(e.getValue());
        }
    }
    Matcher matcher = null;
    if (!set.isEmpty()) {
        matcher = FilterFactory.makeMatcher(set, bug);
        if (notFilterCheck.isSelected()) {
            matcher = FilterFactory.invertMatcher(matcher);
        }
    }
    return matcher;
}
Also used : JCheckBox(javax.swing.JCheckBox) Matcher(edu.umd.cs.findbugs.filter.Matcher) NotMatcher(edu.umd.cs.findbugs.filter.NotMatcher) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 2 with Matcher

use of edu.umd.cs.findbugs.filter.Matcher in project spotbugs by spotbugs.

the class FilterFactoryTest method shouldReturnTheOriginalMatcherWhenAskedToInvertANotMatcher.

@Test
public void shouldReturnTheOriginalMatcherWhenAskedToInvertANotMatcher() {
    BugInstance bug = new BugInstance("UUF_UNUSED_FIELD", 0);
    Matcher originalMatcher = FilterFactory.makeMatcher(asList(Sortables.BUGCODE), bug);
    Matcher notMatcher = FilterFactory.invertMatcher(originalMatcher);
    Matcher notNotMatcher = FilterFactory.invertMatcher(notMatcher);
    assertSame("Should return the originally wrapped matcher.", originalMatcher, notNotMatcher);
    assertTrue("Original matcher should now not match.", notNotMatcher.match(bug));
}
Also used : NotMatcher(edu.umd.cs.findbugs.filter.NotMatcher) Matcher(edu.umd.cs.findbugs.filter.Matcher) BugInstance(edu.umd.cs.findbugs.BugInstance) Test(org.junit.Test)

Example 3 with Matcher

use of edu.umd.cs.findbugs.filter.Matcher in project spotbugs by spotbugs.

the class MainFrameLoadSaveHelper method importFilter.

/**
 * This method is for when the user wants to open a file.
 */
void importFilter() {
    filterOpenFileChooser.setDialogTitle(L10N.getLocalString("dlg.importFilter_ttl", "Import and merge filter..."));
    boolean retry = true;
    File f;
    while (retry) {
        retry = false;
        int value = filterOpenFileChooser.showOpenDialog(mainFrame);
        if (value != JFileChooser.APPROVE_OPTION) {
            return;
        }
        f = filterOpenFileChooser.getSelectedFile();
        if (!f.exists()) {
            JOptionPane.showMessageDialog(filterOpenFileChooser, "No such file", "Invalid File", JOptionPane.WARNING_MESSAGE);
            retry = true;
            continue;
        }
        Filter filter;
        try {
            filter = Filter.parseFilter(f.getPath());
        } catch (IOException e) {
            JOptionPane.showMessageDialog(filterOpenFileChooser, "Could not load filter.");
            retry = true;
            continue;
        }
        mainFrame.setProjectChanged(true);
        Filter suppressionFilter = mainFrame.getProject().getSuppressionFilter();
        for (Matcher m : filter.getChildren()) {
            suppressionFilter.addChild(m);
        }
        PreferencesFrame.getInstance().updateFilterPanel();
    }
}
Also used : Filter(edu.umd.cs.findbugs.filter.Filter) FileFilter(javax.swing.filechooser.FileFilter) Matcher(edu.umd.cs.findbugs.filter.Matcher) IOException(java.io.IOException) File(java.io.File)

Example 4 with Matcher

use of edu.umd.cs.findbugs.filter.Matcher in project spotbugs by spotbugs.

the class MainFrameTree method createBranchPopUpMenu.

/**
 * Creates the branch pop up menu that ask if the user wants to hide all the
 * bugs in that branch.
 */
JPopupMenu createBranchPopUpMenu() {
    JPopupMenu popupMenu = new JPopupMenu();
    JMenuItem filterMenuItem = MainFrameHelper.newJMenuItem("menu.filterTheseBugs", "Filter these bugs");
    filterMenuItem.addActionListener(evt -> {
        try {
            int startCount;
            TreePath path = MainFrame.getInstance().getTree().getSelectionPath();
            TreePath deletePath = path;
            startCount = ((BugAspects) (path.getLastPathComponent())).getCount();
            int count = ((BugAspects) (path.getParentPath().getLastPathComponent())).getCount();
            while (count == startCount) {
                deletePath = deletePath.getParentPath();
                if (// We are at the
                deletePath.getParentPath() == null) // top of the
                // tree, don't
                // let this be
                // removed,
                // rebuild tree
                // from root.
                {
                    Matcher m1 = mainFrame.getCurrentSelectedBugAspects().getMatcher();
                    Filter suppressionFilter1 = MainFrame.getInstance().getProject().getSuppressionFilter();
                    suppressionFilter1.addChild(m1);
                    PreferencesFrame.getInstance().updateFilterPanel();
                    FilterActivity.notifyListeners(FilterListener.Action.FILTERING, null);
                    return;
                }
                count = ((BugAspects) (deletePath.getParentPath().getLastPathComponent())).getCount();
            }
            /*
                 * deletePath should now be a path to the highest ancestor
                 * branch with the same number of elements as the branch to
                 * be deleted in other words, the branch that we actually
                 * have to remove in order to correctly remove the selected
                 * branch.
                 */
            BugTreeModel model = MainFrame.getInstance().getBugTreeModel();
            TreeModelEvent event = new TreeModelEvent(mainFrame, deletePath.getParentPath(), new int[] { model.getIndexOfChild(deletePath.getParentPath().getLastPathComponent(), deletePath.getLastPathComponent()) }, new Object[] { deletePath.getLastPathComponent() });
            Matcher m2 = mainFrame.getCurrentSelectedBugAspects().getMatcher();
            Filter suppressionFilter2 = MainFrame.getInstance().getProject().getSuppressionFilter();
            suppressionFilter2.addChild(m2);
            PreferencesFrame.getInstance().updateFilterPanel();
            model.sendEvent(event, BugTreeModel.TreeModification.REMOVE);
            // FilterActivity.notifyListeners(FilterListener.Action.FILTERING,
            // null);
            mainFrame.setProjectChanged(true);
            // Selects
            MainFrame.getInstance().getTree().setSelectionRow(0);
        // the
        // top
        // of
        // the
        // Jtree
        // so
        // the
        // CommentsArea
        // syncs
        // up.
        } catch (RuntimeException e) {
            MainFrame.getInstance().showMessageDialog("Unable to create filter: " + e.getMessage());
        }
    });
    popupMenu.add(filterMenuItem);
    return popupMenu;
}
Also used : TreeModelEvent(javax.swing.event.TreeModelEvent) TreePath(javax.swing.tree.TreePath) Matcher(edu.umd.cs.findbugs.filter.Matcher) Filter(edu.umd.cs.findbugs.filter.Filter) JMenuItem(javax.swing.JMenuItem) JPopupMenu(javax.swing.JPopupMenu)

Example 5 with Matcher

use of edu.umd.cs.findbugs.filter.Matcher in project spotbugs by spotbugs.

the class PreferencesFrame method updateFilterPanel.

void updateFilterPanel() {
    ArrayList<MatchBox> boxes = new ArrayList<>();
    final Filter f = MainFrame.getInstance().getProject().getSuppressionFilter();
    for (final Matcher m : f.getChildren()) {
        MatchBox box = new MatchBox(m.toString(), m);
        box.addItemListener(evt -> {
            boolean isSelected = ((JCheckBox) evt.getSource()).isSelected();
            boolean wasSelected = f.isEnabled(m);
            if (isSelected == wasSelected) {
                return;
            }
            f.setEnabled(m, isSelected);
            updateFilters(isSelected);
        });
        box.setSelected(f.isEnabled(m));
        boxes.add(box);
    }
    filterCheckBoxList.setListData(boxes.toArray(new MatchBox[boxes.size()]));
}
Also used : JCheckBox(javax.swing.JCheckBox) Filter(edu.umd.cs.findbugs.filter.Filter) FileFilter(javax.swing.filechooser.FileFilter) Matcher(edu.umd.cs.findbugs.filter.Matcher) ArrayList(java.util.ArrayList)

Aggregations

Matcher (edu.umd.cs.findbugs.filter.Matcher)7 Filter (edu.umd.cs.findbugs.filter.Filter)4 NotMatcher (edu.umd.cs.findbugs.filter.NotMatcher)3 BugInstance (edu.umd.cs.findbugs.BugInstance)2 JCheckBox (javax.swing.JCheckBox)2 JMenuItem (javax.swing.JMenuItem)2 FileFilter (javax.swing.filechooser.FileFilter)2 Test (org.junit.Test)2 MainFrameHelper.newJMenuItem (edu.umd.cs.findbugs.gui2.MainFrameHelper.newJMenuItem)1 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 ButtonGroup (javax.swing.ButtonGroup)1 JCheckBoxMenuItem (javax.swing.JCheckBoxMenuItem)1 JPopupMenu (javax.swing.JPopupMenu)1 JRadioButtonMenuItem (javax.swing.JRadioButtonMenuItem)1 TreeModelEvent (javax.swing.event.TreeModelEvent)1