Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Inter-type declaration of constructor usingannotations

Hi,

first of all thanks for reply. The problem with using around advice on constructor is that I can't use it. In the end you just have to return Article class anyways so you must invoke constructor somewhere. There is no problem with this as far as I can short-circuit all calls and set/get in this constructor using

private pointcut basicCalls() : call(* *.*(..)) || get(* *.*) || set(* *.*); /**
    * Bypasses all methods/getters/setters invoked in
    * persistable objects constructors in loading mode
**/ Object around() : whileLoading() && cflow(entityCreation()) && basicCalls() && !within(Persistence) {
       return null;
   }

The problem is when a constructor takes some parameters like in my example. Maybe I could use reflection capabilities to inspect that and push there some nulls and default values. Looks like some sort of crippled dependency injection. I'll try that one.

Johno

Ron Bodkin wrote:
Hi Johno,

There are a number of code generation capabilities that AspectJ doesn't
provide, and this is a good example of one that would be useful but AspectJ
doesn't support. In AspectJ 1.0 you actually could write an ITD like you
wrote for #1 (e.g., public Persistable+.new(..)), but the AspectJ 1.1+
restriction on writing ITD's to only simple types resulting in losing that
ability. It would nice if the restriction of ITD's to work only on a simple
type could be relaxed (using the least common denominator type for type
patterns). But failing having an enhancement like that...

One approach that you often see in O/R frameworks is requiring a no-op
constructor on the persistable classes. If you follow that approach, you
could instead use an ITD to define a method void Persistable.init(Transfer)
that runs after construction.  I'd make my persistent classes implement an
interface Persistable rather than a superclass (it's not good to require
them to all have a common superclass, of course).

But if you really want to avoid adding another constructor to all the
classes, it is possible to use a somewhat ugly work-around. You could define
a common factory method to create persistent objects (e.g., Article article
= Persistence.create(Article.class);) that uses reflection to invoke a
normal constructor of the domain object, but use around advice to
short-circuit the normal constructor behavior, then delegate on to a method
like Persistable.init(Transfer). Given that this code will be encapsulated
inside your persistence engine, it might be reasonable to do this (as long
as you are weaving into any persisted class).

HTH,
Ron

-----Original Message-----
From: aspectj-users-bounces@xxxxxxxxxxx
[mailto:aspectj-users-bounces@xxxxxxxxxxx] On Behalf Of Johno
Sent: Sunday, November 12, 2006 3:54 AM
To: aspectj-users@xxxxxxxxxxx
Subject: [aspectj-users] Inter-type declaration of constructor
usingannotations

Hi,

I'm currently working on a object-relational mapper using annotations.
Here is a simple example of a persistable class.

@Persistent
public class Article {
    private Person author;

    public Article(Person anAuthor) {
        author = anAuthor;
    }
}

The main problem is that there are two kinds of object construction in
context of OR mapping. Creating a new object that will be inserted into
DB and loading an object from DB. So you need two constructors for a
persistable class.

@Persistent
public class Article {
    private Person author;

    public Article(Transfer transfer) {
        // load data from transfer to entities
    }

    public Article(Person anAuthor) {
        author = anAuthor;
    }
}

I would like to introduce this new "transfer" constructor using
inter-type declarations. Is it possible? I have tried various things:

1)  There is no way to use
public (@Persistent *).new(Transfer transfer) {...}

2) If I create a PersistentRoot class with transfer constructor and use
declare parents : @Perstistent * extends PersistentRoot;

then I cannot use this constructor when creating subclasses. new Article(Transfer transfer); is not usable.

3) With mentioned PersistentRoot I must add constructor to every persistent class by hand.

public Article.new(Transfer transfer) { super(transfer); }

Which is the only working way I have found, but its really ugly hack.

I am new to AspectJ so probably I am missing something. Any ideas?

_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users

_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users




Back to the top