You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2022/10/24 17:23:00 UTC

[jira] [Commented] (GROOVY-7382) java.lang.IllegalAccessError during foo( new PackagePrivateClass() ) in JUnit test

    [ https://issues.apache.org/jira/browse/GROOVY-7382?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17623298#comment-17623298 ] 

Eric Milles commented on GROOVY-7382:
-------------------------------------

The JVM enforces same-class-loader for these kinds of situations.  Not sure if you can satisfy that requirement for pre-compiled and runtime-parsed classes.

> java.lang.IllegalAccessError during foo( new PackagePrivateClass() ) in JUnit test
> ----------------------------------------------------------------------------------
>
>                 Key: GROOVY-7382
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7382
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 1.8.0, 2.1.0, 2.4.3
>         Environment: Windows 7, Oracle jdk1.7.0_55, _JAVA_OPTIONS: -Duser.language=en, JUnit 4.12
>            Reporter: Daniel Huss
>            Assignee: Eric Milles
>            Priority: Minor
>         Attachments: minimal-example.zip
>
>
> Running the this example results in {{java.lang.IllegalAccessError: tried to access class foo.bar.PackagePrivateClass from class foo.bar.Issue}} at a strange location.
> {code:title=Issue.groovy|borderStyle=solid}
> package foo.bar
> import org.junit.Test
> final class Issue {
>     @Test
>     void test() {
>         // This seems to work even though PackagePrivateClass is loaded by a different class loader
>         def works = new PackagePrivateClass()
>         assert works.state == 42
>         // No problem
>         foo(works)
>         // But this throws IllegalAccessError?!
>         foo( new PackagePrivateClass() )
>     }
>     PackagePrivateClass field
>     Issue foo( PackagePrivateClass value ) {
>         field = value
>         this
>     }
> }
> {code}
> {code:title=GroovyRunner.java|borderStyle=solid}
> package foo.bar;
> import groovy.lang.GroovyClassLoader;
> import groovy.lang.GroovyCodeSource;
> import java.io.File;
> import java.nio.charset.StandardCharsets;
> import java.nio.file.Path;
> import java.nio.file.Paths;
> import javax.script.ScriptEngine;
> import javax.script.ScriptEngineManager;
> import junit.framework.Test;
> import org.codehaus.groovy.control.CompilerConfiguration;
> import org.junit.runners.Suite;
> public final class GroovyRunner extends Suite {
>     public GroovyRunner( Class<?> klass ) throws Throwable {
>         super( klass, loadGroovyTestClass( klass ) );
>     }
>     private static Class<?>[] loadGroovyTestClass( Class<?> annotatedClass ) throws Throwable {
>         CompilerConfiguration config = new CompilerConfiguration();
>         config.setSourceEncoding( StandardCharsets.UTF_8.name() );
>         config.getClasspath().listIterator().add( Paths.get( "src", "test", "groovy" ).toAbsolutePath().toString() );
>         GroovyClassLoader loader = new GroovyClassLoader( annotatedClass.getClassLoader(), config );
>         File testFile = Paths.get( "src", "test", "groovy", "foo", "bar", "Issue.groovy" ).toFile();
>         return new Class<?>[] { loader.parseClass( new GroovyCodeSource( testFile ) ) };
>     }
> }
> {code}
> {code:title=RunThisTest.java|borderStyle=solid}
> package foo.bar;
> import org.junit.runner.RunWith;
> @RunWith(GroovyRunner.class)
> public class RunThisTest {}
> {code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)