Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Java annotation to execute code before and after method

I am trying to use this solution (from this post), but I am getting an error during the build process with Quarkus.

Custom annotation:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface WaitCursor {}

The method interceptor:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public class WaitCursorInterceptor implements MethodInterceptor {
    public Object invoke(MethodInvocation invocation) throws Throwable {
        // show the cursor
        MainUI.getInstance().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        // execute the method annotated with `@WaitCursor`
        Object result = invocation.proceed();
        // hide the waiting cursor
        MainUI.getInstance().setCursor(Cursor.getDefaultCursor());
        return result;
    }
}

And the module to bind the interceptor on any method having the annotation.

public class WaitCursorModule extends AbstractModule {
    protected void configure() {
        bindInterceptor(Matchers.any(), Matchers.annotatedWith(WaitCursor.class), new WaitCursorInterceptor());
    }
}

The error I am getting:

[ERROR] Failed to execute goal io.quarkus.platform:quarkus-maven-plugin:2.15.1.Final:dev (default-cli) on project projectName: Unable to execute mojo: Compilation failure
[ERROR] /git/projectName/api/src/main/java/com/packageName/shared/modules/WaitCursorModule.java:[25,9] cannot find symbol
[ERROR]   symbol:   method bindInterceptor(com.google.inject.matcher.Matcher<java.lang.Object>,com.google.inject.matcher.Matcher<java.lang.reflect.AnnotatedElement>,com.packageName.shared.modules.WaitCursorModule)
[ERROR]   location: class com.packageName.shared.modules.WaitCursorModule

>Solution :

That example code requires Google Guice. Quarkus comes with its own mechanism. https://quarkus.io/guides/cdi contains some examples, in section 14.2.

In short:

@InterceptorBinding // added
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD }) // TYPE added
@Inherited // added
@interface WaitCursor {}
@WaitCursor
@Interceptor 
public class WaitCursorInterceptor {

    @AroundInvoke 
    public Object invoke(InvocationContext invocation) throws Throwable {
        // show the cursor
        MainUI.getInstance().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        // execute the method annotated with `@WaitCursor`
        Object result = invocation.proceed();
        // hide the waiting cursor
        MainUI.getInstance().setCursor(Cursor.getDefaultCursor());
        return result;
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading