Method Class | isBridge() Method in Java

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

/*
* 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 is any Exception occurs
            e.printStackTrace();
        }
    }
}

chevron_right


Output:

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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();
        }
    }
}

chevron_right


Output:

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



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.