test/tools/javac/8015701/AnonymousParameters.java

Thu, 06 Jun 2013 15:38:42 +0100

author
mcimadamore
date
Thu, 06 Jun 2013 15:38:42 +0100
changeset 1814
5b039297151e
parent 1808
8717586f7b05
permissions
-rw-r--r--

Merge

     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  */
    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;
    36 public class AnonymousParameters {
    38     String[] names = {
    39         "this$0",
    40         "val$message"
    41     };
    43     public static void main(String... args) throws Exception {
    44         new AnonymousParameters().run();
    45     }
    47     void run() throws Exception {
    48         Class<?> cls = new ParameterNames().makeInner("hello").getClass();
    49         Constructor<?> ctor = cls.getDeclaredConstructors()[0];
    50         Parameter[] params = ctor.getParameters();
    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");
    62         if(0 != errors)
    63             throw new Exception("MethodParameters test failed with " +
    64                                 errors + " errors");
    65     }
    67     void error(String msg) {
    68         System.err.println("Error: " + msg);
    69         errors++;
    70     }
    72     int errors;
    73 }
    75 class ParameterNames {
    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     }
    85     public static void main(String... args) throws Exception {
    86         ParameterNames test = new ParameterNames();
    87         System.out.println(test.makeInner("Hello").call());
    88     }
    89 }

mercurial