Java synthetic method and bridge method confusion
NickName:Görkem Mülayim Ask DateTime:2017-12-04T17:50:56

Java synthetic method and bridge method confusion

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();?

Copyright Notice:Content Author:「Görkem Mülayim」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/47630572/java-synthetic-method-and-bridge-method-confusion

Answers
Eugene 2017-12-04T13:48:31

If you are simply looking for an example of such:\n\nFunction<String, Integer> func = s -> s.length();\n\nArrays.stream(DeleteMe.class.getDeclaredMethods())\n .peek(m -> System.out.println(m.getName() + \" isSynth : \" + m.isSynthetic() + \" isBridge : \" + m.isBridge()))\n .forEach(System.out::println);\n\n\nThere will be entries like:\n\nlambda$0 isSynth : true isBridge : false\nlambda$1 isSynth : true isBridge : false\n",


Evgeniy Dorofeev 2017-12-04T09:59:23

Bridge is synthetic but synthetic is not necessarily bridge. Example:\n\npublic class Test {\n\n public Test clone() {\n return null;\n }\n\n\nbytecode outline:\n\n // access flags 0x1041\n public synthetic bridge clone()Ljava/lang/Object; throws java/lang/CloneNotSupportedException \n L0\n...\n",


More about “Java synthetic method and bridge method confusion” related questions

Java synthetic method and bridge method confusion

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 re...

Show Detail

Why is this Java method reported as a bridge method?

Consider the following Java program: import java.lang.reflect.Method; public class BridgeTest { public static class ClassA { public void foo() { } } protected static c...

Show Detail

Writing Synthetic/Bridge method in java

I am writing an application which checks if the method is sythentic or bridge. For testing this application I have added various methods in my stub. But for none of the method this block is getting

Show Detail

Can a synthetic or bridge method be used to smooth an int -> double API change?

Java has special markers on methods called synthetic and bridge. JLS 13.1.7, "Any constructs introduced by a Java compiler that do not have a corresponding construct in the source code must be

Show Detail

Java Generics - Bridge method?

Something called the "bridge method" concept related to Java Generics made me stop at a point and think over it. Btw, I only know that it occurs at the bytecode level and is not available ...

Show Detail

Java Generics - Bridge method?

Something called the "bridge method" concept related to Java Generics made me stop at a point and think over it. Btw, I only know that it occurs at the bytecode level and is not available ...

Show Detail

Java Generics - Bridge method?

Something called the "bridge method" concept related to Java Generics made me stop at a point and think over it. Btw, I only know that it occurs at the bytecode level and is not available ...

Show Detail

SonarQube is unable to analyze file: bridge method not marked as synthetic

I've got this exception when analyzing a project with SonarQube (the project utilizes JavaFX, Spring, AOP with compile-time weaving). I'm using the maven plugin to run the analysis sonar:sonar but...

Show Detail

Is it a bridge method?

Why it is possible to compile class with two methods with equal signatures? "foo" methods have only different return types. But return type is not a part of method signature in java. Does java comp...

Show Detail

Java interface synthetic method generation while narrowing return type

I have 2 interfaces, and 2 return types. interface interfaceA { Publisher&lt;String&gt; doSomething(); } interface interfaceB extends interfaceA { Flow&lt;String&gt; doSomething(); } inter..

Show Detail