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
import java.lang.reflect.Method;
import java.math.BigInteger;
public class GFG {
public static void main(String args[])
{
try {
Class bigInt = BigInteger. class ;
Method[] methods = bigInt.getMethods();
System.out.println("Bridge Methods of BigInteger Class are");
for (Method m : methods) {
if (m.isBridge()) {
System.out.println("Method: " + m.getName());
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
|
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.
Java
import java.lang.reflect.Method;
public class GFG {
protected class Shape {
public void draw() {}
}
public class Rectangle extends Shape {
}
public static void main(String args[])
{
try {
Method m = Rectangle. class .getDeclaredMethod("draw");
boolean isBridge = m.isBridge();
System.out.println(m + " method is Bridge Method :"
+ isBridge);
}
catch (NoSuchMethodException | SecurityException e) {
e.printStackTrace();
}
}
}
|
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
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
06 Apr, 2023
Like Article
Save Article