Pico / Paranamer抛出java.lang.ArrayIndexOutOfBoundsException

时间:2019-01-15 10:49:51

标签: java dependency-injection pico

我发现在Java(jdk1.8.0_141)上使用Pico(picocontainer)2.14.3时;指某东西的用途 只有在构造函数中有lambda引用时,用于构造函数依赖项注入的Characteristics.USE_NAME才会使Paranamer 2.8引发以下异常[1];在注释掉构造函数中的lambda引用时,依赖项注入似乎可以工作[3] 。这是一个已知的问题?如果是这样,我们有解决方法。

可以使用以下方法重新创建它:

[1]    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 19562
        at org.picocontainer.paranamer.BytecodeReadingParanamer$ClassReader.accept(BytecodeReadingParanamer.java:562)
        at org.picocontainer.paranamer.BytecodeReadingParanamer$ClassReader.access$200(BytecodeReadingParanamer.java:337)
        at org.picocontainer.paranamer.BytecodeReadingParanamer.lookupParameterNames(BytecodeReadingParanamer.java:102)
        at org.picocontainer.paranamer.AdaptiveParanamer.lookupParameterNames(AdaptiveParanamer.java:75)
        at org.picocontainer.paranamer.AnnotationParanamer.lookupParameterNames(AnnotationParanamer.java:110)
        at org.picocontainer.paranamer.CachingParanamer.lookupParameterNames(CachingParanamer.java:75)
        at org.picocontainer.injectors.ParameterNameBinding.getName(ParameterNameBinding.java:38)
        at org.picocontainer.injectors.ConstructorInjector.getGreediestSatisfiableConstructor(ConstructorInjector.java:149)
        at org.picocontainer.injectors.ConstructorInjector.getGreediestSatisfiableConstructor(ConstructorInjector.java:110)
        at org.picocontainer.injectors.ConstructorInjector.access$100(ConstructorInjector.java:51)
        at org.picocontainer.injectors.ConstructorInjector$1.run(ConstructorInjector.java:331)
        at org.picocontainer.injectors.AbstractInjector$ThreadLocalCyclicDependencyGuard.observe(AbstractInjector.java:270)
        at org.picocontainer.injectors.ConstructorInjector.getComponentInstance(ConstructorInjector.java:364)

我的粗略实现:

public class PicoTest {    

    public static void main(String[] args) {

              final MutablePicoContainer pico = new PicoBuilder(new EmptyPicoContainer())
                .withConstructorInjection()
                .withNamedFieldInjection()
                .withCaching()
                .build();

        pico.addComponent("first", "First");
        pico.addComponent("second", "Second");
        pico.addComponent("third", "Third");

        pico.as(Characteristics.USE_NAMES).addComponent(MyPicoImpl.class);
        System.out.println(pico.getComponent(MyPicoImpl.class));

    }

[2]   //Lambda reference in constructor causes this to happen.
public class MyPicoImpl {


    private final String first;
    private final String second;
    private String third;


    public MyPicoImpl(String first, String second, String third) {
        this.first = first;
        this.second = second;
        this.third = third;

        Runnable r = () -> {
            System.out.println("Gamble ....");
        };
        System.out.println(String.format("Received  1 %s  2 %s  3 %s  ", this.first, this.second, this.third));

    }

注释掉lambda参考后,我得到了预期的结果[3]

public class MyPicoImpl {    

    private final String first;
    private final String second;
    private String third;   

    public MyPicoImpl(String first, String second, String third) {
        this.first = first;
        this.second = second;
        this.third = third;

       /* Runnable r = () -> {
            System.out.println("Gamble ....");
        }; */

        System.out.println(String.format("Received  1 %s  2 %s  3 %s  ", this.first, this.second, this.third));

    }   
}
}

[3]预期结果:

Received  1 First  2 Second  3 Third  
com.company.MyPicoImpl@46ee7fe8

0 个答案:

没有答案