2

As stated in this Java Tutorial a bridge method implies that it is also a synthetic method. The question is, is it possible that invocation of isSynthetic method returns true but isBridge method returns false for a Method object, i.e., if a method is synthetic does it imply that it is also a bridge method?

It's not exactly the same but the source code for isSynthetic and isBridge methods looks like below:

static final int SYNTHETIC = 0x00001000;
public boolean isSynthetic() {
    return (getModifiers() & SYNTHETIC) != 0;
}

static final int BRIDGE = 0x00000040;
public boolean isBridge() {
    return (getModifiers() & BRIDGE) != 0;
}

Why isBridge method body is not like return isSynthetic();?

Görkem Mülayim
  • 1,139
  • 1
  • 12
  • 22
  • These methods are just returning the value of the corresponding modifier; the class is probably just a data holder. Crucial is the logic of how the modifiers are set. – user85421 Dec 04 '17 at 10:35
  • I was thinking the same. The modifiers are set through constructor. But I can not find a usage for it. – Görkem Mülayim Dec 04 '17 at 11:43
  • Related question for reference: https://stackoverflow.com/questions/6557586/java-generics-bridge-method – Happy Oct 23 '19 at 05:15

3 Answers3

2

If you are simply looking for an example of such:

Function<String, Integer> func = s -> s.length();

Arrays.stream(DeleteMe.class.getDeclaredMethods())
     .peek(m -> System.out.println(m.getName() + " isSynth : " + m.isSynthetic() + " isBridge : " + m.isBridge()))
     .forEach(System.out::println);

There will be entries like:

lambda$0 isSynth : true isBridge : false
lambda$1 isSynth : true isBridge : false
Eugene
  • 117,005
  • 15
  • 201
  • 306
1

Bridge is synthetic but synthetic is not necessarily bridge. Example:

public class Test {

    public Test clone() {
        return null;
    }

bytecode outline:

  // access flags 0x1041
  public synthetic bridge clone()Ljava/lang/Object; throws java/lang/CloneNotSupportedException 
   L0
...
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • I get two clone methods by invoking getDeclaredMethods() on your Test class. One of them is both synthetic and bridge. – Görkem Mülayim Dec 04 '17 at 10:15
  • clone() with Object return type is bridge method created by compiler to override Object.clone(). All implicit methods created by compiler are synthetic, but they are created for different purposes. – Evgeniy Dorofeev Dec 04 '17 at 10:34
  • 1
    I know what is synthetic, I just wanted to point out that your answer need a clarification. The one you gave is not enough to clarify your conclusion. – Görkem Mülayim Dec 04 '17 at 10:54
0

is it possible that invocation of isSynthetic method returns true but isBridge method returns false for a Method object

Yes, it is possible.

For example, the method AbstractPipeline.lambda$spliterator$0() is synthetic but is it NOT bridge.

According to the JVM Spec:

  • The ACC_SYNTHETIC flag indicates that this method was generated by a compiler and does not appear in source code
  • The ACC_BRIDGE flag is used to indicate a bridge method generated by a compiler for the Java programming language.

So,

  • A bridge method is 100% sure it is synthetic
  • A synthetic method is not necessary bridge

More on bridge method

Based on this article the bridge method is generated by Java Compiler for type erasure purpose of Java Generics.

Happy
  • 757
  • 9
  • 18