1 Reply Latest reply on Nov 3, 2004 1:42 PM by Bill Burke

    ClassCastException in caller side advices

    Szczepan Kuzniarz Newbie

      Hi,

      I have the following aspect class:

      public class CounterAspect {
       private int counter;
       ... some other fields ...
      
       public Object start( Invocation invocation ) throws Throwable {
       counter = 0;
       return invocation.invokeNext();
       }
      
       public Object count( Invocation invocation ) throws Throwable {
       counter += 1;
       ... some log4j stuff ...
       return invocation.invokeNext();
       }
      }


      and I try to advice IndexWriter class form Jakarta Lucene:

      <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
      <aop>
       <pointcut
       name="IndexWriter.start"
       expr="call(org.apache.lucene.index.IndexWriter->new(..))"/>
      
       <pointcut
       name="IndexWriter.count"
       expr="call(* org.apache.lucene.index.IndexWriter->addDocument(..))"/>
      
       <aspect
       class="CounterAspect"
       scope="PER_INSTANCE"/>
      
       <bind pointcut="IndexWriter.start">
       <advice name="start"
       aspect="CounterAspect"/>
       </bind>
      
       <bind pointcut="IndexWriter.count">
       <advice name="count"
       aspect="CounterAspect"/>
       </bind>
      
      </aop>


      When one of the IndexWriter.addDocument(..) methods is called I get an exception:

      Caused by: java.lang.ClassCastException: org.apache.lucene.index.IndexWriter
       at org.jboss.aop.advice.PerInstanceAdvice.invoke(PerInstanceAdvice.java:46)
       ...


      As I can see in the JBossAOP source, it is caused by casting target object (IndexWriter) to Advised type. Why JBossAOP assumes target object implements this interface (but doesn't modify original class)? It is _caller_ side pointcut, so I think only caller side should be affected.


      Best ragards,

      Szczepan Kuzniarz

        • 1. ClassCastException in caller side advices
          Bill Burke Master

          Hi,

          I have the following aspect class:

          public class CounterAspect {
           private int counter;
           ... some other fields ...
          
           public Object start( Invocation invocation ) throws Throwable {
           counter = 0;
           return invocation.invokeNext();
           }
          
           public Object count( Invocation invocation ) throws Throwable {
           counter += 1;
           ... some log4j stuff ...
           return invocation.invokeNext();
           }
          }


          and I try to advice IndexWriter class form Jakarta Lucene:

          <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
          <aop>
           <pointcut
           name="IndexWriter.start"
           expr="call(org.apache.lucene.index.IndexWriter->new(..))"/>
          
           <pointcut
           name="IndexWriter.count"
           expr="call(* org.apache.lucene.index.IndexWriter->addDocument(..))"/>
          
           <aspect
           class="CounterAspect"
           scope="PER_INSTANCE"/>
          
           <bind pointcut="IndexWriter.start">
           <advice name="start"
           aspect="CounterAspect"/>
           </bind>
          
           <bind pointcut="IndexWriter.count">
           <advice name="count"
           aspect="CounterAspect"/>
           </bind>
          
          </aop>


          When one of the IndexWriter.addDocument(..) methods is called I get an exception:

          Caused by: java.lang.ClassCastException: org.apache.lucene.index.IndexWriter
           at org.jboss.aop.advice.PerInstanceAdvice.invoke(PerInstanceAdvice.java:46)
           ...


          As I can see in the JBossAOP source, it is caused by casting target object (IndexWriter) to Advised type. Why JBossAOP assumes target object implements this interface (but doesn't modify original class)? It is _caller_ side pointcut, so I think only caller side should be affected.


          Best ragards,

          Szczepan Kuzniarz