]> source.dussan.org Git - aspectj.git/commitdiff
test cases for all examples in the new generics chapter of the ajdk
authoracolyer <acolyer>
Wed, 17 Aug 2005 08:37:11 +0000 (08:37 +0000)
committeracolyer <acolyer>
Wed, 17 Aug 2005 08:37:11 +0000 (08:37 +0000)
tests/java5/generics/ajdk/ArgsExamples.aj [new file with mode: 0644]
tests/java5/generics/ajdk/BridgeMethodExamples.aj [new file with mode: 0644]
tests/java5/generics/ajdk/ErasureMatching.aj [new file with mode: 0644]
tests/java5/generics/ajdk/MixedParameterizedAndTypeVariables.aj [new file with mode: 0644]
tests/java5/generics/ajdk/SignatureWildcards.aj [new file with mode: 0644]
tests/java5/generics/ajdk/SimpleParameterizedTypeExamples.aj [new file with mode: 0644]

diff --git a/tests/java5/generics/ajdk/ArgsExamples.aj b/tests/java5/generics/ajdk/ArgsExamples.aj
new file mode 100644 (file)
index 0000000..833d9d1
--- /dev/null
@@ -0,0 +1,63 @@
+import java.util.*;
+
+class C {
+    
+    public void foo(List<String> listOfStrings) {}
+        
+    public void bar(List<Double> listOfDoubles) {}
+        
+    public void goo(List<? extends Number> listOfSomeNumberType) {}
+  
+}
+
+aspect A {
+
+    before(List<Double> listOfDoubles) : execution(* C.*(..)) && args(listOfDoubles) {
+       for (Double d : listOfDoubles) {
+          // do something
+       }
+    }           
+    
+    @org.aspectj.lang.annotation.SuppressAjWarnings
+    before(List<Double> listOfDoubles) : execution(* C.*(..)) && args(listOfDoubles) {
+        for (Double d : listOfDoubles) {
+           // do something
+        }
+     }         
+    
+    @org.aspectj.lang.annotation.SuppressAjWarnings("uncheckedArgument")
+    before(List<Double> listOfDoubles) : execution(* C.*(..)) && args(listOfDoubles) {
+        for (Double d : listOfDoubles) {
+           // do something
+        }
+     }         
+    
+    before(List<Double> listOfDoubles) : execution(* C.*(List<Double>)) && args(listOfDoubles) {
+        for (Double d : listOfDoubles) {
+           // do something
+        }
+     }      
+    
+}
+
+public aspect ArgsExamples {
+       
+       before() : args(List) && execution(* *(..)) {
+               System.out.println("args(List)");
+       }
+       
+       before() : args(List<String>) && execution(* *(..)) {
+               System.out.println("args List of String");
+       }
+       
+       before() : args(List<Double>) && execution(* *(..)) {
+               System.out.println("args List of Double");
+       }
+       
+       public static void main(String[] args) {
+               C c = new C();
+               c.foo(new ArrayList<String>());
+               c.bar(new ArrayList<Double>());
+               c.goo(new ArrayList<Float>());
+       }
+}
\ 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 (file)
index 0000000..cbf3e64
--- /dev/null
@@ -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<T> {
+       
+       public T foo(T someObject) {
+       return someObject;
+    }
+
+}
+
+class SubGeneric<N extends Number> extends Generic<N> {
+
+    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 (file)
index 0000000..8f0686d
--- /dev/null
@@ -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> T first(List<T> ts) { return null; }
+            
+    /** instance generic method */
+    <T extends Number> T max(T t1, T t2) { return t1; }
+                
+}
+          
+class G<T> {
+           
+       // field with parameterized type
+       T myData = null;
+       // method with parameterized return type
+     public List<T> 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 (file)
index 0000000..3037253
--- /dev/null
@@ -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<String>)) : "mixed match";
+       declare warning : execution(* *(List<String>)) : "params only match";
+       declare warning : execution(List<Object> G.foo(List<String>)) : "wrong erasure";
+               
+}
+
+class G<T> {
+       
+       List<T> foo(List<String> 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 (file)
index 0000000..7b93949
--- /dev/null
@@ -0,0 +1,19 @@
+import java.util.List;
+
+public aspect SignatureWildcards {
+       
+       declare warning : execution(* C.*(List)) : "any list";
+       declare warning : execution(* C.*(List<? extends Number>)) : "only foo";
+       declare warning : execution(* C.*(List<?>)) : "some list";
+       declare warning : execution(* C.*(List<? extends Object+>)) : "any list with upper bound";
+}
+
+class C {
+
+    public void foo(List<? extends Number> listOfSomeNumberType) {}
+    
+    public void bar(List<?> listOfSomeType) {}
+    
+    public void goo(List<Double> 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 (file)
index 0000000..1ecb5cb
--- /dev/null
@@ -0,0 +1,41 @@
+import java.util.*;
+
+public aspect SimpleParameterizedTypeExamples {
+       
+       declare warning : get(List Foo.myStrings) : "get myStrings 1";
+       declare warning : get(List<String> Foo.myStrings) : "get myStrings 2";
+       declare warning : get(List<Number> *) : "get myStrings 3 - no match";
+
+       declare warning : get(List Foo.myFloats) : "get myFloats 1";
+       declare warning : get(List<Float> *) : "get myFloats 2";
+       declare warning : get(List<Number+> *) : "get myFloats 3";
+       declare warning : get(List<Double> *) : "get myFloats 4 - no match";
+       
+       declare warning : execution(List get*(..)) : "getter 1";
+       declare warning : execution(List<*> get*(..)) : "getter 2";
+       declare warning : execution(List<String> get*(..)) : "getter 3";
+       declare warning : execution(List<Number+> get*(..)) : "getter 4";
+
+       declare warning : call(* addStrings(List)) : "call 1";
+       declare warning : call(* addStrings(List<String>)) : "call 2";
+       declare warning : call(* addStrings(List<Number>)) : "call 3 - no match";
+       
+       void bar() {
+               Foo f = new Foo();
+               f.addStrings(null);
+       }
+}
+
+class Foo {
+    
+    List<String> myStrings;
+    List<Float>  myFloats;
+        
+    public List<String> getStrings() { return myStrings; }
+    public List<Float> getFloats() { return myFloats; }
+        
+    public void addStrings(List<String> evenMoreStrings) {
+       myStrings.addAll(evenMoreStrings);   
+    }
+        
+}
\ No newline at end of file