Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] How to create "conditional ITDs"?

Take a look at -Xhasmember option to ajc. You should be able to do something along the following lines:

interface NeedsPrePersist {
}

declare parents: !hasfield(@PrePersist * *) && (@Entity *) extends NeedPrePersist;

Then introduce fields and methods to only types that implement NeedPrePersist.

-Ramnivas

On Tue, Aug 4, 2009 at 3:32 PM, Matthew Adams <matthew@xxxxxxxxxxxxxxx> wrote:
Hi all,

I was wondering how it might be possible to achieve what I'm calling a
conditional inter-type declaration.

Here's a simple auditing example.  For a JPA entity, I want to
introduce a @PrePersist method if and only if the target class doesn't
have one because it's an error to have multiple @PrePersist methods on
an entity.  If it already has a @PrePersist method, I want to execute
advice after the target instance's @PrePersist method is invoked by
the JPA implementation.  Same for @PreUpdate.

The example below will work for Person, but not for Document --
Document will end up with two @PrePersist methods.  How can I refactor
this to work for both?

========
@Entity
public class Document {
 // ...
 @PrePersist
 private void prePersist() { /* ... */ }
}
========
@Entity
public class Person {
 // ...
}
========
// works only for an @Entity that DOES NOT define a @PrePersist or a
@PreUpdate method
public aspect AuditingItd {
       private interface Auditable {
       }

       declare parents:  (@javax.persistence.Entity *) implements Auditable;

       @Column(name = "updated")
       private Date Auditable.updated;

       @PrePersist
       private void Auditable.auditablePrePersist() {
               updated = new Date();
       }

       @PreUpdate
       private void Auditable.auditablePreUpdate() {
               updated = new Date();
       }
}
=========

-matthew

--
mailto:matthew@xxxxxxxxxxxxxxx
skype:matthewadams12
yahoo:matthewadams
aol:matthewadams12
google-talk:matthewadams12@xxxxxxxxx
msn:matthew@xxxxxxxxxxxxxxx
http://matthewadams.me
http://www.linkedin.com/in/matthewadams
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users


Back to the top