1 /*
   2  * Copyright (c) 2012, 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  * @summary javac should generate method parameters correctly.
  27  */
  28 
  29 import java.lang.*;
  30 import java.lang.reflect.*;
  31 import java.util.List;
  32 
  33 public class WithoutParameters {
  34 
  35     private static final Class<?>[] qux_types = {
  36         int.class,
  37         Foo.class,
  38         List.class,
  39         List.class,
  40         List.class,
  41         String[].class
  42     };
  43 
  44     public static void main(String argv[]) throws Exception {
  45         int error = 0;
  46         Method[] methods = Foo.class.getMethods();
  47         for(Method m : methods) {
  48             System.err.println("Inspecting method " + m);
  49             Parameter[] parameters = m.getParameters();
  50             if(parameters == null)
  51                 throw new Exception("getParameters should never be null");
  52             for(int i = 0; i < parameters.length; i++) {
  53                     Parameter p = parameters[i];
  54                     if(!p.getDeclaringExecutable().equals(m)) {
  55                         System.err.println(p + ".getDeclaringExecutable != " + m);
  56                         error++;
  57                     }
  58                     if(null == p.getType()) {
  59                         System.err.println(p + ".getType() == null");
  60                         error++;
  61                     }
  62                     if(null == p.getParameterizedType()) {
  63                         System.err.println(p + ".getParameterizedType == null");
  64                         error++;
  65                     }
  66             }
  67             if(m.getName().equals("qux")) {
  68                 if(6 != parameters.length) {
  69                     System.err.println("Wrong number of parameters for qux");
  70                     error++;
  71                 }
  72                 for(int i = 0; i < parameters.length; i++) {
  73                     Parameter p = parameters[i];
  74                     // The getType family work with or without
  75                     // parameter attributes compiled in.
  76                     if(!parameters[i].getType().equals(qux_types[i])) {
  77                         System.err.println("Wrong parameter type for " + parameters[0] + ": expected " + qux_types[i] + ", but got " + parameters[i].getType());
  78                         error++;
  79                     }
  80                 }
  81                 if(!parameters[0].getParameterizedType().equals(int.class)) {
  82                     System.err.println("getParameterizedType for quux is wrong");
  83                     error++;
  84                 }
  85                 if(!parameters[1].getParameterizedType().equals(Foo.class)) {
  86                     System.err.println("getParameterizedType for quux is wrong");
  87                     error++;
  88                 }
  89                 if(!(parameters[2].getParameterizedType() instanceof
  90                      ParameterizedType)) {
  91                     System.err.println("getParameterizedType for l is wrong");
  92                     error++;
  93                 } else {
  94                     ParameterizedType pt =
  95                         (ParameterizedType) parameters[2].getParameterizedType();
  96                     if(!pt.getRawType().equals(List.class)) {
  97                         System.err.println("Raw type for l is wrong");
  98                         error++;
  99                     }
 100                     if(1 != pt.getActualTypeArguments().length) {
 101                         System.err.println("Number of type parameters for l is wrong");
 102                         error++;
 103                     }
 104                     if(!(pt.getActualTypeArguments()[0] instanceof WildcardType)) {
 105                         System.err.println("Type parameter for l is wrong");
 106                         error++;
 107                     }
 108                 }
 109                 if(!(parameters[3].getParameterizedType() instanceof
 110                      ParameterizedType)) {
 111                     System.err.println("getParameterizedType for l2 is wrong");
 112                     error++;
 113                 } else {
 114                     ParameterizedType pt =
 115                         (ParameterizedType) parameters[3].getParameterizedType();
 116                     if(!pt.getRawType().equals(List.class)) {
 117                         System.err.println("Raw type for l2 is wrong");
 118                         error++;
 119                     }
 120                     if(1 != pt.getActualTypeArguments().length) {
 121                         System.err.println("Number of type parameters for l2 is wrong");
 122                         error++;
 123                     }
 124                     if(!(pt.getActualTypeArguments()[0].equals(Foo.class))) {
 125                         System.err.println("Type parameter for l2 is wrong");
 126                         error++;
 127                     }
 128                 }
 129                 if(!(parameters[4].getParameterizedType() instanceof
 130                      ParameterizedType)) {
 131                     System.err.println("getParameterizedType for l3 is wrong");
 132                     error++;
 133                 } else {
 134                     ParameterizedType pt =
 135                         (ParameterizedType) parameters[4].getParameterizedType();
 136                     if(!pt.getRawType().equals(List.class)) {
 137                         System.err.println("Raw type for l3 is wrong");
 138                         error++;
 139                     }
 140                     if(1 != pt.getActualTypeArguments().length) {
 141                         System.err.println("Number of type parameters for l3 is wrong");
 142                         error++;
 143                     }
 144                     if(!(pt.getActualTypeArguments()[0] instanceof WildcardType)) {
 145                         System.err.println("Type parameter for l3 is wrong");
 146                         error++;
 147                     } else {
 148                         WildcardType wt = (WildcardType)
 149                             pt.getActualTypeArguments()[0];
 150                         if(!wt.getUpperBounds()[0].equals(Foo.class)) {
 151                             System.err.println("Upper bounds on type parameter fol l3 is wrong");
 152                             error++;
 153                         }
 154                     }
 155                 }
 156                 if(!parameters[5].isVarArgs()) {
 157                     System.err.println("isVarArg for rest is wrong");
 158                     error++;
 159                 }
 160                 if(!(parameters[5].getParameterizedType().equals(String[].class))) {
 161                     System.err.println("getParameterizedType for rest is wrong");
 162                     error++;
 163                 }
 164 
 165             }
 166         }
 167         if(0 != error)
 168             throw new Exception("Failed " + error + " tests");
 169     }
 170 
 171     public class Foo {
 172         int thing;
 173         public void qux(int quux, Foo quuux,
 174                         List<?> l, List<Foo> l2,
 175                         List<? extends Foo> l3,
 176                         String... rest) {}
 177         public class Inner {
 178             int thang;
 179             public Inner(int theng) {
 180                 thang = theng + thing;
 181             }
 182         }
 183     }
 184 
 185 }