lapots
lapots

Reputation: 13415

aspect is not invoked

I've got a simple aspect that supposed to set the value of class fied, that has annotation @GuiceInject.

Originally I have this

@GuiceInject(module=RepositoryModule.class)
private IRacesRepository repository;

And I expect to get similar to this

private IRacesRepository repository = GuiceInject.getInstance(IRacesRepository.class);

And here is my aspect

public aspect InjectionAspect {

    Object around(): get(@GuiceInject * *) {
        System.out.println(thisJoinPointStaticPart);
        // instantiate object as it supposed to be null originally
        return GuiceInjector.getInstance(thisJoinPoint.getTarget().getClass());
    }
}

As far as I understand - I am new to AOP - it supposed to replace get invokations of the field with the code in aspect.

It compiles fine, but when I run the application - nothing happens. I get NullPointerException for readRaces method as it stays null so aspect did not work.

My main class looks like this

public class Example {
    @GuiceInject(module=RepositoryModule.class)
    private IRacesRepository racesRepository;

    private void execute() {
        System.out.println("List of races: " + racesRepository.readRaces());
    }

    public static void main(String[] args) {
        new Example().execute();
    }
}

What is the problem? Annotation has this definition

@Target(ElementType.FIELD)
// make annotation visible in runtime for AspectJ
@Retention(RetentionPolicy.RUNTIME)
public @interface GuiceInject {
    Class<? extends AbstractModule> module();
}

Upvotes: 1

Views: 68

Answers (1)

fg78nc
fg78nc

Reputation: 5232

Please try to redefine pointcut syntax as

Object around(): get(@package.subpackage.GuiceInject * *.*) 

Correct field signature must specify the type of the field, the declaring type, and name. If your annotation is in different package, it should be fully qualified.

Upvotes: 1

Related Questions