DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > AspectJ: Using Advised Class Fields

AspectJ: Using Advised Class Fields

A few quick code snippets to explain how to call other methods or use fields in AspectJ.

Jaime Metcher user avatar by
Jaime Metcher
·
Aug. 04, 16 · Java Zone · Tutorial
Like (1)
Save
Tweet
3.71K Views

Join the DZone community and get the full member experience.

Join For Free

A short post to clarify something that was a little mysterious from the documentation.

AspectJ around advice typically looks something like:

pointcut myPointCut( ) : execution(void my.package.myClass.myMethod());
 
void around(): myPointCut() {
    // do some stuff
    proceed(); // call the advised method
    // do some other stuff
    
}


What if I want to call other methods or use fields from myClass in the advice? There are a few moving parts here:

pointcut myPointCut( ) : execution(void my.package.myClass.myMethod());
 
void around(my.package.myClass myClass): target(myClass) && myPointCut() {
    myClass.method1(); // do some stuff
    proceed(myClass); // call the advised method
    myClass.publicField = null; // do some other stuff
    
}


To break it down:

  1. Add a parameter to around() with the type of the advised class.
  2. Use the AspectJ target() method to populate that parameter.
  3. Use the parameter value within the advice however you like. But note that you’re limited to public accessible methods and members – despite what you might think, the advice isnot within the lexical scope of the advised class.
  4. Add the parameter value as the first parameter to proceed().

This example is for an advised method with no parameters. If the method has parameters:

pointcut myPointCut( ) : execution(void my.package.myClass.myMethod(my.package.ParamClass param));
 
void around(my.package.myClass myClass, my.package.ParamClass param): target(myClass) 
        && args(param) && myPointCut() {
    myClass.method1(); // do some stuff
    proceed(myClass, param); // call the advised method
    myClass.publicField = null; // do some other stuff
    
}


AspectJ

Published at DZone with permission of Jaime Metcher, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • XAMPP vs WAMP: Which Local Server Is Best?
  • Agile Micromanagement — Seriously?
  • What Makes the Architecture of Geo-Distributed Apps Different?
  • API Security Weekly: Issue 173

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo