0

In the generic section of 《Java Core Technology Volume 1》, the author mentions the decompilation results of the bridge method. However, the main test of jad, luyten and javap did not get the same results as the author. I want to know how to really prove the existence of the bridge method through the decompilation tool. My native language is not English. If the description is unclear, please forgive me. The relevant code and results are as follows:

I tried javap, jad and luyten these decompilation tools

public class Pair<T> {
    private T first;

    private T second;

    public Pair() {
    }

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

    public T getFirst() {
        return first;
    }

    public void setFirst(T first) {
        this.first = first;
    }

    public T getSecond() {
        return second;
    }

    public void setSecond(T second) {
        this.second = second;
    }
}
import java.time.LocalDate;

public class DateInterval extends Pair<LocalDate> {
}

I want to get the same result in the original book, I can see the decompilation result of the bridge method.decompile result by javap

rainbowecho
  • 145
  • 1
  • 1
  • 9
  • I don't see a `bridge` method. – Sweeper Aug 06 '19 at 04:20
  • Re: "I want to know how to really prove the existence of the bridge method through the decompilation tool": I don't quite agree with this premise -- I don't think the output of a decompilation tool really "proves" anything about the class-file, though it can be instructive. – ruakh Aug 06 '19 at 04:42
  • @Sweeper: See https://docs.oracle.com/javase/tutorial/java/generics/bridgeMethods.html. – ruakh Aug 06 '19 at 04:42
  • A bridge method is: https://stackoverflow.com/a/5007394/2924784 – orirab Aug 06 '19 at 04:53

3 Answers3

2

You need to override the method, e.g.:

public class DateInterval extends Pair<LocalDate> {
    @Override
    public void setFirst(LocalDate first) {
        super.setFirst(first);
    }
}

Ideone Demo

shmosel
  • 49,289
  • 6
  • 73
  • 138
  • It turned out to be the case, I did not understand that I can now get the desired result through the javap tool, thank you very much.But there has been a new problem. When I use javap to decompile the Pair class, there is still a type parameter T in the decompilation result.`public class top.rainbowecho.syntax.generic.Pair { private T first; private T second; public top.rainbowecho.syntax.generic.Pair(); public top.rainbowecho.syntax.generic.Pair(T, T); public T getFirst(); public void setFirst(T); public T getSecond(); public void setSecond(T); }` – rainbowecho Aug 06 '19 at 05:31
  • Why is that a problem? – shmosel Aug 06 '19 at 16:32
1

Bridge Methods are synthesized only when needed. Its prime purpose is to maintain the polymorphic character of child classes. In your example,

public class DateInterval extends Pair<LocalDate> {
}

Though you are extending the class Pair<> you are not actually overriding any behavior, to achieve the polymorphism. The bridges are created, so that after type erasure the overriden signatures are same. See here

This is the reason, when you added the overriden method as per shmosel's answer you saw the bridge method on decompilation.

if you don't have any overriden methods, then there is no need of a bridge synthesis. Hope this gives you an idea.

Kris
  • 8,680
  • 4
  • 39
  • 67
  • But the new problem is that after I use javap to decompile the superclass, there is still a type variable T in the decompiled result.[compile-Pair](http://image.rainbowecho.top/compile-Pair.png) – rainbowecho Aug 06 '19 at 06:10
  • Type erasure is applied on usage of type. Erasure will not be applied to parent unless you have a concrete type assignment. So parent class will not be changed, the place you used the parent behaviors will be changed with erasure & if needed the bridge methods. – Kris Aug 06 '19 at 06:18
0

Many tools will hide bridge methods by default since they are rarely of interest.

If you want to see what's really in a classfile, I'd recommend using the Krakatau disassembler. You could also try javap with the -p option.

Antimony
  • 37,781
  • 10
  • 100
  • 107
  • I have tried using the -p option of javap, but I still can't see the bridge method in the subclass. I will try the Krakatau you said. – rainbowecho Aug 06 '19 at 04:49