希望在Groovy中扩展内部类

时间:2014-09-04 02:36:22

标签: groovy inner-classes

我正在寻找在Groovy中扩展内部类,它是混合模式的一部分,需要在类构造之后添加groovy计算方法,但本机使用主类的访问器。在Java中工作,而不是在Groovy中工作。帮助赞赏。

P.S。意识到with { } groovy构造可能是另一种选择......试图让它发挥作用

基类(在Java中):

public class JavaClass {

    private String foo = "Foo";
    private Calculation calculation;

    public JavaClass() {
        calculation = new Calculation();
    }

    public JavaClass(Calculation calculation) {
        this.calculation = calculation;
    }

    public String getFoo() {
        return foo;
    }

    public String getBar() {
        return calculation.getBar();
    }

    public class Calculation {
        public String getBar() {
            return null;
        }
    }

}

我创建了一个Groovy实现:(我知道它有点笨拙,忍受我)

class GroovyCalculationClass extends JavaClass {
    public GroovyCalculationClass() { }

    public Calculation getCalculation() {
        return new Calculation();
    }

    public class Calculation extends JavaClass.Calculation {
        public String getBar() {
            return getFoo() + "Bar";
        }
    }
}

运行此测试:

class TestCalculation {

    @Test
    public void testCalculation() {

        JavaClass javaClass = new JavaClass(
            new GroovyCalculationClass().getCalculation());
        Assert.assertEquals("FooBar", javaClass.getBar());
    }


}

得到这个:

  

java.lang.ArrayIndexOutOfBoundsException:0       在com.modelshop.datacore.generator.GroovyCalculationClass $ Calculation。(GroovyCalculationClass.groovy)       at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)       at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)       at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)       在java.lang.reflect.Constructor.newInstance(Constructor.java:525)       在org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:77)       at org.codehaus.groovy.runtime.callsite.ConstructorSite $ ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:102)       在org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:57)       在org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:182)       在org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:190)       在com.modelshop.datacore.generator.GroovyCalculationClass.getCalculation(GroovyCalculationClass.groovy:12)       在com.modelshop.datacore.generator.GroovyCalculationClass $ getCalculation.call(未知来源)       在org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)       在org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)       在org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)       在com.modelshop.datacore.generator.TestCalculation.testCalculation(TestCalculation.groovy:17)       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)       在org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:45)       在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)       在org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)       在org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)       在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)       在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)       在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)       在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:231)       在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:60)       在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)       在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:50)       在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:222)       在org.junit.runners.ParentRunner.run(ParentRunner.java:300)       在org.junit.runner.JUnitCore.run(JUnitCore.java:157)       at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:77)       在com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)       在com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)       在com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

用相同的JavaCalculationClass替换GroovyCalculationClass,一切正常。

0 个答案:

没有答案