Announcement Announcement Module
Collapse
No announcement yet.
Pointcut for Object.equals() Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Pointcut for Object.equals()

    I've been trying for a few days now (way too long) to write a pointcut for Object.equals(), but with no success.

    I've tried about everything. I'm using LTW with @Aspect. Here's what I've tried:

    Code:
    @Pointcut("execution(boolean equals(Object))")
    public void equalsMethod() {}
    Code:
    @Pointcut("call(boolean equals(Object))")
    public void equalsMethod() {}
    I even tried all methods, and this didn't even pick up the call to Object's equals:

    Code:
    @Pointcut("execution(* *(..))")
    public void allMethods() {}
    I'm beginning to wonder if advice on Object's methods are not possible with AspectJ. The only time I have been able to see anything making it into my pointcuts is when I override the equals method in on my classes which are being weaved by the LTW.


    Here's my entire aspect:
    Code:
    @Aspect
    public class NodeEntityEqualsAspect {
    	private Log logger = LogFactory.getLog(NodeEntityEqualsAspect.class);
    
    	public static NodeEntityEqualsAspect aspectOf() {
    		return new NodeEntityEqualsAspect();
    	}
    
    	@Around("equalsMethod()")
    	public Object evaluateEquals(ProceedingJoinPoint jp) throws Throwable {
    		Throwable exception = null;
    		Object retVal = null;
    
    		try {
    			logger.debug("proceed with original method invocation...");
    			retVal = jp.proceed();
    		} catch (Throwable e) {
    			exception = e;
    			throw e;
    		}
    		return retVal;
    	}
    
    	@Pointcut("execution(boolean Object.equals(Object))")
    	public void equalsMethod() {
    	}
    }
    Last edited by jzcfk9; May 30th, 2011, 12:52 PM.

  • #2
    this behaviour seems pretty normal to me, I wouldn't have thought otherwise. AspectJ works by weaving bytecode inside .class files, either when source is compiled or before the class is loaded. Object is a class of the Java runtime environment and as such neither compile time weaving nor load time weaving are possible, thus Java base classes can't be advised. When you call Object's equals on your own class, the bytecode of equals has thus been imported "as is" and it can't possibly show advice behaviour. But, when you override a method of Object in your own class, then this method can be advised since it's in one of your own files and the bytecode can be changed, either at compile time or at load time.

    Comment


    • #3
      Ok, thanks for the great explanation.

      Comment


      • #4
        I just wanted to let anyone who may follow this question that you can use native AspectJ to accomplish what I was trying to do previously using LTW. The LTW weaving didn't seem to do the trick, but the compile-time weaving using the same pointcut works:

        Code:
        public aspect EqualsAspect
        {
        
          /**
           * Pointcut for calls to equals()
           */
          public pointcut equals():call(boolean equals(Object));
        
        
          /**
           * Around advice for equals() calls
           * 
           * @return
           */
          Object around():equals(){
            //your code here
           }
        }

        Comment

        Working...
        X