[
https://issues.apache.org/jira/browse/GROOVY-7382?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17462227#comment-17462227
]
Eric Milles commented on GROOVY-7382:
-------------------------------------
The "works" assignment succeeds because the target type is Object. When
calling the method the JVM is trying to confirm that it can assign the new
instance to the parameter type. If the parameter of "foo" is changed to def,
the call succeeds and the assignment "field = value" is the new point of
failure.
> 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
> 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.1#820001)