|
1 /* |
|
2 * Copyright (c) 2014, 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 import com.sun.tools.classfile.Attribute; |
|
25 import com.sun.tools.classfile.ClassFile; |
|
26 import com.sun.tools.classfile.SourceFile_attribute; |
|
27 |
|
28 import java.util.ArrayList; |
|
29 import java.util.List; |
|
30 import java.util.Map; |
|
31 import javax.tools.JavaFileObject; |
|
32 |
|
33 public class SourceFileTestBase extends TestBase { |
|
34 |
|
35 protected void test(Class<?> classToTest, String fileName) throws Exception { |
|
36 assertAttributePresent(ClassFile.read(getClassFile(classToTest)), fileName); |
|
37 } |
|
38 |
|
39 protected void test(String classToTest, String fileName) throws Exception { |
|
40 assertAttributePresent(ClassFile.read(getClassFile(classToTest + ".class")), fileName); |
|
41 } |
|
42 |
|
43 /** |
|
44 * Compile sourceCode and for all "classesToTest" checks SourceFile attribute. |
|
45 */ |
|
46 protected void compileAndTest(String sourceCode, String... classesToTest) throws Exception { |
|
47 |
|
48 Map<String, ? extends JavaFileObject> classes = compile(sourceCode); |
|
49 String fileName = ToolBox.getJavaFileNameFromSource(sourceCode); |
|
50 for (String className : classesToTest) { |
|
51 assertAttributePresent(ClassFile.read(classes.get(className).openInputStream()), fileName); |
|
52 } |
|
53 } |
|
54 |
|
55 private void assertAttributePresent(ClassFile classFile, String fileName) throws Exception { |
|
56 |
|
57 //We need to count attributes with the same names because there is no appropriate API in the ClassFile. |
|
58 |
|
59 List<SourceFile_attribute> sourceFileAttributes = new ArrayList<>(); |
|
60 for (Attribute a : classFile.attributes.attrs) { |
|
61 if (Attribute.SourceFile.equals(a.getName(classFile.constant_pool))) { |
|
62 sourceFileAttributes.add((SourceFile_attribute) a); |
|
63 } |
|
64 } |
|
65 |
|
66 assertEquals(sourceFileAttributes.size(), 1, "Should be the only SourceFile attribute"); |
|
67 |
|
68 SourceFile_attribute attribute = sourceFileAttributes.get(0); |
|
69 |
|
70 assertEquals(classFile.constant_pool.getUTF8Info(attribute.attribute_name_index).value, |
|
71 Attribute.SourceFile, "Incorrect attribute name"); |
|
72 assertEquals(classFile.constant_pool.getUTF8Info(attribute.sourcefile_index).value, fileName, |
|
73 "Incorrect source file name"); |
|
74 assertEquals(attribute.attribute_length, 2, "Incorrect attribute length"); |
|
75 } |
|
76 } |