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. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:50: type parameters of <T>T cannot be determined; no unique maximal instance exists for type variable T with upper bounds java.lang.Object,Main.Foo,Main.Bar
        show(getFooBar());
                      ^
Main.java:51: type parameters of <T>T cannot be determined; no unique maximal instance exists for type variable T with upper bounds java.lang.Object,Main.Foo,Main.Bar
        show(getFooBar1());
                       ^
Main.java:52: type parameters of <T>T cannot be determined; no unique maximal instance exists for type variable T with upper bounds java.lang.Object,Main.Foo,Main.Bar
        show(getFooBar2());
                       ^
Note: Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
3 errors
stdout
Standard output is empty