Search in sources :

Example 1 with PluginException

use of edu.umd.cs.findbugs.PluginException in project sonar-findbugs by spotbugs.

the class FindbugsExecutor method loadFindbugsPlugins.

private Collection<Plugin> loadFindbugsPlugins(boolean useFbContrib, boolean useFindSecBugs) {
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    List<String> pluginJarPathList = Lists.newArrayList();
    try {
        Enumeration<URL> urls = contextClassLoader.getResources("findbugs.xml");
        while (urls.hasMoreElements()) {
            URL url = urls.nextElement();
            pluginJarPathList.add(normalizeUrl(url));
        }
        // Add fb-contrib plugin.
        if (useFbContrib && configuration.getFbContribJar() != null) {
            // fb-contrib plugin is packaged by Maven. It is not available during execution of unit tests.
            pluginJarPathList.add(configuration.getFbContribJar().getAbsolutePath());
        }
        // Add find-sec-bugs plugin. (same as fb-contrib)
        if (useFindSecBugs && configuration.getFindSecBugsJar() != null) {
            pluginJarPathList.add(configuration.getFindSecBugsJar().getAbsolutePath());
        }
    } catch (IOException e) {
        throw new IllegalStateException(e);
    } catch (URISyntaxException e) {
        throw new IllegalStateException(e);
    }
    List<Plugin> customPluginList = Lists.newArrayList();
    for (String path : pluginJarPathList) {
        try {
            Plugin plugin = Plugin.addCustomPlugin(new File(path).toURI(), contextClassLoader);
            if (plugin != null) {
                customPluginList.add(plugin);
                LOG.info("Loading findbugs plugin: " + path);
            }
        } catch (PluginException e) {
            LOG.warn("Failed to load plugin for custom detector: " + path);
            LOG.debug("Cause of failure", e);
        } catch (DuplicatePluginIdException e) {
            // FB Core plugin is always loaded, so we'll get an exception for it always
            if (!FINDBUGS_CORE_PLUGIN_ID.equals(e.getPluginId())) {
                // log only if it's not the FV Core plugin
                LOG.debug("Plugin already loaded: exception ignored: " + e.getMessage(), e);
            }
        }
    }
    return customPluginList;
}
Also used : PluginException(edu.umd.cs.findbugs.PluginException) DuplicatePluginIdException(edu.umd.cs.findbugs.plugins.DuplicatePluginIdException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URL(java.net.URL) File(java.io.File) Plugin(edu.umd.cs.findbugs.Plugin)

Example 2 with PluginException

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

the class PreferencesFrame method createPluginPane.

private JPanel createPluginPane() {
    final JPanel pluginPanel = new JPanel();
    pluginPanel.setLayout(new BorderLayout());
    pluginPanelCenter = new JPanel();
    pluginPanel.add(pluginPanelCenter, BorderLayout.CENTER);
    pluginPanelCenter.setBorder(new EmptyBorder(10, 10, 10, 10));
    pluginPanelCenter.setLayout(new GridBagLayout());
    JButton addButton = new JButton("Install new plugin...");
    JPanel south = new JPanel();
    south.add(addButton);
    addButton.addActionListener(e -> {
        JFileChooser chooser = new JFileChooser();
        chooser.addChoosableFileFilter(new FileFilter() {

            @Override
            public String getDescription() {
                return "SpotBugs Plugin (*.jar)";
            }

            @Override
            public boolean accept(File f) {
                if (f.isDirectory()) {
                    return true;
                }
                if (!f.canRead()) {
                    return false;
                }
                if (f.getName().endsWith(".jar")) {
                    return true;
                }
                return false;
            }
        });
        chooser.setDialogTitle("Select a SpotBugs plugin jar");
        int retvalue = chooser.showDialog(PreferencesFrame.this, "Install");
        if (retvalue == JFileChooser.APPROVE_OPTION) {
            File f = chooser.getSelectedFile();
            try {
                // load and enable for project (if loaded)
                Plugin plugin = Plugin.loadCustomPlugin(f, PreferencesFrame.this.getCurrentProject());
                GUISaveState guiSaveState = GUISaveState.getInstance();
                URL url = f.toURI().toURL();
                // add to FBGUI custom plugins list
                guiSaveState.addCustomPlugin(url);
                // add to list of enabled plugins
                guiSaveState.setPluginEnabled(plugin.getPluginId());
                plugin.setGloballyEnabled(true);
                guiSaveState.save();
                pluginsAdded = true;
                rebuildPluginCheckboxes();
            } catch (PluginException | MalformedURLException e1) {
                LOGGER.log(Level.WARNING, "Could not load " + f.getPath(), e1);
                JOptionPane.showMessageDialog(PreferencesFrame.this, "Could not load " + f.getPath() + "\n\n" + e1.getClass().getSimpleName() + ": " + e1.getMessage(), "Error Loading Plugin", JOptionPane.ERROR_MESSAGE);
            }
        }
    });
    pluginPanel.add(south, BorderLayout.SOUTH);
    return pluginPanel;
}
Also used : JPanel(javax.swing.JPanel) MalformedURLException(java.net.MalformedURLException) GridBagLayout(java.awt.GridBagLayout) PluginException(edu.umd.cs.findbugs.PluginException) JButton(javax.swing.JButton) URL(java.net.URL) BorderLayout(java.awt.BorderLayout) JFileChooser(javax.swing.JFileChooser) EmptyBorder(javax.swing.border.EmptyBorder) FileFilter(javax.swing.filechooser.FileFilter) File(java.io.File) Plugin(edu.umd.cs.findbugs.Plugin)

Example 3 with PluginException

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

the class GUISaveState method loadInstance.

public static void loadInstance() {
    GUISaveState newInstance = new GUISaveState();
    newInstance.recentFiles = new ArrayList<>();
    Preferences p = Preferences.userNodeForPackage(GUISaveState.class);
    newInstance.tabSize = p.getInt(TAB_SIZE, 4);
    newInstance.fontSize = p.getFloat(FONT_SIZE, 12.0f);
    newInstance.starterDirectoryForLoadBugs = new File(p.get(GUISaveState.STARTERDIRECTORY, SystemProperties.getProperty("user.dir")));
    int prevCommentsSize = p.getInt(GUISaveState.PREVCOMMENTSSIZE, 0);
    for (int x = 0; x < prevCommentsSize; x++) {
        String comment = p.get(GUISaveState.COMMENTKEYS[x], "");
        newInstance.previousComments.add(comment);
    }
    int size = Math.min(MAXNUMRECENTPROJECTS, p.getInt(GUISaveState.NUMPROJECTS, 0));
    for (int x = 0; x < size; x++) {
        newInstance.addRecentFile(new File(p.get(GUISaveState.RECENTPROJECTKEYS[x], "")));
    }
    int sorterSize = p.getInt(GUISaveState.SORTERTABLELENGTH, -1);
    if (sorterSize != -1) {
        ArrayList<Sortables> sortColumns = new ArrayList<>();
        String[] sortKeys = GUISaveState.generateSorterKeys(sorterSize);
        for (int x = 0; x < sorterSize; x++) {
            Sortables s = Sortables.getSortableByPrettyName(p.get(sortKeys[x], "*none*"));
            if (s == null) {
                if (MainFrame.GUI2_DEBUG) {
                    System.err.println("Sort order was corrupted, using default sort order");
                }
                newInstance.useDefault = true;
                break;
            }
            sortColumns.add(s);
        }
        if (!newInstance.useDefault) {
            // add in default columns
            Set<Sortables> missingSortColumns = new HashSet<>(Arrays.asList(DEFAULT_COLUMN_HEADERS));
            missingSortColumns.removeAll(sortColumns);
            sortColumns.addAll(missingSortColumns);
            newInstance.sortColumns = sortColumns.toArray(new Sortables[sortColumns.size()]);
        }
    } else {
        newInstance.useDefault = true;
    }
    newInstance.dockingLayout = p.getByteArray(DOCKINGLAYOUT, new byte[0]);
    String boundsString = p.get(FRAME_BOUNDS, null);
    Rectangle r = new Rectangle(0, 0, 800, 650);
    if (boundsString != null) {
        String[] a = boundsString.split(",", 4);
        if (a.length > 0) {
            try {
                r.x = Math.max(0, Integer.parseInt(a[0]));
            } catch (NumberFormatException nfe) {
                assert true;
            }
        }
        if (a.length > 1) {
            try {
                r.y = Math.max(0, Integer.parseInt(a[1]));
            } catch (NumberFormatException nfe) {
                assert true;
            }
        }
        if (a.length > 2) {
            try {
                r.width = Math.max(40, Integer.parseInt(a[2]));
            } catch (NumberFormatException nfe) {
                assert true;
            }
        }
        if (a.length > 3) {
            try {
                r.height = Math.max(40, Integer.parseInt(a[3]));
            } catch (NumberFormatException nfe) {
                assert true;
            }
        }
    }
    newInstance.frameBounds = r;
    newInstance.extendedWindowState = p.getInt(EXTENDED_WINDOW_STATE, Frame.NORMAL);
    newInstance.splitMain = p.getInt(SPLIT_MAIN, 400);
    newInstance.splitSummary = p.getInt(SPLIT_SUMMARY_NEW, 400);
    newInstance.splitTop = p.getInt(SPLIT_TOP, -1);
    newInstance.splitTreeComments = p.getInt(SPLIT_TREE_COMMENTS, 250);
    newInstance.packagePrefixSegments = p.getInt(PACKAGE_PREFIX_SEGEMENTS, 3);
    String plugins = p.get(CUSTOM_PLUGINS, "");
    if (plugins.length() > 0) {
        for (String s : plugins.split(" ")) {
            try {
                URI u = new URI(s);
                Plugin.addCustomPlugin(u);
                newInstance.customPlugins.add(u);
            } catch (PluginException e) {
                assert true;
            } catch (URISyntaxException e) {
                assert true;
            }
        }
    }
    String enabledPluginsString = p.get(ENABLED_PLUGINS, "");
    String disabledPluginsString = p.get(DISABLED_PLUGINS, "");
    newInstance.enabledPlugins = new ArrayList<>(Arrays.asList(enabledPluginsString.split(",")));
    newInstance.disabledPlugins = new ArrayList<>(Arrays.asList(disabledPluginsString.split(",")));
    instance = newInstance;
}
Also used : PluginException(edu.umd.cs.findbugs.PluginException) ArrayList(java.util.ArrayList) Rectangle(java.awt.Rectangle) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) Preferences(java.util.prefs.Preferences) File(java.io.File) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 4 with PluginException

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

the class AnalysisRunner method createProject.

@CheckReturnValue
private Project createProject(Path[] files) {
    final Project project = new Project();
    project.setProjectName(getClass().getSimpleName());
    if (PLUGIN_JAR != null) {
        try {
            String pluginId = Plugin.addCustomPlugin(PLUGIN_JAR.toURI()).getPluginId();
            project.setPluginStatusTrinary(pluginId, Boolean.TRUE);
        } catch (PluginException e) {
            throw new AssertionError("Failed to load plugin", e);
        }
    }
    for (Path file : files) {
        project.addFile(file.toAbsolutePath().toString());
    }
    for (Path auxClasspathEntry : auxClasspathEntries) {
        project.addAuxClasspathEntry(auxClasspathEntry.toAbsolutePath().toString());
    }
    return project;
}
Also used : Path(java.nio.file.Path) Project(edu.umd.cs.findbugs.Project) PluginException(edu.umd.cs.findbugs.PluginException) CheckReturnValue(edu.umd.cs.findbugs.annotations.CheckReturnValue)

Example 5 with PluginException

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

the class FindbugsPlugin method addCustomPlugin.

protected static void addCustomPlugin(HashSet<Plugin> enabled, URI uri) {
    try {
        // bug 3117769 - we must provide our own classloader
        // to allow third-party plugins extend the classpath via
        // "Buddy" classloading
        // see also: Eclipse-BuddyPolicy attribute in MANIFEST.MF
        Plugin fbPlugin = Plugin.addCustomPlugin(uri, FindbugsPlugin.class.getClassLoader());
        if (fbPlugin != null) {
            // TODO line below required to enable this *optional* plugin
            // but it should be taken by FB core from the findbugs.xml,
            // which currently only works for *core* plugins only
            fbPlugin.setGloballyEnabled(true);
            enabled.add(fbPlugin);
        }
    } catch (PluginException e) {
        getDefault().logException(e, "Failed to load plugin for custom detector: " + uri);
    } catch (DuplicatePluginIdException e) {
        getDefault().logException(e, e.getPluginId() + " already loaded from " + e.getPreviouslyLoadedFrom() + ", ignoring: " + uri);
    }
}
Also used : PluginException(edu.umd.cs.findbugs.PluginException) DuplicatePluginIdException(edu.umd.cs.findbugs.plugins.DuplicatePluginIdException) ResourcesPlugin(org.eclipse.core.resources.ResourcesPlugin) Plugin(edu.umd.cs.findbugs.Plugin) AbstractUIPlugin(org.eclipse.ui.plugin.AbstractUIPlugin)

Aggregations

PluginException (edu.umd.cs.findbugs.PluginException)5 Plugin (edu.umd.cs.findbugs.Plugin)3 File (java.io.File)3 DuplicatePluginIdException (edu.umd.cs.findbugs.plugins.DuplicatePluginIdException)2 URISyntaxException (java.net.URISyntaxException)2 URL (java.net.URL)2 Project (edu.umd.cs.findbugs.Project)1 CheckReturnValue (edu.umd.cs.findbugs.annotations.CheckReturnValue)1 BorderLayout (java.awt.BorderLayout)1 GridBagLayout (java.awt.GridBagLayout)1 Rectangle (java.awt.Rectangle)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URI (java.net.URI)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Preferences (java.util.prefs.Preferences)1 JButton (javax.swing.JButton)1