test/java/lang/reflect/Parameter/WithParameters.java

Print this page




   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     };


  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;


   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.annotation.*;
  33 import java.lang.reflect.*;
  34 import java.util.List;
  35 
  36 public class WithParameters {
  37 
  38     private static final Class<?>[] qux_types = {
  39         int.class, Foo.class, List.class, List.class, List.class, String[].class
  40     };
  41 
  42     private static final String[] qux_names = {
  43         "quux", "quuux", "l", "l2", "l3", "rest"
  44     };


  67                     }
  68             }
  69             if(m.getName().equals("qux")) {
  70                 if(6 != parameters.length) {
  71                     System.err.println("Wrong number of parameters for qux");
  72                     error++;
  73                 }
  74                 for(int i = 0; i < parameters.length; i++) {
  75                     Parameter p = parameters[i];
  76                     if(!parameters[i].getName().equals(qux_names[i])) {
  77                         System.err.println("Wrong parameter name for " + parameters[i]);
  78                         error++;
  79                     }
  80                     // The getType family work with or without
  81                     // parameter attributes compiled in.
  82                     if(!parameters[i].getType().equals(qux_types[i])) {
  83                         System.err.println("Wrong parameter type for " + parameters[0] + ": expected " + qux_types[i] + ", but got " + parameters[i].getType());
  84                         error++;
  85                     }
  86                 }
  87                 if(!parameters[0].toString().equals("final int quux")) {
  88                     System.err.println("toString for quux is wrong, expected \"final int quux\", got \"" + parameters[0] + "\"");
  89                     error++;
  90                 }
  91                 if(parameters[0].getModifiers() != Modifier.FINAL) {
  92                     System.err.println("quux is not final");
  93                     error++;
  94                 }
  95                 if(parameters[0].isVarArgs()) {
  96                     System.err.println("isVarArg for quux is wrong");
  97                     error++;
  98                 }
  99                 if(!parameters[0].getParameterizedType().equals(int.class)) {
 100                     System.err.println("getParameterizedType for quux is wrong");
 101                     error++;
 102                 }
 103                 if(!parameters[1].toString().equals("WithParameters$Foo quuux")) {
 104                     System.err.println("toString for quuux is wrong, expected \"WithParameters$Foo quuux\", got \"" + parameters[1] + "\"");
 105                     error++;
 106                 }
 107                 if(parameters[1].isVarArgs()) {
 108                     System.err.println("isVarArg for quuux is wrong");
 109                     error++;
 110                 }
 111                 if(!parameters[1].getParameterizedType().equals(Foo.class)) {
 112                     System.err.println("getParameterizedType for quuux is wrong");
 113                     error++;
 114                 }
 115                 Annotation[] anns = parameters[1].getAnnotations();
 116                 if(1 != anns.length) {
 117                     System.err.println("getAnnotations missed an annotation");
 118                     error++;
 119                 } else if(!anns[0].annotationType().equals(Thing.class)) {
 120                     System.err.println("getAnnotations has the wrong annotation");
 121                     error++;
 122                 }
 123                 if(!parameters[2].toString().equals("java.util.List<?> l")) {
 124                     System.err.println("toString for l is wrong, expected \"java.util.List<?> l\", got \"" + parameters[2] + "\"");
 125                     error++;
 126                 }
 127                 if(parameters[2].isVarArgs()) {
 128                     System.err.println("isVarArg for l is wrong");
 129                     error++;
 130                 }
 131                 if(!(parameters[2].getParameterizedType() instanceof
 132                      ParameterizedType)) {
 133                     System.err.println("getParameterizedType for l is wrong");
 134                     error++;
 135                 } else {
 136                     ParameterizedType pt =
 137                         (ParameterizedType) parameters[2].getParameterizedType();
 138                     if(!pt.getRawType().equals(List.class)) {
 139                         System.err.println("Raw type for l is wrong");
 140                         error++;
 141                     }
 142                     if(1 != pt.getActualTypeArguments().length) {
 143                         System.err.println("Number of type parameters for l is wrong");
 144                         error++;
 145                     }
 146                     if(!(pt.getActualTypeArguments()[0] instanceof WildcardType)) {
 147                         System.err.println("Type parameter for l is wrong");
 148                         error++;
 149                     }
 150                 }
 151                 if(!parameters[3].toString().equals("java.util.List<WithParameters$Foo> l2")) {
 152                     System.err.println("toString for l2 is wrong, expected \"java.util.List<WithParameters$Foo> l2\", got \"" + parameters[3] + "\"");
 153                     error++;
 154                 }
 155                 if(parameters[3].isVarArgs()) {
 156                     System.err.println("isVarArg for l2 is wrong");
 157                     error++;
 158                 }
 159                 if(!(parameters[3].getParameterizedType() instanceof
 160                      ParameterizedType)) {
 161                     System.err.println("getParameterizedType for l2 is wrong");
 162                     error++;
 163                 } else {
 164                     ParameterizedType pt =
 165                         (ParameterizedType) parameters[3].getParameterizedType();
 166                     if(!pt.getRawType().equals(List.class)) {
 167                         System.err.println("Raw type for l2 is wrong");
 168                         error++;
 169                     }
 170                     if(1 != pt.getActualTypeArguments().length) {
 171                         System.err.println("Number of type parameters for l2 is wrong");
 172                         error++;
 173                     }
 174                     if(!(pt.getActualTypeArguments()[0].equals(Foo.class))) {
 175                         System.err.println("Type parameter for l2 is wrong");
 176                         error++;
 177                     }
 178                 }
 179                 if(!parameters[4].toString().equals("java.util.List<? extends WithParameters$Foo> l3")) {
 180                     System.err.println("toString for l3 is wrong, expected \"java.util.List<? extends WithParameters$Foo> l3\", got \"" + parameters[3] + "\"");
 181                     error++;
 182                 }
 183                 if(parameters[4].isVarArgs()) {
 184                     System.err.println("isVarArg for l3 is wrong");
 185                     error++;
 186                 }
 187                 if(!(parameters[4].getParameterizedType() instanceof
 188                      ParameterizedType)) {
 189                     System.err.println("getParameterizedType for l3 is wrong");
 190                     error++;
 191                 } else {
 192                     ParameterizedType pt =
 193                         (ParameterizedType) parameters[4].getParameterizedType();
 194                     if(!pt.getRawType().equals(List.class)) {
 195                         System.err.println("Raw type for l3 is wrong");
 196                         error++;
 197                     }
 198                     if(1 != pt.getActualTypeArguments().length) {
 199                         System.err.println("Number of type parameters for l3 is wrong");
 200                         error++;
 201                     }
 202                     if(!(pt.getActualTypeArguments()[0] instanceof WildcardType)) {
 203                         System.err.println("Type parameter for l3 is wrong");
 204                         error++;
 205                     } else {
 206                         WildcardType wt = (WildcardType)
 207                             pt.getActualTypeArguments()[0];
 208                         if(!wt.getUpperBounds()[0].equals(Foo.class)) {
 209                             System.err.println("Upper bounds on type parameter fol l3 is wrong");
 210                             error++;
 211                         }
 212                     }
 213                 }
 214                 if(!parameters[5].toString().equals("java.lang.String... rest")) {
 215                     System.err.println("toString for rest is wrong, expected \"java.lang.String... rest\", got \"" + parameters[5] + "\"");
 216                     error++;
 217                 }
 218                 if(!parameters[5].isVarArgs()) {
 219                     System.err.println("isVarArg for rest is wrong");
 220                     error++;
 221                 }
 222                 if(!(parameters[5].getParameterizedType().equals(String[].class))) {
 223                     System.err.println("getParameterizedType for rest is wrong");
 224                     error++;
 225                 }
 226             }
 227         }
 228         if(0 != error)
 229             throw new Exception("Failed " + error + " tests");
 230     }
 231 
 232     void test(int test) {}
 233 
 234     public class Foo {
 235         int thing;