@aspect - @around annotations used - custom annotated method not getting executed

大兔子大兔子 提交于 2019-12-25 08:40:20

问题


I created a custom annotation using AspectJ , and tried to create annotation that will help in skipping method if annotation is not valid.

But my method body is not getting executed any case

here it is my code

it is the constraint

package com.test;
public @interface SwitchLiteConstraint {

  Class<SwitchLiteValidator>[] validatedBy();

}

below is constraint validator

package com.test;
import java.lang.annotation.Annotation;

public interface SwitchLiteConstraintValidator<T1 extends Annotation, T2> {
  void initialize(T1 annotation);
  boolean isValid(T2 value, SwitchLiteConstraintValidatorContext validatorContext);
}

below is my aspect class

package com.test;

import java.lang.reflect.Method;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;

@Aspect
public class SwitchLiteValidationAspect {

    @Around("execution(boolean *(..))")
    public boolean skipValidation(ProceedingJoinPoint thisJoinPoint) throws Throwable {

        Method method = MethodSignature.class.cast(thisJoinPoint.getSignature()).getMethod();

        SwitchLiteAnnotation puff = method.getAnnotation(SwitchLiteAnnotation.class);

        if (puff == null) {
            System.out.println(" puff null returning true ");
            return true;
        } else {

            String aspectName = puff.message().trim();
        if(aspectName.equalsIgnoreCase("msg")){
            System.out.println("  returning true ");
            return true;
        }else{
            System.out.println("  returning false ");
            return false;
            }
        }

    }
}

below is the validator implementation

package com.test;
public class SwitchLiteValidator implements SwitchLiteConstraintValidator<SwitchLiteAnnotation, String> {
  @Override
  public void initialize(SwitchLiteAnnotation annotation) {}

  @Override
  public boolean isValid(String value, SwitchLiteConstraintValidatorContext validatorContext) {
    if ("msg".equalsIgnoreCase(value)){
      //return true;
      return true;
    }
    //return false;
    return false;
  }
}

below is my custom annotation

package com.test;

import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*;

import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.*;

@Target({ METHOD, FIELD, PARAMETER })
@Retention(RUNTIME)
@SwitchLiteConstraint(validatedBy = { SwitchLiteValidator.class })
public @interface SwitchLiteAnnotation {
  String message() default "DEFAULT_FALSE";
}

and below is the example , the way i am using annotation

package com.test;


public class Application {
  public static void main(String[] args) {
    Application application = new Application();


    boolean first=false;
    boolean second=false;

    first=application.validate1();
    second=application.validate2();

    System.out.println(first);
    System.out.println(second);
  }

  @SwitchLiteAnnotation(message = "abc")
  public boolean validate1() {
      System.out.println("validate1");
      return true;
  }

  @SwitchLiteAnnotation(message = "msg")
  public boolean validate2() {
      System.out.println("validate2");
      return true;
  }



}

The issue is my System.out.println("validate1"); and System.out.println("validate2"); are getting executed


回答1:


Your annotation SwitchLiteConstraint needs runtime retention, otherwise it will not be visible during runtime and thus invisible to aspects. You probably just forgot that.


Update: I copied all your source code and tried on my computer. I do not understand what your problem is. Without the aspect the output is:

validate1
validate2
true
true

With the aspect the output becomes:

  returning false 
  returning true 
false
true

Is that not what you want?



来源:https://stackoverflow.com/questions/42850782/aspect-around-annotations-used-custom-annotated-method-not-getting-execute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!