|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, 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 | +import java.lang.invoke.MethodHandleProxies; |
| 25 | +import java.lang.invoke.MethodHandles; |
| 26 | +import java.lang.reflect.Constructor; |
| 27 | +import java.lang.reflect.Executable; |
| 28 | +import java.lang.reflect.Parameter; |
| 29 | +import java.util.stream.Stream; |
| 30 | + |
| 31 | +import org.junit.jupiter.params.ParameterizedTest; |
| 32 | +import org.junit.jupiter.params.provider.MethodSource; |
| 33 | + |
| 34 | +import static org.junit.jupiter.api.Assertions.*; |
| 35 | + |
| 36 | +/* |
| 37 | + * @test |
| 38 | + * @bug 8357728 |
| 39 | + * @summary Synthetic parameter names should not be retained. |
| 40 | + * @modules java.base/java.lang.reflect:+open |
| 41 | + * @run junit SyntheticNameRetention |
| 42 | + */ |
| 43 | +public class SyntheticNameRetention { |
| 44 | + |
| 45 | + class Inner { |
| 46 | + Inner() {} |
| 47 | + } |
| 48 | + |
| 49 | + public interface NameGetter { |
| 50 | + String getRawName(Parameter parameter); |
| 51 | + } |
| 52 | + static final NameGetter GETTER; |
| 53 | + |
| 54 | + static { |
| 55 | + try { |
| 56 | + var lookup = MethodHandles.privateLookupIn(Parameter.class, MethodHandles.lookup()); |
| 57 | + GETTER = MethodHandleProxies.asInterfaceInstance(NameGetter.class, lookup.findGetter(Parameter.class, "name", String.class)); |
| 58 | + } catch (ReflectiveOperationException ex) { |
| 59 | + throw new ExceptionInInitializerError(ex); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + static Stream<Executable> methods() throws Throwable { |
| 64 | + return Stream.of(Inner.class.getDeclaredConstructor(SyntheticNameRetention.class), // Has MethodParameters with flags |
| 65 | + SyntheticNameRetention.class.getDeclaredMethod("test", Executable.class)); // No MethodParameters |
| 66 | + } |
| 67 | + |
| 68 | + @ParameterizedTest |
| 69 | + @MethodSource("methods") |
| 70 | + public void test(Executable exec) { |
| 71 | + var params = exec.getParameters(); |
| 72 | + for (int i = 0; i < params.length; i++) { |
| 73 | + var param = params[i]; |
| 74 | + assertEquals("arg" + i, param.getName(), "name " + i); |
| 75 | + assertFalse(param.isNamePresent(), "name present " + i); |
| 76 | + assertNull(GETTER.getRawName(param), "raw name " + i); |
| 77 | + boolean mandated = exec instanceof Constructor<?> && i == 0; |
| 78 | + assertEquals(mandated, param.isImplicit(), "mandated " + i); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments