java - I want to generate a compile warning for JPA annotations with a given value -
i finished reading aspectj in action , trying write simple aspects start with. write aspect generate compile-time warning fields use enumtype.ordinal
persist database, not use enumtype.string
. i've written similar aspects, first 1 i'm trying uses annotation in pointcut, , i'm doing wrong.
i have jpa2.1 entity 1 shown below, , @enumerated(enumtype.ordinal)
annotation attached myenumfieldb
generate compiler warning...
import javax.persistence.entity; import javax.persistence.enumerated; import javax.persistence.enumtype; @entity public class myentity { @enumerated(enumtype.string) // want compile ok protected myenumtype myenumfielda; @enumerated(enumtype.ordinal) // want throw warning protected myenumtype myenumfieldb; // primary key, other fields, getters & setters, etc. omitted }
...and here's copy of code trying several different pointcuts (commented out), error messages included beside them. named pointcut used last commented-out line.
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 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 used in declare statement : "please consider using string persistence of enumerated types instead."; }
my thoughts far are:
- when tried including
if(enumerated.value() == enumtype.ordinal)
error messageif() pointcut designator cannot used in declare statement
. made me think can't use pointcutif()
because evaluated @ runtimewarning
needs generated @ compile time (even though data there @ compile time because annotation's value doesn't vary runtime state)? - same
execution(@enumerated(enumtype.ordinal) * *.*)
because execution occurs @ runtime. error messagesyntax error on token ")", "(" expected
didn't mean me (it's referring second)
). - if use
@annotation(enumerated)
should work @ compile-time, don't know how perform checkenumerated.value() == enumtype.ordinal
before throwing warning? - i've tried using qualified names in pointcut
@javax.persistence.enumerated(javax.persistence.enumtype.ordinal)
still got error messagesyntax error on token "javax", "pointcut name" expected
. - i tried appending asterisks annotation in pointcut specify i'm referring field
@enumerated(enumtype.ordinal) * *.*
still got error messagesyntax error on token "enumerated", "pointcut name" expected
.
for completeness, i'm working in spring tool suite 3.7.2 java 1.8 , using maven manage following aspectj dependencies:
<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>
any appreciated - can't see why @enumerated(enumtype.ordinal)
doesn't work?
actually have basic problem: there no pointcut catching member declarations, read/write access on members. talking get()
, set()
, respectively. consequently, want intercept those. try this:
dummy helper class make sample code compile:
package de.scrum_master.app; public class myenumtype {}
java class annotated members plus main method demonstrating aspect:
package de.scrum_master.app; import javax.persistence.entity; import javax.persistence.enumtype; import javax.persistence.enumerated; @entity public class myentity { @enumerated(enumtype.string) // want compile ok protected myenumtype myenumfielda; @enumerated(enumtype.ordinal) // want throw warning protected myenumtype myenumfieldb; public static void main(string[] args) { myentity myentity = new myentity(); myentity.myenumfielda = new myenumtype(); system.out.println(myentity.myenumfieldb); } }
as can see, myenumfieldb
accessed once in last line. should throw compiler warning.
aspect:
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."; }
this see in eclipse:
Comments
Post a Comment