Advertisement
Guest User

Untitled

a guest
Jan 24th, 2013
547
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.89 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement