CheckACC_STRICTFlagOnPkgAccessClassTest.java revision 2942:08092deced3f
1169695Skan/*
2169695Skan * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
3169695Skan * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4169695Skan *
5169695Skan * This code is free software; you can redistribute it and/or modify it
6169695Skan * under the terms of the GNU General Public License version 2 only, as
7169695Skan * published by the Free Software Foundation.  Oracle designates this
8169695Skan * particular file as subject to the "Classpath" exception as provided
9169695Skan * by Oracle in the LICENSE file that accompanied this code.
10169695Skan *
11169695Skan * This code is distributed in the hope that it will be useful, but WITHOUT
12169695Skan * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13169695Skan * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14169695Skan * version 2 for more details (a copy is included in the LICENSE file that
15169695Skan * accompanied this code).
16169695Skan *
17169695Skan * You should have received a copy of the GNU General Public License version
18169695Skan * 2 along with this work; if not, write to the Free Software Foundation,
19169695Skan * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20169695Skan *
21169695Skan * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22169695Skan * or visit www.oracle.com if you need additional information or have any
23169695Skan * questions.
24169695Skan */
25169695Skan
26169695Skan/*
27169695Skan * @test
28169695Skan * @bug 8005931
29169695Skan * @summary javac doesn't set ACC_STRICT for classes with package access
30169695Skan * @modules jdk.jdeps/com.sun.tools.classfile
31169695Skan * @run main CheckACC_STRICTFlagOnPkgAccessClassTest
32169695Skan */
33169695Skan
34169695Skanimport java.io.File;
35169695Skanimport java.io.IOException;
36169695Skanimport java.net.URI;
37169695Skanimport java.util.ArrayList;
38169695Skanimport java.util.Arrays;
39169695Skanimport java.util.List;
40169695Skanimport javax.tools.JavaCompiler;
41169695Skanimport javax.tools.JavaFileObject;
42169695Skanimport javax.tools.SimpleJavaFileObject;
43169695Skanimport javax.tools.ToolProvider;
44169695Skanimport com.sun.source.util.JavacTask;
45169695Skanimport com.sun.tools.classfile.ClassFile;
46169695Skanimport com.sun.tools.classfile.ConstantPoolException;
47169695Skanimport com.sun.tools.classfile.Descriptor;
48169695Skanimport com.sun.tools.classfile.Descriptor.InvalidDescriptor;
49169695Skanimport com.sun.tools.classfile.Method;
50169695Skan
51169695Skanimport static com.sun.tools.classfile.AccessFlags.ACC_STRICT;
52169695Skan
53169695Skanpublic class CheckACC_STRICTFlagOnPkgAccessClassTest {
54169695Skan
55169695Skan    private static final String AssertionErrorMessage =
56169695Skan        "All methods should have the ACC_STRICT access flag " +
57169695Skan        "please check output";
58169695Skan    private static final String CompilationErrorMessage =
59169695Skan        "Error thrown when compiling the following source:\n";
60169695Skan    private static final String offendingMethodErrorMessage =
61169695Skan        "Method %s of class %s doesn't have the ACC_STRICT access flag";
62169695Skan
63169695Skan    JavaSource source = new JavaSource();
64169695Skan
65169695Skan    private List<String> errors = new ArrayList<>();
66169695Skan
67169695Skan    public static void main(String[] args)
68169695Skan            throws IOException, ConstantPoolException, InvalidDescriptor {
69169695Skan        JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
70169695Skan        new CheckACC_STRICTFlagOnPkgAccessClassTest().run(comp);
71169695Skan    }
72169695Skan
73169695Skan    private void run(JavaCompiler comp)
74169695Skan            throws IOException, ConstantPoolException, InvalidDescriptor {
75169695Skan        compile(comp);
76169695Skan        check();
77169695Skan        if (errors.size() > 0) {
78169695Skan            for (String error: errors) {
79169695Skan                System.err.println(error);
80169695Skan            }
81169695Skan            throw new AssertionError(AssertionErrorMessage);
82169695Skan        }
83169695Skan    }
84169695Skan
85169695Skan    private void compile(JavaCompiler comp) {
86169695Skan        JavacTask ct = (JavacTask)comp.getTask(null, null, null, null, null,
87169695Skan                Arrays.asList(source));
88169695Skan        try {
89169695Skan            if (!ct.call()) {
90169695Skan                throw new AssertionError(CompilationErrorMessage +
91169695Skan                        source.getCharContent(true));
92169695Skan            }
93169695Skan        } catch (Throwable ex) {
94169695Skan            throw new AssertionError(CompilationErrorMessage +
95169695Skan                    source.getCharContent(true));
96169695Skan        }
97169695Skan    }
98169695Skan
99169695Skan    void check()
100169695Skan        throws
101169695Skan            IOException,
102169695Skan            ConstantPoolException,
103169695Skan            Descriptor.InvalidDescriptor {
104169695Skan        ClassFile classFileToCheck = ClassFile.read(new File("Test.class"));
105169695Skan
106169695Skan        for (Method method : classFileToCheck.methods) {
107169695Skan            if ((method.access_flags.flags & ACC_STRICT) == 0) {
108169695Skan                errors.add(String.format(offendingMethodErrorMessage,
109169695Skan                        method.getName(classFileToCheck.constant_pool),
110169695Skan                        classFileToCheck.getName()));
111169695Skan            }
112169695Skan        }
113169695Skan    }
114169695Skan
115169695Skan    class JavaSource extends SimpleJavaFileObject {
116169695Skan
117169695Skan        String source = "strictfp class Test {" +
118169695Skan                "    Test(){}" +
119169695Skan                "    void m(){}" +
120169695Skan                "}";
121169695Skan
122169695Skan        public JavaSource() {
123169695Skan            super(URI.create("Test.java"), JavaFileObject.Kind.SOURCE);
124169695Skan        }
125169695Skan
126169695Skan        @Override
127169695Skan        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
128169695Skan            return source;
129169695Skan        }
130169695Skan    }
131169695Skan}
132169695Skan