You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Paul King (Jira)" <ji...@apache.org> on 2020/07/11 06:30:00 UTC

[jira] [Updated] (GROOVY-8441) IllegalAccessError when calling a public method on a field whose type is declared to be a package-private implementation one

     [ https://issues.apache.org/jira/browse/GROOVY-8441?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Paul King updated GROOVY-8441:
------------------------------
    Fix Version/s: 2.5.13

> IllegalAccessError when calling a public method on a field whose type is declared to be a package-private implementation one
> ----------------------------------------------------------------------------------------------------------------------------
>
>                 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
>            Assignee: Eric Milles
>            Priority: Major
>             Fix For: 3.0.0-rc-2, 2.5.13
>
>
> 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
(v8.3.4#803005)