View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.bcel.classfile;
21  
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  
24  import java.io.ByteArrayInputStream;
25  import java.io.DataInputStream;
26  import java.io.IOException;
27  
28  import org.apache.bcel.Const;
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   * Tests {@link ConstantDynamic}.
33   *
34   * @see <a href="https://docs.oracle.com/javase/specs/jvms/se25/html/jvms-4.html#jvms-4.4.10">JVM CONSTANT_Dynamic_info specification</a>
35   */
36  class ConstantDynamicTest {
37  
38      /**
39       * Both index fields of {@code CONSTANT_Dynamic_info} are unsigned shorts (u2), so a value above 0x7FFF must not be read as a
40       * negative number, matching {@link ConstantInvokeDynamic}.
41       */
42      @Test
43      void testReadConstantReadsIndicesUnsigned() throws IOException {
44          // CONSTANT_Dynamic, bootstrap_method_attr_index 0x9C40 (40000), name_and_type_index 0xABCD (43981)
45          final byte[] data = {Const.CONSTANT_Dynamic, (byte) 0x9C, 0x40, (byte) 0xAB, (byte) 0xCD};
46          try (DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data))) {
47              final ConstantDynamic c = (ConstantDynamic) Constant.readConstant(dis);
48              assertEquals(40000, c.getBootstrapMethodAttrIndex());
49              assertEquals(43981, c.getNameAndTypeIndex());
50          }
51      }
52  }