Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jan 24th, 2013  |  syntax: Java  |  size: 0.89 KB  |  views: 28  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.lang.reflect.*;
  2.  
  3. interface Foo {}
  4. interface Bar {}
  5.  
  6. class FooBar1 implements Foo, Bar {public String toString() { return "FooBar1"; }}
  7. class FooBar2 implements Foo, Bar {public String toString() { return "FooBar2"; }}
  8.  
  9. class FooBar {
  10.         static <T extends Foo & Bar> T getFooBar1() { return (T) new FooBar1(); }
  11.         static <T extends Foo & Bar> T getFooBar2() { return (T) new FooBar2(); }
  12.         static <T extends Foo & Bar> T getFooBar() {
  13.                 return (T)
  14.                 Proxy.newProxyInstance(
  15.                         Foo.class.getClassLoader(),
  16.                         new Class[] { Foo.class, Bar.class },
  17.                         new InvocationHandler() {
  18.                             public Object invoke(Object proxy, Method method, Object[] args) {
  19.                                         return "PROXY!!!";}});
  20.         }
  21.  
  22.         static <U extends Foo & Bar> void show(U u) { System.out.println(u); }
  23.  
  24.         public static void main(String[] args) {
  25.                 show(getFooBar());             
  26.                 show(getFooBar1());
  27.                 show(getFooBar2());
  28.         }
  29.  
  30. }