简体   繁体   中英

Get local variable value from annotated method in aspectJ

I am using java custom annotation with aspectJ

CUSTOM ANNOTATION

@Documented
@Target(ElementType.METHOD)
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface TrxLogger {

String author() default "Shahid Ghafoor";
String comments() default "I am Fan of JAVA";
}

SERVICE

public class Service {

private String name;

public String getName() {
    return name;
}


public void setName(String name) {
    this.name = name;
}

@TrxLogger
public String sayHello(String fn, String ln) {

    this.name=fn;

     String localVariable="Before System.out.println";

    System.out.println("Welcome!" + fn + " " + ln);

    return "The Transaction return Value";
}

}

ASPECT

@Around(value="pcSample(tl)", argNames="tl")
public Object runMethod(ProceedingJoinPoint pjp, TrxLogger tl) throws Throwable {

 return null;
}

is it possible to get local variable value( String localVariable="Before System.out.println"; ) of sayHello() Method in aspect ?

No, a local variable is just that, local. You cannot dig into the method to access it. The body of a method is not accessible through reflection.

when method sayHello execute that time only lovalVariable have value. otherwise it's not known by anybody.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM