0

I have two aspects which are going to perform some validation. The aspects intercept all methods annotated with @NotNull and @IsUnique annotations. For instance:

@NotNull
@IsUnique
public void save(Player p){
    // persisting
}

Aspects:

public aspect NotNullInterceptor{

    pointcut NotNull(): execution(public * (@NotNull *).*(..));

    before() : NotNull() {
        //Validation and handling
    }
}

public aspect IsUniqueInterceptor{

    pointcut IsUnuque(): execution(public * (@IsUnique *).*(..));

    before() : IsUnuque() {
        //Validation and handling
    }
}

The thing is I need to perform NotNull validation strictly before IsUnique validation to avoid throwing NullPointerException.

Is it reliable if I put @NotNull annotation before @IsUnique annotation?

St.Antario
  • 26,175
  • 41
  • 130
  • 318
  • Isn't this reinventing Java Bean validation? What happens with multiple method arguments? If you want to enforce ordering of aspects you need to use another aspect to declare precedence. – M. Deinum May 19 '15 at 10:45
  • @M.Deinum No, BeanValidation doesn't have anything like `@IsUnique ` and some other custom annotation I need to use in the project. So, if I have, say, ten validation annotations I have to declare 10! aspects to declare an order between them, is that right? – St.Antario May 19 '15 at 12:06
  • 1
    Then write your own validation rule instead of your whole validation framework. Java Bean Validation is extendable... Also regarding unique validation http://stackoverflow.com/questions/3495368/unique-constraint-with-jpa-and-bean-validation is an interesting read. And if you really want a stab at it [here](https://omgo.wordpress.com/2009/10/25/creating-a-jsr-303-style-unique-value-annotation-and-using-it-in-spring-3-mvc/) is a sample. – M. Deinum May 19 '15 at 12:09
  • @M.Deinum So, would suggest I using Bean Validation API instead of writing my own validation. Can I validate parameters passed to a method with BeanValidation API? For instance, Ineed all method parameters to be satisfied with a few BeanValidation's annotation? Can I just apply the annotation to a method? – St.Antario May 19 '15 at 14:01
  • See https://rmannibucau.wordpress.com/2013/07/04/bean-validation-1-1-method-validation/ for an example. Hibernate-validator does at least have an implementation for this. – M. Deinum May 19 '15 at 14:13

1 Answers1

1

No. A pointcut is a boolean function that determines whether an aspect applies. It does not influence the ordering of aspects. Moreover, in general it would be impossible to infer such an ordering from pointcut expressions. For instance, consider:

@First
@Second
@Third
void foo();

and the pointcut expressions:

execution(public * (@First *).*(..)) && execution(public * (@Third *).*(..));
execution(public * (@Second *).*(..));

should the first pointcut match first because @First comes before @Second, or last because @Third comes after @Second?

meriton
  • 68,356
  • 14
  • 108
  • 175