test/tools/javac/8015701/AnonymousParameters.java

Print this page




  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 }


  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.List;
  35 import java.util.ArrayList;
  36 
  37 public class AnonymousParameters {
  38 
  39     String[] names = {
  40         "this$0",
  41         "startSize",
  42         "val$message"
  43     };
  44 
  45     public static void main(String... args) throws Exception {
  46         new AnonymousParameters().run();
  47     }
  48 
  49     void run() throws Exception {
  50         Class<?> cls = new ParameterNames().makeInner("hello").getClass();
  51         Constructor<?> ctor = cls.getDeclaredConstructors()[0];
  52         Parameter[] params = ctor.getParameters();
  53 
  54         if(params.length == 3) {
  55             for(int i = 0; i < 3; i++) {
  56                 System.err.println("Testing parameter " + params[i].getName());
  57                 if(!params[i].getName().equals(names[i]))
  58                     error("Expected parameter name " + names[i] +
  59                           " got " + params[i].getName());
  60             }
  61         } else
  62             error("Expected 3 parameters");
  63 
  64         if(0 != errors)
  65             throw new Exception("MethodParameters test failed with " +
  66                                 errors + " errors");
  67     }
  68 
  69     void error(String msg) {
  70         System.err.println("Error: " + msg);
  71         errors++;
  72     }
  73 
  74     int errors;
  75 }
  76 
  77 class ParameterNames {
  78 
  79     public ArrayList<String> makeInner(final String message) {
  80         class List extends ArrayList<String> {
  81             public List(int startSize) {
  82                 super(startSize);
  83                 add(message);
  84             }
  85         };
  86 
  87         return new List(2);
  88     }
  89 
  90     public static void main(String... args) throws Exception {
  91         ParameterNames test = new ParameterNames();
  92         System.out.println(test.makeInner("Hello").get(0));
  93     }
  94 }