Path: blob/master/test/langtools/tools/javac/6917288/T6917288.java
41149 views
/*1* Copyright (c) 2010, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 691728825* @summary Unnamed nested class is not generated26* @modules jdk.compiler27*/2829import java.io.*;30import java.util.*;3132public class T6917288 {33// refers to kind of reference to an anon inner class that may be generated34enum Kind { NONE, FALSE, TRUE, ALWAYS };3536public static void main(String... args) throws Exception {37new T6917288().run();38}3940void run() throws Exception {41for (Kind k: Kind.values()) {42test(k);43}4445if (errors > 0)46throw new Exception(errors + " errors occurred");47}4849/**50* Run a test case for Kind k.51*/52void test(Kind k) throws Exception {53System.err.println("Test " + (++count) + ": " + k);54File testDir = new File("test" + count);55File srcDir = new File(testDir, "src");56srcDir.mkdirs();57File classesDir = new File(testDir, "classes");58classesDir.mkdirs();5960List<String> opts = new ArrayList<String>();61opts.add("-d");62opts.add(classesDir.getPath());6364File f = writeFile(srcDir, k);65int rc = compile(opts, f);66if (rc != 0) {67error("compilation failed: rc=" + rc);68return;69}7071switch (k) {72case ALWAYS:73case TRUE:74check(classesDir, "Test.class", "Test$Inner.class", "Test$1.class");75break;76default:77check(classesDir, "Test.class", "Test$Inner.class");78}79}8081/**82* Compile files with given options.83* Display any output from compiler, and return javac return code.84*/85int compile(List<String> opts, File... files) {86List<String> args = new ArrayList<String>();87args.addAll(opts);88for (File f: files)89args.add(f.getPath());90StringWriter sw = new StringWriter();91PrintWriter pw = new PrintWriter(sw);92int rc = com.sun.tools.javac.Main.compile(args.toArray(new String[args.size()]), pw);93pw.close();94String out = sw.toString();95if (out.length() > 0)96System.err.println(out);97return rc;98}99100/**101* Check that a directory contains the expected files.102*/103void check(File dir, String... paths) {104Set<String> found = new TreeSet<String>(Arrays.asList(dir.list()));105Set<String> expect = new TreeSet<String>(Arrays.asList(paths));106if (found.equals(expect))107return;108for (String f: found) {109if (!expect.contains(f))110error("Unexpected file found: " + f);111}112for (String e: expect) {113if (!found.contains(e))114error("Expected file not found: " + e);115}116}117118/**119* Write source file for test case k.120*/121File writeFile(File dir, Kind k) throws Exception {122StringBuilder sb = new StringBuilder();123sb.append("public class Test {\n");124sb.append(" private Inner inner;\n");125126// generate different cases of an anon inner class127if (k != Kind.NONE) {128sb.append(" private void m() {\n");129sb.append(" ");130switch (k) {131case FALSE: case TRUE:132sb.append("if (" + k.toString().toLowerCase() + ") ");133}134sb.append("new Runnable() { public void run() { } };\n");135sb.append(" }\n");136}137138sb.append(" private void init() {\n");139sb.append(" inner = new Inner();\n");140sb.append(" }\n");141sb.append("\n");142sb.append(" private static class Inner {\n");143sb.append(" private Inner() {\n");144sb.append(" }\n");145sb.append(" }\n");146sb.append("}\n");147148File f = new File(dir, "Test.java");149FileWriter w = new FileWriter(f);150w.write(sb.toString());151w.close();152return f;153}154155/**156* Record an error message.157*/158void error(String msg) {159System.err.println(msg);160errors++;161}162163int count;164int errors;165}166167168