The java.lang.reflect.Method.isBridge() method is used to check whether a function is a Bridge Function or not. This method returns true if method object is a bridge method otherwise it returns false. Bridge Method: These are methods that create an intermediate layer between the source and the target functions. It is usually used as part of the type erasure process. It means that the bridge method is required as a type safe interface. For example, In the Example below, the compare() method taking Object as parameter behave as bridge method. It will cast the Object to String and call compare function which takes String as parameters. So compare (Object a, Object b) act bridge part in between the source (The method that calls compare()) and the target (compare(String, String)). Example:
public class compareValues implements Comparator { // target method public int compare(String a, String b) { } // bridge method public int compare(Object a, Object b) { return compare((String)a, (String)b); } }
Syntax:
public boolean isBridge()
Return Value: This method returns true if method object is a bridge method by JVM specifications, else it returns false. Below program illustrates isBridge() method of Method class: Program 1: Program to return all the Bridge Methods of BigInteger class. Explanation: In this Method at first BigInteger Class Object is created. After creating object, a list of Method Objects is created by calling getMethods() of class Object. Now the fetched Method list is iterated and checked for isBridge(). Hence we get Bridge Methods. At last print Bridge Method names.
Java
/* * Program Demonstrate isBridge() method * of Method Class. */ import java.lang.reflect.Method; import java.math.BigInteger; public class GFG { // create main method public static void main(String args[]) { try { // create BigInteger class object Class bigInt = BigInteger. class ; // get list of Method object Method[] methods = bigInt.getMethods(); System.out.println("Bridge Methods of BigInteger Class are"); // Loop through Methods list for (Method m : methods) { // check whether the method is Bridge Method or not if (m.isBridge()) { // Print Method name System.out.println("Method: " + m.getName()); } } } catch (Exception e) { // print Exception if any Exception occurs e.printStackTrace(); } } } |
Bridge Methods of BigInteger Class are Method: compareTo
Program 2: Check for a custom method isBridge() or not. Explanation: When a child class inherits the method from parent class then the inherited method act as Bridge Method. In this Code First, a Shape class is created containing draw method then a Rectangle class is created which extends the Shape class. In the main method, Method object for draw method of Rectangle class is created. Now it is checked whether it isBridge() Method or not. And finally print the result.
Java
// Program Demonstrate isBridge() method // of Method Class. // In this program a custom bridge method is created // and by use of isBridge(), checked for Bridge Method import java.lang.reflect.Method; public class GFG { // create class protected class Shape { public void draw() {} } // create a class which extends Shape class public class Rectangle extends Shape { } // create main method public static void main(String args[]) { try { // create class object for class // Rectangle and get method object Method m = Rectangle. class .getDeclaredMethod("draw"); // check method is bridge or not boolean isBridge = m.isBridge(); // print result System.out.println(m + " method is Bridge Method :" + isBridge); } catch (NoSuchMethodException | SecurityException e) { // Print Exception if any Exception occurs e.printStackTrace(); } } } |
public void GFG$Rectangle.draw() method is Bridge Method :true
Reference: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#isBridge– https://stackoverflow.com/questions/5007357/java-generics-bridge-method http://stas-blogspot.blogspot.com/2010/03/java-bridge-methods-explained.html