Custom ResourceUnclose not working

I am trying to create a custom rule for unclose resource before I try anything I just wanted to test it as it is.
I have copied this custom rule from the Java-frontend sonar source. I have compiled it successfully. but unable to run the tests successfully.

sharing the .git link and details of rule.

Rule >> org.sonar.samples.java.checks.CustomUnclosedResourcesCheck
test >> org.sonar.samples.java.checks.CustomUnclosedResourcesCheckTest
testFiles >> src/test/files/customresources

here is the exception.

**java.lang.AssertionError: No issues has been raised, at least one issue expected.**
	at org.sonar.java.checks.verifier.CheckVerifier.assertMultipleIssue(CheckVerifier.java:212)
	at org.sonar.java.checks.verifier.CheckVerifier.checkIssues(CheckVerifier.java:187)
	at org.sonar.java.checks.verifier.JavaCheckVerifier.scanFile(JavaCheckVerifier.java:288)
	at org.sonar.java.checks.verifier.JavaCheckVerifier.scanFile(JavaCheckVerifier.java:268)
	at org.sonar.java.checks.verifier.JavaCheckVerifier.scanFile(JavaCheckVerifier.java:222)
	at org.sonar.java.checks.verifier.JavaCheckVerifier.verify(JavaCheckVerifier.java:106)
	at org.sonar.samples.java.checks.CustomUnclosedResourcesCheckTest.constructorClosed(CustomUnclosedResourcesCheckTest.java:32)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Hello @Avkash_Hirpara ,

SEChecks are not supposed to be written in custom rules plugin. These checks are not part of the public API of the SonarSource Analyzer for Java. Therefore, what you try to achieve is not at all supported, and won’t work on the long run, even if you would have managed to make your tests pass.

However, the Java Analyzer provides a Rule Template for custom resources to be checked. Please have a look at rule java:3546, which is a parameterizable version of java:S2095 (Resources should be closed). This rule might help you to achieve your goal.

Regards,
Michael

Thanks Michael
For you quick response. Let me try 3546.

Hello Michael,

I have tried 3546 and it works fine for custom resource except for, it doesn’t compel to close it in the finally block like 2095.

Do you have any workaround for that ?

Thanks,
Avkash.

Hello Avkash,

Can you provide an example of your code (compiling) that would require a particular behavior in a try-catch-finally?
It would help me understand what is not working in the template version of the rule.

Rule S3546 is allowing you to configure which are the closing methods (that I expect you would call in catch-finally block) and, therefore, it should be taken into account.

Cheers,
Michael