jdk

Форк
0
/
CheckACC_STRICTFlagOnclinitTest.java 
108 строк · 3.7 Кб
1
/*
2
 * Copyright (c) 2013, 2024, 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 7166455
27
 * @summary javac doesn't set ACC_STRICT bit on <clinit> for strictfp class
28
 * @library /tools/lib /test/lib
29
 * @enablePreview
30
 */
31

32
import jdk.test.lib.compiler.CompilerUtils;
33
import toolbox.ToolBox;
34

35
import java.lang.classfile.ClassFile;
36
import java.lang.classfile.ClassModel;
37
import java.lang.classfile.MethodModel;
38
import java.nio.file.Path;
39
import java.util.ArrayList;
40
import java.util.List;
41
import java.io.IOException;
42

43
public class CheckACC_STRICTFlagOnclinitTest {
44
    private static final String AssertionErrorMessage =
45
        "All methods should have the ACC_STRICT access flag " +
46
        "please check output";
47
    private static final String offendingMethodErrorMessage =
48
        "Method %s of class %s doesn't have the ACC_STRICT access flag";
49

50
    private static final String SOURCE = """
51
            public strictfp class Test {
52
                static {
53
                    class Foo {
54
                        class Bar {
55
                            void m11() {}
56
                        }
57
                        void m1() {}
58
                    }
59
                }
60
                void m2() {
61
                    class Any {
62
                        void m21() {}
63
                    }
64
                }
65
            }
66
            """;
67

68
    private final List<String> errors = new ArrayList<>();
69

70
    public static void main(String[] args)
71
            throws IOException {
72
        new CheckACC_STRICTFlagOnclinitTest().run();
73
    }
74

75
    private void run()
76
            throws IOException {
77
        Path in = Path.of("in");
78
        Path out = Path.of("out");
79
        ToolBox toolBox = new ToolBox();
80
        toolBox.writeJavaFiles(in, SOURCE);
81
        CompilerUtils.compile(in, out, "--release", "16");
82
        check(out,
83
              "Test.class",
84
              "Test$1Foo.class",
85
              "Test$1Foo$Bar.class",
86
              "Test$1Any.class");
87
        if (!errors.isEmpty()) {
88
            for (String error: errors) {
89
                System.err.println(error);
90
            }
91
            throw new AssertionError(AssertionErrorMessage);
92
        }
93
    }
94

95
    void check(Path dir, String... fileNames) throws IOException {
96
        for (String fileName : fileNames) {
97
            ClassModel classFileToCheck = ClassFile.of().parse(dir.resolve(fileName));
98

99
            for (MethodModel method : classFileToCheck.methods()) {
100
                if ((method.flags().flagsMask() & ClassFile.ACC_STRICT) == 0) {
101
                    errors.add(String.format(offendingMethodErrorMessage,
102
                            method.methodName().stringValue(),
103
                            classFileToCheck.thisClass().asInternalName()));
104
                }
105
            }
106
        }
107
    }
108
}
109

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.