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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:59:55+00:00 2026-06-13T06:59:55+00:00

Is it possible to write a effective pointcut that matches a method that changes

  • 0

Is it possible to write a effective pointcut that matches a method that changes a class variable of a specific class type?
The point of doing this is that my classes have a lastModificationDate that I want to update to the latest date whenever a class variable is changed.

Example of method:

public void stupidMethod() {

 ...
 for (int i = 0; i < 100; i++) this.var = whatever;
 ...
} <--- I want to match here

Currently I have this, but it is not very optimal:

after(SimpleEntity entity) : set(* *.*) && target(entity) && !within(SimpleEntityAspect)
  • 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-06-13T06:59:57+00:00Added an answer on June 13, 2026 at 6:59 am

    No. getand set pointcuts exist for members only, but not for local variables. Consequently, your example will only match member variable assignments for SimpleEntity objects. If this is what you want to do, please rephrase your question’s heading and content so as to make clear what you really want to achieve. Please also provide some more code context, such as e.g. the relevant part of the type declaration for SimpleEntity.

    My best guess for now is that you want to match the exit point of methods in which a certain type’s members are changed. If you also tell us what exactly you want to do in your advice (e.g. print the method name or the assigned value etc.), we might be able to better help you.


    Update: Okay, I have found a solution which does what you want, using pertarget aspect instantiation plus ITD (inter-type declaration). I have not tested performance or memory consumption, I am leaving that up to you.

    Sample entity class:

    public class SimpleEntity {
        private static int currentId = 1;
    
        private int id;
        private String name;
        private long lastModification;
    
        public SimpleEntity(String name) {
            this.id = currentId++;
            this.name = name;
        }
    
        public void stupidMethod(final int count) {
            for (int i = 0; i < count; i++)
                name = name.replaceFirst("_[0-9]+$", "") + "_" + i;
        }
    
        public int tripleValue(final int value) {
            return 3 * value;
        }
    
        @Override
        public String toString() {
            return "SimpleEntity [id=" + id + ", name=" + name + ", lastModification=" + lastModification + "]";
        }
    }
    

    Sample application class creating and using entities:

    public class Application {
        public static void main(String[] args) {
            SimpleEntity entity1 = new SimpleEntity("Adam");
            entity1.stupidMethod(3);
            entity1.tripleValue(11);
            entity1.stupidMethod(3);
            SimpleEntity entity2 = new SimpleEntity("Eve");
            entity2.stupidMethod(3);
            entity1.tripleValue(22);
            entity2.stupidMethod(3);
        }
    }
    

    Aspect doing what was requested by Piotr Blasiak:

    public privileged aspect SetterCallingMethodAspect pertarget(entitySetter(SimpleEntity)) {
        private static interface MemberChangeDetector {}
        private boolean MemberChangeDetector.changed;
    
        declare parents : SimpleEntity implements MemberChangeDetector;
    
        pointcut entitySetter(SimpleEntity entity) :
            set (* SimpleEntity+.*) && target(entity) && !within(SetterCallingMethodAspect);
        pointcut constructorExecution() :
            execution(*.new(..)) && !cflow(adviceexecution());
        pointcut methodExecution() :
            execution(* *(..)) && !cflow(adviceexecution());
    
        after(SimpleEntity entity) : entitySetter(entity) {
            entity.changed = true;
            System.out.println(this + ",  " + thisJoinPointStaticPart + "  ->  " + entity);
        }
    
        after(SimpleEntity entity) : if(entity.changed) && target(entity)
            && (constructorExecution() || methodExecution())
        {
            entity.changed = false;
            entity.lastModification = System.nanoTime();
            System.out.println(
                this + ",  " + thisJoinPointStaticPart +
                "  ->  update lastModification to " + entity.lastModification
            );
        }
    }
    

    Sample console output:

    SetterCallingMethodAspect@bb6ab6,  set(int SimpleEntity.id)  ->  SimpleEntity [id=1, name=null, lastModification=0]
    SetterCallingMethodAspect@bb6ab6,  set(String SimpleEntity.name)  ->  SimpleEntity [id=1, name=Adam, lastModification=0]
    SetterCallingMethodAspect@bb6ab6,  execution(SimpleEntity(String))  ->  update lastModification to 1863715110885880
    SetterCallingMethodAspect@bb6ab6,  set(String SimpleEntity.name)  ->  SimpleEntity [id=1, name=Adam_0, lastModification=1863715110885880]
    SetterCallingMethodAspect@bb6ab6,  set(String SimpleEntity.name)  ->  SimpleEntity [id=1, name=Adam_1, lastModification=1863715110885880]
    SetterCallingMethodAspect@bb6ab6,  set(String SimpleEntity.name)  ->  SimpleEntity [id=1, name=Adam_2, lastModification=1863715110885880]
    SetterCallingMethodAspect@bb6ab6,  execution(void SimpleEntity.stupidMethod(int))  ->  update lastModification to 1863715112627443
    SetterCallingMethodAspect@bb6ab6,  set(String SimpleEntity.name)  ->  SimpleEntity [id=1, name=Adam_0, lastModification=1863715112627443]
    SetterCallingMethodAspect@bb6ab6,  set(String SimpleEntity.name)  ->  SimpleEntity [id=1, name=Adam_1, lastModification=1863715112627443]
    SetterCallingMethodAspect@bb6ab6,  set(String SimpleEntity.name)  ->  SimpleEntity [id=1, name=Adam_2, lastModification=1863715112627443]
    SetterCallingMethodAspect@bb6ab6,  execution(void SimpleEntity.stupidMethod(int))  ->  update lastModification to 1863715114328497
    SetterCallingMethodAspect@12d03f9,  set(int SimpleEntity.id)  ->  SimpleEntity [id=2, name=null, lastModification=0]
    SetterCallingMethodAspect@12d03f9,  set(String SimpleEntity.name)  ->  SimpleEntity [id=2, name=Eve, lastModification=0]
    SetterCallingMethodAspect@12d03f9,  execution(SimpleEntity(String))  ->  update lastModification to 1863715120762834
    SetterCallingMethodAspect@12d03f9,  set(String SimpleEntity.name)  ->  SimpleEntity [id=2, name=Eve_0, lastModification=1863715120762834]
    SetterCallingMethodAspect@12d03f9,  set(String SimpleEntity.name)  ->  SimpleEntity [id=2, name=Eve_1, lastModification=1863715120762834]
    SetterCallingMethodAspect@12d03f9,  set(String SimpleEntity.name)  ->  SimpleEntity [id=2, name=Eve_2, lastModification=1863715120762834]
    SetterCallingMethodAspect@12d03f9,  execution(void SimpleEntity.stupidMethod(int))  ->  update lastModification to 1863715121338606
    SetterCallingMethodAspect@12d03f9,  set(String SimpleEntity.name)  ->  SimpleEntity [id=2, name=Eve_0, lastModification=1863715121338606]
    SetterCallingMethodAspect@12d03f9,  set(String SimpleEntity.name)  ->  SimpleEntity [id=2, name=Eve_1, lastModification=1863715121338606]
    SetterCallingMethodAspect@12d03f9,  set(String SimpleEntity.name)  ->  SimpleEntity [id=2, name=Eve_2, lastModification=1863715121338606]
    SetterCallingMethodAspect@12d03f9,  execution(void SimpleEntity.stupidMethod(int))  ->  update lastModification to 1863715121829729
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to write generic class with one constructor which explicitly defines type
Is it possible to write GLSL ES fragment shaders under iOS that generate multiple
Is it possible to write a method in Scala which returns an object of
Is it possible to write this query in codeigniter active records? $Main_Nav_Query = mysql_query(SELECT
Is it possible to write a program that will change the phone numbers a
if this possible to write a com control or activex in C# and use
Possible Duplicate: What are the differences between struct and class in C++ This question
Is possible to write language agnostic Robotium tests? For example, if you use a
Is it possible to write a program to do things you constantly do on
Is it possible to write a servlet filter to take inspect HTTP response codes?

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.