Community
Participate
Working Groups
using an intertype declaration to add a field to a generic class compiles, but the field is not added: class Normal {} class Generic<T> {} aspect Injector { void Normal.method() {} void Generic.method() {} int Normal.field; int Generic.field; void test() { new Normal().method(); // works new Generic<Integer>().method(); // works int normal = new Normal().field; // works int generic = new Generic<Integer>().field; // unresolved field } }
Ok, this fails because of how the compiler represents Parameterized Types. Generic types (Generic<T>) are represented by SourceTypeBindings that are a subclass of ReferenceBinding. Within SourceTypeBinding we have added AspectJ extensions to resolve inter-type declarations. This is the 'memberFinder' that exists in our variant of SourceTypeBinding. A specific parameterized instance of the generic type (Generic<Integer>) is represented by a ParameterizedTypeBinding - this is also a subtype of ReferenceBinding (it is *not* a subtype of SourceTypeBinding). ParameterizedTypeBinding stores a reference to its generic type (SourceTypeBinding) in its 'type' field. Now, ParameterizedTypeBinding knows nothing about intertype field declarations and so when asked for 'field' below it cannot be found. A potential fix is to override getField() in ParameterizedTypeBinding - have it look locally first - if it is not found then delegate to its generic type to find the field (which will result in it finding the ITD'd field - you can't ITD on parameterized types so it doesn't make sense to duplicate the lookup code in ParameterizedTypeBinding). This fix *works* - I just need to think it through a little more and make sure its right. I'm also interested as to why 'new Generic<Integer>().method()' resolves OK - as there is no specialized processing in ParameterizedTypeBinding for it (I dont think)?
Fix checked in as written up - testcases too. waiting on build before closing.
Fix available, see aspectj download page: aspectj-DEVELOPMENT-20050614164300.jar