diff options
author | Fabrice Bellingard <bellingard@gmail.com> | 2012-05-15 14:24:41 +0200 |
---|---|---|
committer | Fabrice Bellingard <bellingard@gmail.com> | 2012-05-15 15:59:36 +0200 |
commit | b6d1c37af4d3eb6f57b1f7b27b21df2d883d77a6 (patch) | |
tree | 479031bdaadadb2059f6381527de7c3044c39bf3 /plugins | |
parent | da10b8a6c8f326b2f57dd7e02d21ce17dc34f9a7 (diff) | |
download | sonarqube-b6d1c37af4d3eb6f57b1f7b27b21df2d883d77a6.tar.gz sonarqube-b6d1c37af4d3eb6f57b1f7b27b21df2d883d77a6.zip |
SONAR-3074 Update code to use Findbugs 2.0
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/sonar-findbugs-plugin/pom.xml | 11 | ||||
-rw-r--r-- | plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsExecutor.java | 72 |
2 files changed, 49 insertions, 34 deletions
diff --git a/plugins/sonar-findbugs-plugin/pom.xml b/plugins/sonar-findbugs-plugin/pom.xml index 28fa5eb4e03..0e14cae61e0 100644 --- a/plugins/sonar-findbugs-plugin/pom.xml +++ b/plugins/sonar-findbugs-plugin/pom.xml @@ -14,7 +14,7 @@ <description>FindBugs is a program that uses static analysis to look for bugs in Java code. It can detect a variety of common coding mistakes, including thread synchronization problems, misuse of API methods.</description> <properties> - <findbugs.version>1.3.9</findbugs.version> + <findbugs.version>2.0.0</findbugs.version> </properties> <dependencies> @@ -75,6 +75,13 @@ <artifactId>sonar-testing-harness</artifactId> <scope>test</scope> </dependency> + <!-- fbContrib used as a test dependency to test loading of custom plugins --> + <dependency> + <groupId>com.mebigfatguy</groupId> + <artifactId>fbcontrib</artifactId> + <version>4.6.1</version> + <scope>test</scope> + </dependency> </dependencies> <build> @@ -135,7 +142,7 @@ <configuration> <rules> <requireFilesSize> - <maxsize>4500000</maxsize> + <maxsize>4600000</maxsize> <minsize>4000000</minsize> <files> <file>${project.build.directory}/${project.build.finalName}.jar</file> diff --git a/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsExecutor.java b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsExecutor.java index 05eab0ff516..e1d6464c3ac 100644 --- a/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsExecutor.java +++ b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsExecutor.java @@ -19,11 +19,17 @@ */ package org.sonar.plugins.findbugs; -import edu.umd.cs.findbugs.Priorities; - import com.google.common.collect.Lists; -import edu.umd.cs.findbugs.*; +import edu.umd.cs.findbugs.DetectorFactoryCollection; +import edu.umd.cs.findbugs.FindBugs; +import edu.umd.cs.findbugs.FindBugs2; +import edu.umd.cs.findbugs.Plugin; +import edu.umd.cs.findbugs.PluginException; +import edu.umd.cs.findbugs.Priorities; +import edu.umd.cs.findbugs.Project; +import edu.umd.cs.findbugs.XMLBugReporter; import edu.umd.cs.findbugs.config.UserPreferences; +import edu.umd.cs.findbugs.plugins.DuplicatePluginIdException; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.io.output.NullOutputStream; @@ -31,7 +37,6 @@ import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.BatchExtension; -import org.sonar.api.utils.Logs; import org.sonar.api.utils.SonarException; import org.sonar.api.utils.TimeProfiler; @@ -39,8 +44,8 @@ import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.io.PrintStream; -import java.lang.reflect.Field; import java.net.URL; +import java.util.Collection; import java.util.Enumeration; import java.util.List; import java.util.concurrent.Callable; @@ -67,12 +72,13 @@ public class FindbugsExecutor implements BatchExtension { Thread.currentThread().setContextClassLoader(FindBugs2.class.getClassLoader()); OutputStream xmlOutput = null; + Collection<Plugin> customPlugins = null; ExecutorService executorService = Executors.newSingleThreadExecutor(); try { - DetectorFactoryCollection detectorFactory = loadFindbugsPlugins(); - final FindBugs2 engine = new FindBugs2(); + customPlugins = loadFindbugsPlugins(); + Project project = configuration.getFindbugsProject(); engine.setProject(project); @@ -106,7 +112,7 @@ public class FindbugsExecutor implements BatchExtension { } } - engine.setDetectorFactoryCollection(detectorFactory); + engine.setDetectorFactoryCollection(DetectorFactoryCollection.instance()); engine.setAnalysisFeatureSettings(FindBugs.DEFAULT_EFFORT); engine.finishSettings(); @@ -115,12 +121,11 @@ public class FindbugsExecutor implements BatchExtension { profiler.stop(); - resetDetectorFactoryCollection(); - return xmlReport; } catch (Exception e) { throw new SonarException("Can not execute Findbugs", e); } finally { + resetCustomPluginList(customPlugins); executorService.shutdown(); IOUtils.closeQuietly(xmlOutput); Thread.currentThread().setContextClassLoader(initialClassLoader); @@ -145,40 +150,43 @@ public class FindbugsExecutor implements BatchExtension { } } - private DetectorFactoryCollection loadFindbugsPlugins() { - List<URL> plugins = Lists.newArrayList(); + private Collection<Plugin> loadFindbugsPlugins() { + ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); + + List<String> pluginJarPathList = Lists.newArrayList(); try { - Enumeration<URL> urls = Thread.currentThread().getContextClassLoader().getResources("findbugs.xml"); + Enumeration<URL> urls = contextClassLoader.getResources("findbugs.xml"); while (urls.hasMoreElements()) { URL url = urls.nextElement(); - String path = StringUtils.removeStart(StringUtils.substringBefore(url.toString(), "!"), "jar:"); - Logs.INFO.info("Found findbugs plugin: " + path); - plugins.add(new URL(path)); + pluginJarPathList.add(StringUtils.removeStart(StringUtils.substringBefore(url.toString(), "!"), "jar:file:")); } } catch (IOException e) { throw new SonarException(e); } - resetDetectorFactoryCollection(); - DetectorFactoryCollection detectorFactory = DetectorFactoryCollection.rawInstance(); - detectorFactory.setPluginList(plugins.toArray(new URL[plugins.size()])); - for (Plugin plugin : detectorFactory.plugins()) { - Logs.INFO.info("Loaded plugin " + plugin.getPluginId()); + 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("Found findbugs plugin: " + path); + } + } catch (PluginException e) { + LOG.warn("Failed to load plugin for custom detector: " + path); + } catch (DuplicatePluginIdException e) { + // simply ignore this + } } - return detectorFactory; + return customPluginList; } - /** - * Unfortunately without reflection it's impossible to reset {@link DetectorFactoryCollection}. - */ - private static void resetDetectorFactoryCollection() { - try { - Field field = DetectorFactoryCollection.class.getDeclaredField("theInstance"); - field.setAccessible(true); - field.set(null, null); - } catch (Exception e) { - throw new SonarException(e); + private static void resetCustomPluginList(Collection<Plugin> customPlugins) { + if (customPlugins != null) { + for (Plugin plugin : customPlugins) { + Plugin.removeCustomPlugin(plugin); + } } } |