title: java.lang.ClassCastException: com.sun.proxy.$Proxy47 cannot be cast to
body: <p>I am running a test under Arquillian OSGi with this code:</p>
  
  <p>An annotation:</p>
  
  <pre><code>@Target(ElementType.FIELD)
  @Retention(RetentionPolicy.RUNTIME)
  public @interface Size {
      int min() default 0;
  
      int max() default 2147483647;
  }
  </code></pre>
  
  <p>This class</p>
  
  <pre><code>public class FooEntity {
      @Size(min = 0, max = 100)
      public int number;
  
      public int getNumber() {
          return number;
      }
  
      public void setNumber(int number) {
          this.number = number;
      }
  }
  </code></pre>
  
  <p>And a validation method somewhere else:</p>
  
  <pre><code>@Override
  public List&lt;String&gt; validateEntity(Object entity) throws IllegalAccessException {
      List&lt;String&gt; list = new ArrayList&lt;&gt;();
      for(Field field  : entity.getClass().getDeclaredFields()) {
          for (Annotation annotation : field.getDeclaredAnnotations()) {
              if (annotation.annotationType().getName().equals(Size.class.getName())) {
                  Size size = (Size) annotation; // --&gt; Throws 'ClassCastException' exception!!
                  int value = field.getInt(entity);
                  if (value &lt; size.min() || value &gt; size.max())
                      list.add("Error Message: Invalid size.");
              }
          }
      }
      return list;
  }
  </code></pre>
  
  <p>When I debug this part of the code, I place this same line in the 'watch' window of the IDE and it shows the code is correct, I mean, it returns <code>annotation</code> as <code>Size</code> type, but during run-time the code just breaks. Does anybody knows why is this cast not working? And why is this annotation working under a Proxy class?</p>
  
  <p>Here is the StackTrace of the exception:</p>
  
  <pre><code>java.lang.ClassCastException: com.sun.proxy.$Proxy47 cannot be cast to persistence.ext.annotation.Size
      at persistence.ext.internal.PersistenceImpl.validateEntity(PersistenceImpl.java:144)
      at integration.PersistenceExtTest.ValidateSizeAnnotation(PersistenceExtTest.java:84)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
      at org.jboss.arquillian.junit.Arquillian$8$1.invoke(Arquillian.java:370)
      at org.jboss.arquillian.container.test.impl.execution.LocalTestExecuter.execute(LocalTestExecuter.java:60)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
      at org.jboss.arquillian.core.impl.EventContextImpl.invokeObservers(EventContextImpl.java:99)
      at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:81)
      at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:145)
      at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:116)
      at org.jboss.arquillian.core.impl.EventImpl.fire(EventImpl.java:67)
      at org.jboss.arquillian.container.test.impl.execution.ContainerTestExecuter.execute(ContainerTestExecuter.java:38)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
      at org.jboss.arquillian.core.impl.EventContextImpl.invokeObservers(EventContextImpl.java:99)
      at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:81)
      at org.jboss.arquillian.test.impl.TestContextHandler.createClassContext(TestContextHandler.java:92)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
      at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88)
      at org.jboss.arquillian.test.impl.TestContextHandler.createTestContext(TestContextHandler.java:130)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
      at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88)
      at org.jboss.arquillian.test.impl.TestContextHandler.createSuiteContext(TestContextHandler.java:73)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
      at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88)
      at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:145)
      at org.jboss.arquillian.test.impl.EventTestRunnerAdaptor.test(EventTestRunnerAdaptor.java:136)
      at org.jboss.arquillian.junit.Arquillian$8.evaluate(Arquillian.java:363)
      at org.jboss.arquillian.junit.Arquillian$4.evaluate(Arquillian.java:245)
      at org.jboss.arquillian.junit.Arquillian.multiExecute(Arquillian.java:422)
      at org.jboss.arquillian.junit.Arquillian.access$200(Arquillian.java:54)
      at org.jboss.arquillian.junit.Arquillian$5.evaluate(Arquillian.java:259)
      at org.jboss.arquillian.junit.Arquillian$7$1.invoke(Arquillian.java:315)
      at org.jboss.arquillian.container.test.impl.execution.BeforeLifecycleEventExecuter.on(BeforeLifecycleEventExecuter.java:35)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
      at org.jboss.arquillian.core.impl.EventContextImpl.invokeObservers(EventContextImpl.java:99)
      at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:81)
      at org.jboss.arquillian.test.impl.TestContextHandler.createClassContext(TestContextHandler.java:92)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
      at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88)
      at org.jboss.arquillian.test.impl.TestContextHandler.createTestContext(TestContextHandler.java:130)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
      at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88)
      at org.jboss.arquillian.test.impl.TestContextHandler.createSuiteContext(TestContextHandler.java:73)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
      at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88)
      at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:145)
      at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:116)
      at org.jboss.arquillian.test.impl.EventTestRunnerAdaptor.fireCustomLifecycle(EventTestRunnerAdaptor.java:159)
      at org.jboss.arquillian.junit.Arquillian$7.evaluate(Arquillian.java:311)
      at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
      at org.jboss.arquillian.junit.Arquillian$2.evaluate(Arquillian.java:204)
      at org.jboss.arquillian.junit.Arquillian.multiExecute(Arquillian.java:422)
      at org.jboss.arquillian.junit.Arquillian.access$200(Arquillian.java:54)
      at org.jboss.arquillian.junit.Arquillian$3.evaluate(Arquillian.java:218)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
      at org.jboss.arquillian.junit.Arquillian.run(Arquillian.java:166)
      at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
      at org.junit.runner.JUnitCore.run(JUnitCore.java:138)
      at org.jboss.arquillian.junit.container.JUnitTestRunner.execute(JUnitTestRunner.java:66)
      at org.jboss.arquillian.osgi.JUnitBundleTestRunner.execute(JUnitBundleTestRunner.java:51)
      at org.jboss.arquillian.protocol.jmx.JMXTestRunner.doRunTestMethod(JMXTestRunner.java:180)
      at org.jboss.arquillian.protocol.jmx.JMXTestRunner.runTestMethodInternal(JMXTestRunner.java:162)
      at org.jboss.arquillian.protocol.jmx.JMXTestRunner.runTestMethod(JMXTestRunner.java:120)
      at org.jboss.arquillian.osgi.ArquillianBundleActivator$2.runTestMethod(ArquillianBundleActivator.java:111)
      at org.jboss.arquillian.protocol.jmx.JMXTestRunner.runTestMethod(JMXTestRunner.java:137)
      at org.jboss.arquillian.osgi.ArquillianBundleActivator$2.runTestMethod(ArquillianBundleActivator.java:124)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
      at com.sun.jmx.mbeanserver.StandardMBeanIntrospector.invokeM2(StandardMBeanIntrospector.java:112)
      at com.sun.jmx.mbeanserver.StandardMBeanIntrospector.invokeM2(StandardMBeanIntrospector.java:46)
      at com.sun.jmx.mbeanserver.MBeanIntrospector.invokeM(MBeanIntrospector.java:237)
      at com.sun.jmx.mbeanserver.PerInterface.invoke(PerInterface.java:138)
      at com.sun.jmx.mbeanserver.MBeanSupport.invoke(MBeanSupport.java:252)
      at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
      at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
      at javax.management.MBeanServerInvocationHandler.invoke(MBeanServerInvocationHandler.java:298)
      at com.sun.proxy.$Proxy27.runTestMethod(Unknown Source)
      at org.jboss.arquillian.protocol.jmx.JMXMethodExecutor.invoke(JMXMethodExecutor.java:80)
      at org.jboss.arquillian.container.test.impl.execution.RemoteTestExecuter.execute(RemoteTestExecuter.java:109)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
      at org.jboss.arquillian.core.impl.EventContextImpl.invokeObservers(EventContextImpl.java:99)
      at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:81)
      at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:145)
      at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:116)
      at org.jboss.arquillian.core.impl.EventImpl.fire(EventImpl.java:67)
      at org.jboss.arquillian.container.test.impl.execution.ClientTestExecuter.execute(ClientTestExecuter.java:57)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
      at org.jboss.arquillian.core.impl.EventContextImpl.invokeObservers(EventContextImpl.java:99)
      at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:81)
      at org.jboss.arquillian.container.test.impl.client.ContainerEventController.createContext(ContainerEventController.java:142)
      at org.jboss.arquillian.container.test.impl.client.ContainerEventController.createTestContext(ContainerEventController.java:129)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
      at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88)
      at org.jboss.arquillian.test.impl.TestContextHandler.createClassContext(TestContextHandler.java:92)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
      at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88)
      at org.jboss.arquillian.test.impl.TestContextHandler.createTestContext(TestContextHandler.java:130)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
      at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88)
      at org.jboss.arquillian.test.impl.TestContextHandler.createSuiteContext(TestContextHandler.java:73)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
      at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88)
      at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:145)
      at org.jboss.arquillian.test.impl.EventTestRunnerAdaptor.test(EventTestRunnerAdaptor.java:136)
      at org.jboss.arquillian.junit.Arquillian$8.evaluate(Arquillian.java:363)
      at org.jboss.arquillian.junit.Arquillian$4.evaluate(Arquillian.java:245)
      at org.jboss.arquillian.junit.Arquillian.multiExecute(Arquillian.java:422)
      at org.jboss.arquillian.junit.Arquillian.access$200(Arquillian.java:54)
      at org.jboss.arquillian.junit.Arquillian$5.evaluate(Arquillian.java:259)
      at org.jboss.arquillian.junit.Arquillian$7.evaluate(Arquillian.java:321)
      at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
      at org.jboss.arquillian.junit.Arquillian$2.evaluate(Arquillian.java:204)
      at org.jboss.arquillian.junit.Arquillian.multiExecute(Arquillian.java:422)
      at org.jboss.arquillian.junit.Arquillian.access$200(Arquillian.java:54)
      at org.jboss.arquillian.junit.Arquillian$3.evaluate(Arquillian.java:218)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
      at org.jboss.arquillian.junit.Arquillian.run(Arquillian.java:166)
      at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
      at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
      at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
      at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
  </code></pre>