From d7bb145d6e9ea76fa2e9cc1648ab0869ca5e6c2c Mon Sep 17 00:00:00 2001 From: acolyer Date: Wed, 17 Aug 2005 08:37:11 +0000 Subject: test cases for all examples in the new generics chapter of the ajdk --- tests/java5/generics/ajdk/ArgsExamples.aj | 63 ++++++++++++++++++++++ tests/java5/generics/ajdk/BridgeMethodExamples.aj | 29 ++++++++++ tests/java5/generics/ajdk/ErasureMatching.aj | 33 ++++++++++++ .../ajdk/MixedParameterizedAndTypeVariables.aj | 15 ++++++ tests/java5/generics/ajdk/SignatureWildcards.aj | 19 +++++++ .../ajdk/SimpleParameterizedTypeExamples.aj | 41 ++++++++++++++ 6 files changed, 200 insertions(+) create mode 100644 tests/java5/generics/ajdk/ArgsExamples.aj create mode 100644 tests/java5/generics/ajdk/BridgeMethodExamples.aj create mode 100644 tests/java5/generics/ajdk/ErasureMatching.aj create mode 100644 tests/java5/generics/ajdk/MixedParameterizedAndTypeVariables.aj create mode 100644 tests/java5/generics/ajdk/SignatureWildcards.aj create mode 100644 tests/java5/generics/ajdk/SimpleParameterizedTypeExamples.aj (limited to 'tests/java5/generics') diff --git a/tests/java5/generics/ajdk/ArgsExamples.aj b/tests/java5/generics/ajdk/ArgsExamples.aj new file mode 100644 index 000000000..833d9d154 --- /dev/null +++ b/tests/java5/generics/ajdk/ArgsExamples.aj @@ -0,0 +1,63 @@ +import java.util.*; + +class C { + + public void foo(List listOfStrings) {} + + public void bar(List listOfDoubles) {} + + public void goo(List listOfSomeNumberType) {} + +} + +aspect A { + + before(List listOfDoubles) : execution(* C.*(..)) && args(listOfDoubles) { + for (Double d : listOfDoubles) { + // do something + } + } + + @org.aspectj.lang.annotation.SuppressAjWarnings + before(List listOfDoubles) : execution(* C.*(..)) && args(listOfDoubles) { + for (Double d : listOfDoubles) { + // do something + } + } + + @org.aspectj.lang.annotation.SuppressAjWarnings("uncheckedArgument") + before(List listOfDoubles) : execution(* C.*(..)) && args(listOfDoubles) { + for (Double d : listOfDoubles) { + // do something + } + } + + before(List listOfDoubles) : execution(* C.*(List)) && args(listOfDoubles) { + for (Double d : listOfDoubles) { + // do something + } + } + +} + +public aspect ArgsExamples { + + before() : args(List) && execution(* *(..)) { + System.out.println("args(List)"); + } + + before() : args(List) && execution(* *(..)) { + System.out.println("args List of String"); + } + + before() : args(List) && execution(* *(..)) { + System.out.println("args List of Double"); + } + + public static void main(String[] args) { + C c = new C(); + c.foo(new ArrayList()); + c.bar(new ArrayList()); + c.goo(new ArrayList()); + } +} \ No newline at end of file diff --git a/tests/java5/generics/ajdk/BridgeMethodExamples.aj b/tests/java5/generics/ajdk/BridgeMethodExamples.aj new file mode 100644 index 000000000..cbf3e6411 --- /dev/null +++ b/tests/java5/generics/ajdk/BridgeMethodExamples.aj @@ -0,0 +1,29 @@ +public aspect BridgeMethodExamples { + + declare warning : execution(Object SubGeneric.foo(Object)) : "no match"; + declare warning : execution(Object Generic.foo(Object)) : "double match"; + declare warning : call(Object SubGeneric.foo(Object)) : "match"; + + void foo() { + SubGeneric rawType = new SubGeneric(); + rawType.foo("hi"); // call to bridge method (will result in a runtime failure in this case) + Object n = new Integer(5); + rawType.foo(n); // call to bridge method that would succeed at runtime + } +} + +class Generic { + + public T foo(T someObject) { + return someObject; + } + +} + +class SubGeneric extends Generic { + + public N foo(N someNumber) { + return someNumber; + } + +} \ No newline at end of file diff --git a/tests/java5/generics/ajdk/ErasureMatching.aj b/tests/java5/generics/ajdk/ErasureMatching.aj new file mode 100644 index 000000000..8f0686ddf --- /dev/null +++ b/tests/java5/generics/ajdk/ErasureMatching.aj @@ -0,0 +1,33 @@ +import java.util.List; + +public aspect ErasureMatching { + + declare warning : execution(static Object Utils.first(List)) : "static generic method match"; + + declare warning : execution(Number Utils.max(Number, Number)) : "instance generic method match"; + + declare warning : execution(public List G.getAllDataItems()) : "method in generic type match"; + + declare warning : set(Object G.myData) : "field in generic type match"; + +} + +class Utils { + + /** static generic method */ + static T first(List ts) { return null; } + + /** instance generic method */ + T max(T t1, T t2) { return t1; } + +} + +class G { + + // field with parameterized type + T myData = null; + + // method with parameterized return type + public List getAllDataItems() { return null; } + +} \ No newline at end of file diff --git a/tests/java5/generics/ajdk/MixedParameterizedAndTypeVariables.aj b/tests/java5/generics/ajdk/MixedParameterizedAndTypeVariables.aj new file mode 100644 index 000000000..3037253f5 --- /dev/null +++ b/tests/java5/generics/ajdk/MixedParameterizedAndTypeVariables.aj @@ -0,0 +1,15 @@ +import java.util.List; +public aspect MixedParameterizedAndTypeVariables { + + declare warning : execution(List G.foo(List)) : "erasure match"; + declare warning : execution(List G.foo(List)) : "mixed match"; + declare warning : execution(* *(List)) : "params only match"; + declare warning : execution(List G.foo(List)) : "wrong erasure"; + +} + +class G { + + List foo(List ls) { return null; } + +} \ No newline at end of file diff --git a/tests/java5/generics/ajdk/SignatureWildcards.aj b/tests/java5/generics/ajdk/SignatureWildcards.aj new file mode 100644 index 000000000..7b93949f8 --- /dev/null +++ b/tests/java5/generics/ajdk/SignatureWildcards.aj @@ -0,0 +1,19 @@ +import java.util.List; + +public aspect SignatureWildcards { + + declare warning : execution(* C.*(List)) : "any list"; + declare warning : execution(* C.*(List)) : "only foo"; + declare warning : execution(* C.*(List)) : "some list"; + declare warning : execution(* C.*(List)) : "any list with upper bound"; +} + +class C { + + public void foo(List listOfSomeNumberType) {} + + public void bar(List listOfSomeType) {} + + public void goo(List listOfDoubles) {} + + } \ No newline at end of file diff --git a/tests/java5/generics/ajdk/SimpleParameterizedTypeExamples.aj b/tests/java5/generics/ajdk/SimpleParameterizedTypeExamples.aj new file mode 100644 index 000000000..1ecb5cb26 --- /dev/null +++ b/tests/java5/generics/ajdk/SimpleParameterizedTypeExamples.aj @@ -0,0 +1,41 @@ +import java.util.*; + +public aspect SimpleParameterizedTypeExamples { + + declare warning : get(List Foo.myStrings) : "get myStrings 1"; + declare warning : get(List Foo.myStrings) : "get myStrings 2"; + declare warning : get(List *) : "get myStrings 3 - no match"; + + declare warning : get(List Foo.myFloats) : "get myFloats 1"; + declare warning : get(List *) : "get myFloats 2"; + declare warning : get(List *) : "get myFloats 3"; + declare warning : get(List *) : "get myFloats 4 - no match"; + + declare warning : execution(List get*(..)) : "getter 1"; + declare warning : execution(List<*> get*(..)) : "getter 2"; + declare warning : execution(List get*(..)) : "getter 3"; + declare warning : execution(List get*(..)) : "getter 4"; + + declare warning : call(* addStrings(List)) : "call 1"; + declare warning : call(* addStrings(List)) : "call 2"; + declare warning : call(* addStrings(List)) : "call 3 - no match"; + + void bar() { + Foo f = new Foo(); + f.addStrings(null); + } +} + +class Foo { + + List myStrings; + List myFloats; + + public List getStrings() { return myStrings; } + public List getFloats() { return myFloats; } + + public void addStrings(List evenMoreStrings) { + myStrings.addAll(evenMoreStrings); + } + +} \ No newline at end of file -- cgit v1.2.3