jdk

Форк
0
/
NoStringToLower.java 
133 строки · 4.7 Кб
1
/*
2
 * Copyright (c) 2013, 2022, 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 8029800
27
 * @summary String.toLowerCase()/toUpperCase is generally dangerous, check it is not used in langtools
28
 * @enablePreview
29
 * @modules java.base/jdk.internal.classfile.impl
30
 */
31

32
import java.io.*;
33
import java.util.*;
34
import javax.tools.*;
35
import java.lang.classfile.*;
36
import java.lang.classfile.constantpool.*;
37

38
public class NoStringToLower {
39
    public static void main(String... args) throws Exception {
40
        NoStringToLower c = new NoStringToLower();
41
        if (c.run(args))
42
            return;
43

44
        if (is_jtreg())
45
            throw new Exception(c.errors + " errors occurred");
46
        else
47
            System.exit(1);
48
    }
49

50
    static boolean is_jtreg() {
51
        return (System.getProperty("test.src") != null);
52
    }
53

54
    /**
55
     * Main entry point.
56
     */
57
    boolean run(String... args) throws Exception {
58
        JavaCompiler c = ToolProvider.getSystemJavaCompiler();
59
        try (JavaFileManager fm = c.getStandardFileManager(null, null, null)) {
60
            JavaFileManager.Location javacLoc = findJavacLocation(fm);
61
            String[] pkgs = {
62
                "javax.annotation.processing",
63
                "javax.lang.model",
64
                "javax.tools",
65
                "com.sun.source",
66
                "jdk.internal.classfile",
67
                "com.sun.tools.doclint",
68
                "com.sun.tools.javac",
69
                "com.sun.tools.javah",
70
                "com.sun.tools.javap",
71
                "com.sun.tools.jdeps",
72
                "jdk.javadoc"
73
            };
74
            for (String pkg: pkgs) {
75
                for (JavaFileObject fo: fm.list(javacLoc,
76
                        pkg, EnumSet.of(JavaFileObject.Kind.CLASS), true)) {
77
                    scan(fo);
78
                }
79
            }
80

81
            return (errors == 0);
82
        }
83
    }
84

85
    // depending on how the test is run, javac may be on bootclasspath or classpath
86
    JavaFileManager.Location findJavacLocation(JavaFileManager fm) {
87
        JavaFileManager.Location[] locns =
88
            { StandardLocation.PLATFORM_CLASS_PATH, StandardLocation.CLASS_PATH };
89
        try {
90
            for (JavaFileManager.Location l: locns) {
91
                JavaFileObject fo = fm.getJavaFileForInput(l,
92
                    "com.sun.tools.javac.Main", JavaFileObject.Kind.CLASS);
93
                if (fo != null)
94
                    return l;
95
            }
96
        } catch (IOException e) {
97
            throw new Error(e);
98
        }
99
        throw new IllegalStateException("Cannot find javac");
100
    }
101

102
    /**
103
     * Verify there are no references to String.toLowerCase() in a class file.
104
     */
105
    void scan(JavaFileObject fo) throws IOException {
106
        try (InputStream in = fo.openInputStream()) {
107
            ClassModel cf = ClassFile.of().parse(in.readAllBytes());
108
            for (PoolEntry pe : cf.constantPool()) {
109
                if (pe instanceof MethodRefEntry ref) {
110
                    String methodDesc = ref.owner().name().stringValue() + "." + ref.name().stringValue() + ":" + ref.type().stringValue();
111

112
                    if ("java/lang/String.toLowerCase:()Ljava/lang/String;".equals(methodDesc)) {
113
                        error("found reference to String.toLowerCase() in: " + fo.getName());
114
                    }
115
                    if ("java/lang/String.toUpperCase:()Ljava/lang/String;".equals(methodDesc)) {
116
                        error("found reference to String.toLowerCase() in: " + fo.getName());
117
                    }
118
                }
119
            }
120
        } catch (ConstantPoolException ignore) {
121
        }
122
    }
123

124
    /**
125
     * Report an error.
126
     */
127
    void error(String msg) {
128
        System.err.println("Error: " + msg);
129
        errors++;
130
    }
131

132
    int errors;
133
}
134

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

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

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

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