langtools/test/tools/javac/8015701/AnonymousParameters.java
changeset 19671 fa7b69611589
parent 19650 9ad5128fe73f
parent 19670 197392292aad
child 19672 2be9465c1cb2
equal deleted inserted replaced
19650:9ad5128fe73f 19671:fa7b69611589
     1 /*
       
     2  * Copyright (c) 2012, 2013, 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 8015701
       
    27  * @summary javac should generate method parameters correctly.
       
    28  * @compile -parameters AnonymousParameters.java
       
    29  * @run main AnonymousParameters
       
    30  */
       
    31 import java.lang.Class;
       
    32 import java.lang.reflect.Constructor;
       
    33 import java.lang.reflect.Parameter;
       
    34 import java.util.concurrent.Callable;
       
    35 
       
    36 public class AnonymousParameters {
       
    37 
       
    38     String[] names = {
       
    39         "this$0",
       
    40         "val$message"
       
    41     };
       
    42 
       
    43     public static void main(String... args) throws Exception {
       
    44         new AnonymousParameters().run();
       
    45     }
       
    46 
       
    47     void run() throws Exception {
       
    48         Class<?> cls = new ParameterNames().makeInner("hello").getClass();
       
    49         Constructor<?> ctor = cls.getDeclaredConstructors()[0];
       
    50         Parameter[] params = ctor.getParameters();
       
    51 
       
    52         if(params.length == 2) {
       
    53             for(int i = 0; i < 2; i++) {
       
    54                 System.err.println("Testing parameter " + params[i].getName());
       
    55                 if(!params[i].getName().equals(names[i]))
       
    56                     error("Expected parameter name " + names[i] +
       
    57                           " got " + params[i].getName());
       
    58             }
       
    59         } else
       
    60             error("Expected 2 parameters");
       
    61 
       
    62         if(0 != errors)
       
    63             throw new Exception("MethodParameters test failed with " +
       
    64                                 errors + " errors");
       
    65     }
       
    66 
       
    67     void error(String msg) {
       
    68         System.err.println("Error: " + msg);
       
    69         errors++;
       
    70     }
       
    71 
       
    72     int errors;
       
    73 }
       
    74 
       
    75 class ParameterNames {
       
    76 
       
    77     public Callable<String> makeInner(final String message) {
       
    78         return new Callable<String>()  {
       
    79             public String call() throws Exception {
       
    80                 return message;
       
    81             }
       
    82         };
       
    83     }
       
    84 
       
    85     public static void main(String... args) throws Exception {
       
    86         ParameterNames test = new ParameterNames();
       
    87         System.out.println(test.makeInner("Hello").call());
       
    88     }
       
    89 }