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