/* * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* * @test * @bug 8015701 * @summary javac should generate method parameters correctly. * @compile -parameters CaptureTest.java * @run main CaptureTest */ import java.lang.Class; import java.lang.reflect.Constructor; import java.lang.reflect.Parameter; import java.lang.reflect.Modifier; import java.util.List; import java.util.ArrayList; public class CaptureTest { private static final int SYNTHETIC = 0x1000; private static final int MANDATED = 0x8000; private final String[] Inner_names = { "this$0", "startSize", "val$message" }; private final int[] Inner_modifiers = { Modifier.FINAL | MANDATED, Modifier.FINAL, Modifier.FINAL | SYNTHETIC }; private final Class[] Inner_types = { ParameterNames.class, int.class, String.class }; private final String[] Anon_names = { "this$0", "val$message" }; private final int[] Anon_modifiers = { Modifier.FINAL | MANDATED, Modifier.FINAL | SYNTHETIC }; private final Class[] Anon_types = { ParameterNames.class, String.class }; public static void main(String... args) throws Exception { new CaptureTest().run(); } private void checkConstructor(final Class cls, final String[] names, final int[] modifiers, final Class[] types) { Constructor ctor = cls.getDeclaredConstructors()[0]; Parameter[] params = ctor.getParameters(); if(params.length == names.length) { for(int i = 0; i < names.length; i++) { System.err.println("Testing parameter " + params[i].getName()); if(!params[i].getName().equals(names[i])) error("Expected parameter name " + names[i] + " got " + params[i].getName()); if(params[i].getModifiers() != modifiers[i]) error("Expected parameter modifiers " + Modifier.toString(modifiers[i]) + " got " + Modifier.toString(params[i].getModifiers())); if(!params[i].getType().equals(types[i])) error("Expected parameter type " + types[i] + " got " + params[i].getType()); } } else error("Expected " + names.length + " parameters"); } private void run() throws Exception { checkConstructor(new ParameterNames().makeInner("hello").getClass(), Inner_names, Inner_modifiers, Inner_types); checkConstructor(new ParameterNames().makeAnon("hello").getClass(), Anon_names, Anon_modifiers, Anon_types); checkConstructor(new ParameterNames().makeAnonExtendsInner("hello").getClass(), Inner_names, Inner_modifiers, Inner_types); if(0 != errors) throw new Exception("MethodParameters test failed with " + errors + " errors"); } private void error(final String msg) { System.err.println("Error: " + msg); errors++; } int errors; } class ParameterNames { public ArrayList makeInner(final String message) { class InnerList extends ArrayList { public InnerList(final int startSize) { super(startSize); add(message); } } return new InnerList(2); } public ArrayList makeAnonExtendsInner(final String message) { class InnerList extends ArrayList { public InnerList(final int startSize) { super(startSize); add(message); } } return new InnerList(2) {}; } public List makeAnon(final String message) { return new ArrayList() { public String get(final int index) { if (index == -1) return message; else return super.get(index); } }; } }