How can I pass a custom configuration to a CobolCheck class?


(Jim Kaib) #1

I’m working on a simple custom Cobol plugin, based entirely on the sample custom Cobol plugin project. I want to code a rule that operates on statement nodes. I’d like that rule to be configurable, preferably at the server level, as the rule would be applied enterprise-wide.

How do I get the property definitions I’ve created in my plugin to the CobolCheck class that implements my rule logic? I don’t see anything in the JavaDoc that relates to the the Configuration interface.


(Pierre-Yves Nicolas) #2

Could you try something like that and tell us whether it helps you?

public class MyCheckRepository 
  implements CobolCheckRepository, RulesDefinition {

  private Configuration config;  

  public MyCheckRepository(Configuration configuration) {
    this.config = configuration;
  public Collection<Object> getCheckClassesOrObjects() {
    return Arrays.asList(new MyCustomRule(this.config));

(Jim Kaib) #3

I get a ClassCastException:

public Collection getCheckClassesOrObjects() {

    return Arrays.asList(new OneRuleToRuleThemAll(this.config));

Log entry:

2019.03.22 12:05:47 ERROR web[o.s.s.p.Platform] Background initialization failed. Stopping SonarQube
java.lang.ClassCastException: Cannot cast com.sauron.OneRuleToRuleThemAll to java.lang.Class
at java.lang.Class.cast(Unknown Source)
at$3$1.accept(Unknown Source)
at java.util.Spliterators$ArraySpliterator.forEachRemaining(Unknown Source)
at Source)
at Source)
at$ForEachOp.evaluateSequential(Unknown Source)
at$ForEachOp$OfRef.evaluateSequential(Unknown Source)
at Source)
at Source)
at com.sonar.sauron.CobolCustomCheckRepository.define(
at org.sonar.server.rule.RuleDefinitionsLoader.load(
at org.sonar.server.rule.RegisterRules.start(
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.picocontainer.lifecycle.ReflectionLifecycleStrategy.invokeMethod(
at org.picocontainer.lifecycle.ReflectionLifecycleStrategy.start(
at org.picocontainer.injectors.AbstractInjectionFactory$LifecycleAdapter.start(
at org.picocontainer.behaviors.AbstractBehavior.start(
at org.picocontainer.behaviors.Stored$RealComponentLifecycle.start(
at org.picocontainer.behaviors.Stored.start(
at org.picocontainer.DefaultPicoContainer.potentiallyStartAdapter(
at org.picocontainer.DefaultPicoContainer.startAdapters(
at org.picocontainer.DefaultPicoContainer.start(
at org.sonar.core.platform.ComponentContainer.startComponents(
at org.sonar.server.platform.platformlevel.PlatformLevel.start(
at org.sonar.server.platform.platformlevel.PlatformLevelStartup.access$001(
at org.sonar.server.platform.platformlevel.PlatformLevelStartup$1.doPrivileged(
at org.sonar.server.user.DoPrivileged.execute(
at org.sonar.server.platform.platformlevel.PlatformLevelStartup.start(
at org.sonar.server.platform.Platform.executeStartupTasks(
at org.sonar.server.platform.Platform.access$400(
at org.sonar.server.platform.Platform$1.lambda$doRun$1(
at org.sonar.server.platform.Platform$AutoStarterRunnable.runIfNotAborted(
at org.sonar.server.platform.Platform$1.doRun(
at org.sonar.server.platform.Platform$
at Source)
2019.03.22 12:05:47 INFO web[o.s.p.StopWatcher] Stopping process

(Pierre-Yves Nicolas) #4

Yes, that comes from another part of the code of the sample plugin which you copy-pasted.

You have to update it since getCheckClassesOrObjects now returns objects which are not classes:

annotationLoader.load(repository, OneRuleToRuleThemAll.class);

(Jim Kaib) #5

If I remove the cast on line 73, I get the expected compiler error on line 74, as the RulesDefinitionAnnotationLoader.load(RulesDefinition.NewExtendedRepository,Class... ) method still expects a class. I don’t see any overloaded method signatures that take an instance of any kind. Am I missing something?

I think I could subclass CobolCheck, add Configuration instance to it and use a Sensor implementation to populate it in the execute method, but I really want to leverage the existing SonarCobol sensor.

(Pierre-Yves Nicolas) #6

I was suggesting to replace:

  .forEach(ruleClass -> annotationLoader.load(repository, ruleClass));


annotationLoader.load(repository, OneRuleToRuleThemAll.class);

Did you try that?

(Jim Kaib) #7

I adjusted my code, adding a method that returns a list of classes based on the list of objects getCheckClassesOrObjects returns, and now my custom rule has access to the Configuration.

Thank you for your help. I do have a related question. Is there some documentation you can point me to that describes how dependency injection works in the plugin framework? I’ve seen numerous classes that use DI in an almost arbitrary way, but I’m sure that’s because I don’t understand how it’s used within the SonarQube server.

(Pierre-Yves Nicolas) #8

I found one documentation page which briefly mentions dependency injection:

If you want more details on that topic, you should start a new thread: the people who may help you would be more likely to see it than the current one which mentions COBOL…

(Jim Kaib) #9

Thank you for the link. It’s the page that first made me wonder how DI works in SonarQube, but I still don’t understand why it appears some constructors can simply declare an argument and have it automagically satisfied by the container.

I’ll probably create a new topic for it. Thanks again for your help.