fork download
  1. import java.lang.reflect.InvocationHandler;
  2. import java.lang.reflect.Method;
  3. import java.lang.reflect.Proxy;
  4.  
  5. public class Main {
  6.  
  7. interface Foo { }
  8.  
  9. interface Bar { }
  10.  
  11. static class FooBar1 implements Foo, Bar {
  12. public String toString() {
  13. return "FooBar1";
  14. }
  15. }
  16.  
  17. static class FooBar2 implements Foo, Bar {
  18. public String toString() {
  19. return "FooBar2";
  20. }
  21. }
  22.  
  23.  
  24.  
  25. static <T extends Foo & Bar> T getFooBar1() {
  26. return (T) new FooBar1();
  27. }
  28.  
  29. static <T extends Foo & Bar> T getFooBar2() {
  30. return (T) new FooBar2();
  31. }
  32.  
  33. static <T extends Foo & Bar> T getFooBar() {
  34. return (T) Proxy.newProxyInstance(
  35. Foo.class.getClassLoader(),
  36. new Class<?>[] {Foo.class, Bar.class},
  37. public Object invoke(Object proxy, Method method, Object[] args) {
  38. return "PROXY!!!";
  39. }
  40. }
  41. );
  42. }
  43.  
  44. static <U extends Foo & Bar> void show(U u) {
  45. System.out.println(u);
  46. }
  47.  
  48. public static void main(String[] args) {
  49. show(getFooBar());
  50. show(getFooBar1());
  51. show(getFooBar2());
  52. }
  53. }
Success #stdin #stdout 0.06s 215744KB
stdin
Standard input is empty
stdout
PROXY!!!
FooBar1
FooBar2