diff --git a/README.md b/README.md index 545464c..acd1672 100644 --- a/README.md +++ b/README.md @@ -132,6 +132,9 @@ as you have to ensure that bridge methods are implemented on all the classes tha for example by adding `@WithBridgeMethods` on every implementation of the method in question, or by introducing a base class that provides a bridge method. +For adapter methods, the bridge method annotation on the interface does not need to declare the +interface, but the bridge method annotation on the interface does. + See the Javadoc for more details: - [`bridge-method-annotation`](https://javadoc.jenkins.io/component/bridge-method-annotation/) diff --git a/injector/src/test/client/Main.java b/injector/src/test/client/Main.java index 9ae26bf..9a492ab 100644 --- a/injector/src/test/client/Main.java +++ b/injector/src/test/client/Main.java @@ -35,9 +35,7 @@ public static void main(String[] args) throws Exception { // using reflection to ensure that JIT isn't doing inlining check((Foo)Foo.class.newInstance(),args[0]); - - // still a work in progress -// check((Bar)Bar.class.newInstance(),args[0]); + check((Bar)Bar.class.newInstance(),args[0]); Adapter a = new Adapter(); assertEquals(1,a.i()); @@ -72,5 +70,8 @@ private static void check(IBar f, String expected) { String n = f.widen(); assertEquals(expected,n); + + String u = f.adapter(); + assertEquals("http://example.com/", u); } } diff --git a/injector/src/test/v1/Bar.java b/injector/src/test/v1/Bar.java index 9b1eb50..80b38c6 100644 --- a/injector/src/test/v1/Bar.java +++ b/injector/src/test/v1/Bar.java @@ -1,4 +1,5 @@ public class Bar implements IBar { public String widen() { return "foo"; } public Object narrow() { return "foo"; } + public String adapter() { return "http://example.com/"; } } \ No newline at end of file diff --git a/injector/src/test/v1/IBar.java b/injector/src/test/v1/IBar.java index 7b206b9..714c2ad 100644 --- a/injector/src/test/v1/IBar.java +++ b/injector/src/test/v1/IBar.java @@ -1,4 +1,5 @@ public interface IBar { String widen(); Object narrow(); + String adapter(); } \ No newline at end of file diff --git a/injector/src/test/v2/Bar.java b/injector/src/test/v2/Bar.java index 855a49b..ce65fe2 100644 --- a/injector/src/test/v2/Bar.java +++ b/injector/src/test/v2/Bar.java @@ -1,4 +1,25 @@ +import com.infradna.tool.bridge_method_injector.WithBridgeMethods; +import java.io.UncheckedIOException; +import java.net.MalformedURLException; +import java.net.URL; + public class Bar implements IBar { + @WithBridgeMethods(value = String.class, castRequired = true) public Object widen() { return "bar"; } + + @WithBridgeMethods(Object.class) public String narrow() { return "bar"; } + + @WithBridgeMethods(value = String.class, adapterMethod = "convert") + public URL adapter() { + try { + return new URL("http://example.com/"); + } catch (MalformedURLException e) { + throw new UncheckedIOException(e); + } + } + + private Object convert(URL url, Class type) { + return url.toString(); + } } \ No newline at end of file diff --git a/injector/src/test/v2/IBar.java b/injector/src/test/v2/IBar.java index 7dbf665..783fb26 100644 --- a/injector/src/test/v2/IBar.java +++ b/injector/src/test/v2/IBar.java @@ -1,8 +1,11 @@ import com.infradna.tool.bridge_method_injector.WithBridgeMethods; +import java.net.URL; public interface IBar { @WithBridgeMethods(value=String.class,castRequired=true) Object widen(); @WithBridgeMethods(Object.class) String narrow(); + @WithBridgeMethods(String.class) + URL adapter(); } \ No newline at end of file