sevntu-checkstyle / sevntu.checkstyle

Additional Checkstyle checks, that could be added as extension to EclipseCS plugin and maven-checkstyle-plugin, Sonar checkstyle plugin, extension for CheckStyle IDEA plugin.
http://sevntu-checkstyle.github.io/sevntu.checkstyle/
190 stars 146 forks source link

Running checkstyle w/ sevntu from Ant throws ClassNotFoundException after switching to JDK11 #935

Closed pci-mthompson closed 1 year ago

pci-mthompson commented 1 year ago

C:\PCI_Dev\jenkins>javac Test.java

C:\PCI_Dev\jenkins>type Test.java class Test { public void test(){ try { } catch (Exception e) { } finally { return; } } }

/var/tmp $ cat config.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE module PUBLIC "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN" "https://checkstyle.org/dtds/configuration_1_3.dtd">

For Windows users:

C:\PCI_Dev\jenkins>java -classpath cs2\sevntu-checks-1.42.0.jar;cs2\checkstyle-10.4-all.jar com.puppycrawl.tools.checkstyle.Main -c config.xml Test.java Starting audit... [ERROR] C:\PCI_Dev\jenkins\Test.java:7:1: Finally block should not contain return statements. [ForbidReturnInFinallyBlock] Audit done. Checkstyle ends with 1 errors.


C:\PCI_Dev\jenkins>call ant -buildfile build2.xml -lib C:\PCI_Dev\jenkins\checkstyle-8.41.1 Buildfile: C:\PCI_Dev\jenkins\build2.xml

build:

BUILD FAILED C:\PCI_Dev\jenkins\build2.xml:4: java.lang.IllegalStateException: java.lang.ClassNotFoundException: com.github.sevntu.checkstyle.checks.annotation.ForbidAnnotationElementValueCheck at com.google.common.reflect.ClassPath$ClassInfo.load(ClassPath.java:382) at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195) at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948) at java.base/java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:658) at java.base/java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:274) at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133) at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801) at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484) at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474) at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913) at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578) at com.puppycrawl.tools.checkstyle.utils.ModuleReflectionUtil.getCheckstyleModules(ModuleReflectionUtil.java:64) at com.puppycrawl.tools.checkstyle.PackageObjectFactory.generateThirdPartyNameToFullModuleName(PackageObjectFactory.java:306) at com.puppycrawl.tools.checkstyle.PackageObjectFactory.createModule(PackageObjectFactory.java:191) at com.puppycrawl.tools.checkstyle.TreeWalker.setupChild(TreeWalker.java:120) at com.puppycrawl.tools.checkstyle.api.AutomaticBean.configure(AutomaticBean.java:201) at com.puppycrawl.tools.checkstyle.Checker.setupChild(Checker.java:476) at com.puppycrawl.tools.checkstyle.api.AutomaticBean.configure(AutomaticBean.java:201) at com.puppycrawl.tools.checkstyle.ant.CheckstyleAntTask.createRootModule(CheckstyleAntTask.java:427) at com.puppycrawl.tools.checkstyle.ant.CheckstyleAntTask.realExecute(CheckstyleAntTask.java:323) at com.puppycrawl.tools.checkstyle.ant.CheckstyleAntTask.execute(CheckstyleAntTask.java:305) at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:299) 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.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:99) at org.apache.tools.ant.Task.perform(Task.java:350) at org.apache.tools.ant.Target.execute(Target.java:449) at org.apache.tools.ant.Target.performTasks(Target.java:470) at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1401) at org.apache.tools.ant.Project.executeTarget(Project.java:1374) at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41) at org.apache.tools.ant.Project.executeTargets(Project.java:1264) at org.apache.tools.ant.Main.runBuild(Main.java:818) at org.apache.tools.ant.Main.startAnt(Main.java:223) at org.apache.tools.ant.launch.Launcher.run(Launcher.java:284) at org.apache.tools.ant.launch.Launcher.main(Launcher.java:101) Caused by: java.lang.ClassNotFoundException: com.github.sevntu.checkstyle.checks.annotation.ForbidAnnotationElementValueCheck at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) at com.google.common.reflect.ClassPath$ClassInfo.load(ClassPath.java:379) ... 38 more

Total time: 1 second

Using Java 8 everything works as expected (running Checkstyle 8.41, SevNTU 1.40). Switching to Java 11 or 17 causes this problem with both the older and newest versions of Checkstyle and SevNTU.

If I comment out the block at line 32 from config.xml it works fine. Similar issues happen if I uncomment other SevNTU checks further up in the file.

rnveach commented 1 year ago

sevntu-checks-1.42.0.jar;cs2\checkstyle-10.4-all.jar

Latest sevntu only supports checkstyle 10.0 . See https://github.com/sevntu-checkstyle/sevntu.checkstyle/blob/master/sevntu-checks/pom.xml#L19 .

C:\PCI_Dev\jenkins\checkstyle-8.41.1 running Checkstyle 8.41, SevNTU 1.40

Same statement as above, and also the latest sevntu does not support such an old checkstyle version and I would not recommend mixing them.

Caused by: java.lang.ClassNotFoundException: com.github.sevntu.checkstyle.checks.annotation.ForbidAnnotationElementValueCheck

It sounds like the way you are executing checkstyle in this instance is not including sevntu.

It is impossible to diagnose more without your ant configuration as it is looking like just a configuration issue.

pci-mthompson commented 1 year ago

Sorry I meant to attach the ant config. I have trimmed it down the bare minimum.

<?xml version="1.0"?>


This is running with checkstyle-10.4-all.jar and sevntu-checks-1.42.0.jar in the cs2 folder.

C:\PCI_Dev\jenkins>call ant -buildfile build2.xml -lib C:\PCI_Dev\jenkins\cs2\ Buildfile: C:\PCI_Dev\jenkins\build2.xml

build:

BUILD FAILED C:\PCI_Dev\jenkins\build2.xml:4: java.lang.IllegalStateException: java.lang.ClassNotFoundException: com.github.sevntu.checkstyle.checks.annotation.ForbidAnnotationElementValueCheck at com.google.common.reflect.ClassPath$ClassInfo.load(ClassPath.java:382) at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195) at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948) at java.bas

pci-mthompson commented 1 year ago

config.xml.txt build2.xml.txt

rnveach commented 1 year ago

build2.xml.txt

There is no mention of sevntu assuming it is not already on the classpath. Example: https://github.com/sevntu-checkstyle/checkstyle-samples/blob/master/ant-project/build.xml#L35

There is something wrong specifically with your setup and you will need to debug more. You will need to ensure sevntu is on the classpath at the time you execute checkstyle.

running Checkstyle 8.41, SevNTU 1.40

After thinking about this more, I am convinced this is the issue. We upgraded Checkstyle to Java 11 in 10.0 and I remember there being classloader breaking changes with the new JDK. This impacted guava's ability to scan classpaths for custom modules which we use in Checkstyle and it was an non-backward compatible change in the dependency. Using an old checkstyle in a newer JDK means the classpath scanning won't work.

Unfortunately , if you will not upgrade Checkstyle to the supported version by Sevntu, we won't be able to help you anymore. 8 is drastically out of date (pre 2018) and we can't help diagnosing every mix and max version users want.

pci-mthompson commented 1 year ago

SevNTU is in the cs2 folder included from the command line "call ant -buildfile build2.xml -lib C:\PCI_Dev\jenkins\cs2". I get the same behavior with the latest versions of Checkstyle and SevNTU. image

rnveach commented 1 year ago

Thank you for trying with the newer version. Just saying again, sevntu only supports 10.0 and nothing else.

I am able to reproduce and created a reproducing repo at https://github.com/rnveach/sevntu_issue_935 .

I am unsure what the issue is right now, but I am suspecting it to be related to class loading since you say Java 8 works, but 11 doesn't.

Our ant samples run fine with no issues, so it is odd to me. https://app.travis-ci.com/github/sevntu-checkstyle/checkstyle-samples/builds/257329432#L242

I am unable to reproduce in Eclipse while running checkstyle with the sevntu jar added to the classpath and the commands -c Z:\sevntu_issue_935\config.xml Z:\sevntu_issue_935\Test.java.

Upgrading ant to 1.9.16 did not change anything.

Final Edit: I think this is either a guava issue or an ant issue. Our ant with maven works just fine. I can get this ant to run similar to our sample, but it requires a FQCP as guava won't return any 3rd party modules.

rnveach commented 1 year ago

@pci-mthompson I moved your issue to https://github.com/checkstyle/checkstyle/issues/12385 .

I agree something is wrong, but I do not believe this to be a sevntu issue. It seems to either be an ANT issue or a Guava dependency issue from Checkstyle.

I moved it to main repo to raise awareness and see if more assistance can be given.

I did mention in the new issue a temporary workaround is to switch your ANT to how we define ours in our samples ( https://github.com/sevntu-checkstyle/checkstyle-samples/tree/master/ant-project ) and switch from simple module names to the fully qualified classpath for anything coming from sevntu.

pci-mthompson commented 1 year ago

Thank you for your help. I'll move my comments over there!