Skip to content

Commit

Permalink
Add tests for interfaces (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
basil committed Aug 3, 2023
1 parent 3a001a5 commit 195398c
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand Down
7 changes: 4 additions & 3 deletions injector/src/test/client/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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);
}
}
1 change: 1 addition & 0 deletions injector/src/test/v1/Bar.java
Original file line number Diff line number Diff line change
@@ -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/"; }
}
1 change: 1 addition & 0 deletions injector/src/test/v1/IBar.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
public interface IBar {
String widen();
Object narrow();
String adapter();
}
21 changes: 21 additions & 0 deletions injector/src/test/v2/Bar.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
3 changes: 3 additions & 0 deletions injector/src/test/v2/IBar.java
Original file line number Diff line number Diff line change
@@ -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();
}

0 comments on commit 195398c

Please sign in to comment.