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

  • SEARCH
  • Home
  • 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 4271194
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:24:32+00:00 2026-05-21T07:24:32+00:00

I was wondering if there is anyway of determining what method was active when

  • 0

I was wondering if there is anyway of determining what method was active when this aspect was triggered. I found the method JointPoint.getSourceLocation() which returns the source code line. I realised I could try to parse that source file and try to determine the method from that… but there seems like there ought to be a better way.

Basically, if has the following code:

 class Monkey{
      public void feed( Banana b ){
           b.eat()
      }
 }
 class Banana{
    private static int bananaIdGen;
    public final int bananaId = ++bananaIdGen;
    private boolean eaten = false;

    public void eat(){
         if( eaten )
              throw IllegalStateException( "Already eaten." );
         else
              eaten = true;
    }
 }

I’d like to have an aspect like

 @After("call(void Banana.eat()) && target(bbb)") 
 public void whereEaten( Banana bbb ){
      ....
 }

Where in the body I could print out something like `”Banana 47 eaten by org.example.Monkey(org.example.Banana)”.

The reason is that that I would like to throw an error if a method has been called by a method without a certain annotation on it. For that I’d need to have the Method of the method.

  • 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-21T07:24:33+00:00Added an answer on May 21, 2026 at 7:24 am

    I suppose this question with its thisEnclosingJoinPointStaticPart can help you.

    But the best way to solve your problem is to construct the right join point using withincode and call pointcuts as in example here (see Contract Enforcement section). This way you prevent calls from methods with or without certain annotation.

    The list of pointcuts is available here.

    What about your sample code lets introduce annotation:

    package com.riapriority.test;
    
    public @interface CanEat {
    }
    

    Our Banana class:

    package com.riapriority.test;
    
    public class Banana {
        private static int bananaIdGen;
        private final int bananaId = ++bananaIdGen;
        private boolean eaten = false;
    
        public void eat() {
            if (eaten)
                throw new IllegalStateException("Already eaten.");
            else
                eaten = true;
        }
    
        public int getBananaId() {
            return bananaId;
        }
    }
    

    Our Monkey class with corresponding annotation:

    package com.riapriority.test;
    
    public class Monkey {
        @CanEat
        public void feed(Banana b) {
            b.eat();
        }
    }
    

    Our Airplane class which, of course, can’t eat and so hasn’t @CanEat annotation:

    package com.riapriority.test;
    
    public class Airplane {
        public void feed(Banana b) {
            b.eat();
        }
    }
    

    Our simple main class for testing:

    package com.riapriority.test;
    
    public class WithincodeTest {
        public static void main(String[] args) {
            Banana monkeyBanana = new Banana();
            Monkey monkey = new Monkey();
            monkey.feed(monkeyBanana);
            try {
                monkey.feed(monkeyBanana);
            } catch (IllegalStateException e) {
                System.out.println(e.getMessage());
            }
            Banana airplaneBanana = new Banana();
            Airplane airplane = new Airplane();
            try {
                airplane.feed(airplaneBanana);
            } catch (IllegalStateException e) {
                System.out.println(e.getMessage());
            }
        }
    }
    

    So we need to avoid eating bananas by Airplane. And the corresponding aspect to obtain this:

    package com.riapriority.test;
    
    import java.text.MessageFormat;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.annotation.AfterReturning;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    
    @Aspect
    public class EatingAspect {
        @Pointcut("call(void Banana.eat()) && target(banana)")
        public void eatCall(Banana banana) {
        }
    
        @Pointcut("@withincode(CanEat)")
        public void canEat() {
        }
    
        @AfterReturning("eatCall(banana) && canEat()")
        public void whereEaten(Banana banana,
                               JoinPoint.EnclosingStaticPart thisEnclosingStaticPart) {
            System.out.println(MessageFormat.format("Banana {0} eaten by {1}", banana.getBananaId(),
                    thisEnclosingStaticPart.getSignature()));
        }
    
        @Before("eatCall(banana) && !canEat()")
        public void forbidEating(Banana banana,
                                 JoinPoint.EnclosingStaticPart thisEnclosingStaticPart) {
            throw new IllegalStateException(MessageFormat.format("Can''t eat {0} by {1}", banana.getBananaId(),
                    thisEnclosingStaticPart.getSignature()));
        }
    }
    

    So now our test main class produces the right output:

    Banana 1 eaten by void com.riapriority.test.Monkey.feed(Banana)
    Already eaten.
    Can't eat 2 by void com.riapriority.test.Airplane.feed(Banana)
    

    Hope this solves your problem.

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

Sidebar

Related Questions

Hello i was just wondering is there anyway to reverse this code so that
I'm wondering is there anyway to create 'Get-Set' method only once that can be
I am using python unit test module. I am wondering is there anyway to
I was wondering if there was anyway to use functions like ReadFileEx that require
I was wondering if there's anyway to filter a set using NSPredicate sorted by
I am wondering is there any way to execute following shell script, which waits
i was wondering if there was anyway of combining two characters to form one
I was wondering if there's anyway to get a 'dynamic path' into a .js
I was wondering if there is anyway to lock and unlock the clipboard from
I was just wondering is there anyway to stop application being terminated at all

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.