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 4020062
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:10:54+00:00 2026-05-20T10:10:54+00:00

This question would not have existed if AspectJ worked the same way as EJB

  • 0

This question would not have existed if AspectJ worked the same way as EJB interceptors work.

Consider basic scenario the EJB-interceptor way:

@AroundInvoke
public Object log(final InvocationContext ctx) throws Exception {
    // do stuff before
    final Object result = ctx.proceed();
    // do stuff after
    return result;
}

Now I need the Method object that’s being intercepted. Here I can simply do this:

        Method method = ctx.getMethod();

And that’s it, after this I will be inspecting intercepted method’s annotations.

Now I’m working with application which is deployed to servlet container (Tomcat) and hence has no EJBs. AOP in it is implemented using Spring + AspectJ.

The around method looks like this:

@Around("execution(* foo.bar.domain.integration.database.dao..*(..))")
public Object log(final ProceedingJoinPoint pjp) throws Throwable {
    // do stuff before
    final Object result = pjp.proceed();
    // do stuff after
    return result;
}

Here I can no longer do this:

Method method = pjp.getMethod(); // no such method exists

Instead I am forced to get the Method object myself using reflection as follows:

    final String methodName = pjp.getSignature().getName();
    final Object[] arguments = pjp.getArgs();
    final Class[] argumentTypes = getArgumentTypes(arguments);
    final Method method = pjp.getTarget().getClass().getMethod(methodName, argumentTypes);

It works until you want to get a hold of the template method:

@Transactional
public <T extends Identifiable> T save(T t) {
    if (null == t.getId()) {
        create(t);
        return t;
    }
    return update(t);
}

Assume you invoke this method as follows:

Person person = new Person("Oleg");
personService.save(person);

You will receive a:

Caused by: java.lang.NoSuchMethodException: foo.bar.domain.integration.database.dao.EntityService.save(foo.bar.domain.entity.Person)

which is thrown by:

pjp.getTarget().getClass().getMethod()

The problem is that generics do not exist at runtime and the actual method signature is:

public Identifiable save(Identifiable t) {}

final Class[] argumentTypes will contain one element and its type will be Person. So this call:

pjp.getTarget().getClass().getMethod(methodName, argumentTypes);

will be looking up method:

save(Person p)

and will not find it.

So the question is: how do I get instance of java’s template Method object which represents the exact method that has been intercepted?

I guess one of the ways is to do it the brute force way:
get arguments superclasses/interfaces and try getting the method using those types until you no longer get the NoSuchMethodException but that is plain ugly. Is there a normal clean way I can do that?

  • 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-20T10:10:54+00:00Added an answer on May 20, 2026 at 10:10 am
    MethodSignature methodSignature = (MethodSignature) thisJoinPoint.getSignature();
    Method targetMethod = methodSignature.getMethod();
    

    This is not immediately obvious, for which the API is to blame.

    Also see Spring AOP: how to get the annotations of the adviced method. I haven’t tested this myself. The OP there says it solved the issue for him. For another use an additional @Around(annotation...) annotation was needed. (Try setting the target to be only METHOD for example and see how it behaves)

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

Sidebar

Related Questions

Following on from this question what would be the best way to write a
Following on from this question, what would be the best way to represent a
This question would probably apply equally as well to other languages with C-like multi-line
This question is simple. What function would I use in a PHP script to
I saw this question asked about C# I would like an answer for PHP.
This is the sequel to this question . I would like to combine three
Reading through this question on multi-threaded javascript, I was wondering if there would be
I apologize in advance if this question seems remedial. Which would be considered more
I apologise in advance if this question isn't very specific. Would it be possible
This seems like a silly question but I would really like your comments and

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.