Search in sources :

Example 6 with Filter

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

the class SAXBugCollectionHandler method addMatcher.

private void addMatcher(Matcher m) {
    if (m == null) {
        throw new IllegalArgumentException("matcher must not be null");
    }
    CompoundMatcher peek = matcherStack.peek();
    if (peek == null) {
        throw new NullPointerException("Top of stack is null");
    }
    peek.addChild(m);
    if (nextMatchedIsDisabled) {
        if (peek instanceof Filter) {
            ((Filter) peek).disable(m);
        } else {
            assert false;
        }
        nextMatchedIsDisabled = false;
    }
}
Also used : CompoundMatcher(edu.umd.cs.findbugs.filter.CompoundMatcher) Filter(edu.umd.cs.findbugs.filter.Filter)

Example 7 with Filter

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

the class SAXBugCollectionHandler method startElement.

@Override
public void startElement(String uri, String name, String qName, Attributes attributes) throws SAXException {
    if (discardedElement(qName)) {
        nestingOfIgnoredElements++;
    } else if (nestingOfIgnoredElements > 0) {
    // ignore it
    } else {
        // We should be parsing the outer BugCollection element.
        if (elementStack.isEmpty() && !qName.equals(topLevelName)) {
            throw new SAXException("Invalid top-level element (expected " + topLevelName + ", saw " + qName + ")");
        }
        if (BUG_COLLECTION.equals(qName)) {
            BugCollection bugCollection = this.bugCollection;
            assert bugCollection != null;
            // Read and set the sequence number.
            String version = getOptionalAttribute(attributes, "version");
            if (bugCollection instanceof SortedBugCollection) {
                bugCollection.setAnalysisVersion(version);
            }
            // Read and set the sequence number.
            String sequence = getOptionalAttribute(attributes, "sequence");
            long seqval = parseLong(sequence, 0L);
            bugCollection.setSequenceNumber(seqval);
            // Read and set timestamp.
            String timestamp = getOptionalAttribute(attributes, "timestamp");
            long tsval = parseLong(timestamp, -1L);
            bugCollection.setTimestamp(tsval);
            // Read and set timestamp.
            String analysisTimestamp = getOptionalAttribute(attributes, "analysisTimestamp");
            if (analysisTimestamp != null) {
                bugCollection.setAnalysisTimestamp(parseLong(analysisTimestamp, -1L));
            }
            String analysisVersion = getOptionalAttribute(attributes, "version");
            if (analysisVersion != null) {
                bugCollection.setAnalysisVersion(analysisVersion);
            }
            // Set release name, if present.
            String releaseName = getOptionalAttribute(attributes, "release");
            bugCollection.setReleaseName((releaseName != null) ? releaseName : "");
        } else if (isTopLevelFilter(qName)) {
            if (project != null) {
                filter = new Filter();
                project.setSuppressionFilter(filter);
            }
            matcherStack.clear();
            pushCompoundMatcher(filter);
        } else if (PROJECT.equals(qName)) {
            Project project = this.project;
            assert project != null;
            // Project element
            String projectName = getOptionalAttribute(attributes, Project.PROJECTNAME_ATTRIBUTE_NAME);
            if (projectName != null) {
                project.setProjectName(projectName);
            }
        } else {
            String outerElement = elementStack.get(elementStack.size() - 1);
            if (BUG_COLLECTION.equals(outerElement)) {
                // Parsing a top-level element of the BugCollection
                if ("BugInstance".equals(qName)) {
                    // BugInstance element - get required type and priority
                    // attributes
                    String type = getRequiredAttribute(attributes, "type", qName);
                    String priority = getRequiredAttribute(attributes, "priority", qName);
                    try {
                        int prio = Integer.parseInt(priority);
                        bugInstance = new BugInstance(type, prio);
                    } catch (NumberFormatException e) {
                        throw new SAXException("BugInstance with invalid priority value \"" + priority + "\"", e);
                    }
                    String firstVersion = getOptionalAttribute(attributes, "first");
                    if (firstVersion != null) {
                        bugInstance.setFirstVersion(Long.parseLong(firstVersion));
                    }
                    String lastVersion = getOptionalAttribute(attributes, "last");
                    if (lastVersion != null) {
                        bugInstance.setLastVersion(Long.parseLong(lastVersion));
                    }
                    if (bugInstance.isDead() && bugInstance.getFirstVersion() > bugInstance.getLastVersion()) {
                        throw new IllegalStateException("huh");
                    }
                    String introducedByChange = getOptionalAttribute(attributes, "introducedByChange");
                    if (introducedByChange != null) {
                        bugInstance.setIntroducedByChangeOfExistingClass(Boolean.parseBoolean(introducedByChange));
                    }
                    String removedByChange = getOptionalAttribute(attributes, "removedByChange");
                    if (removedByChange != null) {
                        bugInstance.setRemovedByChangeOfPersistingClass(Boolean.parseBoolean(removedByChange));
                    }
                    String oldInstanceHash = getOptionalAttribute(attributes, "instanceHash");
                    if (oldInstanceHash == null) {
                        oldInstanceHash = getOptionalAttribute(attributes, "oldInstanceHash");
                    }
                    if (oldInstanceHash != null) {
                        bugInstance.setOldInstanceHash(oldInstanceHash);
                    }
                } else if ("FindBugsSummary".equals(qName)) {
                    BugCollection bugCollection = this.bugCollection;
                    assert bugCollection != null;
                    String timestamp = getRequiredAttribute(attributes, "timestamp", qName);
                    String vmVersion = getOptionalAttribute(attributes, "vm_version");
                    String totalClasses = getOptionalAttribute(attributes, "total_classes");
                    if (totalClasses != null && totalClasses.length() > 0) {
                        bugCollection.getProjectStats().setTotalClasses(Integer.parseInt(totalClasses));
                    }
                    String totalSize = getOptionalAttribute(attributes, "total_size");
                    if (totalSize != null && totalSize.length() > 0) {
                        bugCollection.getProjectStats().setTotalSize(Integer.parseInt(totalSize));
                    }
                    String referencedClasses = getOptionalAttribute(attributes, "referenced_classes");
                    if (referencedClasses != null && referencedClasses.length() > 0) {
                        bugCollection.getProjectStats().setReferencedClasses(Integer.parseInt(referencedClasses));
                    }
                    bugCollection.getProjectStats().setVMVersion(vmVersion);
                    try {
                        bugCollection.getProjectStats().setTimestamp(timestamp);
                    } catch (java.text.ParseException e) {
                        throw new SAXException("Unparseable sequence number: '" + timestamp + "'", e);
                    }
                }
            } else if ("BugInstance".equals(outerElement)) {
                parseBugInstanceContents(qName, attributes);
            } else if ("Method".equals(outerElement) || "Field".equals(outerElement) || "Class".equals(outerElement) || "Type".equals(outerElement)) {
                if ("SourceLine".equals(qName)) {
                    // package member elements can contain nested SourceLine
                    // elements.
                    bugAnnotationWithSourceLines.setSourceLines(createSourceLineAnnotation(qName, attributes));
                }
            } else if (BugCollection.ERRORS_ELEMENT_NAME.equals(outerElement)) {
                if (BugCollection.ANALYSIS_ERROR_ELEMENT_NAME.equals(qName) || BugCollection.ERROR_ELEMENT_NAME.equals(qName)) {
                    analysisError = new AnalysisError("Unknown error");
                    stackTrace.clear();
                }
            } else if ("FindBugsSummary".equals(outerElement) && "PackageStats".equals(qName)) {
                BugCollection bugCollection = this.bugCollection;
                assert bugCollection != null;
                String packageName = getRequiredAttribute(attributes, "package", qName);
                int numClasses = Integer.parseInt(getRequiredAttribute(attributes, "total_types", qName));
                int size = Integer.parseInt(getRequiredAttribute(attributes, "total_size", qName));
                bugCollection.getProjectStats().putPackageStats(packageName, numClasses, size);
            } else if ("PackageStats".equals(outerElement)) {
                BugCollection bugCollection = this.bugCollection;
                assert bugCollection != null;
                if ("ClassStats".equals(qName)) {
                    String className = getRequiredAttribute(attributes, "class", qName);
                    Boolean isInterface = Boolean.valueOf(getRequiredAttribute(attributes, "interface", qName));
                    int size = Integer.parseInt(getRequiredAttribute(attributes, "size", qName));
                    String sourceFile = getOptionalAttribute(attributes, "sourceFile");
                    bugCollection.getProjectStats().addClass(className, sourceFile, isInterface, size, false);
                }
            } else if (isTopLevelFilter(outerElement) || isCompoundElementTag(outerElement)) {
                parseMatcher(qName, attributes);
            } else if ("ClassFeatures".equals(outerElement)) {
                if (ClassFeatureSet.ELEMENT_NAME.equals(qName)) {
                    String className = getRequiredAttribute(attributes, "class", qName);
                    classFeatureSet = new ClassFeatureSet();
                    classFeatureSet.setClassName(className);
                }
            } else if (ClassFeatureSet.ELEMENT_NAME.equals(outerElement)) {
                if (ClassFeatureSet.FEATURE_ELEMENT_NAME.equals(qName)) {
                    String value = getRequiredAttribute(attributes, "value", qName);
                    classFeatureSet.addFeature(value);
                }
            } else if (BugCollection.HISTORY_ELEMENT_NAME.equals(outerElement)) {
                if (AppVersion.ELEMENT_NAME.equals(qName)) {
                    BugCollection bugCollection = this.bugCollection;
                    assert bugCollection != null;
                    try {
                        String sequence = getRequiredAttribute(attributes, "sequence", qName);
                        String timestamp = getOptionalAttribute(attributes, "timestamp");
                        String releaseName = getOptionalAttribute(attributes, "release");
                        String codeSize = getOptionalAttribute(attributes, "codeSize");
                        String numClasses = getOptionalAttribute(attributes, "numClasses");
                        AppVersion appVersion = new AppVersion(Long.parseLong(sequence));
                        if (timestamp != null) {
                            appVersion.setTimestamp(Long.parseLong(timestamp));
                        }
                        if (releaseName != null) {
                            appVersion.setReleaseName(releaseName);
                        }
                        if (codeSize != null) {
                            appVersion.setCodeSize(Integer.parseInt(codeSize));
                        }
                        if (numClasses != null) {
                            appVersion.setNumClasses(Integer.parseInt(numClasses));
                        }
                        bugCollection.addAppVersion(appVersion);
                    } catch (NumberFormatException e) {
                        throw new SAXException("Invalid AppVersion element", e);
                    }
                }
            } else if (BugCollection.PROJECT_ELEMENT_NAME.equals(outerElement)) {
                Project project = this.project;
                assert project != null;
                if (Project.PLUGIN_ELEMENT_NAME.equals(qName)) {
                    String pluginId = getRequiredAttribute(attributes, Project.PLUGIN_ID_ATTRIBUTE_NAME, qName);
                    Boolean enabled = Boolean.valueOf(getRequiredAttribute(attributes, Project.PLUGIN_STATUS_ELEMENT_NAME, qName));
                    project.setPluginStatusTrinary(pluginId, enabled);
                }
            }
        }
    }
    textBuffer.delete(0, textBuffer.length());
    elementStack.add(qName);
}
Also used : ClassFeatureSet(edu.umd.cs.findbugs.model.ClassFeatureSet) SAXException(org.xml.sax.SAXException) Filter(edu.umd.cs.findbugs.filter.Filter)

Example 8 with Filter

use of edu.umd.cs.findbugs.filter.Filter 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)

Example 9 with Filter

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

the class MainFrameLoadSaveHelper method exportFilter.

boolean exportFilter() {
    filterOpenFileChooser.setDialogTitle(L10N.getLocalString("dlg.exportFilter_ttl", "Export filter..."));
    boolean retry = true;
    boolean alreadyExists;
    File f;
    while (retry) {
        retry = false;
        int value = filterOpenFileChooser.showSaveDialog(mainFrame);
        if (value != JFileChooser.APPROVE_OPTION) {
            return false;
        }
        f = filterOpenFileChooser.getSelectedFile();
        alreadyExists = f.exists();
        if (alreadyExists) {
            int response = JOptionPane.showConfirmDialog(filterOpenFileChooser, L10N.getLocalString("dlg.file_exists_lbl", "This file already exists.\nReplace it?"), L10N.getLocalString("dlg.warning_ttl", "Warning!"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
            if (response == JOptionPane.OK_OPTION) {
                retry = false;
            }
            if (response == JOptionPane.CANCEL_OPTION) {
                retry = true;
                continue;
            }
        }
        Filter suppressionFilter = mainFrame.getProject().getSuppressionFilter();
        try {
            suppressionFilter.writeEnabledMatchersAsXML(new FileOutputStream(f));
        } catch (IOException e) {
            JOptionPane.showMessageDialog(mainFrame, L10N.getLocalString("dlg.saving_error_lbl", "An error occurred in saving."));
            return false;
        }
    }
    return true;
}
Also used : Filter(edu.umd.cs.findbugs.filter.Filter) FileFilter(javax.swing.filechooser.FileFilter) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File)

Example 10 with Filter

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

the class MainFrameMenu method setViewMenu.

void setViewMenu() {
    viewMenu.removeAll();
    viewMenu.add(groupByMenuItem);
    if (mainFrame.getProjectPackagePrefixes().size() > 0 && mainFrame.getBugCollection() != null) {
        JMenuItem selectPackagePrefixMenu = new JMenuItem("Select class search strings by project...");
        selectPackagePrefixMenu.addActionListener(e -> mainFrame.selectPackagePrefixByProject());
        viewMenu.add(selectPackagePrefixMenu);
    }
    if (viewMenu.getItemCount() > 0) {
        viewMenu.addSeparator();
    }
    ButtonGroup rankButtonGroup = new ButtonGroup();
    for (final ViewFilter.RankFilter r : ViewFilter.RankFilter.values()) {
        JRadioButtonMenuItem rbMenuItem = new JRadioButtonMenuItem(r.toString());
        AnnotatedString.localiseButton(rbMenuItem, "menu.rankFilter_" + r.name().toLowerCase(Locale.ENGLISH), r.toString(), true);
        rankButtonGroup.add(rbMenuItem);
        if (r == ViewFilter.RankFilter.ALL) {
            rbMenuItem.setSelected(true);
        }
        rbMenuItem.addActionListener(e -> {
            mainFrame.getViewFilter().setRank(r);
            mainFrame.resetViewCache();
        });
        viewMenu.add(rbMenuItem);
    }
    viewMenu.addSeparator();
    ButtonGroup priorityButtonGroup = new ButtonGroup();
    for (final ViewFilter.PriorityFilter r : ViewFilter.PriorityFilter.values()) {
        JRadioButtonMenuItem rbMenuItem = new JRadioButtonMenuItem(r.toString());
        AnnotatedString.localiseButton(rbMenuItem, "menu.priorityFilter_" + r.name().toLowerCase(Locale.ENGLISH), r.toString(), true);
        priorityButtonGroup.add(rbMenuItem);
        if (r == ViewFilter.PriorityFilter.ALL_BUGS) {
            rbMenuItem.setSelected(true);
        }
        rbMenuItem.addActionListener(e -> {
            mainFrame.getViewFilter().setPriority(r);
            mainFrame.resetViewCache();
        });
        viewMenu.add(rbMenuItem);
    }
    viewMenu.addSeparator();
    final Filter suppressionFilter = MainFrame.getInstance().getProject().getSuppressionFilter();
    Collection<Matcher> filters = suppressionFilter.getChildren();
    JMenuItem filterMenu = new JMenuItem(filters.isEmpty() ? "Add Filters..." : "Filters...");
    filterMenu.addActionListener(e -> {
        PreferencesFrame preferenceFrame = PreferencesFrame.getInstance();
        preferenceFrame.showFilterPane();
        preferenceFrame.setLocationRelativeTo(mainFrame);
        preferenceFrame.setVisible(true);
    });
    viewMenu.add(filterMenu);
    for (final Matcher m : filters) {
        JCheckBoxMenuItem f = new JCheckBoxMenuItem(m.toString(), suppressionFilter.isEnabled(m));
        viewMenu.add(f);
        f.addItemListener(e -> {
            boolean enabled = e.getStateChange() == ItemEvent.SELECTED;
            suppressionFilter.setEnabled(m, enabled);
            FilterActivity.notifyListeners(enabled ? Action.FILTERING : Action.UNFILTERING, null);
        });
    }
}
Also used : ButtonGroup(javax.swing.ButtonGroup) Filter(edu.umd.cs.findbugs.filter.Filter) Matcher(edu.umd.cs.findbugs.filter.Matcher) JRadioButtonMenuItem(javax.swing.JRadioButtonMenuItem) JMenuItem(javax.swing.JMenuItem) MainFrameHelper.newJMenuItem(edu.umd.cs.findbugs.gui2.MainFrameHelper.newJMenuItem) JCheckBoxMenuItem(javax.swing.JCheckBoxMenuItem)

Aggregations

Filter (edu.umd.cs.findbugs.filter.Filter)12 Matcher (edu.umd.cs.findbugs.filter.Matcher)5 FileFilter (javax.swing.filechooser.FileFilter)4 Project (edu.umd.cs.findbugs.Project)3 JMenuItem (javax.swing.JMenuItem)3 GridBagConstraints (java.awt.GridBagConstraints)2 GridBagLayout (java.awt.GridBagLayout)2 Insets (java.awt.Insets)2 File (java.io.File)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 JButton (javax.swing.JButton)2 JLabel (javax.swing.JLabel)2 JPanel (javax.swing.JPanel)2 JPopupMenu (javax.swing.JPopupMenu)2 JScrollPane (javax.swing.JScrollPane)2 TreeModelEvent (javax.swing.event.TreeModelEvent)2 TreePath (javax.swing.tree.TreePath)2 CompoundMatcher (edu.umd.cs.findbugs.filter.CompoundMatcher)1 FilterActivityNotifier (edu.umd.cs.findbugs.gui2.FilterActivity.FilterActivityNotifier)1