Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] Method & constructor validation pointcuts

Hi all,

I'm trying to use a JSR-303 ValidationProvider (Apache BVal, to be
exact) along with an aspect to do POJO method & constructor
validation.  I'd like to apply before advice to validated constructors
& methods, and after advice to validated return values, where
"validated" means the following.

Constructors:
* Any parameter is annotated with any javax.validation.constraints annotation

Methods (all must be true):
* Nonstatic method
* Any parameter is annotated with any javax.validation.constraints annotation

Return value (all must be true):
* Nonstatic method
* Method annotated with any javax.validation.constraints annotation

Here are my pointcuts.

pointcut executingValidatedConstructorParameters() :
execution(*.new(.., @javax.validation.constraints..* *, ..));

pointcut executingInstanceMethod() : execution(!static * *(..));

pointcut executingValidatedReturnValue() :
executingInstanceMethod() &&
execution((@javax.validation.constraints..* !void) *(..));

pointcut executingValidatedMethodParameters() :
executingInstanceMethod() &&
execution(* *(.., @javax.validation.constraints..* *, ..));

They don't match anything.

If I add a criterion "execution(@Valid *.new(..))" to
executingValidatedConstructorParameters, and "execution(@Valid *
*(..))" to executingValidatedMethodParameters &
executingValidatedReturnValue, then they match ok, but require me to
put the @Valid annotation on the method or constructor **in addition
to** whatever other constraint annotations (like @NotNull) are there,
which I feel is redundant.  The presence of the actual constraint
annotation should be sufficient to trigger validation.

Further, I noticed that if I change "@javax.validation.constraints..*"
to one of the actual constraint annotations,
"@javax.validation.constraints.NotNull", I get a new warning about
unmatched aspects:

'does not match because annotation
@javax.validation.constraints.NotNull has
@Target{ElementType.FIELD,ElementType.ANNOTATION_TYPE,ElementType.CONSTRUCTOR,ElementType.METHOD,ElementType.PARAMETER}
[Xlint:unmatchedTargetKind]'

Then, I tried to change the annotation expression to match where
elementType includes ElementType.PARAMETER (and ElementType.METHOD for
executingValidatedReturnValue), but I couldn't figure out the syntax.

So, how can I match constructors & methods where any parameter is
annotated with any javax.validation.constraints..* annotation, and
return values where the method is annotated with any
javax.validation.constraints..* annotation?

Thanks,
Matthew
--
mailto:matthew@xxxxxxxxxxxxxxx
skype:matthewadams12
googletalk:matthew@xxxxxxxxxxxxxxx
http://matthewadams.me
http://www.linkedin.com/in/matthewadams


Back to the top