美文网首页
Java Bridge Method

Java Bridge Method

作者: 全都是泡沫啦 | 来源:发表于2019-03-07 14:35 被阅读0次

    1.java桥接方法

    import java.lang.reflect.Method;
    import java.util.Arrays;
    
    public class TestBridgeMethod {
        public static class Warehouse<T>{
            public T getValue() {
                return value;
            }
            public void setValue(T value) {
                this.value = value;
            }
            private T value;
        }
    
        public static class WarehouseInteger extends Warehouse<Integer>{
            @Override
            public void setValue(Integer value) {
                super.setValue(value);
            }
        }
    
        public static void main(String[] args) throws InterruptedException {
            Method[] declaredMethods = WarehouseInteger.class.getDeclaredMethods();
            Arrays.asList(declaredMethods).forEach((value)->{
                System.out.println(value);
            });
            Thread.sleep(Integer.MAX_VALUE);
        }
    }
    
    结果显示:
    public void TestBridgeMethod$WarehouseInteger.setValue(java.lang.Integer)
    public void TestBridgeMethod$WarehouseInteger.setValue(java.lang.Object)
    

    使用javap -c 查看class文件:

    Compiled from "TestBridgeMethod.java"
    public class TestBridgeMethod$Warehouse<T> {
      public TestBridgeMethod$Warehouse();
        Code:
           0: aload_0
           1: invokespecial #1                  // Method java/lang/Object."<init>":()V
           4: return
    
      public T getValue();
        Code:
           0: aload_0
           1: getfield      #2                  // Field value:Ljava/lang/Object;
           4: areturn
    
      public void setValue(T);
        Code:
           0: aload_0
           1: aload_1
           2: putfield      #2                  // Field value:Ljava/lang/Object;
           5: return
    }
    
    
    Compiled from "TestBridgeMethod.java"
    public class TestBridgeMethod$WarehouseInteger extends TestBridgeMethod$Warehouse<java.lang.Integer> {
      public TestBridgeMethod$WarehouseInteger();
        Code:
           0: aload_0
           1: invokespecial #1                  // Method TestBridgeMethod$Warehouse."<init>":()V
           4: return
    
      public void setValue(java.lang.Integer);
        Code:
           0: aload_0
           1: aload_1
           2: invokespecial #2                  // Method TestBridgeMethod$Warehouse.setValue:(Ljava/lang/Object;)V
           5: return
    
      public void setValue(java.lang.Object);
        Code:
           0: aload_0
           1: aload_1
           2: checkcast     #3                  // class java/lang/Integer
           5: invokevirtual #4                  // Method setValue:(Ljava/lang/Integer;)V
           8: return
    }
    

    在TestBridgeMethod$WarehouseInteger类中setValue:(Ljava/lang/Object;)V中调用了本类的setValue:(Ljava/lang/Integer;)V,该方法为编译器帮我们生成的桥接方法

    相关文章

      网友评论

          本文标题:Java Bridge Method

          本文链接:https://www.haomeiwen.com/subject/dnycpqtx.html