1 /*
   2  * Copyright (c) 2014, 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 8055063
  27  * @compile -parameters InnerClassToString.java
  28  * @run main InnerClassToString
  29  * @summary javac should generate method parameters correctly.
  30  */
  31 
  32 import java.lang.reflect.Constructor;
  33 import java.lang.reflect.Parameter;
  34 import java.util.Set;
  35 
  36 // Test copied and expanded from webbug group report.
  37 public class InnerClassToString {
  38     private static final Class<?>[] genericParamClasses = new Class<?>[] {
  39         InnerClassToString.class, Set.class
  40     };
  41 
  42     private static final Class<?>[] nongenericParamClasses = new Class<?>[] {
  43         InnerClassToString.class, String.class
  44     };
  45 
  46     public static void main(String[] args) throws Exception {
  47         int errors = 0;
  48         final Constructor<MyEntity> genericConstructor =
  49             MyEntity.class.getConstructor(InnerClassToString.class, Set.class);
  50         final Parameter[] genericParams = genericConstructor.getParameters();
  51         for (int i = 0; i < genericParams.length; i++) {
  52             final Parameter parameter = genericParams[i];
  53             System.out.println(parameter.toString());
  54 
  55             if (!parameter.getType().equals(genericParamClasses[i])) {
  56                 errors++;
  57                 System.err.println("Expected type " + genericParamClasses[i] +
  58                                    " but got " + parameter.getType());
  59             }
  60 
  61             System.out.println(parameter.getParameterizedType()); 
  62             System.out.println(parameter.getAnnotatedType()); 
  63         }
  64         Constructor<MyEntity> nongenericConstructor =
  65             MyEntity.class.getConstructor(InnerClassToString.class, String.class);
  66         final Parameter[] nongenericParams = nongenericConstructor.getParameters();
  67         for (int i = 0; i < nongenericParams.length; i++) {
  68             final Parameter parameter = nongenericParams[i];
  69             System.out.println(parameter.toString());
  70 
  71             if (!parameter.getType().equals(nongenericParamClasses[i])) {
  72                 errors++;
  73                 System.err.println("Expected type " + nongenericParamClasses[i] +
  74                                    " but got " + parameter.getType());
  75             }
  76 
  77             System.out.println(parameter.getParameterizedType()); 
  78             System.out.println(parameter.getAnnotatedType()); 
  79         }
  80 
  81         if (errors != 0)
  82             throw new RuntimeException(errors + " errors in test");
  83     }
  84 
  85     public class MyEntity {
  86         public MyEntity(Set<?> names) {}
  87         public MyEntity(String names) {}
  88     }
  89 }