I want to generate a compilation warning for JPA annotations with a given value

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.ORDINALto 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 myEnumFieldBgenerate a compiler warning ...

import javax.persistence.Entity;
import javax.persistence.Enumerated;
import javax.persistence.EnumType;

@Entity
public class myEntity {
    @Enumerated(EnumType.STRING) // I want this to compile ok
    protected MyEnumType myEnumFieldA;

    @Enumerated(EnumType.ORDINAL) // I want this to throw a warning
    protected MyEnumType myEnumFieldB;

    // primary key, other fields, getters & setters, etc. omitted
}

... 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) ?

+4
1

: pointcut, / . get() set(), . , , . :

-, :

package de.scrum_master.app;

public class MyEnumType {}

Java- , :

package de.scrum_master.app;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;

@Entity
public class MyEntity {
    @Enumerated(EnumType.STRING) // I want this to compile ok
    protected MyEnumType myEnumFieldA;

    @Enumerated(EnumType.ORDINAL) // I want this to throw a warning
    protected MyEnumType myEnumFieldB;

    public static void main(String[] args) {
        MyEntity myEntity = new MyEntity();
        myEntity.myEnumFieldA = new MyEnumType();
        System.out.println(myEntity.myEnumFieldB);
    }
}

, myEnumFieldB . .

:

package de.scrum_master.aspect;

import javax.persistence.Enumerated;
import javax.persistence.EnumType;

public aspect DetectEnumPersistencePolicy {
    pointcut ordinalEnumPersistence() :
        set(@Enumerated(value=EnumType.ORDINAL) * *) ||
        get(@Enumerated(value=EnumType.ORDINAL) * *);

    declare warning : ordinalEnumPersistence() :
        "Please consider using string persistence of enumerated types instead.";
}

, Eclipse:

Eclipse Compiler Warning

+2

Source: https://habr.com/ru/post/1627716/


All Articles