1 /*
   2  * Copyright (c) 2008, 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 import java.io.*;
  25 import java.util.*;
  26 
  27 /*
  28  * @test
  29  * @bug 4501661
  30  * @summary disallow mixing -public, -private, and -protected
  31  * @modules jdk.compiler
  32  */
  33 public class T4501661 {
  34     public static void main(String... args) throws Exception {
  35         new T4501661().run();
  36     }
  37 
  38     void run() throws Exception {
  39         File javaFile = writeTestFile();
  40         File classFile = compileTestFile(javaFile);
  41         boolean[] values = { false, true };
  42         for (boolean pack : values) {
  43             for (boolean priv : values) {
  44                 for (boolean prot : values) {
  45                     for (boolean publ : values) {
  46                         test(pack, priv, prot, publ, classFile);
  47                     }
  48                 }
  49             }
  50         }
  51 
  52         if (errors > 0)
  53             throw new Exception(errors + " errors found");
  54     }
  55 
  56     void test(boolean pack, boolean priv, boolean prot, boolean publ, File classFile) {
  57         List<String> args = new ArrayList<String>();
  58         if (pack)
  59             args.add("-package");
  60         if (priv)
  61             args.add("-private");
  62         if (prot)
  63             args.add("-protected");
  64         if (publ)
  65             args.add("-public");
  66         boolean expectOK = (args.size() <= 1);
  67         args.add(classFile.getPath());
  68         String out = javap(args, expectOK);
  69         if (out == null)
  70             return;
  71         if (!priv && !prot && !publ)
  72             checkNone(out, "private");
  73         if (prot)
  74             checkNone(out, "private", "package");
  75         if (publ)
  76             checkNone(out, "private", "package", "protected");
  77     }
  78 
  79     File writeTestFile() throws IOException {
  80         File f = new File("Test.java");
  81         PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));
  82         out.println("abstract class Test { ");
  83         out.println("  public void public_m() { }");
  84         out.println("  protected void protected_m() { }");
  85         out.println("  private void private_m() { }");
  86         out.println("  void package_m() { }");
  87         out.println("}");
  88         out.close();
  89         return f;
  90     }
  91 
  92     File compileTestFile(File f) {
  93         int rc = com.sun.tools.javac.Main.compile(new String[] { "-g", f.getPath() });
  94         if (rc != 0)
  95             throw new Error("compilation failed. rc=" + rc);
  96         String path = f.getPath();
  97         return new File(path.substring(0, path.length() - 5) + ".class");
  98     }
  99 
 100     String javap(List<String> args, boolean expectOK) {
 101         StringWriter sw = new StringWriter();
 102         PrintWriter pw = new PrintWriter(sw);
 103         int rc = com.sun.tools.javap.Main.run(args.toArray(new String[args.size()]), pw);
 104         System.err.println(args);
 105         System.err.println(sw);
 106         if (expectOK) {
 107             if (rc == 0)
 108                 return sw.toString();
 109             else
 110                 error("javap failed unexpectedly; rc=" + rc + "\n" + sw);
 111         } else {
 112             if (rc == 0)
 113                 error("javap succeeded unexpectedly");
 114         }
 115         return null;
 116     }
 117 
 118     void checkNone(String log, String... words) {
 119         for (String word: words) {
 120             if (log.indexOf(word) != -1)
 121                 error("\"" + word + "\" unexpectedly found in output");
 122         }
 123     }
 124 
 125     void error(String msg) {
 126         System.err.println("error: " + msg);
 127         errors++;
 128     }
 129 
 130     int errors;
 131 }