Writing a synthesis / bridge method in java

I am writing an application that checks if a method is a valid or a bridge. To test this application, I added various methods to my stub. But for none of the methods this block is not considered in the test example. Stub contains methods like validate (Object o), etc., Like any other normal java class.

Which method should I add to my stub so this line is covered?

code:

Method[] methods = inputClass.getMethods(); for (Method method : methods) { if (method.isSynthetic() || method.isBridge()) { isInternal = true; } // More code. } 
+4
source share
1 answer

Bridge methods in Java are synthetic methods required to implement some of the Java language features. The most famous examples are the covariant return type and the case in generics, when erasing the arguments of the base method is different from the actual method being called.

 import java.lang.reflect.*; /** * * @author Administrator */ class SampleTwo { public static class A<T> { public T getT(T args) { return args; } } static class B extends A<String> { public String getT(String args) { return args; } } } public class BridgeTEst { public static void main(String[] args) { test(SampleTwo.B.class); } public static boolean test(Class c) { Method[] methods = c.getMethods(); for (Method method : methods) { if (method.isSynthetic() || method.isBridge()) { System.out.println("Method Name = "+method.getName()); System.out.println("Method isBridge = "+method.isBridge()); System.out.println("Method isSynthetic = "+method.isSynthetic()); return true; } // More code. } return false; } } 


see also

+2
source

All Articles