1 /* 2 * Copyright (c) 2002, 2015, 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 /* 25 * @test 26 * @bug 8000518 27 * @summary Javac generates duplicate name_and_type constant pool entry for 28 * class BinaryOpValueExp.java 29 * @modules jdk.jdeps/com.sun.tools.classfile 30 * @run main DuplicateConstantPoolEntry 31 */ 32 33 import com.sun.source.util.JavacTask; 34 import com.sun.tools.classfile.ClassFile; 35 import com.sun.tools.classfile.ConstantPoolException; 36 import java.io.File; 37 import java.io.IOException; 38 import java.net.URI; 39 import java.util.Arrays; 40 import java.util.List; 41 import javax.tools.JavaCompiler; 42 import javax.tools.JavaFileObject; 43 import javax.tools.SimpleJavaFileObject; 44 import javax.tools.ToolProvider; 45 46 /* 47 * This bug was reproduced having two classes B and C referenced from a class A 48 * class C should be compiled and generated in advance. Later class A and B should 49 * be compiled like this: javac A.java B.java 50 */ 51 52 public class DuplicateConstantPoolEntry { 53 54 public static void main(String args[]) throws Exception { 55 new DuplicateConstantPoolEntry().run(); 56 } 57 58 void run() throws Exception { 59 generateFilesNeeded(); 60 checkReference(); 61 } 62 63 void generateFilesNeeded() throws Exception { 64 65 StringJavaFileObject[] CSource = new StringJavaFileObject[] { 66 new StringJavaFileObject("C.java", 67 "class C {C(String s) {}}"), 68 }; 69 70 List<StringJavaFileObject> AandBSource = Arrays.asList( 71 new StringJavaFileObject("A.java", 72 "class A {void test() {new B(null);new C(null);}}"), 73 new StringJavaFileObject("B.java", 74 "class B {B(String s) {}}") 75 ); 76 77 final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); 78 JavacTask compileC = (JavacTask)tool.getTask(null, null, null, null, null, 79 Arrays.asList(CSource)); 80 if (!compileC.call()) { 81 throw new AssertionError("Compilation error while compiling C.java sources"); 82 } 83 JavacTask compileAB = (JavacTask)tool.getTask(null, null, null, 84 Arrays.asList("-cp", "."), null, AandBSource); 85 if (!compileAB.call()) { 86 throw new AssertionError("Compilation error while compiling A and B sources"); 87 } 88 } 89 90 void checkReference() throws IOException, ConstantPoolException { 91 File file = new File("A.class"); 92 ClassFile classFile = ClassFile.read(file); 93 for (int i = 1; 94 i < classFile.constant_pool.size() - 1; 95 i += classFile.constant_pool.get(i).size()) { 96 for (int j = i + classFile.constant_pool.get(i).size(); 97 j < classFile.constant_pool.size(); 98 j += classFile.constant_pool.get(j).size()) { 99 if (classFile.constant_pool.get(i).toString(). 100 equals(classFile.constant_pool.get(j).toString())) { 101 throw new AssertionError( 102 "Duplicate entries in the constant pool at positions " + 103 i + " and " + j); 104 } 105 } 106 } 107 } 108 109 private static class StringJavaFileObject extends SimpleJavaFileObject { 110 StringJavaFileObject(String name, String text) { 111 super(URI.create(name), JavaFileObject.Kind.SOURCE); 112 this.text = text; 113 } 114 @Override 115 public CharSequence getCharContent(boolean b) { 116 return text; 117 } 118 private String text; 119 } 120 }