felipebz / zpa

Parser and static code analysis tool for PL/SQL and Oracle SQL.
https://zpa.felipebz.com
GNU Lesser General Public License v3.0
213 stars 78 forks source link

plsql-custom-rules doesn't compile due to missing com.sonar.sslr.api #115

Closed JoopBL closed 5 years ago

JoopBL commented 5 years ago

This issue concerns version 2.3.0.

When trying to build the plsql-custom-rules project, the unit tests fail. The reason are the missing com.sonar.sslr.api objects in the sonar-plsql-open-plugin.

In the sonar-plsql-open-plugin project these objects are relocated to org.sonar.plugins.plsqlopen.api.sslr by the shade plugin.

I can fix theproblem by removing the relocation option in the pom file, but probably you have an intension for it. A solution for the plsql-custom-rules project would be preferable.

felipebz commented 5 years ago

Hi! Could you confirm what classes are imported in your source? The code included in the repository seems correct and I couldn't reproduce the issue here:

https://github.com/felipebz/zpa/blob/a7433dda6352b34d2ff85c999440e2a406d58e16/plsql-custom-rules/src/main/java/com/company/plsql/ForbiddenDmlCheck.java#L5-L9

JoopBL commented 5 years ago

Hello Felipe,

the problem is in the unittest files, not the checks.

The class only contains a call of PlSqlCheckVerifier

` package nl.cb.sonarplsql.plsql;

import org.junit.Test; import org.sonar.plsqlopen.checks.verifier.PlSqlCheckVerifier;

public class NCConstantCheckTest {

@Test
public void test() {
    PlSqlCheckVerifier.verify("src/test/resources/naming_convention_constant.sql", new NCConstantCheck());
}

} `

felipebz commented 5 years ago

Well, that's strange... In the pom.xml you have a dependency on <artifactId>sonar-plsql-open-plugin</artifactId> right? Isn't there any reference to other modules, like plsql-frontend?

JoopBL commented 5 years ago

the pom file contains the dependency:

org.sonar.plsqlopen sonar-plsql-open-plugin ${sonarplsqlopen.version} provided

I think the problem is due to the relocation.

The unit test gives the following error: [ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.349 s <<< FAILURE! - in nl.cb.sonarplsql.plsql.NCConstantCheckTest [ERROR] test(nl.cb.sonarplsql.plsql.NCConstantCheckTest) Time elapsed: 0.255 s <<< ERROR! java.lang.NoClassDefFoundError: com/sonar/sslr/api/AstNodeType at nl.cb.sonarplsql.plsql.NCConstantCheckTest.test(NCConstantCheckTest.java:10) Caused by: java.lang.ClassNotFoundException: com.sonar.sslr.api.AstNodeType at nl.cb.sonarplsql.plsql.NCConstantCheckTest.test(NCConstantCheckTest.java:10)

But the AstNodeType has been relocated at: \org\sonar\plugins\plsqlopen\api\sslr\

So the class won't be found.

felipebz commented 5 years ago

Oh, I see... This was changed on purpose. See this page for reference: https://github.com/felipebz/zpa/wiki/Create-a-plugin-with-custom-rules#migrating-a-plugin-from-a-previous-version

Specifically:

In your classes, you will need to reimport many classes that are now located in the org.sonar.plugins.plsqlopen.api.* package.

JoopBL commented 5 years ago

I have the plsql and child projects also in my IDE. So when importing it will take the path from these projects, and will not take into account the relocation of the maven plugin.

When running the unit test stand-alone, it just works (due to the correct import). When building using Maven, the import is not correct due since it uses the jar file containing the relocated path.

I find it useful to have the plsql project open, for reference and debugging.

felipebz commented 5 years ago

Thanks for the explanation. I'm used to open the custom plugin in a different window/workspace (without the main project) because of this issue. I don't how to avoid it since the packages need to be relocated...

JoopBL commented 5 years ago

You can use the maven-source-plugin to generate a jar file with sources, so you can see the sources when working in the custom plugin. I don't know if the shade plugin also reallocates the source jar, but it is worth a try.

JoopBL commented 5 years ago

I still can't get it working. I have the main project and the custom project split now. The jar of the main project contains the relocated paths. The custom project import is using the main project jar file. When deploying the custom project in SonarQube the following error occurs: java.lang.NoClassDefFoundError: org/sonar/plsqlopen/checks/AbstractBaseCheck at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(Unknown Source) at java.security.SecureClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.access$100(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at org.sonar.classloader.ClassRealm.loadClassFromSelf(ClassRealm.java:125) at org.sonar.classloader.ParentFirstStrategy.loadClass(ParentFirstStrategy.java:37) at org.sonar.classloader.ClassRealm.loadClass(ClassRealm.java:87) at org.sonar.classloader.ClassRealm.loadClass(ClassRealm.java:76) at nl.cb.sonarplsql.plsql.PlSqlCustomRulesDefinition.checkClasses(PlSqlCustomRulesDefinition.java:21) at org.sonar.plugins.plsqlopen.api.CustomPlSqlRulesDefinition.define(CustomPlSqlRulesDefinition.java:41) The custom plugin is being called and found, but the AbstractBaseCheck class can't be found. The main project jar file contains the class at the correct path. Do you have any suggestions?

felipebz commented 5 years ago

The AbstractBaseCheck isn't part of the public API. Your check must extend org.sonar.plugins.plsqlopen.api.checks.PlSqlCheck 😉

JoopBL commented 5 years ago

Now it works! The checks were still based on the 2.2.0 version.