You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Mauro Molinari (JIRA)" <ji...@apache.org> on 2018/01/10 13:36:06 UTC

[jira] [Created] (GROOVY-8441) IllegalAccessError when calling a public method on a package-private implementation instance

Mauro Molinari created GROOVY-8441:
--------------------------------------

             Summary: IllegalAccessError when calling a public method on a package-private implementation instance
                 Key: GROOVY-8441
                 URL: https://issues.apache.org/jira/browse/GROOVY-8441
             Project: Groovy
          Issue Type: Bug
          Components: Static compilation
    Affects Versions: 2.4.13
            Reporter: Mauro Molinari


This seems somewhat similar to GROOVY-7549.

Consider the following interface:

{code:java}
package test18;

public interface Iface {
	String getBar();
}
{code}

and a package-private implementation:

{code:java}
package test18;

class IfaceImpl implements Iface {

	@Override
	public String getBar() {
		return "bar";
	}
}
{code}

Now the following base class:

{code:java}
package test18;

public class Base {

	private IfaceImpl foo = new IfaceImpl(); 
	
	public Iface getFoo() {
		return foo;
	}
}
{code}

Please note: the field is declared with the implementation type, the getter is public, but the getter declared return type is the public interface.

Then this Groovy class:

{code:groovy}
package test18.sub

import groovy.transform.CompileDynamic
import groovy.transform.CompileStatic
import test18.Base

class Test18 extends Base {
	
	@CompileStatic
	void foobar() {
		println foo.bar
	}
	
	static main(args) {
		Test18 t = new Test18()
		println t.foo.bar
		t.foobar()
	}
}
{code}

Please note the package is different.

If you run this code, the first {{bar}} is written correctly, the second invocation fails with: {{java.lang.IllegalAccessError: tried to access class test18.IfaceImpl from class test18.sub.Test18}}.

Workarounds:
- remove {{@CompileStatic}} from {{Test18.foobar()}} (or declare it with {{@CompileDynamic}} if the whole class is statically compiled)
- change the declaration of {{Base.foo}} to be of type {{Iface}} rather than {{IfaceImpl}}




--
This message was sent by Atlassian JIRA
(v6.4.14#64029)