Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Generics in AJ5

Excellent clarifications Adrian. Thanks a lot.

I already seen where I was mistaking :-S.

:alex |.::the_mindstorm::.|

#: Adrian Colyer changed the world a bit at a time by saying on  9/1/2005 10:26 AM :#
On 01/09/05, Alexandru Popescu <the.mindstorm.mailinglist@xxxxxxxxx> wrote:
Hi!

I was re-reading the Chapter 3: Generics in AspectJ 5 and I have some questions that I wasn't able
to clarify:

1/
[quote]
AspectJ 5 does not allow the use of type variables in pointcut expressions and type patterns.
Instead, members that use type parameters as part of their signature are matched by their erasure.
[/quote]

isn't this conflicting with:

[quote]
Pointcut matching can be further restricted to match only given parameterizations of parameter types
(methods and constructors), return types (methods) and field types (fields). This is achieved by
specifying a parameterized type pattern at the appropriate point in the signature pattern.
[/quote]

[self answering]

you cannot write:

pointcut nameType(): get(List<T> Foo.myStrings)
but you can write:
pointcut nameParametrized(): get(List<String> Foo.myStrings)

That's right. Imagine some type Foo<T> with a method:

T myFavourite(List<T> someTs) { ... }

This method uses the type variable T as part of its signature, and
AspectJ 5 has no syntax for saying "type variable" in a type pattern
(if you write "T", it means the type called T - probably resulting in
an invalid absolute type name error). A signature pattern

Object myFavourite(List)
can be used to match against this method.

Now imagine some other method

String myFavourite(List<String> names) {....}

this doesn't use any type variables in its signature, but does use
*parameterized* types. AspectJ 5 can match against this method using
either the erasure or the parameterized versions of the signature:

String myFavourite(List)  // matches
String myFavourite(List<String>) // also matches

2/
[quote]
execution(* C.*(List<? extends Object+>))
     matches both the execution of foo and the execution of bar since the upper bound of List<?> is
implicitly Object.
[/quote]

Q1: isn't enought in the pointcut to say List<? extends Object>

The declaration of foo in that example is :

public void foo(List<? extends Number> listOfSomeNumberType) {}

List<? extends Object> does not match List<? extends Number> (they are
different types), this is just like the fact that

foo(Object)  does not match a method declared as void foo(Number
aNumber) {...}, but
foo(Object+) will match such a method.

Q2: upper bound of List<?> is not List? (my understanding of generics may be weak here but what is
the difference between List<?> and List<T>?)

"upper bound" refers to the most specific type that elements in the
collection can be guaranteed to be an instance of. With a List<?> the
most specific type you can guarantee is Object.

List<?> is a parameterized type indicating that a bound variable of
this type is a list containing elements of some unknown type. It might
be a List<String>, or it might be a List<Number>, we don't know.
Because of this it is allowed to remove elements from the List, but
only as "Object"s (since Object is the upper bound of ?), and it is
not permitted to add elements to the list (since we don't know what
type of element they are supposed to be).

List<T> is also a parameterized type, but the type parameter is a type
variable. Although T could be bound to anything (assuming the type
variable T is unbounded), from the perspective of the generic type or
method we do know exactly what the type of the elements in the list is
: it's T! Thus we can remove elements from the list as "T"s, and also
add "T"s to the list.




Some small corrections to the document:
1/
[quote]
(List<String>, List<?>, List<? extends Number> and so on.
[/quote]

should be
[quote]
(List<String>, List<?>, List<? extends Number> and so on).
[/quote]

2/
[quote]
           class G<T> {

                List<T> foo(List<String ls) { return null; }

           }
[/quote]

should be
[quote]
           class G<T> {

                List<T> foo(List<String> ls) { return null; }

           }
[/quote]


Now fixed in the docbook sources. Thanks, Adrian.


thanks for your time,
:alex |.::the_mindstorm::.|
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users





Back to the top