|
1 /* |
|
2 * Copyright (c) 2013, 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. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 /* |
|
27 * @test |
|
28 * @bug 8005931 |
|
29 * @summary javac doesn't set ACC_STRICT for classes with package access |
|
30 * @run main CheckACC_STRICTFlagOnPkgAccessClassTest |
|
31 */ |
|
32 |
|
33 import java.io.File; |
|
34 import java.io.IOException; |
|
35 import java.net.URI; |
|
36 import java.util.ArrayList; |
|
37 import java.util.Arrays; |
|
38 import java.util.List; |
|
39 import javax.tools.JavaCompiler; |
|
40 import javax.tools.JavaFileObject; |
|
41 import javax.tools.SimpleJavaFileObject; |
|
42 import javax.tools.ToolProvider; |
|
43 import com.sun.source.util.JavacTask; |
|
44 import com.sun.tools.classfile.ClassFile; |
|
45 import com.sun.tools.classfile.ConstantPoolException; |
|
46 import com.sun.tools.classfile.Descriptor; |
|
47 import com.sun.tools.classfile.Descriptor.InvalidDescriptor; |
|
48 import com.sun.tools.classfile.Method; |
|
49 |
|
50 import static com.sun.tools.classfile.AccessFlags.ACC_STRICT; |
|
51 |
|
52 public class CheckACC_STRICTFlagOnPkgAccessClassTest { |
|
53 |
|
54 private static final String AssertionErrorMessage = |
|
55 "All methods should have the ACC_STRICT access flag " + |
|
56 "please check output"; |
|
57 private static final String CompilationErrorMessage = |
|
58 "Error thrown when compiling the following source:\n"; |
|
59 private static final String offendingMethodErrorMessage = |
|
60 "Method %s of class %s doesn't have the ACC_STRICT access flag"; |
|
61 |
|
62 JavaSource source = new JavaSource(); |
|
63 |
|
64 private List<String> errors = new ArrayList<>(); |
|
65 |
|
66 public static void main(String[] args) |
|
67 throws IOException, ConstantPoolException, InvalidDescriptor { |
|
68 JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); |
|
69 new CheckACC_STRICTFlagOnPkgAccessClassTest().run(comp); |
|
70 } |
|
71 |
|
72 private void run(JavaCompiler comp) |
|
73 throws IOException, ConstantPoolException, InvalidDescriptor { |
|
74 compile(comp); |
|
75 check(); |
|
76 if (errors.size() > 0) { |
|
77 for (String error: errors) { |
|
78 System.err.println(error); |
|
79 } |
|
80 throw new AssertionError(AssertionErrorMessage); |
|
81 } |
|
82 } |
|
83 |
|
84 private void compile(JavaCompiler comp) { |
|
85 JavacTask ct = (JavacTask)comp.getTask(null, null, null, null, null, |
|
86 Arrays.asList(source)); |
|
87 try { |
|
88 if (!ct.call()) { |
|
89 throw new AssertionError(CompilationErrorMessage + |
|
90 source.getCharContent(true)); |
|
91 } |
|
92 } catch (Throwable ex) { |
|
93 throw new AssertionError(CompilationErrorMessage + |
|
94 source.getCharContent(true)); |
|
95 } |
|
96 } |
|
97 |
|
98 void check() |
|
99 throws |
|
100 IOException, |
|
101 ConstantPoolException, |
|
102 Descriptor.InvalidDescriptor { |
|
103 ClassFile classFileToCheck = ClassFile.read(new File("Test.class")); |
|
104 |
|
105 for (Method method : classFileToCheck.methods) { |
|
106 if ((method.access_flags.flags & ACC_STRICT) == 0) { |
|
107 errors.add(String.format(offendingMethodErrorMessage, |
|
108 method.getName(classFileToCheck.constant_pool), |
|
109 classFileToCheck.getName())); |
|
110 } |
|
111 } |
|
112 } |
|
113 |
|
114 class JavaSource extends SimpleJavaFileObject { |
|
115 |
|
116 String source = "strictfp class Test {" + |
|
117 " Test(){}" + |
|
118 " void m(){}" + |
|
119 "}"; |
|
120 |
|
121 public JavaSource() { |
|
122 super(URI.create("Test.java"), JavaFileObject.Kind.SOURCE); |
|
123 } |
|
124 |
|
125 @Override |
|
126 public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
|
127 return source; |
|
128 } |
|
129 } |
|
130 } |