You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Peter Myrén (JIRA)" <ji...@apache.org> on 2017/02/08 15:10:41 UTC

[jira] [Comment Edited] (GROOVY-5728) Accessing private constructor from a static factory

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

Peter Myrén edited comment on GROOVY-5728 at 2/8/17 3:10 PM:
-------------------------------------------------------------

Same problem with non anonymous inner class.
{code}
public abstract class FooMain {
    private FooMain() {}
    public abstract String bar();
    public static FooMain factory() {
      return new FooMainNested();
    }
    public static void main(String[] args) {
        System.out.println(factory().bar());
    }
    
   private static class FooMainNested extends FooMain{
        public String bar() { return "xxx"; }
   }
}

{code}

It woks with protected constructor though.
{code}
public abstract class FooMain {
    protected FooMain() {}
    public abstract String bar();
    public static FooMain factory() {
      return new FooMain() {
        public String bar() { return "xxx"; }
      };
    }    
    public static void main(String[] args) {
        System.out.println(factory().bar());
    }   
}
{code}

Tested with groovy 2.4.8


was (Author: petermyren):
Same problem with non anonymous inner class.

It woks with protected constructor though.
{code}
public abstract class FooMain {
    protected FooMain() {}
    public abstract String bar();
    public static FooMain factory() {
      return new FooMain() {
        public String bar() { return "xxx"; }
      };
    }    
    public static void main(String[] args) {
        System.out.println(factory().bar());
    }   
}
{code}

Tested with groovy 2.4.8

> Accessing private constructor from a static factory
> ---------------------------------------------------
>
>                 Key: GROOVY-5728
>                 URL: https://issues.apache.org/jira/browse/GROOVY-5728
>             Project: Groovy
>          Issue Type: Bug
>          Components: groovy-runtime
>    Affects Versions: 2.0.4, 2.4.0-beta-3
>            Reporter: Paul King
>            Priority: Minor
>
> The following code works fine from Java but fails in Groovy:
> {code}
> public abstract class FooMain {
>     private FooMain() {}
>     public abstract String bar();
>     public static FooMain factory() {
>       return new FooMain() {
>         public String bar() { return "xxx"; }
>       };
>     }
>     public static void main(String[] args) {
>         System.out.println(factory().bar());
>     }
> }
> // => java.lang.IllegalAccessError: tried to access method FooMain.<init>()V from class FooMain$1
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.15#6346)