test/tools/javap/OptionTest.java

Tue, 03 Jun 2008 13:26:47 -0700

author
jjg
date
Tue, 03 Jun 2008 13:26:47 -0700
changeset 46
7708bd6d800d
child 54
eaf608c64fec
permissions
-rw-r--r--

4075303: Use javap to enquire aboput a specific inner class
4348375: Javap is not internationalized
4459541: "javap -l" shows line numbers as signed short; they should be unsigned
4501660: change diagnostic of -help as 'print this help message and exit'
4776241: unused source file in javap...
4870651: javap should recognize generics, varargs, enum
4876942: javap invoked without args does not print help screen
4880663: javap could output whitespace between class name and opening brace
4975569: javap doesn't print new flag bits
6271787: javap dumps LocalVariableTypeTable attribute in hex, needs to print a table
6305779: javap: support annotations
6439940: Clean up javap implementation
6469569: wrong check of searchpath in JavapEnvironment
6474890: javap does not open .zip files in -classpath
6587786: Javap throws error : "ERROR:Could not find <classname>" for JRE classes
6622215: javap ignores certain relevant access flags
6622216: javap names some attributes incorrectly
6622232: javap gets whitespace confused
6622260: javap prints negative bytes incorrectly in hex
Reviewed-by: ksrini

     1 /*
     2  * Copyright 2007 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
    21  * have any questions.
    22  */
    24 import java.io.*;
    25 import java.util.*;
    27 /*
    28  * @test
    29  * @bug 6439940
    30  * @summary Cleanup javap implementation
    31  * @run main/othervm OptionTest
    32  */
    33 public class OptionTest {
    34     public static void main(String[] args) throws Exception {
    35         new OptionTest().run();
    36     }
    38     OptionTest() {
    39         String v = System.getProperty("view.cmd");
    40         if (v != null) {
    41             viewResults = true;
    42             viewCmd = Arrays.asList(v.split(" +"));
    43         }
    44     }
    47     void run() throws Exception {
    48         int count = 0;
    49         int pass = 0;
    50         // try combinations of options and compare old javap against new javap
    51         for (int i = 0; i < (1<<8); i++) {
    52             List<String> options = new ArrayList<String>();
    53             if ((i & 0x01) != 0)
    54                 options.add("-c");
    55             if ((i & 0x02) != 0)
    56                 options.add("-l");
    57             if ((i & 0x04) != 0)
    58                 options.add("-public");
    59             if ((i & 0x08) != 0)
    60                 options.add("-protected");
    61             if ((i & 0x10) != 0)
    62                 options.add("-package");
    63             if ((i & 0x20) != 0)
    64                 options.add("-private");
    65             if ((i & 0x40) != 0)
    66                 options.add("-s");
    67             if ((i & 0x80) != 0)
    68                 options.add("-verbose");
    69             count++;
    70             if (test(options))
    71                 pass++;
    72         }
    74         if (pass < count)
    75             throw new Error(pass + "/" + count + " test cases passed");
    76     }
    78     boolean test(List<String> options) throws Exception {
    79         String[] args = new String[options.size() + 1];
    80         options.toArray(args);
    81         args[args.length - 1] = testClassName;
    82         String oldOut = runOldJavap(args);
    83         String newOut = runNewJavap(args);
    84         boolean ok = oldOut.equals(newOut);
    85         System.err.println((ok ? "pass" : "FAIL") + ": " + options);
    86         if (!ok && viewResults)
    87             view(oldOut, newOut);
    88         return ok;
    89     }
    91     String runOldJavap(String[] args) {
    92         //System.err.println("OLD: " + Arrays.asList(args));
    93         PrintStream oldOut = System.out;
    94         ByteArrayOutputStream out = new ByteArrayOutputStream();
    95         System.setOut(new PrintStream(out));
    96         try {
    97             sun.tools.javap.Main.entry(args);
    98         } finally {
    99             System.setOut(oldOut);
   100         }
   101         return out.toString();
   102     }
   104     String runNewJavap(String[] args) {
   105         String[] nArgs = new String[args.length + 2];
   106         nArgs[0] = "-XDcompat";
   107         nArgs[1] = "-XDignore.symbol.file";
   108         System.arraycopy(args, 0, nArgs, 2, args.length);
   109         //System.err.println("NEW: " + Arrays.asList(nArgs));
   110         StringWriter out = new StringWriter();
   111         com.sun.tools.javap.Main.run(nArgs, new PrintWriter(out, true));
   112         return out.toString();
   113     }
   115     File write(String text, String suffix) throws IOException {
   116         File f = File.createTempFile("OptionTest", suffix);
   117         FileWriter out = new FileWriter(f);
   118         out.write(text);
   119         out.close();
   120         return f;
   121     }
   123     void view(String oldOut, String newOut) throws Exception {
   124         File oldFile = write(oldOut, "old");
   125         File newFile = write(newOut, "new");
   126         List<String> cmd = new ArrayList<String>();
   127         cmd.addAll(viewCmd);
   128         cmd.add(oldFile.getPath());
   129         cmd.add(newFile.getPath());
   130         Process p = new ProcessBuilder(cmd).redirectErrorStream(true).start();
   131         p.getOutputStream().close();
   132         String line;
   133         BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
   134         while ((line = in.readLine()) != null)
   135             System.err.println(line);
   136         in.close();
   137         p.waitFor();
   138     }
   140     String testClassName = "java.lang.SecurityManager";
   141     boolean viewResults;
   142     List<String> viewCmd;
   143 }

mercurial