A bridge method may be created by the compiler when extending a parameterized type whose methods have parameterized arguments.
You can find in this class BridgeMethodResolver a way to get the actual Method referred by a 'bridge method'.
See Create Frame, Synchronize, Transfer Control:
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; } }
Now, given an invocation
C c = new D();
c.id(new Object()); // fails with a 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.
Such situations can only arise if the program gives rise to an unchecked warning (§5.1.9).
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); }
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:
- 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.
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();
相关推荐
### 正文 #### 一、引言与背景 本文主要探讨了如何通过蒙特卡罗方法结合分层抽样和逆高斯桥技术来对正态逆高斯(Normal Inverse Gaussian,简称NIG)过程下的期权进行估值。NIG模型在金融领域内被广泛用于股票回报...
描述中提到的 "Javabridge: a wrapper for the Java Native Interface." 明确指出 JavaBridge 是一个 JNI 的包装器,它为 Python 开发者提供了更友好的 API,使得在 Python 中调用和操作 Java 类和方法变得更加便捷...
Bridge Separates an object’s interface from its implementation Composite A tree structure of simple and composite objects Decorator Add responsibilities to objects dynamically Facade A single...
Polya reveals how the mathematical method of demonstrating a proof or finding an unknown can be of help in attacking any problem that can be "reasoned" out - from building a bridge to winning a game ...
It lets you decouple the creation of objects from their usage by providing a factory method interface for creating objects. For example, suppose you're developing a framework for document editors. ...
Chapter 2: A Case Study: Designing an App Chapter 3: Prototype Chapter 4: Factory Method Chapter 5: Abstract Factory Chapter 6: Builder Chapter 7: Singleton Chapter 8: Adapter Chapter 9: Bridge ...
Different types of damage on the bridge of the numerical model are simulated and transient analyses are performed by incorporating a load which moves constantly along the beam nodes. Continuous ...
Learn how to implement design patterns in Java: each pattern in Java Design Patterns ...Chapter 22: Bridge Patterns (Or Handle/Body Patterns) Chapter 23: Visitor Patterns Chapter 24: Interpreter Patterns
Rainald Löhner introduces the reader to the techniques required to achieve efficient CFD solvers, forming a bridge between basic theoretical and algorithmic aspects of the finite element method and ...
To this end, we propose a novel and efficient crossdomain human parsing model to bridge the cross-domain differences in terms of visual appearance and environment conditions and fully exploit ...
The design is made by the H-bridge topology with the use of MOSFET as a switch. The simulation for the system constructed with the help of MATLAB/SIMULINK. The SPWM method improves the efficiency and...
This book was conceived as a method of helping pharmacists and prescribers to 'bridge the gap' between the acquisition of theoretical knowledge about drugs and its practical application to individual...
longer released before a blocking operation during control method execution. This effectively makes the AML Interpreter single- threaded. The overhead of a semaphore per-method is eliminated. Fixed a...
Bridge Composite Decorator Facade Flyweight Private Class Data Proxy Behavioral patterns Chain of Responsibility Command Interpreter Iterator Mediator Memento Null Object Observer State Strategy ...
// An example method that takes a comma-separated list of numbers and returns the array rpc ExampleMethod ( TestMessage ) returns ( TestMessage ); } message MyRequest { string message = 1 ; } ...
This helps ensure the bridge functions properly during a long service life and guards against catastrophic failure under extreme events. Although these systems have achieved some success, this ...
"Specification by Example" is a comprehensive guide authored by Gojko Adzic, focusing on the collaborative method of specifying requirements and tests through realistic examples. This approach aims to...
NSString *result = @"Native method completed!"; callback(result); // 在这里调用JavaScript的回调函数 }); } ``` 4. **性能和安全**: 虽然JavaScriptCore提供了一种方便的互操作方式,但需要注意性能和...
A novel method of producing optimum switching functions for the voltage and harmonic control of DC-to-AC bridge inverters using neural networks is presented. Results obtained an experimental ...