(用什么java.lang.reflect.Method.isBridge)呢? [英] what java.lang.reflect.Method.isBridge () used for?

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

问题描述

在法类的导航我整个功能isBridge来了()的javadoc,其中说,其真正的只有Java规范声明是真实的方法。

During navigation of Method class I came across the function isBridge(), javadoc of which says, that its true only if 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.

您可以在这个类<一发现href=\"https://fisheye.springsource.org/browse/spring-framework/spring-core/src/main/java/org/springframework/core/BridgeMethodResolver.java?r=02a4473c62d8240837bec297f0a1f3cb67ef8a7b\"相对=nofollow> 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(字符串)的不同之处从的编译时方法声明, C.id(对象)。前者以一个String类型的参数,而后者使用对象类型的参数。调用失败,执行方法的正文前一个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.

这种情况=nofollow的>§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(新对象())上面所示的调用来调用的方法,它会执行演员和失败,因为必需的。

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.

另请参阅 <一个href=\"http://books.google.com/books?id=zaoK0%5A2STlkC&pg=PA50&lpg=PA50&dq=java+bridge+%22covariant+return+types%22&source=web&ots=6WoofeXYFM&sig=8bS5Y0sw%5ApBu%5ACuf1EkIR_L4Ddo&hl=en&sa=X&oi=book_result&resnum=4&ct=result#PPA48,M1\"相对=nofollow>桥

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.

通常情况下,一个方法对象的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();

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

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