Skip to content

Instantly share code, notes, and snippets.

@mkarg
Last active August 29, 2015 14:23
Show Gist options
  • Save mkarg/0da7f7dce8d9025511bb to your computer and use it in GitHub Desktop.
Save mkarg/0da7f7dce8d9025511bb to your computer and use it in GitHub Desktop.
proxy7test
import java.time.Duration;
import java.util.Optional;
import java.lang.reflect.*;
public class Jre7Test {
public static interface I7 {
public void say(String greeting);
}
public static interface I8 extends I7 {
public Optional<Duration> optional(java.time.Duration duration);
public void say(String greeting);
}
public static class MyClass7 implements I7 {
public void say(String greeting) {
System.out.println("J7 says: " + greeting);
}
}
public static class MyClass8 extends MyClass7 implements I8 {
public Optional<Duration> optional(java.time.Duration duration) {
return Optional.of(duration);
}
public void say(String greeting) {
System.out.println("J8 says: " + greeting);
}
}
public static void main(String[] args) {
InvocationHandler handler = new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) {
try {
return method.invoke(new MyClass8(), args);
} catch (Exception e) {
return new RuntimeException(e);
}
}
};
I7 proxy = (I7) Proxy.newProxyInstance(Jre7Test.class.getClassLoader(), new Class[] { I7.class } , handler);
proxy.say("Hello from proxy");
proxy.getClass().getMethods();
}
}
"C:\Program Files\Java\jdk1.8.0_45\bin\javac.exe" -source 1.7 -target 1.7 Jre7Test.java
warning: [options] bootstrap class path not set in conjunction with -source 1.7
1 warning
"C:\Program Files\Java\jre7\bin\java.exe" Jre7Test
J7 says: Hello from proxy
@kjurka
Copy link

kjurka commented Jun 18, 2015

Isn't the problem that I7 and I8 both have the same name, something like java.sql.ResultSet? There aren't separate interfaces for different JDBC versions, so how do you deal with that?

@mkarg
Copy link
Author

mkarg commented Jun 18, 2015

I think we can stop the discussion at this point. As long as the use of Reflection API upon JDBC objects is a wanted feature, the idea of sharing one single implementation simply won't work, or it will get too complicated to make it work.

When I started this discussion, I only had the JDBC spec in mind as-is, without any use of Reflection API on the JDBC objects, as neither of our own use cases covers this (we de no use such tools).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment