Community
Participate
Working Groups
public aspect InstanceOfArgs { private static Map val; private static Object pval; public static void main(String args[]) { setVal(null); setMap(null); val = null; pval = null; val = new HashMap(); } public static void setVal(Object aVal) { val = (Map)aVal; } public static void setMap(Map aVal) { val = aVal; } void around(Map map) : set(* *.*) && args(map) { System.out.println("around set firing for "+map+" at "+thisJoinPoint+", "+thisJoinPoint.getSourceLocation()); proceed(map); } void around(Map map) : call(* set*(*)) && args(map) { System.out.println("around call firing for "+map+" at "+thisJoinPoint); proceed(map); } } Actual Output around set firing for null at set(Map InstanceOfArgs.val), InstanceOfArgs.aj:16 around call firing for null at call(void InstanceOfArgs.setMap(Map)) around set firing for null at set(Map InstanceOfArgs.val), InstanceOfArgs.aj:20 around set firing for null at set(Map InstanceOfArgs.val), InstanceOfArgs.aj:10 around set firing for {} at set(Map InstanceOfArgs.val), InstanceOfArgs.aj:12 Expected output: around set firing for {} at set(Map InstanceOfArgs.val), InstanceOfArgs.aj:12
this is an old chestnut. I'm marking M4 so that I make sure to come back and respond properly
AspectJ is working as designed here. If null did not match, writing advice that checked for null arguments etc. would not be possible. The semantics are that null matches iff the static type of the argument guarantees that any argument is an instance of the target type without the need for a runtime check. Thus the sets for val (of type Map) all match, because a field of type Map is guaranteed to have a (possibly null) Map argument. The sets for pval do not match as a field of type Object is not guaranteed to have a Map argument. See the rigourous defence of these semantics by Erik in bug 68603. *** This bug has been marked as a duplicate of 68603 ***