spotbugs / discuss

SpotBugs mailing list
6 stars 1 forks source link

How to pass user-configurable options to a detector? #1

Open sewe opened 7 years ago

sewe commented 7 years ago

This issue is meant to discuss best practices for how to pass additional, user-configurable options to a detector. As far as I can see, there are three alternatives to do so:

  1. Pass the option in as a system property (-jvmArgs -Dkey=value command line option)
  2. Pass the option in as a key/value pair in the user preferences file (-userPrefs command line option)
  3. Pass a (pre-defined) key/value pair in through activation/deactivation of a special plug-in with <GlobalOption> (-choosePlugins command line option)

The preferred alternative should offer a straight-forward way to access the value in the detector:

  1. The canonical way to access them seems to be SystemProperties.getProperty(String)
  2. I haven’t found a way to get at a UserPreferences object from within a Detector
  3. I haven’t found a way (spotbugs/spotbugs@dbdd6c50acf232d9ec80ad3fbcefb5eda2f28aed removed DetectorCollectionFactory.getGlobalOption)

Moreover, the preferred alternative should not only work when SpotBugs is invoked from the command line, but should also be settable from within Eclipse, e.g., through a preference page.

  1. Seems trivial, but is surprisingly tricky, as the system property needs to be set on every restart. (Just because the SpotBugs Eclipse plug-in uses the findbugsPlugins extension point to load a SpotBugs plugin doesn’t mean that the surrounding Eclipse plug-in wrapper’s Activator is run; so you can’t set the system property anew on demand when the wrapper is activated.)
  2. Can be achieved through FindbugsPlugin.getUserPreferences(...) and saveUserPreferences(...) called from the preference page or even (per-project) from the project’s Properties page
  3. Activation/Deactivation can be done in the Java > SpotBugs > Plugins and misc. Settings preferences. (This is limited to boolean options, of course.)

Am I missing any other alternatives? How can alternatives 2 and 3 above access the configured value in the detector? And which alternative should in your opinion be considered a best practice?

sewe commented 7 years ago
  1. I haven’t found a way to get at a UserPreferences object from within a Detector

Found a way:

AnalysisContext.currentAnalysisContext().getSourceFinder().getProject().getConfiguration()

Given the length of the call chain, I’m not sure whether I would call this a “best practice,” however.

iloveeclipse commented 7 years ago

Yep. It would make more sense if we would have this: AnalysisContext.currentAnalysisContext().getProject().getConfiguration()