aoqi@0: /* aoqi@0: * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 8029800 aoqi@0: * @summary String.toLowerCase()/toUpperCase is generally dangerous, check it is not used in langtools aoqi@0: */ aoqi@0: aoqi@0: import java.io.*; aoqi@0: import java.util.*; aoqi@0: import javax.tools.*; aoqi@0: import com.sun.tools.classfile.*; aoqi@0: import com.sun.tools.classfile.ConstantPool.CONSTANT_Methodref_info; aoqi@0: aoqi@0: public class NoStringToLower { aoqi@0: public static void main(String... args) throws Exception { aoqi@0: NoStringToLower c = new NoStringToLower(); aoqi@0: if (c.run(args)) aoqi@0: return; aoqi@0: aoqi@0: if (is_jtreg()) aoqi@0: throw new Exception(c.errors + " errors occurred"); aoqi@0: else aoqi@0: System.exit(1); aoqi@0: } aoqi@0: aoqi@0: static boolean is_jtreg() { aoqi@0: return (System.getProperty("test.src") != null); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Main entry point. aoqi@0: */ aoqi@0: boolean run(String... args) throws Exception { aoqi@0: JavaCompiler c = ToolProvider.getSystemJavaCompiler(); aoqi@0: JavaFileManager fm = c.getStandardFileManager(null, null, null); aoqi@0: JavaFileManager.Location javacLoc = findJavacLocation(fm); aoqi@0: String[] pkgs = { aoqi@0: "javax.annotation.processing", aoqi@0: "javax.lang.model", aoqi@0: "javax.tools", aoqi@0: "com.sun.source", aoqi@0: "com.sun.tools.classfile", aoqi@0: "com.sun.tools.doclet", aoqi@0: "com.sun.tools.doclint", aoqi@0: "com.sun.tools.javac", aoqi@0: "com.sun.tools.javadoc", aoqi@0: "com.sun.tools.javah", aoqi@0: "com.sun.tools.javap", aoqi@0: "com.sun.tools.jdeps", aoqi@0: "com.sun.tools.sjavac" aoqi@0: }; aoqi@0: for (String pkg: pkgs) { aoqi@0: for (JavaFileObject fo: fm.list(javacLoc, aoqi@0: pkg, EnumSet.of(JavaFileObject.Kind.CLASS), true)) { aoqi@0: scan(fo); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return (errors == 0); aoqi@0: } aoqi@0: aoqi@0: // depending on how the test is run, javac may be on bootclasspath or classpath aoqi@0: JavaFileManager.Location findJavacLocation(JavaFileManager fm) { aoqi@0: JavaFileManager.Location[] locns = aoqi@0: { StandardLocation.PLATFORM_CLASS_PATH, StandardLocation.CLASS_PATH }; aoqi@0: try { aoqi@0: for (JavaFileManager.Location l: locns) { aoqi@0: JavaFileObject fo = fm.getJavaFileForInput(l, aoqi@0: "com.sun.tools.javac.Main", JavaFileObject.Kind.CLASS); aoqi@0: if (fo != null) aoqi@0: return l; aoqi@0: } aoqi@0: } catch (IOException e) { aoqi@0: throw new Error(e); aoqi@0: } aoqi@0: throw new IllegalStateException("Cannot find javac"); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Verify there are no references to String.toLowerCase() in a class file. aoqi@0: */ aoqi@0: void scan(JavaFileObject fo) throws IOException { aoqi@0: InputStream in = fo.openInputStream(); aoqi@0: try { aoqi@0: ClassFile cf = ClassFile.read(in); aoqi@0: for (ConstantPool.CPInfo cpinfo: cf.constant_pool.entries()) { aoqi@0: if (cpinfo.getTag() == ConstantPool.CONSTANT_Methodref) { aoqi@0: CONSTANT_Methodref_info ref = (CONSTANT_Methodref_info) cpinfo; aoqi@0: String methodDesc = ref.getClassInfo().getName() + "." + ref.getNameAndTypeInfo().getName() + ":" + ref.getNameAndTypeInfo().getType(); aoqi@0: aoqi@0: if ("java/lang/String.toLowerCase:()Ljava/lang/String;".equals(methodDesc)) { aoqi@0: error("found reference to String.toLowerCase() in: " + fo.getName()); aoqi@0: } aoqi@0: if ("java/lang/String.toUpperCase:()Ljava/lang/String;".equals(methodDesc)) { aoqi@0: error("found reference to String.toLowerCase() in: " + fo.getName()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } catch (ConstantPoolException ignore) { aoqi@0: } finally { aoqi@0: in.close(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Report an error. aoqi@0: */ aoqi@0: void error(String msg) { aoqi@0: System.err.println("Error: " + msg); aoqi@0: errors++; aoqi@0: } aoqi@0: aoqi@0: int errors; aoqi@0: }