class A { } class B extends A { } public class Test { public void call(A a) { System. out .println( "A" ); } public void call(B b) { System. out .println( "B" ); } public static void main( String [] argv) { A b = new B(); Test test = new Test(); test.call(b); } } * This source code was highlighted with Source Code Highlighter .
class A { } class B extends A { } public class Test { public void call(A a) { System. out .println( "A" ); } public void call(B b) { System. out .println( "B" ); } public static void main( String [] argv) { A b = new B(); Test test = new Test(); test.call(b); } } * This source code was highlighted with Source Code Highlighter .
class A { } class B extends A { } public class Test { public void call(A a) { System. out .println( "A" ); } public void call(B b) { System. out .println( "B" ); } public static void main( String [] argv) { A b = new B(); Test test = new Test(); test.call(b); } } * This source code was highlighted with Source Code Highlighter .
class A { } class B extends A { } public class Test { public void call(A a) { System. out .println( "A" ); } public void call(B b) { System. out .println( "B" ); } public static void main( String [] argv) { A b = new B(); Test test = new Test(); test.call(b); } } * This source code was highlighted with Source Code Highlighter .
class A { } class B extends A { } public class Test { public void call(A a) { System. out .println( "A" ); } public void call(B b) { System. out .println( "B" ); } public static void main( String [] argv) { A b = new B(); Test test = new Test(); test.call(b); } } * This source code was highlighted with Source Code Highlighter .
class A { } class B extends A { } public class Test { public void call(A a) { System. out .println( "A" ); } public void call(B b) { System. out .println( "B" ); } public static void main( String [] argv) { A b = new B(); Test test = new Test(); test.call(b); } } * This source code was highlighted with Source Code Highlighter .
class A { } class B extends A { } public class Test { public void call(A a) { System. out .println( "A" ); } public void call(B b) { System. out .println( "B" ); } public static void main( String [] argv) { A b = new B(); Test test = new Test(); test.call(b); } } * This source code was highlighted with Source Code Highlighter .
class A { } class B extends A { } public class Test { public void call(A a) { System. out .println( "A" ); } public void call(B b) { System. out .println( "B" ); } public static void main( String [] argv) { A b = new B(); Test test = new Test(); test.call(b); } } * This source code was highlighted with Source Code Highlighter .
class A { } class B extends A { } public class Test { public void call(A a) { System. out .println( "A" ); } public void call(B b) { System. out .println( "B" ); } public static void main( String [] argv) { A b = new B(); Test test = new Test(); test.call(b); } } * This source code was highlighted with Source Code Highlighter .
class A { } class B extends A { } public class Test { public void call(A a) { System. out .println( "A" ); } public void call(B b) { System. out .println( "B" ); } public static void main( String [] argv) { A b = new B(); Test test = new Test(); test.call(b); } } * This source code was highlighted with Source Code Highlighter .
class A { } class B extends A { } public class Test { public void call(A a) { System. out .println( "A" ); } public void call(B b) { System. out .println( "B" ); } public static void main( String [] argv) { A b = new B(); Test test = new Test(); test.call(b); } } * This source code was highlighted with Source Code Highlighter .
class A { } class B extends A { } public class Test { public void call(A a) { System. out .println( "A" ); } public void call(B b) { System. out .println( "B" ); } public static void main( String [] argv) { A b = new B(); Test test = new Test(); test.call(b); } } * This source code was highlighted with Source Code Highlighter .
class A { } class B extends A { } public class Test { public void call(A a) { System. out .println( "A" ); } public void call(B b) { System. out .println( "B" ); } public static void main( String [] argv) { A b = new B(); Test test = new Test(); test.call(b); } } * This source code was highlighted with Source Code Highlighter .
class A { } class B extends A { } public class Test { public void call(A a) { System. out .println( "A" ); } public void call(B b) { System. out .println( "B" ); } public static void main( String [] argv) { A b = new B(); Test test = new Test(); test.call(b); } } * This source code was highlighted with Source Code Highlighter .
class A { } class B extends A { } public class Test { public void call(A a) { System. out .println( "A" ); } public void call(B b) { System. out .println( "B" ); } public static void main( String [] argv) { A b = new B(); Test test = new Test(); test.call(b); } } * This source code was highlighted with Source Code Highlighter .
class A { } class B extends A { } public class Test { public void call(A a) { System. out .println( "A" ); } public void call(B b) { System. out .println( "B" ); } public static void main( String [] argv) { A b = new B(); Test test = new Test(); test.call(b); } } * This source code was highlighted with Source Code Highlighter .
class A { } class B extends A { } public class Test { public void call(A a) { System. out .println( "A" ); } public void call(B b) { System. out .println( "B" ); } public static void main( String [] argv) { A b = new B(); Test test = new Test(); test.call(b); } } * This source code was highlighted with Source Code Highlighter .
class A { } class B extends A { } public class Test { public void call(A a) { System. out .println( "A" ); } public void call(B b) { System. out .println( "B" ); } public static void main( String [] argv) { A b = new B(); Test test = new Test(); test.call(b); } } * This source code was highlighted with Source Code Highlighter .
class A { } class B extends A { } public class Test { public void call(A a) { System. out .println( "A" ); } public void call(B b) { System. out .println( "B" ); } public static void main( String [] argv) { A b = new B(); Test test = new Test(); test.call(b); } } * This source code was highlighted with Source Code Highlighter .
public void call(A a)
method will be called, and the “A” is displayed accordingly, which may seem strange because the actual type of object is B
In order to call the method we need, we need to rewrite the code for calling the method like this:* This source code was highlighted with Source Code Highlighter .
- if (b instanceof B) {
- test.call ((B) b);
- }
java.lang.invoke
package will help us:MethodHandle
is a kind of a pointer to a method, this thing will be widely used in closures and lambda expressions (to be honest, I thought everything would be done there in the simplest way - lambdas will be converted to anonymous classes and this will be the end of the story).MethodType
- represents the signature of the method, namely the return value and the list of input parameters.MethodHandles
is a class of various utilities for working with methods.Lookup
- the class is part of the MethodHandles
class and provides an interface for searching for methods with specified parameters.* This source code was highlighted with Source Code Highlighter .
- import static java.lang.invoke.MethodHandles. *;
- import java.lang.invoke.MethodHandle;
- import java.lang.invoke.MethodType;
- class A {
- }
- class B extends A {
- }
- public class Test {
- public void call (A a) {
- System. out .println ( "A" );
- }
- public void call (b b) {
- System. out .println ( "B" );
- }
- public static void main ( String [] argv) {
- A b = new B ();
- Test test = new Test ();
- try {
- Lookup lookup = lookup ();
- MethodType mt = MethodType.methodType ( void . Class , b.getClass ());
- MethodHandle mh = lookup.findVirtual (Test. Class , "call" , mt);
- mh.invokeWithArguments (test, b);
- } catch (Throwable e) {
- e.printStackTrace ();
- }
- }
- }
methodType
method, then the types of the parameter list of the desired method. Line 30 - we get in MethodHandle
the search results for methods in the Test
class, with the name call
and the return value and mt
parameter types. In line 31, we call the found method on the test
object with the b
parameter and see the expected “B” on the screen.Lookup
class, there are still quite a few methods, for example, to search for static functions, but the reader remains to study these features as homework.Source: https://habr.com/ru/post/125274/
All Articles