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