4 Replies Latest reply on Sep 1, 2003 5:36 AM by Adrian Brock

    How to apply aspects to javax.* classes

    analogue Newbie

      I simply trying to apply an method level advise to javax.swing.JLabel and it seems not to work simply for the reason that it is a system class. Is this possible?

      This is a modified docs/oreilly-aop/example1 from the source distrubution:

      ===================================
      public class POJO
      {
      public POJO() {}

      public void helloWorld() { System.out.println("Hello World!"); }

      public static void main(String[] args)
      {
      POJO pojo = new POJO();
      pojo.helloWorld();

      new JLabel("dfdf").setText("hello");
      }
      }
      ==================================
      public class TracingInterceptor implements Interceptor
      {
      public String getName() { return "TracingInterceptor"; }

      public InvocationResponse invoke(Invocation invocation) throws Throwable
      {
      String message = null;
      if (invocation.getType() == InvocationType.METHOD)
      {
      MethodInvocation mi = (MethodInvocation)invocation;

      message = "method: " + mi.method.getName();
      }
      else if (invocation.getType() == InvocationType.CONSTRUCTOR)
      {
      ConstructorInvocation ci = (ConstructorInvocation)invocation;
      message = "constructor: " + ci.constructor.getName();
      }
      else
      {
      // Do nothing for fields. Just too verbose
      return invocation.invokeNext();
      }
      System.out.println("Entering " + message);
      InvocationResponse rsp = invocation.invokeNext();
      System.out.println("Leaving " + message);
      return rsp;
      }
      }
      ==================================

        • 1. Re: How to apply aspects to javax.* classes
          analogue Newbie

          Ooops..posted too soon. Heres the rest of the message:

          ====================================
          <?xml version="1.0" encoding="UTF-8"?>

          <interceptor-pointcut class="POJO">



          </interceptor-pointcut>

          <interceptor-pointcut class="javax.swing.JLabel">



          </interceptor-pointcut>


          ==================================

          No signs of JLabel invocations in the output:

          Entering method: main
          Entering constructor: POJO
          Leaving constructor: POJO
          Entering method: helloWorld
          Hello World!
          Leaving method: helloWorld
          Leaving method: main

          Any thoughts?

          • 2. Re: How to apply aspects to javax.* classes
            Adeyemi E . Adegbile Newbie

            Hi,
            First, the design of the Jboss AOP Framework will not allow the instrumentation of the classes from the following packages that start with
            java.
            javax.
            sun.
            com.sun.
            org.apache.xerces.
            org.xml.sax.
            org.w3c.dom.
            based on the method protected Class loadClassByDelegation(String name) in org.jboss.aop.standalone.SystemClassLoader.

            Secondly, the mignt be a simple work around
            as illustrated with this example


            public class Vector {
            java.util.Vector delegate ;

            public Vector(java.util.Vector deco)
            {
            delegate = deco;

            }

            public void add(Object test) {

            delegate.add(test);
            }
            public void remove(Object test) {

            delegate.remove(test);
            }

            }

            // Changes in POJO
            public static void main(String[] args) throws Exception
            {

            POJO pojo = new POJO();
            Vector vec = new Vector(new java.util.Vector());

            vec.add(pojo);
            vec.remove(pojo);


            jboss-aop.xml changes

            <interceptor-pointcut class="Vector">



            </interceptor-pointcut>

            the output are

            Entering TracingInterceptor Calling constructor: public Vector(java.util.Vector)
            Leaving TracingInterceptorCalling constructor: public Vector(java.util.Vector)
            Entering TracingInterceptor Calling method: add
            Leaving TracingInterceptorCalling method: add
            Entering TracingInterceptor Calling method: remove
            Leaving TracingInterceptorCalling method: remove

            Hope this helps
            Adeyemi E Adegbile

            • 3. Re: How to apply aspects to javax.* classes
              analogue Newbie

              Thats a solution, but it kind of defeats the purpose of AOP. Do any of the existing AOP java implementation allow for aspectizing system classes?

              • 4. Re: How to apply aspects to javax.* classes
                Adrian Brock Master

                AspectJ will let you do it using a caller pointcut.
                It will only apply to places in your code, not internal uses
                by Swing classes.

                You can't modify Sun code it is against their license.

                Read the javassist docs on how to do it
                if you are not breaking Sun's license.

                Regards,
                Adrian