Search in sources :

Example 11 with Project

use of edu.umd.cs.findbugs.Project in project spotbugs by spotbugs.

the class FindBugsWorker method work.

/**
 * Run FindBugs on the given collection of resources from same project
 * (note: This is currently not thread-safe)
 *
 * @param resources
 *            files or directories which should be on the project classpath.
 *            All resources must belong to the same project, and no one of
 *            the elements can contain another one. Ergo, if the list
 *            contains a project itself, then it must have only one element.
 * @throws CoreException
 */
public void work(List<WorkItem> resources) throws CoreException {
    if (resources == null || resources.isEmpty()) {
        if (DEBUG) {
            FindbugsPlugin.getDefault().logInfo("No resources to analyse for project " + project);
        }
        return;
    }
    if (DEBUG) {
        System.out.println(resources);
    }
    st = new StopTimer();
    st.newPoint("initPlugins");
    // make sure it's initialized
    FindbugsPlugin.applyCustomDetectors(false);
    st.newPoint("clearMarkers");
    // clear markers
    clearMarkers(resources);
    st.newPoint("configureOutputFiles");
    final Project findBugsProject = new Project();
    findBugsProject.setProjectName(javaProject.getElementName());
    final Reporter bugReporter = new Reporter(javaProject, findBugsProject, monitor);
    if (FindBugsConsole.getConsole() != null) {
        bugReporter.setReportingStream(FindBugsConsole.getConsole().newOutputStream());
    }
    bugReporter.setPriorityThreshold(userPrefs.getUserDetectorThreshold());
    FindBugs.setHome(FindbugsPlugin.getFindBugsEnginePluginLocation());
    Map<IPath, IPath> outLocations = createOutputLocations();
    // collect all related class/jar/war etc files for analysis
    collectClassFiles(resources, outLocations, findBugsProject);
    // attach source directories (can be used by some detectors, see
    // SwitchFallthrough)
    configureSourceDirectories(findBugsProject, outLocations);
    if (findBugsProject.getFileCount() == 0) {
        if (DEBUG) {
            FindbugsPlugin.getDefault().logInfo("No resources to analyse for project " + project);
        }
        return;
    }
    st.newPoint("createAuxClasspath");
    String[] classPathEntries = createAuxClasspath();
    // add to findbugs classpath
    for (String entry : classPathEntries) {
        findBugsProject.addAuxClasspathEntry(entry);
    }
    st.newPoint("configureProps");
    IPreferenceStore store = FindbugsPlugin.getPluginPreferences(project);
    boolean cacheClassData = store.getBoolean(FindBugsConstants.KEY_CACHE_CLASS_DATA);
    final FindBugs2 findBugs = new FindBugs2Eclipse(project, cacheClassData, bugReporter);
    findBugs.setNoClassOk(true);
    findBugs.setProject(findBugsProject);
    findBugs.setBugReporter(bugReporter);
    findBugs.setProgressCallback(bugReporter);
    findBugs.setDetectorFactoryCollection(DetectorFactoryCollection.instance());
    // configure detectors.
    userPrefs.setIncludeFilterFiles(relativeToAbsolute(userPrefs.getIncludeFilterFiles()));
    userPrefs.setExcludeFilterFiles(relativeToAbsolute(userPrefs.getExcludeFilterFiles()));
    userPrefs.setExcludeBugsFiles(relativeToAbsolute(userPrefs.getExcludeBugsFiles()));
    findBugs.setUserPreferences(userPrefs);
    // configure extended preferences
    findBugs.setAnalysisFeatureSettings(userPrefs.getAnalysisFeatureSettings());
    findBugs.setMergeSimilarWarnings(false);
    if (cacheClassData) {
        FindBugs2Eclipse.checkClassPathChanges(findBugs.getProject().getAuxClasspathEntryList(), project);
    }
    st.newPoint("runFindBugs");
    if (DEBUG) {
        FindbugsPlugin.log("Running SpotBugs");
    }
    runFindBugs(findBugs);
    if (DEBUG) {
        FindbugsPlugin.log("Done running SpotBugs");
    }
    // Merge new results into existing results
    // if the argument is project, then it's not incremental
    boolean incremental = !(resources.get(0) instanceof IProject);
    updateBugCollection(findBugsProject, bugReporter, incremental);
    st.newPoint("done");
    st = null;
    monitor.done();
}
Also used : IProject(org.eclipse.core.resources.IProject) Project(edu.umd.cs.findbugs.Project) IJavaProject(org.eclipse.jdt.core.IJavaProject) IPath(org.eclipse.core.runtime.IPath) FindBugs2(edu.umd.cs.findbugs.FindBugs2) Reporter(de.tobject.findbugs.reporter.Reporter) StopTimer(de.tobject.findbugs.util.Util.StopTimer) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore) IProject(org.eclipse.core.resources.IProject)

Example 12 with Project

use of edu.umd.cs.findbugs.Project in project spotbugs by spotbugs.

the class MainFrameTree method bugListPanel.

public JPanel bugListPanel() {
    tableheader = new JTableHeader();
    getTableheader().setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
    // Listener put here for when user double clicks on sorting
    // column header SorterDialog appears.
    getTableheader().addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent e) {
            Debug.println("tableheader.getReorderingAllowed() = " + getTableheader().getReorderingAllowed());
            if (!getTableheader().getReorderingAllowed()) {
                return;
            }
            if (e.getClickCount() == 2) {
                SorterDialog.getInstance().setVisible(true);
            }
        }

        @Override
        public void mouseReleased(MouseEvent arg0) {
            if (!getTableheader().getReorderingAllowed()) {
                return;
            }
            BugTreeModel bt = (BugTreeModel) (getTree().getModel());
            bt.checkSorter();
        }
    });
    sorter = GUISaveState.getInstance().getStarterTable();
    getTableheader().setColumnModel(getSorter());
    getTableheader().setToolTipText(edu.umd.cs.findbugs.L10N.getLocalString("tooltip.reorder_message", "Drag to reorder tree folder and sort order"));
    tree = new JTree();
    getTree().setLargeModel(true);
    getTree().getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    getTree().setCellRenderer(new BugRenderer());
    getTree().setRowHeight((int) (Driver.getFontSize() + 7));
    getTree().setModel(new BugTreeModel(mainFrame, getTree(), getSorter(), new BugSet(new ArrayList<BugLeafNode>())));
    setupTreeListeners();
    mainFrame.setProject(new Project());
    treeScrollPane = new JScrollPane(getTree());
    treePanel = new JPanel(new BorderLayout());
    treePanel.add(treeScrollPane, BorderLayout.CENTER);
    JTable t = new JTable(new DefaultTableModel(0, sortables().length));
    t.setTableHeader(getTableheader());
    textFieldForPackagesToDisplay = new JTextField();
    ActionListener filterAction = e -> {
        try {
            String text = textFieldForPackagesToDisplay.getText();
            if (text.indexOf('/') >= 0) {
                text = text.replace('/', '.');
                textFieldForPackagesToDisplay.setText(text);
            }
            mainFrame.getViewFilter().setPackagesToDisplay(text);
            mainFrame.resetViewCache();
        } catch (IllegalArgumentException err) {
            JOptionPane.showMessageDialog(mainFrame, err.getMessage(), "Bad class search string", JOptionPane.ERROR_MESSAGE);
        }
    };
    textFieldForPackagesToDisplay.addActionListener(filterAction);
    JButton filterButton = new JButton("Filter");
    filterButton.addActionListener(filterAction);
    JPanel filterPanel = new JPanel();
    filterPanel.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.weightx = 1;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.gridy = 1;
    filterPanel.add(textFieldForPackagesToDisplay, gbc);
    gbc.weightx = 0;
    gbc.fill = GridBagConstraints.NONE;
    filterPanel.add(filterButton, gbc);
    filterPanel.setToolTipText("Only show classes containing the word(s) you specify");
    JPanel sortablePanel = new JPanel(new GridBagLayout());
    JLabel sortableLabel = new JLabel("Group bugs by:");
    sortableLabel.setLabelFor(getTableheader());
    gbc = new GridBagConstraints();
    gbc.weightx = 0;
    gbc.gridy = 1;
    gbc.insets = new Insets(3, 3, 3, 3);
    gbc.fill = GridBagConstraints.BOTH;
    sortablePanel.add(sortableLabel, gbc);
    gbc.weightx = 1;
    sortablePanel.add(getTableheader(), gbc);
    getTableheader().setBorder(new LineBorder(Color.BLACK));
    JPanel topPanel = makeNavigationPanel("Class name filter:", filterPanel, sortablePanel, treePanel);
    cardPanel = new JPanel(new CardLayout());
    JPanel waitPanel = new JPanel();
    waitPanel.setLayout(new BoxLayout(waitPanel, BoxLayout.Y_AXIS));
    waitPanel.add(new JLabel("Please wait..."));
    waitStatusLabel = new JLabel();
    waitPanel.add(waitStatusLabel);
    cardPanel.add(topPanel, MainFrame.BugCard.TREECARD.name());
    cardPanel.add(waitPanel, MainFrame.BugCard.WAITCARD.name());
    return cardPanel;
}
Also used : Color(java.awt.Color) Insets(java.awt.Insets) Arrays(java.util.Arrays) TreeModelEvent(javax.swing.event.TreeModelEvent) CardLayout(java.awt.CardLayout) ActionListener(java.awt.event.ActionListener) Filter(edu.umd.cs.findbugs.filter.Filter) JTextField(javax.swing.JTextField) Cursor(java.awt.Cursor) Action(javax.swing.Action) LineBorder(javax.swing.border.LineBorder) JTableHeader(javax.swing.table.JTableHeader) TreeSelectionEvent(javax.swing.event.TreeSelectionEvent) ArrayList(java.util.ArrayList) SwingUtilities(javax.swing.SwingUtilities) TreeSelectionListener(javax.swing.event.TreeSelectionListener) JMenuItem(javax.swing.JMenuItem) FilterActivityNotifier(edu.umd.cs.findbugs.gui2.FilterActivity.FilterActivityNotifier) MouseAdapter(java.awt.event.MouseAdapter) BorderLayout(java.awt.BorderLayout) ActionMap(javax.swing.ActionMap) MouseListener(java.awt.event.MouseListener) BoxLayout(javax.swing.BoxLayout) JComponent(javax.swing.JComponent) Project(edu.umd.cs.findbugs.Project) JButton(javax.swing.JButton) DefaultTableModel(javax.swing.table.DefaultTableModel) TreePath(javax.swing.tree.TreePath) Window(java.awt.Window) JPopupMenu(javax.swing.JPopupMenu) Matcher(edu.umd.cs.findbugs.filter.Matcher) TreeSelectionModel(javax.swing.tree.TreeSelectionModel) GridBagConstraints(java.awt.GridBagConstraints) JOptionPane(javax.swing.JOptionPane) JTree(javax.swing.JTree) MouseEvent(java.awt.event.MouseEvent) JScrollPane(javax.swing.JScrollPane) Dimension(java.awt.Dimension) List(java.util.List) JLabel(javax.swing.JLabel) JTable(javax.swing.JTable) GridBagLayout(java.awt.GridBagLayout) Collections(java.util.Collections) JPanel(javax.swing.JPanel) JPanel(javax.swing.JPanel) GridBagConstraints(java.awt.GridBagConstraints) Insets(java.awt.Insets) GridBagLayout(java.awt.GridBagLayout) DefaultTableModel(javax.swing.table.DefaultTableModel) LineBorder(javax.swing.border.LineBorder) BoxLayout(javax.swing.BoxLayout) JButton(javax.swing.JButton) JTableHeader(javax.swing.table.JTableHeader) JTextField(javax.swing.JTextField) BorderLayout(java.awt.BorderLayout) JScrollPane(javax.swing.JScrollPane) CardLayout(java.awt.CardLayout) MouseEvent(java.awt.event.MouseEvent) MouseAdapter(java.awt.event.MouseAdapter) JLabel(javax.swing.JLabel) Project(edu.umd.cs.findbugs.Project) JTree(javax.swing.JTree) ActionListener(java.awt.event.ActionListener) JTable(javax.swing.JTable)

Example 13 with Project

use of edu.umd.cs.findbugs.Project in project spotbugs by spotbugs.

the class NewProjectWizard method createFilePanel.

private JPanel createFilePanel(final String label, final JList<String> list, final DefaultListModel<String> listModel, final int fileSelectionMode, final FileFilter filter, final String dialogTitle, boolean wizard, final String helpUrl) {
    JPanel myPanel = new JPanel(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.gridheight = 1;
    gbc.gridwidth = 1;
    gbc.weightx = 1;
    gbc.weighty = 0;
    gbc.anchor = GridBagConstraints.WEST;
    myPanel.add(new JLabel(label), gbc);
    gbc.gridx = 1;
    gbc.gridy = 0;
    gbc.gridheight = 1;
    gbc.gridwidth = 1;
    gbc.weightx = 0;
    gbc.weighty = 0;
    gbc.anchor = GridBagConstraints.WEST;
    JButton button = new JButton("<HTML><U>Help");
    button.setFont(button.getFont().deriveFont(Font.PLAIN));
    button.setForeground(Color.BLUE);
    button.setBorderPainted(false);
    button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    button.setContentAreaFilled(false);
    button.addActionListener(e -> {
        try {
            LaunchBrowser.showDocument(new URL(helpUrl));
        } catch (MalformedURLException e1) {
            throw new IllegalStateException(e1);
        }
    });
    myPanel.add(button, gbc);
    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.gridheight = 3;
    gbc.gridwidth = 1;
    gbc.weightx = 1;
    gbc.weighty = 1;
    gbc.fill = GridBagConstraints.BOTH;
    myPanel.add(new JScrollPane(list), gbc);
    list.setModel(listModel);
    gbc.gridx = 1;
    gbc.gridy = 1;
    gbc.gridheight = 1;
    gbc.gridwidth = 1;
    gbc.weightx = 0;
    gbc.weighty = 0;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    final JButton addButton = new JButton(edu.umd.cs.findbugs.L10N.getLocalString("dlg.add_btn", "Add"));
    myPanel.add(addButton, gbc);
    gbc.gridx = 1;
    gbc.gridy = 2;
    gbc.insets = new Insets(5, 0, 0, 0);
    final JButton removeButton = new JButton(edu.umd.cs.findbugs.L10N.getLocalString("dlg.remove_btn", "Remove"));
    myPanel.add(removeButton, gbc);
    gbc.gridx = 1;
    gbc.gridy = 3;
    final JButton wizardButton = new JButton("Wizard");
    if (wizard) {
        final NewProjectWizard thisGUI = this;
        myPanel.add(wizardButton, gbc);
        wizardButton.addActionListener(evt -> {
            final Project tempProject = new Project();
            for (int i1 = 0; i1 < analyzeModel.getSize(); i1++) {
                tempProject.addFile(analyzeModel.get(i1));
            }
            for (int i2 = 0; i2 < auxModel.getSize(); i2++) {
                tempProject.addAuxClasspathEntry(auxModel.get(i2));
            }
            java.awt.EventQueue.invokeLater(() -> {
                final SourceDirectoryWizard dialog = new SourceDirectoryWizard(new javax.swing.JFrame(), true, tempProject, thisGUI);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        if (dialog.discover != null && dialog.discover.isAlive()) {
                            dialog.discover.interrupt();
                        }
                    }
                });
                dialog.setVisible(true);
            });
        });
    }
    gbc.insets = new Insets(0, 0, 0, 0);
    myPanel.add(Box.createGlue(), gbc);
    myPanel.setBorder(border);
    addButton.addActionListener(evt -> {
        chooser.setFileSelectionMode(fileSelectionMode);
        chooser.setMultiSelectionEnabled(true);
        chooser.setApproveButtonText("Choose");
        chooser.setDialogTitle(dialogTitle);
        // Removes all the file filters currently in the chooser.
        for (FileFilter ff : chooser.getChoosableFileFilters()) {
            chooser.removeChoosableFileFilter(ff);
        }
        chooser.setFileFilter(filter);
        if (chooser.showOpenDialog(NewProjectWizard.this) == JFileChooser.APPROVE_OPTION) {
            File[] selectedFiles = chooser.getSelectedFiles();
            for (File selectedFile : selectedFiles) {
                listModel.addElement(selectedFile.getAbsolutePath());
            }
            projectChanged = true;
            // it to enable the finish button of the main dialog
            if (label.equals(edu.umd.cs.findbugs.L10N.getLocalString("dlg.class_jars_dirs_lbl", "Class archives and directories to analyze:"))) {
                finishButton.setEnabled(true);
            }
        }
    });
    removeButton.addActionListener(evt -> {
        if (list.getSelectedValues().length > 0) {
            projectChanged = true;
        }
        for (Object i : list.getSelectedValues()) {
            listModel.removeElement(i);
        // If this is the primary class directories remove button, set
        // it to disable finish when there are no class files being
        // analyzed
        // if (listModel.size()==0 &&
        // label.equals(edu.umd.cs.findbugs.L10N.getLocalString("dlg.class_jars_dirs_lbl",
        // "Class archives and directories to analyze:")))
        // finishButton.setEnabled(false);
        }
    });
    return myPanel;
}
Also used : JPanel(javax.swing.JPanel) GridBagConstraints(java.awt.GridBagConstraints) MalformedURLException(java.net.MalformedURLException) Insets(java.awt.Insets) GridBagLayout(java.awt.GridBagLayout) JButton(javax.swing.JButton) URL(java.net.URL) FileFilter(javax.swing.filechooser.FileFilter) JScrollPane(javax.swing.JScrollPane) JLabel(javax.swing.JLabel) Project(edu.umd.cs.findbugs.Project) File(java.io.File)

Example 14 with Project

use of edu.umd.cs.findbugs.Project in project spotbugs by spotbugs.

the class PreferencesFrame method handleWindowClose.

private void handleWindowClose() {
    TreeModel bt = (MainFrame.getInstance().getTree().getModel());
    if (bt instanceof BugTreeModel) {
        ((BugTreeModel) bt).checkSorter();
    }
    Project project = getCurrentProject();
    boolean changed = pluginsAdded;
    pluginsAdded = false;
    List<String> enabledPlugins = new ArrayList<>();
    List<String> disabledPlugins = new ArrayList<>();
    for (Map.Entry<Plugin, EnabledSettings> entry : pluginEnabledStatus.entrySet()) {
        Plugin plugin = entry.getKey();
        EnabledSettings enabled = entry.getValue();
        if (project != null) {
            Boolean newSetting = enabled.project;
            Boolean existingSetting = project.getPluginStatus(plugin);
            boolean sameSettings = Objects.equals(existingSetting, newSetting);
            if (!sameSettings) {
                project.setPluginStatusTrinary(plugin.getPluginId(), newSetting);
                changed = true;
            }
        }
        if (enabled.global) {
            enabledPlugins.add(plugin.getPluginId());
        } else {
            disabledPlugins.add(plugin.getPluginId());
        }
        if (plugin.isGloballyEnabled() != enabled.global) {
            plugin.setGloballyEnabled(enabled.global);
            changed = true;
        }
    }
    if (changed && project != null) {
        MainFrame.getInstance().updateBugTree();
        MainFrame.getInstance().setProjectChanged(true);
    }
    if (project == null) {
        GUISaveState.getInstance().setPluginsEnabled(enabledPlugins, disabledPlugins);
        GUISaveState.getInstance().save();
    }
}
Also used : TreeModel(javax.swing.tree.TreeModel) Project(edu.umd.cs.findbugs.Project) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) Plugin(edu.umd.cs.findbugs.Plugin)

Example 15 with Project

use of edu.umd.cs.findbugs.Project in project spotbugs by spotbugs.

the class SourceDirectoryWizard method main.

// GEN-LAST:event_finshButtonActionPerformed
/**
 * @param args
 *            the command line arguments
 */
public static void main(String[] args) {
    java.awt.EventQueue.invokeLater(() -> {
        final SourceDirectoryWizard dialog = new SourceDirectoryWizard(new JFrame(), true, new Project(), null);
        dialog.setVisible(true);
    });
}
Also used : Project(edu.umd.cs.findbugs.Project) JFrame(javax.swing.JFrame)

Aggregations

Project (edu.umd.cs.findbugs.Project)35 SortedBugCollection (edu.umd.cs.findbugs.SortedBugCollection)8 File (java.io.File)8 BugInstance (edu.umd.cs.findbugs.BugInstance)6 AnalysisContext (edu.umd.cs.findbugs.ba.AnalysisContext)4 Test (org.junit.jupiter.api.Test)4 DetectorFactoryCollection (edu.umd.cs.findbugs.DetectorFactoryCollection)3 FindBugs2 (edu.umd.cs.findbugs.FindBugs2)3 Plugin (edu.umd.cs.findbugs.Plugin)3 ProjectStats (edu.umd.cs.findbugs.ProjectStats)3 UserPreferences (edu.umd.cs.findbugs.config.UserPreferences)3 Filter (edu.umd.cs.findbugs.filter.Filter)3 GridBagConstraints (java.awt.GridBagConstraints)3 Insets (java.awt.Insets)3 IOException (java.io.IOException)3 Reporter (de.tobject.findbugs.reporter.Reporter)2 StopTimer (de.tobject.findbugs.util.Util.StopTimer)2 ClassAnnotation (edu.umd.cs.findbugs.ClassAnnotation)2 PackageStats (edu.umd.cs.findbugs.PackageStats)2 PluginException (edu.umd.cs.findbugs.PluginException)2