aoqi@0: /* aoqi@0: * Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 8000518 aoqi@0: * @summary Javac generates duplicate name_and_type constant pool entry for aoqi@0: * class BinaryOpValueExp.java aoqi@0: * @run main DuplicateConstantPoolEntry aoqi@0: */ aoqi@0: aoqi@0: import com.sun.source.util.JavacTask; aoqi@0: import com.sun.tools.classfile.ClassFile; aoqi@0: import com.sun.tools.classfile.ConstantPoolException; aoqi@0: import java.io.File; aoqi@0: import java.io.IOException; aoqi@0: import java.net.URI; aoqi@0: import java.util.Arrays; aoqi@0: import java.util.List; aoqi@0: import javax.tools.JavaCompiler; aoqi@0: import javax.tools.JavaFileObject; aoqi@0: import javax.tools.SimpleJavaFileObject; aoqi@0: import javax.tools.ToolProvider; aoqi@0: aoqi@0: /* aoqi@0: * This bug was reproduced having two classes B and C referenced from a class A aoqi@0: * class C should be compiled and generated in advance. Later class A and B should aoqi@0: * be compiled like this: javac A.java B.java aoqi@0: */ aoqi@0: aoqi@0: public class DuplicateConstantPoolEntry { aoqi@0: aoqi@0: public static void main(String args[]) throws Exception { aoqi@0: new DuplicateConstantPoolEntry().run(); aoqi@0: } aoqi@0: aoqi@0: void run() throws Exception { aoqi@0: generateFilesNeeded(); aoqi@0: checkReference(); aoqi@0: } aoqi@0: aoqi@0: void generateFilesNeeded() throws Exception { aoqi@0: aoqi@0: StringJavaFileObject[] CSource = new StringJavaFileObject[] { aoqi@0: new StringJavaFileObject("C.java", aoqi@0: "class C {C(String s) {}}"), aoqi@0: }; aoqi@0: aoqi@0: List AandBSource = Arrays.asList( aoqi@0: new StringJavaFileObject("A.java", aoqi@0: "class A {void test() {new B(null);new C(null);}}"), aoqi@0: new StringJavaFileObject("B.java", aoqi@0: "class B {B(String s) {}}") aoqi@0: ); aoqi@0: aoqi@0: final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); aoqi@0: JavacTask compileC = (JavacTask)tool.getTask(null, null, null, null, null, aoqi@0: Arrays.asList(CSource)); aoqi@0: if (!compileC.call()) { aoqi@0: throw new AssertionError("Compilation error while compiling C.java sources"); aoqi@0: } aoqi@0: JavacTask compileAB = (JavacTask)tool.getTask(null, null, null, aoqi@0: Arrays.asList("-cp", "."), null, AandBSource); aoqi@0: if (!compileAB.call()) { aoqi@0: throw new AssertionError("Compilation error while compiling A and B sources"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void checkReference() throws IOException, ConstantPoolException { aoqi@0: File file = new File("A.class"); aoqi@0: ClassFile classFile = ClassFile.read(file); aoqi@0: for (int i = 1; aoqi@0: i < classFile.constant_pool.size() - 1; aoqi@0: i += classFile.constant_pool.get(i).size()) { aoqi@0: for (int j = i + classFile.constant_pool.get(i).size(); aoqi@0: j < classFile.constant_pool.size(); aoqi@0: j += classFile.constant_pool.get(j).size()) { aoqi@0: if (classFile.constant_pool.get(i).toString(). aoqi@0: equals(classFile.constant_pool.get(j).toString())) { aoqi@0: throw new AssertionError( aoqi@0: "Duplicate entries in the constant pool at positions " + aoqi@0: i + " and " + j); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: private static class StringJavaFileObject extends SimpleJavaFileObject { aoqi@0: StringJavaFileObject(String name, String text) { aoqi@0: super(URI.create(name), JavaFileObject.Kind.SOURCE); aoqi@0: this.text = text; aoqi@0: } aoqi@0: @Override aoqi@0: public CharSequence getCharContent(boolean b) { aoqi@0: return text; aoqi@0: } aoqi@0: private String text; aoqi@0: } aoqi@0: }