Method.isBridge 用于什么? [英] What Method.isBridge used for?

查看:21
本文介绍了Method.isBridge 用于什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java.lang.reflect.Method 类的导航过程中,我遇到了方法isBridge.它的 Javadoc 说只有在 Java 规范声明该方法为真时才返回真.

During navigation of the java.lang.reflect.Method class I came across the method isBridge. Its Javadoc says that it returns true only if the Java spec declares the method as true.

请帮我理解这是做什么用的!如果需要,自定义类是否可以将其方法声明为桥接器?

Please help me understand what this is used for! Can a custom class declare its method as a bridge if required?

推荐答案

在扩展方法具有参数化参数的参数化类型时,编译器可能会创建桥接方法.

A bridge method may be created by the compiler when extending a parameterized type whose methods have parameterized arguments.

你可以在这个类中找到BridgeMethodResolver 一种获取由桥接方法"引用的实际方法的方法.

You can find in this class BridgeMethodResolver a way to get the actual Method referred by a 'bridge method'.

参见创建框架、同步、传输控制:

作为这种情况的一个例子,考虑以下声明:

As an example of such a situation, consider the declarations:

class C<T> { abstract T id(T x); }
class D extends C<String> { String id(String x) { return x; } }

现在,给定一个调用

C c = new D();
c.id(new Object()); // fails with a ClassCastException

被调用的实际方法的擦除,D.id(String) 与编译时方法声明的签名不同,C.id(Object)代码>.前者采用 String 类型的参数,而后者采用 Object 类型的参数.在执行方法体之前,调用失败并出现 ClassCastException.

The erasure of the actual method being invoked, D.id(String) differs in its signature from that of the compile-time method declaration, C.id(Object). The former takes an argument of type String while the latter takes an argument of type Object. The invocation fails with a ClassCastException before the body of the method is executed.

只有在程序产生未经检查的警告时才会出现这种情况(§5.1.9).

Such situations can only arise if the program gives rise to an unchecked warning (§5.1.9).

实现可以通过创建桥接方法来强制执行这些语义.在上面的示例中,将在类 D 中创建以下桥接方法:

Implementations can enforce these semantics by creating bridge methods. In the above example, the following bridge method would be created in class D:

Object id(Object x) { return id((String) x); }

这是Java虚拟机响应上面显示的调用c.id(new Object())而实际调用的方法,它将执行转换并失败,根据需要.

This is the method that would actually be invoked by the Java virtual machine in response to the call c.id(new Object()) shown above, and it will execute the cast and fail, as required.

另见 :

See also Bridge:

如评论中所述,协变覆盖也需要桥接方法:

as mentioned in the comment, bridge methods are also needed for covariant overriding:

  • 在 Java 1.4 及更早版本中,如果签名匹配,一个方法可以覆盖另一个方法
  • 在 Java 5 中,如果参数完全匹配但是覆盖方法的返回类型,如果它是返回类型的子类型,则一个方法可以覆盖另一个另一种方法.
  • In Java 1.4, and earlier, one method can override another if the signatures match exactly.
  • In Java 5, a method can override another if the arguments match exactly but the return type of the overriding method, if it is a subtype of the return type of the other method.

通常,方法 Object clone() 可以被 MyObject clone() 覆盖,但是编译器会生成桥接方法:

Typically, a method Object clone() can be overridden by a MyObject clone(), but a bridge method will be generated by the compiler:

public bridge Object MyObject.clone();

这篇关于Method.isBridge 用于什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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