Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-dev] Pointcutting questions on specific anonymous inner classes

I have attached an example program which will weave a specific anonymous inner class, based mostly on this reported bug <https://bugs.eclipse.org/bugs/show_bug.cgi?id=73050>.

This will suffice for the time being, however, an optimal solution for our problem would be to reference the anonymous inner class specifically, by the exact name that the compiler chose to provide.

Part of the process that we are employing utilizes the BCEL to provide insight into the execution structure of various classes. Thus, we will know the exact name of the inner class, as provided by the compiler (be it ajc, javac, or other).

Are there any plans to support referencing an inner class by specific name (versus a pattern) as a pointcut definition in the future? Does this represent an oversight or a bug in the current pointcut matching routine?

TIA!

--
Keven Ring               | "Oh no,  Not Again..."
The MITRE Corporation    |   Bowl of Petunias -
7515 Colshire Drive      |   The Hitchhikers Guide to the Galaxy
McLean VA 22102-7508     |
PH: (703)883-7026        |

import java.util.Collections;
import java.util.List;
import java.util.Comparator;
import java.util.ArrayList;

public class AnonOuter {
	private Comparator c, c1;
	
	public AnonOuter() {
		c = new Comparator() {
			public int compare(Object o, Object o1) {
			  System.out.println("Comparator$1.compare()");
			  return(((Integer)o).compareTo(o1));
			}
			public boolean equals(Object o) {
			  return false;
			}
		};
		
		c1 = new Comparator() {
		  public int compare(Object o, Object o1) {
		    System.out.println("Comparator$2.compare()");
		    return(-((Integer)o).compareTo(o1));
		  }
		  public boolean equals(Object o) {
		    return false;
		  }
		};
		Integer i = new Integer(10);
		Integer i1 = new Integer(5);
		Integer i2 = new Integer(20);
		
		List list = new ArrayList();
		list.add(i);
		list.add(i1);
		list.add(i2);
		
		System.out.println(list);
		
		Collections.sort(list, c);
		
		System.out.println(list);
		
		Collections.sort(list, c1);
		
		System.out.println(list);
		
		Collections.sort(list, new MyComparator());
		
		System.out.println(list);
	}

	public static void main(String[] args) {
		new AnonOuter();
	}
	
	class MyComparator implements Comparator {
	  public int compare(Object o, Object o1) {
	    System.out.println("Comparator$MyComparator.compare()");
	    return(((Integer)o).compareTo(o1));
	  }
	  
	  public boolean equals(Object o) {
	    return false;
	  }
	}
}
import java.lang.reflect.*;
import java.util.*;

privileged aspect AdviseAnonInner {

	
	pointcut AdviseInnerMethod(Comparator c)
	  : within(AnonOuter) && execution(public int compare(Object, Object)) && target(c);
	
	after(Comparator c) : AdviseInnerMethod(c) {
    System.out.println("After Advised Method ["+
                       thisJoinPoint.getThis().getClass().getName()+"]");
	}
	
	pointcut AdviseInnerMethod2(Comparator c) :
	  within(AnonOuter$MyComparator) && execution(public int compare(Object, Object)) && target(c);
	
	before(Comparator c) : AdviseInnerMethod2(c) {
	  System.out.println("Before Advised Method2 ["+
                       thisJoinPoint.getThis().getClass().getName()+"]");
	}
	
	pointcut AdviseInnerMethod3(Comparator c) :
	  within(AnonOuter.*2) && execution(public int compare(Object, Object)) && target(c);
	
	before(Comparator c) : AdviseInnerMethod3(c) {
	  System.out.println("Before Advised Method3 ["+
	                     thisJoinPoint.getThis().getClass().getName()+"]");
	}
}

Back to the top