1

For some strange reason I am getting the following exception.

2011-02-08 09:53:30,667 ERROR [STDERR] (QuartzScheduler_Worker-9) java.lang.ClassCastException: org.javassist.tmp.java.lang.Object_$$_javassist_seam_7 cannot be cast to no.kommuneforlaget.fagsystem.saksapp.service.report.SystemlogListener
2011-02-08 09:53:30,668 ERROR [STDERR] (QuartzScheduler_Worker-9)       at no.kommuneforlaget.fagsystem.saksapp.jobs.ESBLogListener.checkESBEvents(ESBLogListener.java:71)
2011-02-08 09:53:30,668 ERROR [STDERR] (QuartzScheduler_Worker-9)       at no.kommuneforlaget.fagsystem.saksapp.jobs.ESBLogListener.scheduler(ESBLogListener.java:119)
2011-02-08 09:53:30,668 ERROR [STDERR] (QuartzScheduler_Worker-9)       at sun.reflect.GeneratedMethodAccessor466.invoke(Unknown Source)
2011-02-08 09:53:30,668 ERROR [STDERR] (QuartzScheduler_Worker-9)       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
2

I know it shouldn't be a class cast exception because its the correct class.

The code is:

SystemlogListener systemlogListener = (SystemlogListener) Component.getInstance("systemlogListener", ScopeType.SESSION, true);

And the class is

@Name("systemlogListener")
@Scope(ScopeType.SESSION)
@AutoCreate
@Stateful
@TransactionManagement(TransactionManagementType.CONTAINER) //default
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public class SystemlogListener implements SystemlogListenerLocal, Serializable {

Can anyone spot the mistake I have made?

Shervin Asgari
  • 23,901
  • 30
  • 103
  • 143

1 Answers1

2

I assume that Component.getInstance(...) returning an injected class. The Implementation that is injected does not have to be of the class of your implementation but only of your Local/Remote interface. So you could cast to your interface SystemlogListenerLocal but not to your implementation SystemlogListener.

The reason is the implementation of your AppServer which could use e.g. a proxy and delegates the methods to your real implementation.

cornz
  • 641
  • 4
  • 18
  • Yes of course. I have to use the interface all the way. I can't believe I forgot that :) – Shervin Asgari Feb 08 '11 at 10:22
  • Could you share the working code samle? I am getting same exception, I have a class and interface... But how should I cast myClass to get the instance... – efirat Jan 26 '16 at 09:52
  • I think it should be: myBeanInterface mb = (myBeanInterface) Component.getInstance("myBeanName", true); – efirat Jan 26 '16 at 10:20