Enterprise Java

Mixin in Java with Aspects – for a Scala traits sample

Scala traits allow new behaviors to be mixed into a class. Consider two traits to add auditing and version related fields to JPA entities:
 
 
 
 
 
 
 
 
 

package mvcsample.domain

import javax.persistence.Version
import scala.reflect.BeanProperty
import java.util.Date

trait Versionable {
  @Version
  @BeanProperty
  var version: Int = _
}

trait Auditable {
  @BeanProperty
  var createdAt: Date = _

  @BeanProperty
  var updatedAt: Date = _
}

Now to mix in ‘Versionable’ and ‘Auditable’ with their fields and behavior in a Member entity:

@Entity
@Table(name = 'members')
class Member(f: String, l: String) extends BaseDomain with Auditable with Versionable {

  def this() = this(null, null)

  @BeanProperty
  var first: String = f

  @BeanProperty
  var last: String = l

  @OneToMany(fetch = FetchType.EAGER, mappedBy = 'member')
  @BeanProperty
  var addresses: java.util.List[Address] = _
}

trait BaseDomain {
  @BeanProperty
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = 'id')
  @Id
  var id: Long = 0
}

The Member class above will now have the behavior of the BaseDomain class, and will have the behavior of Versionable trait and the Auditable trait. This kind of mixin is not possible with plain Java, as the equivalent of traits with fields and behavior would be an abstract(or concrete) class and Java allows deriving only from 1 base class. However with AspectJ it is possible to achieve an equivalent of mixin. Consider the following aspects defined using Aspectj language:

package mvcsample.aspect;

import javax.persistence.Column;
import javax.persistence.Version;
import mvcsample.annot.Versioned;

public interface Versionable {

    static aspect VersionableAspect {
        declare parents: @Versioned  mvcsample.domain.* implements Versionable;

        @Version
        @Column(name = 'version')
        private Integer Versionable.version;    

        public Integer Versionable.getVersion() {
        return this.version;
            }

        public void Versionable.setVersion(Integer version) { 
        this.version = version;
        }
    }
}

package mvcsample.aspect;

import java.util.Date;
import javax.persistence.Column;

import mvcsample.annot.Audited;
public interface Auditable {
    static aspect AuditableAspect {
        declare parents: @Audited mvcsample.domain.* implements Auditable ;

        @Column(name='created_at')
        private Date Auditable.createdAt;

        @Column(name='updated_at')
        private Date Auditable.updatedAt;

        public Date Auditable.getCreatedAt(){
            return this.createdAt;
        }

        public void Auditable.setCreatedAt(Date createdAt) {
            this.createdAt = createdAt;
        }

        public Date Auditable.getUpdatedAt(){
            return this.updatedAt;
        }

        public void Auditable.setUpdatedAt(Date updatedAt) {
            this.updatedAt = updatedAt;
        }
    }
}

‘declare parents: @Versioned mvcsample.domain.* implements Versionable;’ aspectj construct adds ‘Versionable’ interface as a parent to any class in package ‘mvcsampple.domain’ annotated with @Versioned, similarly the one for ‘Auditable’. Then the aspect goes about adding fields to the Versionable interface which in turn ends up adding(mixing in) the fields to the targeted entity classes, this way the Audit related and Version related fields and methods get mixed into the entity classes. With these two aspects defined, a target entity class would look like this:

@Entity
@Table(name="members")
@Access(AccessType.FIELD)
@Versioned
@Audited
public class Member extends BaseDomain{
 
 public Member(){}
 
 public Member(String first, String last){
  this.first = first;
  this.last = last;
 }
 
 private String first;
 
 @Size(min=1)
 private String last;
 
 @OneToMany(fetch=FetchType.EAGER, mappedBy="member")
 private List<address>addresses = new ArrayList<>();
        .....
}
</address>

The fields and behavior defined in the Versionable and Auditable aspects would be mixed into this entity(more generally into any entity with @Versioned and @Audited annotations). Probably not as clean as Scala traits but works nicely.
 

Reference: Mixin in Java with Aspects – for a Scala traits sample from our JCG partner Biju Kunjummen at the all and sundry blog.

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button