Tue, 08 Jul 2008 16:59:27 -0700
6715251: javap should be consistent with javac and return 2 if given no arguments
Reviewed-by: ksrini
jjg@46 | 1 | /* |
jjg@46 | 2 | * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. |
jjg@46 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jjg@46 | 4 | * |
jjg@46 | 5 | * This code is free software; you can redistribute it and/or modify it |
jjg@46 | 6 | * under the terms of the GNU General Public License version 2 only, as |
jjg@46 | 7 | * published by the Free Software Foundation. |
jjg@46 | 8 | * |
jjg@46 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jjg@46 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jjg@46 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jjg@46 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
jjg@46 | 13 | * accompanied this code). |
jjg@46 | 14 | * |
jjg@46 | 15 | * You should have received a copy of the GNU General Public License version |
jjg@46 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
jjg@46 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jjg@46 | 18 | * |
jjg@46 | 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
jjg@46 | 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
jjg@46 | 21 | * have any questions. |
jjg@46 | 22 | */ |
jjg@46 | 23 | |
jjg@46 | 24 | import java.io.*; |
jjg@46 | 25 | import java.util.*; |
jjg@46 | 26 | import javax.tools.*; |
jjg@46 | 27 | |
jjg@46 | 28 | /* |
jjg@46 | 29 | * @test |
jjg@46 | 30 | * @bug 6439940 |
jjg@46 | 31 | * @summary Cleanup javap implementation |
jjg@46 | 32 | * @run main/othervm ListTest |
jjg@46 | 33 | */ |
jjg@46 | 34 | public class ListTest { |
jjg@46 | 35 | public static void main(String[] args) throws Exception { |
jjg@46 | 36 | new ListTest().run(); |
jjg@46 | 37 | } |
jjg@46 | 38 | |
jjg@46 | 39 | ListTest() { |
jjg@46 | 40 | String v = System.getProperty("view.cmd"); |
jjg@46 | 41 | // v = "/opt/teamware/7.7/bin/filemerge -r"; |
jjg@46 | 42 | if (v != null) { |
jjg@46 | 43 | viewResults = true; |
jjg@46 | 44 | viewCmd = Arrays.asList(v.split(" +")); |
jjg@46 | 45 | } |
jjg@46 | 46 | } |
jjg@46 | 47 | |
jjg@46 | 48 | void run() throws Exception { |
jjg@46 | 49 | StandardLocation[] locs = new StandardLocation[] { |
jjg@46 | 50 | StandardLocation.PLATFORM_CLASS_PATH, |
jjg@46 | 51 | StandardLocation.CLASS_PATH, |
jjg@46 | 52 | }; |
jjg@46 | 53 | |
jjg@46 | 54 | int count = 0; |
jjg@46 | 55 | int pass = 0; |
jjg@46 | 56 | for (StandardLocation loc: locs) { |
jjg@46 | 57 | for (String testClassName: list(loc)) { |
jjg@46 | 58 | count++; |
jjg@46 | 59 | if (test(testClassName)) |
jjg@46 | 60 | pass++; |
jjg@46 | 61 | } |
jjg@46 | 62 | } |
jjg@46 | 63 | |
jjg@46 | 64 | if (pass < count) |
jjg@46 | 65 | throw new Error(pass + "/" + count + " test cases passed"); |
jjg@46 | 66 | } |
jjg@46 | 67 | |
jjg@46 | 68 | Iterable<String> list(StandardLocation loc) throws IOException { |
jjg@46 | 69 | JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); |
jjg@46 | 70 | StandardJavaFileManager sfm = compiler.getStandardFileManager(null, null, null); |
jjg@46 | 71 | Set<JavaFileObject.Kind> kinds = Collections.singleton(JavaFileObject.Kind.CLASS); |
jjg@46 | 72 | |
jjg@46 | 73 | List<String> list = new ArrayList<String>(); |
jjg@46 | 74 | for (JavaFileObject fo: sfm.list(loc, testPackage, kinds, true)) { |
jjg@46 | 75 | //System.err.println(com.sun.tools.javac.util.Old199.getPath(fo)); |
jjg@46 | 76 | list.add(sfm.inferBinaryName(loc, fo)); |
jjg@46 | 77 | } |
jjg@46 | 78 | return list; |
jjg@46 | 79 | } |
jjg@46 | 80 | |
jjg@46 | 81 | boolean test(String testClassName) throws Exception { |
jjg@46 | 82 | String[] args = new String[options.size() + 1]; |
jjg@46 | 83 | options.toArray(args); |
jjg@46 | 84 | args[args.length - 1] = testClassName; |
jjg@46 | 85 | String oldOut = runOldJavap(args); |
jjg@46 | 86 | String newOut = runNewJavap(args); |
jjg@46 | 87 | boolean ok = oldOut.equals(newOut); |
jjg@46 | 88 | System.err.println((ok ? "pass" : "FAIL") + ": " + testClassName); |
jjg@46 | 89 | if (!ok && viewResults) |
jjg@46 | 90 | view(oldOut, newOut); |
jjg@46 | 91 | return ok; |
jjg@46 | 92 | } |
jjg@46 | 93 | |
jjg@46 | 94 | String runOldJavap(String[] args) { |
jjg@46 | 95 | //System.err.println("OLD: " + Arrays.asList(args)); |
jjg@46 | 96 | PrintStream oldOut = System.out; |
jjg@46 | 97 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
jjg@46 | 98 | System.setOut(new PrintStream(out)); |
jjg@46 | 99 | try { |
jjg@46 | 100 | sun.tools.javap.Main.entry(args); |
jjg@46 | 101 | } finally { |
jjg@46 | 102 | System.setOut(oldOut); |
jjg@46 | 103 | } |
jjg@46 | 104 | return out.toString(); |
jjg@46 | 105 | } |
jjg@46 | 106 | |
jjg@46 | 107 | String runNewJavap(String[] args) { |
jjg@46 | 108 | String[] nArgs = new String[args.length + 2]; |
jjg@46 | 109 | nArgs[0] = "-XDcompat"; |
jjg@46 | 110 | nArgs[1] = "-XDignore.symbol.file"; |
jjg@46 | 111 | System.arraycopy(args, 0, nArgs, 2, args.length); |
jjg@46 | 112 | //System.err.println("NEW: " + Arrays.asList(nArgs)); |
jjg@46 | 113 | StringWriter out = new StringWriter(); |
jjg@46 | 114 | com.sun.tools.javap.Main.run(nArgs, new PrintWriter(out, true)); |
jjg@46 | 115 | return out.toString(); |
jjg@46 | 116 | } |
jjg@46 | 117 | |
jjg@46 | 118 | File write(String text, String suffix) throws IOException { |
jjg@46 | 119 | File f = File.createTempFile("ListTest", suffix); |
jjg@46 | 120 | FileWriter out = new FileWriter(f); |
jjg@46 | 121 | out.write(text); |
jjg@46 | 122 | out.close(); |
jjg@46 | 123 | return f; |
jjg@46 | 124 | } |
jjg@46 | 125 | |
jjg@46 | 126 | void view(String oldOut, String newOut) throws Exception { |
jjg@46 | 127 | File oldFile = write(oldOut, "old"); |
jjg@46 | 128 | File newFile = write(newOut, "new"); |
jjg@46 | 129 | List<String> cmd = new ArrayList<String>(); |
jjg@46 | 130 | cmd.addAll(viewCmd); |
jjg@46 | 131 | cmd.add(oldFile.getPath()); |
jjg@46 | 132 | cmd.add(newFile.getPath()); |
jjg@46 | 133 | Process p = new ProcessBuilder(cmd).redirectErrorStream(true).start(); |
jjg@46 | 134 | p.getOutputStream().close(); |
jjg@46 | 135 | String line; |
jjg@46 | 136 | BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); |
jjg@46 | 137 | while ((line = in.readLine()) != null) |
jjg@46 | 138 | System.err.println(line); |
jjg@46 | 139 | in.close(); |
jjg@46 | 140 | p.waitFor(); |
jjg@46 | 141 | } |
jjg@46 | 142 | |
jjg@46 | 143 | String testPackage = "java.lang"; |
jjg@46 | 144 | List<String> options = Arrays.asList("-v"); |
jjg@46 | 145 | boolean viewResults; |
jjg@46 | 146 | List<String> viewCmd; |
jjg@46 | 147 | } |