Java 8和Bean Info Introspector中接口的默认方法 [英] Default method in interface in Java 8 and Bean Info Introspector

查看:105
本文介绍了Java 8和Bean Info Introspector中接口的默认方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Interface和BeanInfo Introspector中的默认方法有一点问题。
在这个例子中,有接口:接口

I have a little problem with default methods in Interface and BeanInfo Introspector. In this example, there is interface: Interface

public static interface Interface {
    default public String getLetter() {
        return "A";
    }
}

以及两个ClassA和ClassB类:

and two classes ClassA and ClassB:

public static class ClassA implements Interface {
}

public static class ClassB implements Interface {
    public String getLetter() {
        return "B";
    }
}

在main方法应用程序中打印来自BeanInfo的PropertyDescriptors:

In main method app prints PropertyDescriptors from BeanInfo:

public static String formatData(PropertyDescriptor[] pds) {
    return Arrays.asList(pds).stream()
            .map((pd) -> pd.getName()).collect(Collectors.joining(", "));

}

public static void main(String[] args) {


    try {
        System.out.println(
                formatData(Introspector.getBeanInfo(ClassA.class)
                        .getPropertyDescriptors()));
        System.out.println(
                formatData(Introspector.getBeanInfo(ClassB.class)
                        .getPropertyDescriptors()));
    } catch (IntrospectionException e) {
        e.printStackTrace();
    }

}

结果是:

class
class, letter

为什么默认方法letter在ClassA中不可见为属性?是bug还是功能?

Why default method "letter" is not visible as property in ClassA? Is it bug or feature?

推荐答案

我想, Introspector 不会进程接口层次结构链,即使使用Java 8虚拟扩展方法(也称为防御者,默认方法)接口,也可以使某些东西看起来像属性方法。这是一个相当简单的内省探测器声称它: BeanIntrospector

I guess, Introspector does not process interface hierarchy chains, even though with Java 8 virtual extention methods (aka defenders, default methods) interfaces can have something that kinda sorta looks like property methods. Here's a rather simplistic introspector that claims it does: BeanIntrospector

这是否可以被认为是一个灰色区域,这就是为什么我这么认为。

Whether this can be considered a bug is somewhat of a gray area, here's why I think so.

显然,现在,一个类可以从一个接口继承一个方法,该方法具有所谓的getter / setter / mutator的所有特性。但与此同时,整个事情都违背了接口的目的 - 接口不可能提供任何可以被视为属性的东西,因为它是无状态且无行为的,它只是意味着描述行为。甚至防御方法基本上都是静态的,除非它们访问具体实现的真实属性。

Obviously, now a class can "inherit" from an interface a method that has all the qualities of what's oficially considered a getter/setter/mutator. But at the same time, this whole thing is against interface's purpose -- an interface can not possibly provide anything that can be considered a property, since it's stateless and behaviorless, it's only meant to describe behavior. Even defender methods are basically static unless they access real properties of a concrete implementation.

另一方面,如果我们假设防御者正式继承(与提供默认实现相对,这有点不明确的定义),它们应该导致在实现类中创建合成方法,并且属于类和作为 PropertyDescriptor 查找的一部分遍历。 显然这不是它的方式,否则整个事情都会起作用。 :)似乎防守方法在这里得到了某种特殊待遇。

On the other hand, if we assume defenders are officially inherited (as opposed to providing default implementation which is somewhat of an unclear definition), they should result in synthetic methods being created in the implementing class, and those belong to the class and are traversed as part of PropertyDescriptor lookup. Obviously this is not the way it is though, otherwise the whole thing would be working. :) Seems that defender methods are getting some kind of special treatment here.

这篇关于Java 8和Bean Info Introspector中接口的默认方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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