|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, 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. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | + |
| 26 | +/* |
| 27 | + * @test |
| 28 | + * @bug 8304837 |
| 29 | + * @summary Testing BoundAttributes |
| 30 | + * @run junit BoundAttributeTest |
| 31 | + */ |
| 32 | +import jdk.internal.classfile.Attributes; |
| 33 | +import jdk.internal.classfile.ClassModel; |
| 34 | +import jdk.internal.classfile.Classfile; |
| 35 | +import jdk.internal.classfile.CodeBuilder; |
| 36 | +import jdk.internal.classfile.attribute.MethodParameterInfo; |
| 37 | +import jdk.internal.classfile.attribute.MethodParametersAttribute; |
| 38 | +import org.junit.jupiter.api.Test; |
| 39 | +import org.opentest4j.AssertionFailedError; |
| 40 | + |
| 41 | +import java.lang.constant.ClassDesc; |
| 42 | +import java.lang.constant.ConstantDescs; |
| 43 | +import java.lang.constant.MethodTypeDesc; |
| 44 | +import java.util.List; |
| 45 | +import java.util.Optional; |
| 46 | + |
| 47 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 48 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 49 | + |
| 50 | +class BoundAttributeTest { |
| 51 | + |
| 52 | + @Test |
| 53 | + void testReadMethodParametersAttributeWithoutParameterName() { |
| 54 | + // build a simple method: void method(int) |
| 55 | + MethodTypeDesc methodTypeDesc = MethodTypeDesc.of(ConstantDescs.CD_void, ConstantDescs.CD_int); |
| 56 | + byte[] raw = Classfile.build(ClassDesc.of("TestClass"), builder -> { |
| 57 | + builder.withMethod("method", methodTypeDesc, 0, mb -> { |
| 58 | + mb.withCode(CodeBuilder::return_); |
| 59 | + // add a MethodParameters attribute without name for the parameter |
| 60 | + mb.with(MethodParametersAttribute.of(MethodParameterInfo.ofParameter(Optional.empty(), 0))); |
| 61 | + }); |
| 62 | + }); |
| 63 | + ClassModel model = Classfile.parse(raw); |
| 64 | + MethodParametersAttribute methodParametersAttribute = model.methods().get(0) |
| 65 | + .findAttribute(Attributes.METHOD_PARAMETERS) |
| 66 | + .orElseThrow(() -> new AssertionFailedError("Attribute not present")); |
| 67 | + // MethodParametersAttribute#parameters() materializes the parameters |
| 68 | + List<MethodParameterInfo> parameters = assertDoesNotThrow(methodParametersAttribute::parameters); |
| 69 | + assertTrue(parameters.get(0).name().isEmpty()); |
| 70 | + } |
| 71 | +} |
0 commit comments