编译器中的错误或我做错了什么?

时间:2014-04-27 14:15:15

标签: java compiler-construction intellij-idea javac

首先,一些环境:这是Oracle的1.6.0_45 JDK和IDEA 13.1。

我偶然发现了一个最怪异的编译器错误:

public final class AsmFunnels
{
    private AsmFunnels()
    {
    }

    // ...

    public static void funnelFieldNode(final FieldNode node,
        final PrimitiveSink into)
    {
        FieldNodeFunnel.INSTANCE.funnel(node, into);
    }

    // ...

    @ParametersAreNonnullByDefault
    private enum FieldNodeFunnel
        implements Funnel<FieldNode>
    {
        INSTANCE
        {
            @Override
            public void funnel(final FieldNode from, final PrimitiveSink into)
            {
                into.putUnencodedChars(from.name)
                    .putUnencodedChars(from.desc)
                    .putUnencodedChars(from.signature);
            }
        }
    }

    //...

}

FunnelPrimitiveSink来自番石榴;至于FieldNode,它来自ASM 5.0.1(org.objectweb.asm.tree.FieldNode)。

请注意,我使用IDEA(13.1如果重要的话)。现在,首先,我有一个这样定义的方法:

public static void funnelFieldNode(final FieldNode node,
    final PrimitiveSink into)
{
    FieldNodeFunnel.INSTANCE.funnel(node, into);
}

IDEA显示没有警告......但编译器不满意:

Error:(53, 33) java: /home/fge/src/perso/parboiled1/grappa/src/main/java/org/parboiled/transform/process/AsmFunnels.java:53: cannot find symbol
symbol  : method funnel(org.objectweb.asm.tree.FieldNode,com.google.common.hash.PrimitiveSink)
location: class org.parboiled.transform.process.AsmFunnels.FieldNodeFunnel

第53行就是这个:

    FieldNodeFunnel.INSTANCE.funnel(node, into);

这是最离奇的,我不明白为什么。然后我这样做了:

private static Funnel<FieldNode> fieldNodeFunnel()
{
    return FieldNodeFunnel.INSTANCE;
}

public static void funnelFieldNode(final FieldNode node,
    final PrimitiveSink into)
{
    fieldNodeFunnel().funnel(node, into);
}

有了它,它就会编译!

这里发生了什么?谁是对的? IDEA还是javac?

修改

我尝试了这个,然后编译:

((Funnel<FieldNode>) FieldNodeFunnel.INSTANCE).funnel(node, into);

但为什么我需要演员呢?

我还试图使用&#34;常规单身&#34;像这样有效:

@ParametersAreNonnullByDefault
private static final class FieldNodeFunnel
    implements Funnel<FieldNode>
{
    private static final Funnel<FieldNode> INSTANCE
        = new FieldNodeFunnel();

    @Override
    public void funnel(final FieldNode from, final PrimitiveSink into)
    {
        into.putUnencodedChars(from.name)
            .putUnencodedChars(from.desc)
            .putUnencodedChars(from.signature);
    }
}

1 个答案:

答案 0 :(得分:3)

这似乎是一个重复的项目 - Enum class member with interface cannot find methods internally

JDK-6中可能存在错误 - 请参阅:

  1. 枚举实现接口https://bugs.openjdk.java.net/browse/JDK-7049773

  2. 的编译结果不一致
  3. 作为源文件https://bugs.openjdk.java.net/browse/JDK-6724345输入的枚举类的方法解析不正确。