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