NoStringToLower.java revision 2942:08092deced3f
1/*
2 * Copyright (c) 2013, 2015, 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 * @modules jdk.jdeps/com.sun.tools.classfile
29 */
30
31import java.io.*;
32import java.util.*;
33import javax.tools.*;
34import com.sun.tools.classfile.*;
35import com.sun.tools.classfile.ConstantPool.CONSTANT_Methodref_info;
36
37public class NoStringToLower {
38    public static void main(String... args) throws Exception {
39        NoStringToLower c = new NoStringToLower();
40        if (c.run(args))
41            return;
42
43        if (is_jtreg())
44            throw new Exception(c.errors + " errors occurred");
45        else
46            System.exit(1);
47    }
48
49    static boolean is_jtreg() {
50        return (System.getProperty("test.src") != null);
51    }
52
53    /**
54     * Main entry point.
55     */
56    boolean run(String... args) throws Exception {
57        JavaCompiler c = ToolProvider.getSystemJavaCompiler();
58        try (JavaFileManager fm = c.getStandardFileManager(null, null, null)) {
59            JavaFileManager.Location javacLoc = findJavacLocation(fm);
60            String[] pkgs = {
61                "javax.annotation.processing",
62                "javax.lang.model",
63                "javax.tools",
64                "com.sun.source",
65                "com.sun.tools.classfile",
66                "com.sun.tools.doclet",
67                "com.sun.tools.doclint",
68                "com.sun.tools.javac",
69                "com.sun.tools.javadoc",
70                "com.sun.tools.javah",
71                "com.sun.tools.javap",
72                "com.sun.tools.jdeps",
73                "com.sun.tools.sjavac"
74            };
75            for (String pkg: pkgs) {
76                for (JavaFileObject fo: fm.list(javacLoc,
77                        pkg, EnumSet.of(JavaFileObject.Kind.CLASS), true)) {
78                    scan(fo);
79                }
80            }
81
82            return (errors == 0);
83        }
84    }
85
86    // depending on how the test is run, javac may be on bootclasspath or classpath
87    JavaFileManager.Location findJavacLocation(JavaFileManager fm) {
88        JavaFileManager.Location[] locns =
89            { StandardLocation.PLATFORM_CLASS_PATH, StandardLocation.CLASS_PATH };
90        try {
91            for (JavaFileManager.Location l: locns) {
92                JavaFileObject fo = fm.getJavaFileForInput(l,
93                    "com.sun.tools.javac.Main", JavaFileObject.Kind.CLASS);
94                if (fo != null)
95                    return l;
96            }
97        } catch (IOException e) {
98            throw new Error(e);
99        }
100        throw new IllegalStateException("Cannot find javac");
101    }
102
103    /**
104     * Verify there are no references to String.toLowerCase() in a class file.
105     */
106    void scan(JavaFileObject fo) throws IOException {
107        InputStream in = fo.openInputStream();
108        try {
109            ClassFile cf = ClassFile.read(in);
110            for (ConstantPool.CPInfo cpinfo: cf.constant_pool.entries()) {
111                if (cpinfo.getTag() == ConstantPool.CONSTANT_Methodref) {
112                    CONSTANT_Methodref_info ref = (CONSTANT_Methodref_info) cpinfo;
113                    String methodDesc = ref.getClassInfo().getName() + "." + ref.getNameAndTypeInfo().getName() + ":" + ref.getNameAndTypeInfo().getType();
114
115                    if ("java/lang/String.toLowerCase:()Ljava/lang/String;".equals(methodDesc)) {
116                        error("found reference to String.toLowerCase() in: " + fo.getName());
117                    }
118                    if ("java/lang/String.toUpperCase:()Ljava/lang/String;".equals(methodDesc)) {
119                        error("found reference to String.toLowerCase() in: " + fo.getName());
120                    }
121                }
122            }
123        } catch (ConstantPoolException ignore) {
124        } finally {
125            in.close();
126        }
127    }
128
129    /**
130     * Report an error.
131     */
132    void error(String msg) {
133        System.err.println("Error: " + msg);
134        errors++;
135    }
136
137    int errors;
138}
139