DOCX

Java series the bridge on Java methods

By Dawn Hughes,2015-09-12 05:07
9 views 0
Java series the bridge on Java methods

    Java series: the bridge on Java methods

    It in two days to see "the Java core technology volume 1" generic related chapters, in

    which said to the generic subclasses override the parent class of the generic approach, the

    compiler will automatically generate a bridge approach, this is a little look not to understand.

    The book example code is as follows:

public class MyPair {

     private T first;

     private T second;

     public MyPair(){ first = null ; second = null ;}

     public MyPair(T first, T second){ this .first = first; this .second = second;}

     public T getFirst(){ return first;}

     public T getSecond() { return second;}

     public void setFirst(T value){ first = value;}

     public void setSecond(T value) { second = value;}

    }

public class DateInterval extends MyPair {

     public void setSecond(Date second) {

     if (second.compareTo(getFirst()) >= 0 )

     super .setSecond(second);

     }

    }

    public static void main(String[] args) throws InterruptedException {

     // TODO Auto-generated method stub

     DateInterval interval = new DateInterval();

     interval.setFirst( new Date());

     interval.setSecond( new Date());

     System.out.println( "second value of interval: " +

    interval.getSecond().toString());

     Thread.sleep( 10 );

     MyPair datePair = interval;

     datePair.setSecond( new Date());

     System.out.println( "second value of interval: " +

    datePair.getSecond().toString());

     }

    A, through the jd - GUI. Exe to analyze bytecode, can only see the type erasure

    Online access to some data still don't understand, then think should be ok by decompiling tools, then find the jd - GUI. Exe, find out what the decompiled out and original

    in the same class, as follows, about the book mentioned type erasure is real, you can see there

    is no generic in the bytecode, but after doing the type erasure type.

public static void main(String[] args)

     throws InterruptedException

     {

     DateInterval interval = new DateInterval();

     interval.setFirst( new Date());

     interval.setSecond( new Date());

     System.out.println( "second value of interval: " +

    ((Date)interval.getSecond()).toString());

     Thread.sleep( 10L );

     MyPair datePair = interval;

     datePair.setSecond( new Date());

     System.out.println( "second value of interval: " +

    ((Date)datePair.getSecond()).toString());

     }

    Second, see with jclasslib bytecode

    May be jd - GUI. Exe is too high, decompiled over do it, the results to filter out all the information I need, so I find a little more primitive decompiler, jclasslib, use it to open the DateInternal. Class files, you can see the following information.In the source above we really only give DateInternal added a setSecon method, but after decompiling found can see two setSecond method, two methods are as follows.

    1) the first is the setSecond we defined in the source code, the arguments for the Date type setSecond method;

    2) the second is the book of bridge method, can see the flag of this method, in addition to the public, and bridge, synthetic two symbols, this means that it is automatically generated by the compiler bridge method.

    3) in the see method content, in fact, the internal call DateInterval. SetSecond method, and in

    Three, you can also use javap commands to view the bytecode information

    At the command line, enter javap -c - v DateInternal. Class

    Can output the following information, see jclaslib see information similar to here.

Classfile

    /D:/java/eclipse/learnJava/target/classes/me/ygc/javabasic/learnJava/DateInterval.

    class

     Last modified 2015-12-1; size 771 bytes

     MD5 checksum f8d67b651cd0aa143e3fbe03c5edd519

     Compiled from "DateInterval.java"

    public class me.ygc.javabasic.learnJava.DateInterval extends me.ygc.javabasic.learnJava.MyPair

     minor version: 0

     major version: 49

     flags: ACC_PUBLIC, ACC_SUPER

    Constant pool:

     # 1 = Class #2 // me/ygc/javabasic/learnJava/DateInterval

     #2 = Utf8 me/ygc/javabasic/learnJava/ DateInterval

     # 3 = Class #4 // me/ygc/javabasic/learnJava/MyPair

     #4 = Utf8 me/ygc/javabasic/learnJava/ MyPair

     # 5 = Utf8

     # 6 = Utf8 ()V

     # 7 = Utf8 Code

     # 8 = Methodref #3.#9 //

    me/ygc/javabasic/learnJava/MyPair."":()V

     #9 = NameAndType #5:#6 // "":()V

     #10 = Utf8 LineNumberTable

     # 11 = Utf8 LocalVariableTable

     # 12 = Utf8 this

     # 13 = Utf8 Lme/ygc/javabasic/learnJava/ DateInterval;

     # 14 = Utf8 setSecond

     # 15 = Utf8 (Ljava/util/ Date;)V

     # 16 = Methodref #1.#17 //

    me/ygc/javabasic/learnJava/DateInterval.getFirst:()Ljava/lang/Object;

     #17 = NameAndType #18:#19 // getFirst:()Ljava/lang/Object;

     #18 = Utf8 getFirst

     # 19 = Utf8 ()Ljava/lang/ Object;

     # 20 = Class #21 // java/util/Date

     #21 = Utf8 java/util/ Date

     # 22 = Methodref #20.#23 //

    java/util/Date.compareTo:(Ljava/util/Date;)I

     #23 = NameAndType #24:#25 // compareTo:(Ljava/util/Date;)I

     #24 = Utf8 compareTo

     # 25 = Utf8 (Ljava/util/ Date;)I

     # 26 = Methodref #3.#27 //

    me/ygc/javabasic/learnJava/MyPair.setSecond:(Ljava/lang/Object;)V

     #27 = NameAndType #14:#28 // setSecond:(Ljava/lang/Object;)V

     #28 = Utf8 (Ljava/lang/ Object;)V

     # 29 = Utf8 second

     # 30 = Utf8 Ljava/util/ Date;

     # 31 = Methodref #1.#32 //

    me/ygc/javabasic/learnJava/DateInterval.setSecond:(Ljava/util/Date;)V

     #32 = NameAndType #14:#15 // setSecond:(Ljava/util/Date;)V

     #33 = Utf8 SourceFile

     # 34 = Utf8 DateInterval.java

     # 35 = Utf8 Signature

     # 36 = Utf8 Lme/ygc/javabasic/learnJava/MyPair ;

    {

     public me.ygc.javabasic.learnJava.DateInterval();

     descriptor: ()V

     flags: ACC_PUBLIC

     Code:

     stack =1, locals=1, args_size=1

     0 : aload_0

     1: invokespecial #8 // Method me/ygc/javabasic/learnJava/MyPair."":()V

     4: return

     LineNumberTable:

     line 6: 0

     LocalVariableTable:

     Start Length Slot Name Signature

     0 5 0 this Lme/ygc/javabasic/learnJava/ DateInterval;

     public void setSecond(java.util.Date);

     descriptor: (Ljava /util/ Date;)V

     flags: ACC_PUBLIC

     Code:

     stack =2, locals=2, args_size=2

     0 : aload_1

     1 : aload_0

     2: invokevirtual #16 // Method getFirst:()Ljava/lang/Object;

     5: checkcast #20 // class java/util/Date

     8: invokevirtual #22 // Method java/util/Date.compareTo:(Ljava/util/Date;)I

     11: iflt 19

     14 : aload_0

     15 : aload_1

     16: invokespecial #26 // Method me/ygc/javabasic/learnJava/MyPair.setSecond:(Ljava/lang/Object;)V

     19: return

     LineNumberTable:

     line 8: 0

     line 9: 14

     line 10: 19

     LocalVariableTable:

     Start Length Slot Name Signature

     0 20 0 this Lme/ygc/javabasic/learnJava/ DateInterval;

     0 20 1 second Ljava/util/ Date;

     public void setSecond(java.lang.Object);

     descriptor: (Ljava /lang/ Object;)V

     flags: ACC_PUBLIC, ACC_BRIDGE, ACC_SYNTHETIC

     Code:

     stack =2, locals=2, args_size=2

     0 : aload_0

     1 : aload_1

     2: checkcast #20 // class java/util/Date

     5: invokevirtual #31 // Method setSecond:(Ljava/util/Date;)V

     8: return

     LineNumberTable:

     line 1: 0

     LocalVariableTable:

     Start Length Slot Name Signature

    }

    SourceFile: "DateInterval.java"

    Signature: # 36 //

    Lme/ygc/javabasic/learnJava/MyPair;

    Fourth, through code to verify the existence of the bridge method

    If you write the following code:

    public static void main(String[] args) throws InterruptedException {

     // TODO Auto-generated method stub

     MyPair datePair = new DateInterval();

     datePair.setSecond( new Object());

     }

    Run after you will be prompted as follows:

    Exception in thread "main" java.lang.ClassCastException: java.lang.Object cannot be cast to java.util.Date

    at me.ygc.javabasic.learnJava.DateInterval.setSecond(DateInterval.java:1)

    at me.ygc.javabasic.learnJava.MyPair.main(MyPair.java:26)

    Shows that he is actually to invoke a setSecond (Object) method, and then did from Object to Date in the internal transformation, and transformation failed.

Report this document

For any questions or suggestions please email
cust-service@docsford.com