Community
Participate
Working Groups
class C { String f() { return "1"; } Integer g() { return new Integer(1); } public static void main(String[] args) { System.out.println(new C().g()); new C().f(); } } aspect WrongReturnType { Object around(): execution(* C.*()) { System.out.println("Aspect"); return new Integer(2); } } This program compiles and gives a runtime type error. As far as we can tell this is unintentional. Radha. [Developed with Alan Jeffrey/James Riely] rjagadeesan@cs.depaul.edu jriely@cs.depaul.edu ajeffrey@bell-labs.com
AspectJ is very lenient when compiling around advice, if you had used either before or after advice with "Object" as the return type you wouldn't have got a match. The result is you can get runtime (or weave-time) errors. The two methods you are advising expect String and Integer respectively. You are returning Integer[] which cannot be cast to either. What would you suggest AspectJ do to solve this "bug"?
(In reply to comment #1) I cant say that I agree with the underlying design decision --- in harmony with Java's strong type-checking, I'd have expected AspectJ's typechecker to reject this program. Thanks for the clarification, however: clearly, I was misguided in my assumption that this behavior was unintentional.
> You are > returning Integer[] which cannot be cast to either. What would you suggest > AspectJ do to solve this "bug"? Firstly let me correct my first comment: you are actually returning an Integer not an Integer[]. Secondly I will retract my statement about leniency. AspectJ has correctly matched and advised methods that accept Object as a return type. The restriction lies in that AspectJ does not and cannot scan your code to see if the object you actually return can be cast to the return type of the advised method. The type safety is in your hands. You are returning an Integer so it only makes sense to advice methods that also return Integer. To take advantage of the type checking in AspectJ you should have done the following: aspect WrongReturnType { Object around(): execution(Integer+ C.*()) { System.out.println("Aspect"); return new Integer(2); } }
dont believe there is anything for us to do here.