I just finished reading AspectJ in Action and am trying to write some simple aspects to get you started. I would like to write an aspect that will generate a compile-time warning for fields that are used EnumType.ORDINAL
to save to the database, but not for those who use EnumType.STRING
. I wrote similar aspects, but this is the first one I'm trying to use using the annotation in pointcut, and I'm doing something wrong.
I have a JPA2.1 object like the one shown below and would like the annotation @Enumerated(EnumType.ORDINAL)
attached to to myEnumFieldB
generate a compiler warning ...
import javax.persistence.Entity;
import javax.persistence.Enumerated;
import javax.persistence.EnumType;
@Entity
public class myEntity {
@Enumerated(EnumType.STRING)
protected MyEnumType myEnumFieldA;
@Enumerated(EnumType.ORDINAL)
protected MyEnumType myEnumFieldB;
}
... pointcuts (), . pointcut .
import javax.persistence.Enumerated;
import javax.persistence.EnumType;
public aspect DetectEnumPersistencePolicy {
pointcut ordinalEnumPersistence(Enumerated enumerated)
: @annotation(enumerated) && if(enumerated.value() == EnumType.ORDINAL);
// used below in a commented-out pointcut
declare warning
: @Enumerated(EnumType.ORDINAL)
//ERROR: Syntax error on token "Enumerated", "pointcut name" expected
//: @javax.persistence.Enumerated(javax.persistence.EnumType.ORDINAL)
//ERROR: Syntax error on token "javax", "pointcut name" expected
//: @Enumerated(EnumType.ORDINAL) * *.*
//ERROR: Syntax error on token "Enumerated", "pointcut name" expected
//: execution(@Enumerated(EnumType.ORDINAL) * *.*)
//ERROR: Syntax error on token ")", "(" expected
//: ordinalEnumPersistence(enumerated)
// ERROR: if() pointcut designator cannot be used in declare statement
: "Please consider using string persistence of enumerated types instead.";
}
:
if(enumerated.value() == EnumType.ORDINAL)
, if() pointcut designator cannot be used in declare statement
. , pointcut if()
, , warning
( , )?execution(@Enumerated(EnumType.ORDINAL) * *.*)
, . Syntax error on token ")", "(" expected
( )
).@annotation(enumerated)
, , , enumerated.value() == EnumType.ORDINAL
, ?- pointcut
@javax.persistence.Enumerated(javax.persistence.EnumType.ORDINAL)
, Syntax error on token "javax", "pointcut name" expected
. - pointcut, ,
@Enumerated(EnumType.ORDINAL) * *.*
, Syntax error on token "Enumerated", "pointcut name" expected
.
Spring Tool Suite 3.7.2 Java 1.8 Maven AspectJ:
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjlib</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.7</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.8.7</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
- , @Enumerated(EnumType.ORDINAL)
?