Skip to main content

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

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


Back to the top