[jboss-user] [JBoss AOP] - Mixins, introducing new superclasses and class loading time

susanin do-not-reply at jboss.com
Mon Jul 17 07:37:31 EDT 2006


Hi,

Sorry for a bit longish message.

I have several questions:

1. I have a class that implements certain methods. 


  | @NiceAnnotation
  | public class A {
  |    public void methodN();
  | }
  | 

I also have an interface which defines among others the same method:


  | public interface NiceInterface {
  |    public void methodL();
  |    public void methodN();
  |    public void methodM();
  | }
  | 
  | public interface NiceImpl {
  |    public NiceImpl(Object obj){}
  |    public NiceImpl() {}
  |    public void methodL() {}
  |    public void methodN() {}
  |    public void methodM() {}
  | }
  | 
  | 

Now I want to define a mixin by means of AOP so that class A implements the NiceInterface and NiceImpl is the default implementation of it. More over, I wich that methods of NiceInterface implemented by A (i.e. methodN) take precedence before NiceImpl. NiceImpl should be called only when A does not implement a corresponding method.

My idea was to use:

  | <introduction class="A">
  |    <mixin>      
  |     <interfaces>NiceInterface</interfaces>
  |       <class>NiceImpl</class>
  |       <construction>new NiceImpl(this)</construction>
  |    </mixin>
  | </introduction>
  | 

But when I do it, I always become an exception:
Mixin NiceImpl ... is trying to apply an already existing methodN for class A

How can I solve this with JBoss AOP?

2. Is it somehow possible to introduce a new superclass (and I mean really superclass and not an interface) by means of AOP? For example, I'd like to say that class A should become inherited from NiceImpl. The resulting weaved class will look like: 


  | public class A extends NiceImpl {
  |    public void methodN();
  | }
  | 

This would achieve my goal in the previous question.

    If you ask why one would like to do it, I can give you an example. There are some JSRs that dictate for some user-defined objects to extend some standard classes provided by this JSR. Now, they plan to simplify the process and use some annotations (see @NiceAnnotation above). Such an annotation means that class A should be actually extend a class NiceImpl, stndardized by the JSR.

It can be argued that such requirements for extending classes and not using interfaces is a sign of a bad design. I'd even agree with that. But these JSRs are there and there is already a lot of legacy code, etc. So, you cannot easily change the JSR specification. 

   I haven't found any such possibility of adding a new superclass in any of the AOP tools. But I've seem that it is possible with Javassist libray by means of CtClass.setSuperclass. Why is it not allowed when using AOP?  
    
3. Let's concentrate a bit more on the @NiceAnotation in the declaration of A.

I'd like to take some actions when a class with such an annotated class is loaded. Imagine that it happens inside a container. I'd like to use AOP pointcut language to catch such annotated classes (because it is very easy and declarative and is already possible with existing pointcut syntax) and to be able to associate an action that should be taken when such a class is being loaded. The action would consist of analysis of the annotated class and its parameters and would eventually instrument it (e.g. eventually add new methods or selectively define before/after/around advices for existing ones and, which is also very important, add inform the container about information contained in the annotation (e.g. name, port, namepsace), so that container can correctly "register" such a newly deplotyed component).

I mean something like this:

  | <bind pointcut="loadtime(@NiceAnnotation)">
  |    <advice name="processLoading" aspect="NiceLoadtimeAspect"/>
  | </bind>
  | 


At the moment I do not see a way to intercept class loading in a way described above by means of Jboss AOP. There is no way to say: "if a class/method (annotated) like this is detected, do a given action". And I mean do it when it is detected, not when it is called, which is a usual semantics of interceptors. Are there any good reasons why this is impossible?

Of course, one can implement the described behavior by writing a special class loader that would look for the required classes/methods and do some associated actoins. But this requires a lot of tedious coding, not very declarative and essentially just re-implements what AOP weaver usually does, in particular the pointcut matching parts.

I think such a possibility by means of AOP would be very useful for processing some elements (e.g. classes, methods, annotations) only once as they are loaded. AOP tools already provide a declarative and very expressive pointcut language that easily catches any required elements. The only thing  required is to introduce a special kind of interceptors that are applied only at the class loading time in the sene described above. This would eliminate a lot of cases where custom class-loaders are used at the moment.

I'd really like to get your opinion about some of these questions and ideas.

Best Regards,
  Ivan  

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3958437#3958437

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3958437



More information about the jboss-user mailing list