简体   繁体   English

使用不同Java版本的Strange Default Method行为

[英]Strange Default Method behavior with different Java versions

Let's say I have the following class hierarchy: 假设我有以下类层次结构:

interface Collection<E>
{
    Collection<E> $plus(E element)
}

interface MutableCollection<E> extends Collection<E>
{
    @Override
    MutableCollection<E> $plus(E element)
}

interface Set<E> extends Collection<E>
{
    @Override
    Set<E> $plus(E element)
}

interface MutableSet<E> extends Set<E>, MutableCollection<E>
{
    @Override
    default MutableSet<E> $plus(E element)
    {
        // ... implementation
    }
}

abstract class AbstractArraySet<E> implements Set<E>
{
    // ... no $plus(...) implementation
}

class ArraySet<E> extends AbstractArraySet<E> implements MutableSet<E>
{
    // ... no $plus(...) implementation
}

As you can see, only the MutableSet class provides an implementation for the $plus method. 如您所见,只有MutableSet类为$plus方法提供了一个实现。 In a Test case, I am calling this method on an instance of type ArraySet . 在测试用例中,我在ArraySet类型的实例上调用此方法。 The Test always passes in the CI environment, while it always fails with an AbstractMethodError on my local environment. 测试总是在CI环境中传递,而它总是在我的本地环境中使用AbstractMethodError失败。 In both cases, I am using Gradle (2.7). 在这两种情况下,我都在使用Gradle(2.7)。


The Error: 错误:

java.lang.AbstractMethodError: Method dyvil/collection/mutable/ArraySet.$plus(Ljava/lang/Object;)Ldyvil/collection/Collection; is abstract

    at dyvil.collection.mutable.ArraySet.$plus(ArraySet.java)
    at dyvil.tests.CollectionTests.testCollection(CollectionTests.java:99)
    at ...

Test Code : 测试代码

testCollection(new ArraySet());

public void testCollection(Collection collection)
{
    assertEquals(mutable.$plus("newElement"), collection.$plus("newElement"));
}

java -version output: java -version输出:

  • CI (where it works): CI (工作地点):

     java version "1.8.0" Java(TM) SE Runtime Environment (build 1.8.0-b132) Java HotSpot(TM) 64-Bit Server VM (build 25.0-b70, mixed mode) 
  • Local (where it fails): 本地(失败的地方):

     java version "1.8.0_71" Java(TM) SE Runtime Environment (build 1.8.0_71-b15) Java HotSpot(TM) 64-Bit Server VM (build 25.71-b15, mixed mode) 

I am expecting this to be some sort of javac bug, where the compiler fails to add all required bridge methods (the code compiles without any warnings or errors). 我希望这是某种javac错误,编译器无法添加所有必需的桥接方法(代码编译时没有任何警告或错误)。 In IntelliJ IDEA, the problem occurs both using javac and the Eclipse Compiler. 在IntelliJ IDEA中,使用javac和Eclipse编译器都会出现问题。

(Answer is made based on author's comment above: problem was solved): (根据上面作者的评论回答:问题解决了):

Doing a full clean and rebuild fixed the problem as well. 完全清理和重建也解决了问题。

Nevertheless, there must have been a bug involved at some point that caused the faulty binaries. 尽管如此,某些地方肯定有一个错误导致了错误的二进制文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM