< prev index next >

test/java/lang/annotation/TypeAnnotationReflection.java

Print this page




   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8004698 8007073 8022343 8054304 8058595
  27  * @summary Unit test for type annotations
  28  */
  29 
  30 import java.util.*;
  31 import java.lang.annotation.*;
  32 import java.lang.reflect.*;
  33 import java.io.Serializable;
  34 
  35 public class TypeAnnotationReflection {
  36     public static void main(String[] args) throws Exception {
  37         testSuper();
  38         testInterfaces();
  39         testReturnType();
  40         testNested();
  41         testArray();
  42         testRunException();
  43         testClassTypeVarBounds();
  44         testMethodTypeVarBounds();
  45         testFields();
  46         testClassTypeVar();


 341         annos = as[1].getAnnotations();
 342         check(annos.length == 2);
 343         check(((TypeAnno)annos[0]).value().equals("I"));
 344         check(as[1].getAnnotation(TypeAnno2.class).value().equals("I2"));
 345     }
 346 
 347     private static void testNestedParameterizedType() throws Exception {
 348         Method m = TestParameterizedType.class.getDeclaredMethod("foo2", (Class<?>[])null);
 349         AnnotatedType ret = m.getAnnotatedReturnType();
 350         Annotation[] annos;
 351         annos = ret.getAnnotations();
 352         check(annos.length == 1);
 353         check(((TypeAnno)annos[0]).value().equals("I"));
 354 
 355         AnnotatedType[] args = ((AnnotatedParameterizedType)ret).getAnnotatedActualTypeArguments();
 356         check(args.length == 1);
 357         annos = args[0].getAnnotations();
 358         check(annos.length == 2);
 359         check(((TypeAnno)annos[0]).value().equals("I1"));
 360         check(args[0].getAnnotation(TypeAnno2.class).value().equals("I2"));

























 361     }
 362 
 363     private static void testWildcardType() throws Exception {
 364         Method m = TestWildcardType.class.getDeclaredMethod("foo", (Class<?>[])null);
 365         AnnotatedType ret = m.getAnnotatedReturnType();
 366         AnnotatedType[] t;
 367         t = ((AnnotatedParameterizedType)ret).getAnnotatedActualTypeArguments();
 368         check(t.length == 1);
 369         ret = t[0];
 370 
 371         Field f = TestWildcardType.class.getDeclaredField("f1");
 372         AnnotatedWildcardType w = (AnnotatedWildcardType)((AnnotatedParameterizedType)f
 373             .getAnnotatedType()).getAnnotatedActualTypeArguments()[0];
 374         t = w.getAnnotatedLowerBounds();
 375         check(t.length == 0);
 376         t = w.getAnnotatedUpperBounds();
 377         check(t.length == 1);
 378         Annotation[] annos;
 379         annos = t[0].getAnnotations();
 380         check(annos.length == 1);


 549 class Params {
 550     public void noParams() {}
 551     public void onlyAnnotated(@TypeAnno("1") String s1, @TypeAnno("2") String s2, @TypeAnno("3a") @TypeAnno2("3b") String s3) {}
 552     public void mixed(@TypeAnno("1") String s1, String s2, @TypeAnno("3a") @TypeAnno2("3b") String s3) {}
 553     public void unAnnotated(String s1, String s2, String s3) {}
 554 }
 555 
 556 abstract class TestWildcardType {
 557     public <T> List<? super T> foo() { return null;}
 558     public Class<@TypeAnno("1") ? extends @TypeAnno("2") Annotation> f1;
 559     public Class<@TypeAnno("3") ? super @TypeAnno("4") Annotation> f2;
 560     public Class<@TypeAnno("5") ?> f3;
 561 }
 562 
 563 abstract class TestParameterizedType implements @TypeAnno("M") Map<@TypeAnno("S")String, @TypeAnno("I") @TypeAnno2("I2")Integer> {
 564     public ParameterizedOuter<String>.ParameterizedInner<Integer> foo() {return null;}
 565     public @TypeAnno("O") ParameterizedOuter<@TypeAnno("S1") @TypeAnno2("S2") String>.
 566                @TypeAnno("I") ParameterizedInner<@TypeAnno("I1") @TypeAnno2("I2")Integer> foo2() {
 567         return null;
 568     }



 569 }
 570 
 571 class ParameterizedOuter <T> {
 572     class ParameterizedInner <U> {}
 573 }
 574 
 575 abstract class TestClassArray extends @TypeAnno("extends") @TypeAnno2("extends2") Object
 576     implements @TypeAnno("implements serializable") @TypeAnno2("implements2 serializable") Serializable,
 577     Readable,
 578     @TypeAnno("implements cloneable") @TypeAnno2("implements2 cloneable") Cloneable {
 579     public @TypeAnno("return4") Object @TypeAnno("return1") [][] @TypeAnno("return3")[] foo() { return null; }
 580 }
 581 
 582 abstract class TestClassNested {
 583     public @TypeAnno("Outer") Outer.@TypeAnno("Inner")Inner @TypeAnno("array")[] foo() { return null; }
 584 }
 585 
 586 class Outer {
 587     class Inner {
 588     }




   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8004698 8007073 8022343 8054304 8057804 8058595
  27  * @summary Unit test for type annotations
  28  */
  29 
  30 import java.util.*;
  31 import java.lang.annotation.*;
  32 import java.lang.reflect.*;
  33 import java.io.Serializable;
  34 
  35 public class TypeAnnotationReflection {
  36     public static void main(String[] args) throws Exception {
  37         testSuper();
  38         testInterfaces();
  39         testReturnType();
  40         testNested();
  41         testArray();
  42         testRunException();
  43         testClassTypeVarBounds();
  44         testMethodTypeVarBounds();
  45         testFields();
  46         testClassTypeVar();


 341         annos = as[1].getAnnotations();
 342         check(annos.length == 2);
 343         check(((TypeAnno)annos[0]).value().equals("I"));
 344         check(as[1].getAnnotation(TypeAnno2.class).value().equals("I2"));
 345     }
 346 
 347     private static void testNestedParameterizedType() throws Exception {
 348         Method m = TestParameterizedType.class.getDeclaredMethod("foo2", (Class<?>[])null);
 349         AnnotatedType ret = m.getAnnotatedReturnType();
 350         Annotation[] annos;
 351         annos = ret.getAnnotations();
 352         check(annos.length == 1);
 353         check(((TypeAnno)annos[0]).value().equals("I"));
 354 
 355         AnnotatedType[] args = ((AnnotatedParameterizedType)ret).getAnnotatedActualTypeArguments();
 356         check(args.length == 1);
 357         annos = args[0].getAnnotations();
 358         check(annos.length == 2);
 359         check(((TypeAnno)annos[0]).value().equals("I1"));
 360         check(args[0].getAnnotation(TypeAnno2.class).value().equals("I2"));
 361 
 362         // check type args
 363         Field f = TestParameterizedType.class.getDeclaredField("theField");
 364         AnnotatedParameterizedType fType = (AnnotatedParameterizedType)f.getAnnotatedType();
 365         args = fType.getAnnotatedActualTypeArguments();
 366         check(args.length == 1);
 367         annos = args[0].getAnnotations();
 368         check(annos.length == 1);
 369         check(((TypeAnno2)annos[0]).value().equals("Map Arg"));
 370         check(args[0].getAnnotation(TypeAnno2.class).value().equals("Map Arg"));
 371 
 372         // check outer type type args
 373         fType = (AnnotatedParameterizedType)fType.getAnnotatedOwnerType();
 374         args = fType.getAnnotatedActualTypeArguments();
 375         check(args.length == 1);
 376         annos = args[0].getAnnotations();
 377         check(annos.length == 1);
 378         check(((TypeAnno2)annos[0]).value().equals("String Arg"));
 379         check(args[0].getAnnotation(TypeAnno2.class).value().equals("String Arg"));
 380 
 381         // check outer type normal type annotations
 382         annos = fType.getAnnotations();
 383         check(annos.length == 1);
 384         check(((TypeAnno)annos[0]).value().equals("FieldOuter"));
 385         check(fType.getAnnotation(TypeAnno.class).value().equals("FieldOuter"));
 386     }
 387 
 388     private static void testWildcardType() throws Exception {
 389         Method m = TestWildcardType.class.getDeclaredMethod("foo", (Class<?>[])null);
 390         AnnotatedType ret = m.getAnnotatedReturnType();
 391         AnnotatedType[] t;
 392         t = ((AnnotatedParameterizedType)ret).getAnnotatedActualTypeArguments();
 393         check(t.length == 1);
 394         ret = t[0];
 395 
 396         Field f = TestWildcardType.class.getDeclaredField("f1");
 397         AnnotatedWildcardType w = (AnnotatedWildcardType)((AnnotatedParameterizedType)f
 398             .getAnnotatedType()).getAnnotatedActualTypeArguments()[0];
 399         t = w.getAnnotatedLowerBounds();
 400         check(t.length == 0);
 401         t = w.getAnnotatedUpperBounds();
 402         check(t.length == 1);
 403         Annotation[] annos;
 404         annos = t[0].getAnnotations();
 405         check(annos.length == 1);


 574 class Params {
 575     public void noParams() {}
 576     public void onlyAnnotated(@TypeAnno("1") String s1, @TypeAnno("2") String s2, @TypeAnno("3a") @TypeAnno2("3b") String s3) {}
 577     public void mixed(@TypeAnno("1") String s1, String s2, @TypeAnno("3a") @TypeAnno2("3b") String s3) {}
 578     public void unAnnotated(String s1, String s2, String s3) {}
 579 }
 580 
 581 abstract class TestWildcardType {
 582     public <T> List<? super T> foo() { return null;}
 583     public Class<@TypeAnno("1") ? extends @TypeAnno("2") Annotation> f1;
 584     public Class<@TypeAnno("3") ? super @TypeAnno("4") Annotation> f2;
 585     public Class<@TypeAnno("5") ?> f3;
 586 }
 587 
 588 abstract class TestParameterizedType implements @TypeAnno("M") Map<@TypeAnno("S")String, @TypeAnno("I") @TypeAnno2("I2")Integer> {
 589     public ParameterizedOuter<String>.ParameterizedInner<Integer> foo() {return null;}
 590     public @TypeAnno("O") ParameterizedOuter<@TypeAnno("S1") @TypeAnno2("S2") String>.
 591             @TypeAnno("I") ParameterizedInner<@TypeAnno("I1") @TypeAnno2("I2")Integer> foo2() {
 592         return null;
 593     }
 594 
 595     public @TypeAnno("FieldOuter") ParameterizedOuter<@TypeAnno2("String Arg") String>.
 596             @TypeAnno("FieldInner")ParameterizedInner<@TypeAnno2("Map Arg")Map> theField;
 597 }
 598 
 599 class ParameterizedOuter <T> {
 600     class ParameterizedInner <U> {}
 601 }
 602 
 603 abstract class TestClassArray extends @TypeAnno("extends") @TypeAnno2("extends2") Object
 604     implements @TypeAnno("implements serializable") @TypeAnno2("implements2 serializable") Serializable,
 605     Readable,
 606     @TypeAnno("implements cloneable") @TypeAnno2("implements2 cloneable") Cloneable {
 607     public @TypeAnno("return4") Object @TypeAnno("return1") [][] @TypeAnno("return3")[] foo() { return null; }
 608 }
 609 
 610 abstract class TestClassNested {
 611     public @TypeAnno("Outer") Outer.@TypeAnno("Inner")Inner @TypeAnno("array")[] foo() { return null; }
 612 }
 613 
 614 class Outer {
 615     class Inner {
 616     }


< prev index next >