I created a custom java rules based on https://docs.sonarqube.org/display/PLUG/Writing+Custom+Java+Rules+101
but when i put my java-custom-rules-1.0-SNAPSHOT.jar in SONARQUBE_HOME/Extension/plugins and i restart sonarQube i can’t see my new rules.
I create a JavaCustomRulesList.java like this:
package org.sonar.samples.java;
import java.util.List;
import org.sonar.api.internal.google.common.collect.ImmutableList;
import org.sonar.plugins.java.api.JavaCheck;
import org.sonar.samples.java.checks.MyFirstCustomCheck;
import org.sonar.samples.java.checks.MySecondCustomCheck;
public class JavaCustomRulesList {
public static List<Class<? extends JavaCheck>> getJavaChecks() {
return ImmutableList.<Class<? extends JavaCheck>>builder()
// other rules...
.add(MySecondCustomCheck.class)
.add(MyFirstCustomCheck.class)
.build();
}
}
MyFirstCustomCheck look like this:
@Rule(
key = "MyFirstCustomCheck",
name = "Return type and parameter of a method should not be the same",
description = "For a method having a single parameter, the types of its return value and its parameter should never be the same.",
priority = Priority.CRITICAL,
tags = {"bug"})
public class MyFirstCustomCheck extends IssuableSubscriptionVisitor {
@Override
public List<Kind> nodesToVisit() {
return ImmutableList.of(Kind.METHOD);
}
@Override
public void visitNode(Tree tree) {
MethodTree method = (MethodTree) tree;
if (method.parameters().size() == 1) {
MethodSymbol symbol = method.symbol();
Type firstParameterType = symbol.parameterTypes().get(0);
Type returnType = symbol.returnType().type();
if (returnType.is(firstParameterType.fullyQualifiedName())) {
reportIssue(method.simpleName(), "Never do that!");
}
}
}
}
the rules look ok, can you share pom.xml you use to package the plugin? In the resulting jar, does it containt META-INF/MANIFEST.MF file? Can you share its content?
Also, you didn’t share which versions of SonarQube and SonarJava do you use. Make sure that the versions you use to build the rules are matching those were you deploy (it’s ok if there is few versions difference, but it could be a problem if there is a big mismatch).
I am getting this error while running test case of new custom rule.
i feel its problem with my pom.xml
Can any one help me i am new with this, i tried all the example but still error is there.
Using java 1.8 and Apache Maven 3.5.4.
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.google.common.base.Throwables.propagate(Throwables.java:160)
at com.sonar.sslr.impl.typed.ReflectionUtils.invokeMethod(ReflectionUtils.java:38)
at com.sonar.sslr.api.typed.ActionParser.<init>(ActionParser.java:79)
at org.sonar.java.ast.parser.JavaParser.<init>(JavaParser.java:37)
at org.sonar.java.ast.parser.JavaParser.createParser(JavaParser.java:41)
at org.sonar.java.ast.JavaAstScanner.scanSingleFileForTests(JavaAstScanner.java:136)
at org.sonar.java.checks.verifier.JavaCheckVerifier.scanFile(JavaCheckVerifier.java:280)
at org.sonar.java.checks.verifier.JavaCheckVerifier.scanFile(JavaCheckVerifier.java:267)
at org.sonar.java.checks.verifier.JavaCheckVerifier.scanFile(JavaCheckVerifier.java:221)
at org.sonar.java.checks.verifier.JavaCheckVerifier.verify(JavaCheckVerifier.java:105)
at org.sonar.samples.java.checks.AvoidTooManyParametersForMethodTest.test(AvoidTooManyParametersForMethodTest.java:11)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
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)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.sonar.sslr.impl.typed.ReflectionUtils.invokeMethod(ReflectionUtils.java:36)
… 31 more
Caused by: org.sonar.sslr.grammar.GrammarException: The rule ‘f.newTuple20(Object, Object)’ has already been defined somewhere in the grammar.
at org.sonar.sslr.grammar.GrammarBuilder$RuleBuilder.is(GrammarBuilder.java:285)
at com.sonar.sslr.impl.typed.GrammarBuilderInterceptor.replaceByRule(GrammarBuilderInterceptor.java:159)
at com.sonar.sslr.api.typed.ActionParser$ActionMethodInterceptor.intercept(ActionParser.java:138)
at org.sonar.java.ast.parser.TreeFactory$$EnhancerByCGLIB$$803021b3.newTuple20()
at org.sonar.java.ast.parser.JavaGrammar.SWITCH_CASE_EXPRESSION_LIST(JavaGrammar.java:954)
at org.sonar.java.ast.parser.JavaGrammar$$EnhancerByCGLIB$$a0c5716.CGLIB$SWITCH_CASE_EXPRESSION_LIST$88()
at org.sonar.java.ast.parser.JavaGrammar$$EnhancerByCGLIB$$a0c5716$$FastClassByCGLIB$$7513a60e.invoke()
at net.sf.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at com.sonar.sslr.impl.typed.GrammarBuilderInterceptor.intercept(GrammarBuilderInterceptor.java:76)
at org.sonar.java.ast.parser.JavaGrammar$$EnhancerByCGLIB$$a0c5716.SWITCH_CASE_EXPRESSION_LIST()
… 36 moreAvoidTooManyParametersForMethodCheck.txt (389 Bytes) AvoidTooManyParametersForMethodTest.txt (423 Bytes) MyJavaRulesDefinition.txt (6.1 KB) RulesList.txt (2.0 KB) pom.txt (4.9 KB)