5

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 covered in the test case. Stub contains methods like validate(Object o) ,etc its just like any other normal java class.

What kind of method should I add in my stub so this line will get covered?

code :

     Method[] methods = inputClass.getMethods();
        for (Method method : methods) {

        if (method.isSynthetic() || method.isBridge()) {
            isInternal = true;
        }
       // More code.
     }
mmmmmm
  • 32,227
  • 27
  • 88
  • 117
java_enthu
  • 2,279
  • 7
  • 44
  • 74
  • Related question can help to answer your question: https://stackoverflow.com/questions/5007357/java-generics-bridge-method/58515681#58515681 – Happy Oct 23 '19 at 05:20

2 Answers2

4

Bridge methods in Java are synthetic methods, which are necessary to implement some of Java language features. The best known samples are covariant return type and a case in generics when erasure of base method's arguments differs from the actual method being invoked.

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

Community
  • 1
  • 1
jmj
  • 237,923
  • 42
  • 401
  • 438
  • 2
    I think it looks like its for co-veriant return types. When super class method returns Object and sub class overrides with co-varient type (say for ex. String) this also returns true for bridge and synthetic. Code I have written but I am not able to post in this comment space. – java_enthu Jul 20 '11 at 11:17
0

Here we list example Java methods in JDK is tagged with ACC_BRIDGE and/or ACC_SYNTHETIC, so they can be used via reflection to cover your test case easily:

Good luck!

Happy
  • 757
  • 9
  • 18