Bug 174816 - Typing bug in AspectJ
Summary: Typing bug in AspectJ
Status: RESOLVED WORKSFORME
Alias: None
Product: AspectJ
Classification: Tools
Component: Compiler (show other bugs)
Version: 1.5.3   Edit
Hardware: PC Windows XP
: P3 major (vote)
Target Milestone: ---   Edit
Assignee: aspectj inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-02-20 12:33 EST by Radha Jagadeesan CLA
Modified: 2009-01-31 20:03 EST (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Radha Jagadeesan CLA 2007-02-20 12:33:03 EST
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
Comment 1 Matthew Webster CLA 2007-02-21 05:41:28 EST
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"?
Comment 2 Radha Jagadeesan CLA 2007-02-21 10:24:04 EST
(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.  

Comment 3 Matthew Webster CLA 2007-02-21 11:20:06 EST
> 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); 
	}
}
Comment 4 Andrew Clement CLA 2009-01-31 20:03:37 EST
dont believe there is anything for us to do here.