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

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

问题描述

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

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";
    }
}

在主方法中应用程序从 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

为什么默认方法字母"在 ClassA 中作为属性不可见?是错误还是功能?

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

推荐答案

我猜,Introspector 不处理 interface 层次链,即使使用 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 a rather ambiguous 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. :) It seems that defender methods are getting some kind of special treatment here.

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

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