1 /*
   2  * Copyright (c) 2010, 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 /* @test
  25  * @bug 6917288
  26  * @summary Unnamed nested class is not generated
  27  * @modules jdk.compiler
  28  */
  29 
  30 import java.io.*;
  31 import java.util.*;
  32 
  33 public class T6917288 {
  34     // refers to kind of reference to an anon inner class that may be generated
  35     enum Kind { NONE, FALSE, TRUE, ALWAYS };
  36 
  37     public static void main(String... args) throws Exception {
  38         new T6917288().run();
  39     }
  40 
  41     void run() throws Exception {
  42         for (Kind k: Kind.values()) {
  43             test(k);
  44         }
  45 
  46         if (errors > 0)
  47             throw new Exception(errors + " errors occurred");
  48     }
  49 
  50     /**
  51      *  Run a test case for Kind k.
  52      */
  53     void test(Kind k) throws Exception {
  54         System.err.println("Test " + (++count) + ": " + k);
  55         File testDir = new File("test" + count);
  56         File srcDir = new File(testDir, "src");
  57         srcDir.mkdirs();
  58         File classesDir = new File(testDir, "classes");
  59         classesDir.mkdirs();
  60 
  61         List<String> opts = new ArrayList<String>();
  62         opts.add("-d");
  63         opts.add(classesDir.getPath());
  64 
  65         File f = writeFile(srcDir, k);
  66         int rc = compile(opts, f);
  67         if (rc != 0) {
  68             error("compilation failed: rc=" + rc);
  69             return;
  70         }
  71 
  72         switch (k) {
  73             case ALWAYS:
  74             case TRUE:
  75                 check(classesDir, "Test.class", "Test$Inner.class", "Test$1.class");
  76                 break;
  77             default:
  78                 check(classesDir, "Test.class", "Test$Inner.class");
  79         }
  80     }
  81 
  82     /**
  83      *  Compile files with given options.
  84      *  Display any output from compiler, and return javac return code.
  85      */
  86     int compile(List<String> opts, File... files) {
  87         List<String> args = new ArrayList<String>();
  88         args.addAll(opts);
  89         for (File f: files)
  90             args.add(f.getPath());
  91         StringWriter sw = new StringWriter();
  92         PrintWriter pw = new PrintWriter(sw);
  93         int rc = com.sun.tools.javac.Main.compile(args.toArray(new String[args.size()]), pw);
  94         pw.close();
  95         String out = sw.toString();
  96         if (out.length() > 0)
  97             System.err.println(out);
  98         return rc;
  99     }
 100 
 101     /**
 102      *  Check that a directory contains the expected files.
 103      */
 104     void check(File dir, String... paths) {
 105         Set<String> found = new TreeSet<String>(Arrays.asList(dir.list()));
 106         Set<String> expect = new TreeSet<String>(Arrays.asList(paths));
 107         if (found.equals(expect))
 108             return;
 109         for (String f: found) {
 110             if (!expect.contains(f))
 111                 error("Unexpected file found: " + f);
 112         }
 113         for (String e: expect) {
 114             if (!found.contains(e))
 115                 error("Expected file not found: " + e);
 116         }
 117     }
 118 
 119     /**
 120      *  Write source file for test case k.
 121      */
 122     File writeFile(File dir, Kind k) throws Exception {
 123         StringBuilder sb = new StringBuilder();
 124         sb.append("public class Test {\n");
 125         sb.append("    private Inner inner;\n");
 126 
 127         // generate different cases of an anon inner class
 128         if (k != Kind.NONE) {
 129             sb.append("    private void m() {\n");
 130             sb.append("        ");
 131             switch (k) {
 132                 case FALSE: case TRUE:
 133                     sb.append("if (" + k.toString().toLowerCase() + ") ");
 134             }
 135             sb.append("new Runnable() { public void run() { } };\n");
 136             sb.append("    }\n");
 137         }
 138 
 139         sb.append("    private void init() {\n");
 140         sb.append("        inner = new Inner();\n");
 141         sb.append("    }\n");
 142         sb.append("\n");
 143         sb.append("    private static class Inner {\n");
 144         sb.append("        private Inner() {\n");
 145         sb.append("        }\n");
 146         sb.append("    }\n");
 147         sb.append("}\n");
 148 
 149         File f = new File(dir, "Test.java");
 150         FileWriter w = new FileWriter(f);
 151         w.write(sb.toString());
 152         w.close();
 153         return f;
 154     }
 155 
 156     /**
 157      *  Record an error message.
 158      */
 159     void error(String msg) {
 160         System.err.println(msg);
 161         errors++;
 162     }
 163 
 164     int count;
 165     int errors;
 166 }