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.
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.
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).
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.
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
27
* @summary String.toLowerCase()/toUpperCase is generally dangerous, check it is not used in langtools
29
* @modules java.base/jdk.internal.classfile.impl
35
import java.lang.classfile.*;
36
import java.lang.classfile.constantpool.*;
38
public class NoStringToLower {
39
public static void main(String... args) throws Exception {
40
NoStringToLower c = new NoStringToLower();
45
throw new Exception(c.errors + " errors occurred");
50
static boolean is_jtreg() {
51
return (System.getProperty("test.src") != null);
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);
62
"javax.annotation.processing",
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",
74
for (String pkg: pkgs) {
75
for (JavaFileObject fo: fm.list(javacLoc,
76
pkg, EnumSet.of(JavaFileObject.Kind.CLASS), true)) {
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 };
90
for (JavaFileManager.Location l: locns) {
91
JavaFileObject fo = fm.getJavaFileForInput(l,
92
"com.sun.tools.javac.Main", JavaFileObject.Kind.CLASS);
96
} catch (IOException e) {
99
throw new IllegalStateException("Cannot find javac");
103
* Verify there are no references to String.toLowerCase() in a class file.
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();
112
if ("java/lang/String.toLowerCase:()Ljava/lang/String;".equals(methodDesc)) {
113
error("found reference to String.toLowerCase() in: " + fo.getName());
115
if ("java/lang/String.toUpperCase:()Ljava/lang/String;".equals(methodDesc)) {
116
error("found reference to String.toLowerCase() in: " + fo.getName());
120
} catch (ConstantPoolException ignore) {
127
void error(String msg) {
128
System.err.println("Error: " + msg);