Search code examples
groovywicketwicket-6

Groovy Inner Classes wont work with Apache Wicket


Im trying to write simple things with Apache Wicket (6.15.0) and Groovy (2.2.2 or 2.3.1). And Im having trouble with inner classes.

class CreatePaymentPanel extends Panel { 
  public CreatePaymentPanel(String id) {
    super(id)
    add(new PaymentSelectFragment('currentPanel').setOutputMarkupId(true))
}

public class PaymentSelectFragment extends Fragment {
        public PaymentSelectFragment(String id) {
            super(id, 'selectFragment', CreatePaymentPanel.this) // problem here
            add(new AjaxLink('cardButton') {
                @Override
                void onClick(AjaxRequestTarget target) {
                    ... CreatePaymentPanel.this // not accessible here 
                }
            })
            add(new AjaxLink('terminalButton') {
                @Override
                void onClick(AjaxRequestTarget target) {
                    ... CreatePaymentPanel.this // not accessible here 
                }
            });
        }
        } // end of PaymentSelectFragment class
} // end of CreatePaymentPanel class

Groovy tries to find a property "this" in CreatePaymentPanel class.. How to workaround this? It is a valid java code, but not groovy.

However, Test.groovy:

class Test {

    static void main(String[] args) {
        def a = new A()
    }

    static class A {
        A() {
            def c = new C()
        }

        public void sayA() { println 'saying A' }

        class B {
            public B(A instance) {
                A.this.sayA()
                instance.sayA()
            }
        }
        /**
         * The problem occurs here
         */
        class C extends B {
            public C() {
                super(A.this) // groovy tries to find property "this" in A class
                sayA()
            }
        }
    }
}

Above code wont work, the same error occurs, like in Wicket's case.

And TestJava.java, the same and working:

public class TestJava {

    public static void main(String[] args) {
        A a = new A();
    }

    static class A {
        A() {
            C c = new C();
        }

        public void sayA() {
            System.out.println("saying A");
        }

        class B {
            public B(A instance) {
                instance.sayA();
            }
        }

        /**
         * This works fine
         */
        class C extends B {
            public C() {
                super(A.this);
                sayA();
            }
        }
    }
}

What I am missing?


Solution

  • You can't refer to a CreatePaymentPanel.this inside of PaymentSelectFragment because there is no instance of CreatePamentPanel that would be accessible there. What would you expect that to evaluate to if it were allowed?