title: java.lang.ClassCastException on Proxy creation
body: <p>My goal is to create an instance from a <code>class</code> that implements an <code>interface</code> and extends another <code>class</code>.</p>
  
  <p>...Entity annotation:</p>
  
  <pre><code>@Retention(RetentionPolicy.RUNTIME)
  @Target({ElementType.METHOD, ElementType.FIELD})
  public @interface Entity {
  
      String visibileName();
  
  }
  </code></pre>
  
  <p>...implements<code>IEventDesignDialog</code></p>
  
  <pre><code>      public class EventDesignDialog implements IEventDesignDialog{
  
          private String show;
          private String dateAndTimeDisplayFormat;
          private String eventType;
  
  
          @Entity(visibileName = "Show")
          public String getShow() {
              return this.show;
          }
  
          @Entity(visibileName = "Date And Time display format")
          public String getDateAndTimeDisplayFormat() {
              return this.dateAndTimeDisplayFormat;
          }
  
          @Entity(visibileName = "Event Type")
          public String getEventType() {
              System.out.println("get event type method invokde successfully");
              return this.eventType;
          }
  }
  </code></pre>
  
  <p><code>IEventDesignDialog</code> interface:</p>
  
  <pre><code>public interface IEventDesignDialog extends IPage{
  
      public String getShow();
  
      public String getDateAndTimeDisplayFormat();
  
      public String getEventType();
  
  
  }
  </code></pre>
  
  <p><code>IPage</code> interface:</p>
  
  <pre><code>public interface IPage {
  
  }
  </code></pre>
  
  <p>Dynamic proxy implementation:</p>
  
  <pre><code>public class IPageProxy implements InvocationHandler {
      private List&lt;Method&gt; entityMethods;
  
  
  
      private Class &lt;? extends IPage&gt; screenClazz;
  
      public IPageProxy(final Class &lt;? extends IPage&gt; screenClazz) {
          entityMethods = new ArrayList&lt;&gt;();
          getEntityAnnotatedMethods(screenClazz);
          // Accept the real implementation to be proxied
          this.screenClazz = screenClazz;
      }
  
  
      /**
       * create an page instance
       * @param type
       * @param
       * @return
       * @throws InstantiationException
       * @throws IllegalAccessException
       */
      public static IPage getInstance(final Class&lt;? extends IPage&gt; type)
              throws InstantiationException, IllegalAccessException {
  
          List&lt;Class&lt;?&gt;&gt; interfaces = new ArrayList&lt;&gt;();
          interfaces.addAll(Arrays.asList(type.getInterfaces()));
  
          return (IPage) Proxy.newProxyInstance(
                  type.getClassLoader(),
                  findInterfaces(type),
                  new IPageProxy(type)
               );
  
          /*return (IPage) Proxy.newProxyInstance(type.getClassLoader(),
                 interfaces.toArray(new Class&lt;?&gt;[interfaces.size()])
                  , new IPageProxy(type));*/
      }
  
  
      /**
       * get all methods that annotated with @Entity annotation
       * and add it for entityMethods array List
       * @param screenClazz
       */
      private void getEntityAnnotatedMethods(final Class &lt;? extends IPage&gt;  screenClazz) {
          // Scan each interface method for the specific annotation
          // and save each compatible method
          for (final Method m : screenClazz.getDeclaredMethods()) {
              if (m.isAnnotationPresent(Entity.class)) {
                  entityMethods.add(m);
              }
          }
      }
  
  
      static Class&lt;?&gt;[] findInterfaces(final Class&lt;? extends IPage&gt; type) {
          Class&lt;?&gt; current = type;
  
          do {
              final Class&lt;?&gt;[] interfaces = current.getInterfaces();
  
              if (interfaces.length != 0) {
                  return interfaces;
              }
          } while ((current = current.getSuperclass()) != Object.class);
  
          throw new UnsupportedOperationException("The type does not implement any interface");
      }
  
  
  
      @Override
      public Object invoke(
              final Object proxy,
              final Method method,
              final Object[] args) throws InvocationTargetException, IllegalAccessException {
          // A method on MyInterface has been called!
          // Check if we need to go call it directly or if we need to
          // execute something else before!
  
  
          if (entityMethods.contains(method)) {
              // The method exist in our to-be-proxied list
              // Execute something and the call it
              // ... some other things
              System.out.println("Something else");
          }
  
          // Invoke original method
          return method.invoke(screenClazz, args);
      }
  
  }
  </code></pre>
  
  <p>Main class:</p>
  
  <pre><code>public class Main {
  
      public static void main(String[] args) {
          try {
  
              ((EventDesignDialog)getInstance(EventDesignDialog.class)).getEventType();
          } catch (InstantiationException | IllegalAccessException e) {
              e.printStackTrace();
          }
      }
  
  
      @SuppressWarnings("unchecked")
      public static &lt;T extends IPage&gt; T getInstance(final Class&lt;? extends IPage&gt; type) throws InstantiationException, IllegalAccessException {
          return (T) IPageProxy.getInstance(type);
      }
  
  }
  </code></pre>
  
  <p>The following exception is thrown:</p>
  
  <pre><code>  Exception in thread "main" java.lang.ClassCastException: com.sun.proxy.$Proxy2 cannot be cast to abc.EventDesignDialog
      at abc.Main.main(Main.java:8)
  </code></pre>