antlr / antlr4test-maven-plugin

A maven plugin used to test the grammars-v4 repo grammars
BSD 3-Clause "New" or "Revised" License
17 stars 16 forks source link

java.lang.ClassNotFoundException during GrammarInitializer instantiation #20

Closed gilcesarf closed 4 years ago

gilcesarf commented 4 years ago

I have created a new scenario and configured a GrammarInitializer implemented inside my src/test/java root. A ClassNotFound exception was raised as at the end.

I investigated the problem and it is caused because of this snippet inside com.khubla.antlr.antlr4test.Scenario class:

public ClassLoader getClassLoader() throws MalformedURLException, ClassNotFoundException {
    final URL antlrGeneratedURL = new File(baseDir + "/target/classes").toURI().toURL();
    final URL[] urls = new URL[] { antlrGeneratedURL };
    return new URLClassLoader(urls, Thread.currentThread().getContextClassLoader());
}

This is the code used to get a classloader to load the Lexer and Parser. It points explicitly to "/target/classes". But when the GrammarInitializer is put inside /src/test/java, the corresponding .class gets compiled to "/target/test-classes", so it cannot be found.

Until I fix this, a possible workaround is to put the GrammarInitializer implementation inside the main source project root: src/main/java

This workaround solved the ClassNotFoundException. Its drawback is that GrammarInitializer class would be distributed with the .jar of the project.

java.lang.ClassNotFoundException: io.trustep.grammars.mysql.SmokeInitializer
    at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:466)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:563)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
    at com.khubla.antlr.antlr4test.ScenarioExecutor.testGrammar(ScenarioExecutor.java:112)
    at com.khubla.antlr.antlr4test.ScenarioExecutor.testGrammars(ScenarioExecutor.java:72)
    at com.khubla.antlr.antlr4test.GrammarTestMojo.testScenarios(GrammarTestMojo.java:294)
    at com.khubla.antlr.antlr4test.GrammarTestMojo.execute(GrammarTestMojo.java:169)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:957)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:289)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:193)
    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:564)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:347)
gilcesarf commented 4 years ago

The initial idea I have to fix the problem is to add a parameter to the method getClassLoader() and pass "/target/classes" or "/target/test-classes" if instantiating a Lexer/Parser or a GrammarInitializer. Will take a closer look at the code to check if its a good approach.

gilcesarf commented 4 years ago

Already fixed the issue. Waiting on PR to close issue