Error using AbstractMethodDetection in custom rule

Hi,

I am currently trying to implement my own java rules.

I followed Custom Rules 101 and I am trying to detect usage of specific methods. I looked in the Java analyzer and found org.sonar.java.checks.ResultSetIsLastCheck that does what I want by using AbstractMethodDetection.

I had no problems building the plugins but when I’m trying to deploit it, my Sonarqube Instance crash with the following error:

2020.09.22 14:35:51 ERROR web[o.s.s.p.Platform] Background initialization failed. Stopping SonarQube
java.lang.NoClassDefFoundError: org/sonar/java/checks/methods/AbstractMethodDetection

I found this post Unable to deploy custom java rules after using 'AbstractMethodDetection' and from what I understand the AbstractMethodDetection was not available at the time, but should be available from SonarJava version 6.3.
I’m using the latest Sonarqube Docker image which load SonarJava 6.5.1.22586 and I still get this error.
I don’t know what I’m missing, so if you would have an idea of what could be wrong, it would be greatly appreciated.
Let me know if you need any information.

Yann

PS: Below you’ll find the rule built based on the Custom Rules 101 template.

package org.sonar.samples.java.checks;
 
import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.java.checks.methods.AbstractMethodDetection;
import org.sonar.java.model.ExpressionUtils;
import org.sonar.plugins.java.api.semantic.MethodMatchers;
import org.sonar.plugins.java.api.tree.MethodInvocationTree;

 
@Rule(
		key = "UseFileListRule",
		name = "Java.io.File.list is used",
		description = "Java.io.File.list description placeholder",
		priority = Priority.MINOR,
		tags = {"filesystem"})
public class UseFileListCheck extends AbstractMethodDetection {
	
	 @Override
	  protected MethodMatchers getMethodInvocationMatchers() {
	    return MethodMatchers.create()
	      .ofTypes("java.io.File").names("list").addWithoutParametersMatcher().build();
	  }

	  @Override
	  protected void onMethodInvocationFound(MethodInvocationTree mit) {
	    reportIssue(ExpressionUtils.methodName(mit), "Remove this call to \"list()\".");
	  }
 
}

Hello @Cybyan,

While you followed the 101, you probably missed the section What you can use, and what you can’t.

In your check, you are using org.sonar.java.checks.methods.AbstractMethodDetection and org.sonar.java.model.ExpressionUtils.

These two classes are not exposed in our custom rule API, and won’t be available at runtime. So you simply can not use them.

Hope this helps,
Michael

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.