1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   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 8024915
  27  */
  28 
  29 import java.lang.reflect.AnnotatedType;
  30 import java.util.Arrays;
  31 
  32 public class GetAnnotatedReceiverType {
  33     public void method() {}
  34     public void method0(GetAnnotatedReceiverType this) {}
  35     public static void method4() {}
  36 
  37     class Inner0 {
  38         public Inner0() {}
  39     }
  40 
  41     class Inner1 {
  42         public Inner1(GetAnnotatedReceiverType GetAnnotatedReceiverType.this) {}
  43     }
  44 
  45     private static int failures = 0;
  46     private static int tests = 0;
  47 
  48     public static void main(String[] args) throws NoSuchMethodException {
  49         checkEmptyAT(GetAnnotatedReceiverType.class.getMethod("method").getAnnotatedReceiverType(),
  50                 "getAnnotatedReceiverType for \"method\" should return an empty AnnotatedType");
  51         checkEmptyAT(Inner0.class.getConstructor(GetAnnotatedReceiverType.class).getAnnotatedReceiverType(),
  52                 "getAnnotatedReceiverType for a ctor without a \"this\" should return an empty AnnotatedType");
  53 
  54         checkEmptyAT(GetAnnotatedReceiverType.class.getMethod("method0").getAnnotatedReceiverType(),
  55                 "getAnnotatedReceiverType for \"method0\" should return an empty AnnotatedType");
  56         checkEmptyAT(Inner1.class.getConstructor(GetAnnotatedReceiverType.class).getAnnotatedReceiverType(),
  57                 "getAnnotatedReceiverType for a ctor with a \"this\" should return an empty AnnotatedType");
  58 
  59         checkNull(GetAnnotatedReceiverType.class.getMethod("method4").getAnnotatedReceiverType(),
  60                 "getAnnotatedReceiverType() on a static method should return null");
  61 
  62         if (failures != 0)
  63             throw new RuntimeException("Test failed, see log for details");
  64         else if (tests != 5)
  65             throw new RuntimeException("Not all cases ran, failing");
  66     }
  67 
  68     private static void checkNull(Object o, String msg) {
  69         if (o != null) {
  70             failures++;
  71             System.err.println(msg);
  72         }
  73         tests++;
  74     }
  75 
  76     private static void checkEmptyAT(AnnotatedType a, String msg) {
  77         if (a.getAnnotations().length != 0) {
  78             failures++;
  79             System.err.print(msg);
  80         }
  81         tests++;
  82     }
  83 }