spotbugs / discuss

SpotBugs mailing list
6 stars 1 forks source link

How can I run SpotBugs using Java? #34

Closed arpitkath closed 5 years ago

arpitkath commented 6 years ago

I was working on injecting of groovy scripts dynamically in Java. So before executing those scripts, I want to get sure of that they do not have potential bugs using SpotBugs. Here is the Psuedo-Code:

Here it should return the infinite loop bug.

String script = "class Hello { static void main(String []args) { def i = 0; while ( i <= 0) { i = i - 1; } } } ";
List<Bugs> bugs = SpotBugs.getBugs(script);
if (bugs == null) {
    execute(script);
}

So how to do the SpotBugs.getBugs(script) using java, the input script will not be hard-coded as in above example, but will be dynamically fetched.

KengoTODA commented 6 years ago

Check SpotBugsRule class, here is sample usage.

arpitkath commented 6 years ago

SpotBugsRule class is in test package, so how can I use that in my code?

KengoTODA commented 6 years ago

If not, you can read its implementation to understand how FindBugs2 class is working.

arpitkath commented 6 years ago

I was trying to do the same as given in SpotBugsRule file, which uses FindBugs2 internally. Was getting the following stack trace while executing FindBugs2 engine = new FindBugs2();, which belongs to line private static final boolean LIST_ORDER = SystemProperties.getBoolean("findbugs.listOrder"); (line 85: FindBugs2.class)

    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at edu.umd.cs.findbugs.PluginLoader.loadPluginComponents(PluginLoader.java:837)
    at edu.umd.cs.findbugs.PluginLoader.<init>(PluginLoader.java:397)
    at edu.umd.cs.findbugs.PluginLoader.loadCorePlugin(PluginLoader.java:1486)
    at edu.umd.cs.findbugs.PluginLoader.loadInitialPlugins(PluginLoader.java:1440)
    at edu.umd.cs.findbugs.PluginLoader.<clinit>(PluginLoader.java:150)
    at edu.umd.cs.findbugs.DetectorFactoryCollection.getCoreResource(DetectorFactoryCollection.java:351)
    at edu.umd.cs.findbugs.SystemProperties.loadPropertiesFromConfigFile(SystemProperties.java:77)
    at edu.umd.cs.findbugs.SystemProperties.<clinit>(SystemProperties.java:60)
    at edu.umd.cs.findbugs.FindBugs2.<clinit>(FindBugs2.java:85)
KengoTODA commented 6 years ago

Sorry I cannot get your point, which problem you faced?

arpitkath commented 6 years ago

When I tried to execute IFindBugsEngine engine = new FindBugs2();, I got the above stack trace. The above stack trace initiates in FindBugs2 class, line number 85.

Not able to run this simple code. Getting the above stack trace for the following code.

import edu.umd.cs.findbugs.FindBugs2;
import edu.umd.cs.findbugs.IFindBugsEngine;

public class Hello {
    public static void main(String args[]) {
        IFindBugsEngine fb = new FindBugs2();
    }
}
KengoTODA commented 6 years ago

Wmm not reproducible... I guess that you have some trouble around plugin.

at edu.umd.cs.findbugs.PluginLoader.loadPluginComponents(PluginLoader.java:837)

KengoTODA commented 5 years ago

I'll close this stale issue. If necessary, please reopen it freely.