Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1021523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:19:25+00:00 2026-05-16T11:19:25+00:00

I need to create an aspect with a pointcut matching a method if: it

  • 0

I need to create an aspect with a pointcut matching a method if:

  1. it is annoted with MyAnnotationForMethod
  2. One of its parameters (can have many) is annotated with @MyAnnotationForParam (but can have other annotations as well).

The aspect class look like this

@Pointcut("execution(@MyAnnotationForMethod * *(..,@aspects.MyAnnotationForParam Object, ..)) && args(obj)")
void myPointcut(JoinPoint thisJoinPoint, Object obj) {
}

@Before("myPointcut(thisJoinPoint ,  obj)")
public void doStuffOnParam(JoinPoint thisJoinPoint, Object obj) {
    LOGGER.info("doStuffOnParam :"+obj);
}

The annoted method

@MyAnnotationForMethod
public string theMethod(String a, @MyAnnotationForParam @OtherAnnotation Object obj, Object b){ 
    LOGGER.info(a+obj+b);
}

With eclipse -> warnings : On the poincut :

Multiple markers at this line 
    - no match for this type name: MyAnnotationForMethod [Xlint:invalidAbsoluteTypeName] 
    - no match for this type name: aspects.MyAnnotationForParam On the before : advice defined in xxx.xxx.xxx.xxx.MyAspect has not been applied [Xlint:adviceDidNotMatch]

Using last aspectJ plugin from http://download.eclipse.org/tools/ajdt/35/update

With maven command line using aspectj 1.6.9

[WARNING] no match for this type name: MyAnnotationForMethod [Xlint:invalidAbsoluteTypeName]
[WARNING] no match for this type name: aspects.MyAnnotationForParam [Xlint:invalidAbsoluteTypeName]
[WARNING] advice defined in xxx.xxx.xxx.xxx.MyAspect has not been applied [Xlint:adviceDidNotMatch]

The annotations :

package com.xxx.xxx.annotation;
// standard imports stripped
@Documented
@Target( { FIELD, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
public @interface @MyAnnotationForParam {}

and

package com.xxx.xxx.annotation;
// standard imports stripped
@Target(METHOD)
@Retention(RUNTIME)
@Documented
public @interface MyAnnotationForMethod {}

And of course it doesn’ work properly.

Can you tell me what is wrong ?

thx.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T11:19:26+00:00Added an answer on May 16, 2026 at 11:19 am

    Updated:

    OK, the best reference I could find is on this page: Annotations, Pointcuts and Advice.

    You can match the method, however you won’t be able to catch the parameter (just the method and the annotation). So what you will have to do is a combination of pointcut matching and reflection. Something like this:

    @Pointcut(
        "execution(@com.xxx.xxx.annotation.MyAnnotationForMethod * *(.., @com.xxx.xxx.annotation.MyAnnotationForParam (*), ..))")
    public void annotatedMethod(){}
    
    @Before("annotatedMethod()")
    public void doStuffOnParam(final JoinPoint jp){
        final Signature signature = jp.getSignature();
        if(signature instanceof MethodSignature){
            final MethodSignature ms = (MethodSignature) signature;
    
            final Method method = ms.getMethod();
            final String[] parameterNames = ms.getParameterNames();
            final Class<?>[] parameterTypes = ms.getParameterTypes();
            final Annotation[][] parameterAnnotations =
                method.getParameterAnnotations();
            for(int i = 0; i < parameterAnnotations.length; i++){
                final Annotation[] annotations = parameterAnnotations[i];
                final MyAnnotationForParam paramAnnotation =
                    getAnnotationByType(annotations, MyAnnotationForParam.class);
                if(paramAnnotation != null){
                    this.processParameter(ms.toShortString(),
                        parameterNames[i],
                        parameterTypes[i],
                        paramAnnotation);
                }
    
            }
        }
    }
    
    /**
     * In an array of annotations, find the annotation of the specified type, if any.
     * @return the annotation if available, or null
     */
    @SuppressWarnings("unchecked")
    private static <T extends Annotation> T getAnnotationByType(final Annotation[] annotations,
        final Class<T> clazz){
    
        T result = null;
        for(final Annotation annotation : annotations){
            if(clazz.isAssignableFrom(annotation.getClass())){
                result = (T) annotation;
                break;
            }
        }
        return result;
    }
    
    /**
     * Do some processing based on what we found.
     * @param signature method signature
     * @param paramName parameter name
     * @param paramType parameter type
     * @param paramAnnotation annotation we found
     */
    private void processParameter(final String signature,
        final String paramName,
        final Class<?> paramType,
        final MyAnnotationForParam paramAnnotation){
    
        System.out.println(MessageFormat.format(
            "Found parameter ''{0}'' \n  of type ''{1}'' \n  with annotation ''{2}'' \n  in method ''{3}''",
            paramName,
            paramType,
            paramAnnotation,
            signature));
    }
    

    Here is my test class for the above aspect:

    public class TestClass{
    
        @MyAnnotationForMethod
        public void simpleTestMethod(@MyAnnotationForParam final String param1){
            System.out.println("Method body (simple)");
        };
    
        @MyAnnotationForMethod
        public void complexTestMethod(final String param1,
            @MyAnnotationForParam final Float param2,
            @MyAnnotationForParam final Boolean param3){
            System.out.println("Method body (complex)");
        };
    
        public static void main(final String[] args){
            System.out.println("Starting up");
            final TestClass testObject = new TestClass();
            testObject.simpleTestMethod("Hey");
            testObject.complexTestMethod("Hey", 123.4f, false);
            System.out.println("Finished");
        }
    
    }
    

    and here is the output:

    Starting up
    Found parameter 'param1' 
      of type 'class java.lang.String' 
      with annotation '@com.xxx.xxx.annotation.MyAnnotationForParam()' 
      in method 'TestClass.simpleTestMethod(..)'
    Method body (simple)
    Found parameter 'param2' 
      of type 'class java.lang.Float' 
      with annotation '@com.xxx.xxx.annotation.MyAnnotationForParam()' 
      in method 'TestClass.complexTestMethod(..)'
    Found parameter 'param3' 
      of type 'class java.lang.Boolean' 
      with annotation '@com.xxx.xxx.annotation.MyAnnotationForParam()' 
      in method 'TestClass.complexTestMethod(..)'
    Method body (complex)
    Finished
    

    Hint

    You will probably want to cache a lot of this, there is no need to parse every parameter of every annotation in every execution. Keep a map of which parameter of which method carries the annotation and process only those parameters.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create an aspect with a pointcut matching a method if: Is
i need create an email list sending to many emails. what is best solution
I need create clone repository. but I do not know where can I get
I need create a document word with Java. And I ask, how can I
I have dynamically created WrapPanel (_wp) with several Borders. And I need create handler
I need to create a WP site which will have multiple subjects. Each subject
I need to create an aspect that I find hard to describe, so let
I'm learning android. Fort testing every aspect of android SDK I need to create
I have a need to create multiple processing threads in a new application. Each
Suppose I have an aspect public aspect Hack { pointcut authHack(String user, String pass):

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.