Reputation: 177
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
Upvotes: 0
Views: 252
Reputation: 8873
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.
Upvotes: 1
Reputation: 50756
You need to override the method, e.g.:
public class DateInterval extends Pair<LocalDate> {
@Override
public void setFirst(LocalDate first) {
super.setFirst(first);
}
}
Upvotes: 2
Reputation: 39461
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.
Upvotes: 0