getDeclaredMethods()在Java 7与Java 8中的表现不同 [英] getDeclaredMethods() behaving differently in Java 7 vs. Java 8

查看:251
本文介绍了getDeclaredMethods()在Java 7与Java 8中的表现不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下小例子:

package prv.rli.codetest;

import java.lang.reflect.Method;

public class BreakingInterfaces  {
    interface Base {
        BaseFoo foo();
        interface BaseFoo {           
        }
    }

    interface Derived extends Base {
        DerivedFoo foo();
        interface DerivedFoo extends BaseFoo {

        }
    }

    public static void main(String[] args) {       
        dumpDeclaredMethods(Derived.class);
    }

    private static void dumpDeclaredMethods(Class<?> class1) {
        System.out.println("---" + class1.getSimpleName() + "---");
        Method[] methods = class1.getDeclaredMethods();
        for (Method method : methods) {
            System.out.println(method);
        }
        System.out.println("----------");
    }
}

如果用jdk1.7.0编译上面的例子。 55输出为:

If you compile the above example with jdk1.7.0.55 the output is:

 ---Derived---
public abstract BreakingInterfaces$Derived$DerivedFoo BreakingInterfaces$Derived.foo()
----------

但在使用时jdk1.8.0.25输出为:

But when using jdk1.8.0.25 the output is:

---Derived---
public abstract prv.rli.codetest.BreakingInterfaces$Derived$DerivedFoo prv.rli.codetest.BreakingInterfaces$Derived.foo()
public default prv.rli.codetest.BreakingInterfaces$Base$BaseFoo prv.rli.codetest.BreakingInterfaces$Derived.foo()
----------

有谁知道,这是不是jdk1.8.0.25中的错误或为什么公共默认方法在这里重现?

Does anybody know, whether this is a bug in jdk1.8.0.25 or why the public default Method resurfaces here?

推荐答案

getDeclaredMethods( )在这里表现正确,因为它告诉你它在课堂上发现了什么。如果您使用Java 7目标(或较旧的编译器)编译的接口,您将看到的Java 7实现的输出没有区别getDeclaredMethods()

getDeclaredMethods() behaves correctly here as it tells you exactly what it has found in the class. If you feed in an interface compiled with Java 7 target (or an older compiler) you will see no difference to the output of the Java 7 implementation of getDeclaredMethods().

编译器的行为方式不同。在Java 8中编译这样的子< - c $ c>接口时,将生成一个不会为Java 7目标生成的桥接方法,因为它甚至不可能用于Java 7目标。

It’s the compiler which behaves differently. When compiling such a sub-interface in Java 8, a bridge method will be generated which will not be generated for a Java 7 target as it is not even possible for the Java 7 target.

现在为接口生成桥接方法的原因是您通常拥有比接口更多的实现类,因此具有默认值 bridge方法可以避免将桥接方法添加到每个实现中。此外,如果只有一个 abstract 方法并且没有实现桥接方法,它会使lambda类生成变得更加容易。

The reason why bridge methods are generated for interfaces now is that you usually have more implementation classes than interfaces, therefore having a default bridge method in the interface saves you from adding that bridge method to every implementation. Further, it makes lambda class generation much easier if there is only one abstract method and no bridge method to implement.

接口层次结构需要桥接方法但不提供缺省时,编译器必须使用 LambdaMetafactory.altMetafactory 而不是 LambdaMetafactory.metafactory 指定每个桥接方法必需。

When an interface hierarchy requires bridge methods but provides no defaults, the compiler has to generate code using LambdaMetafactory.altMetafactory rather than LambdaMetafactory.metafactory specifying every bridge method that is required.

这篇关于getDeclaredMethods()在Java 7与Java 8中的表现不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆