gamelover
gamelover

Reputation: 15

Running aspect on an Annotated Class using AspectJ

I have a class annotated with a custom annotation. I want to run an aspect that should trigger before all the method calls and constructor call whenever an object or static method of that class is called. Can we do this in AspectJ?

Annotation Class

import com.stackoverflow

@Target({ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RunAdivceBeforeCall {
// Advice logic
}

Aspect Class

import com.stackoverflow
@Aspect
Class HandleAspect{

@Before("@annotation(RunAdivceBeforeCall) && execution(@RunAdivceBeforeCall * *.*(..))")
    public void runAnAdviceBeforeCall(JoinPoint joinpoint) {
     //Advice logic
 }

}

Class to run the Aspects

import com.stackoverflow

@RunAdivceBeforeCall
public Class NavigationBar {

    NavigationBar(){
    }

 public void navigateToHomePage(){
 }
 
 public void navigateToUserAccount(){
 }

 public void navigateToHelp(){
 }

}

The above logic i added doesn't work. Is there any other way to trigger before all the method and constructor call whenever an NavigationBar object methods, constructor call and static method of that class is called. I need this only in AspectJ as my project is not spring?

Upvotes: 0

Views: 24

Answers (0)

Related Questions