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  package org.apache.bcel.generic;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertNotEquals;
23  
24  import org.junit.jupiter.api.Test;
25  
26  /**
27   * Tests that the constant pool dedup tables do not conflate distinct references whose names contain one of the ASCII
28   * delimiter characters. The JVMS only forbids {@code . ; [ /} (and {@code < >} for members) in names, so a class or
29   * member name may legally contain {@code : # &}.
30   */
31  class ConstantPoolGenKeyCollisionTest {
32  
33      @Test
34      void testFieldrefNamesContainingDelimiter() {
35          final ConstantPoolGen cpg = new ConstantPoolGen();
36          final int a = cpg.addFieldref("A", "b&c", "I");
37          final int b = cpg.addFieldref("A&b", "c", "I");
38          assertNotEquals(a, b, "distinct fieldrefs collided to constant pool index " + a);
39      }
40  
41      @Test
42      void testInterfaceMethodrefNamesContainingDelimiter() {
43          final ConstantPoolGen cpg = new ConstantPoolGen();
44          final int a = cpg.addInterfaceMethodref("I", "m#n", "()V");
45          final int b = cpg.addInterfaceMethodref("I#m", "n", "()V");
46          assertNotEquals(a, b, "distinct interface methodrefs collided to constant pool index " + a);
47      }
48  
49      @Test
50      void testMethodrefDedupStillWorks() {
51          final ConstantPoolGen cpg = new ConstantPoolGen();
52          final int a = cpg.addMethodref("Foo", "bar", "()V");
53          final int b = cpg.addMethodref("Foo", "bar", "()V");
54          assertEquals(a, b, "identical methodrefs must reuse the same constant pool entry");
55      }
56  
57      @Test
58      void testMethodrefNamesContainingDelimiter() {
59          final ConstantPoolGen cpg = new ConstantPoolGen();
60          final int a = cpg.addMethodref("Foo", "bar:baz", "()V");
61          final int b = cpg.addMethodref("Foo:bar", "baz", "()V");
62          assertNotEquals(a, b, "distinct methodrefs collided to constant pool index " + a);
63      }
64  }