Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/javac/6917288/T6917288.java
38813 views
/*1* Copyright (c) 2010, 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*/2728import java.io.*;29import java.util.*;3031public class T6917288 {32// refers to kind of reference to an anon inner class that may be generated33enum Kind { NONE, FALSE, TRUE, ALWAYS };3435public static void main(String... args) throws Exception {36new T6917288().run();37}3839void run() throws Exception {40for (Kind k: Kind.values()) {41test(k);42}4344if (errors > 0)45throw new Exception(errors + " errors occurred");46}4748/**49* Run a test case for Kind k.50*/51void test(Kind k) throws Exception {52System.err.println("Test " + (++count) + ": " + k);53File testDir = new File("test" + count);54File srcDir = new File(testDir, "src");55srcDir.mkdirs();56File classesDir = new File(testDir, "classes");57classesDir.mkdirs();5859List<String> opts = new ArrayList<String>();60opts.add("-d");61opts.add(classesDir.getPath());6263File f = writeFile(srcDir, k);64int rc = compile(opts, f);65if (rc != 0) {66error("compilation failed: rc=" + rc);67return;68}6970check(classesDir, "Test.class", "Test$Inner.class", "Test$1.class");71}7273/**74* Compile files with given options.75* Display any output from compiler, and return javac return code.76*/77int compile(List<String> opts, File... files) {78List<String> args = new ArrayList<String>();79args.addAll(opts);80for (File f: files)81args.add(f.getPath());82StringWriter sw = new StringWriter();83PrintWriter pw = new PrintWriter(sw);84int rc = com.sun.tools.javac.Main.compile(args.toArray(new String[args.size()]), pw);85pw.close();86String out = sw.toString();87if (out.length() > 0)88System.err.println(out);89return rc;90}9192/**93* Check that a directory contains the expected files.94*/95void check(File dir, String... paths) {96Set<String> found = new TreeSet<String>(Arrays.asList(dir.list()));97Set<String> expect = new TreeSet<String>(Arrays.asList(paths));98if (found.equals(expect))99return;100for (String f: found) {101if (!expect.contains(f))102error("Unexpected file found: " + f);103}104for (String e: expect) {105if (!found.contains(e))106error("Expected file not found: " + e);107}108}109110/**111* Write source file for test case k.112*/113File writeFile(File dir, Kind k) throws Exception {114StringBuilder sb = new StringBuilder();115sb.append("public class Test {\n");116sb.append(" private Inner inner;\n");117118// generate different cases of an anon inner class119if (k != Kind.NONE) {120sb.append(" private void m() {\n");121sb.append(" ");122switch (k) {123case FALSE: case TRUE:124sb.append("if (" + k.toString().toLowerCase() + ") ");125}126sb.append("new Runnable() { public void run() { } };\n");127sb.append(" }\n");128}129130sb.append(" private void init() {\n");131sb.append(" inner = new Inner();\n");132sb.append(" }\n");133sb.append("\n");134sb.append(" private static class Inner {\n");135sb.append(" private Inner() {\n");136sb.append(" }\n");137sb.append(" }\n");138sb.append("}\n");139140File f = new File(dir, "Test.java");141FileWriter w = new FileWriter(f);142w.write(sb.toString());143w.close();144return f;145}146147/**148* Record an error message.149*/150void error(String msg) {151System.err.println(msg);152errors++;153}154155int count;156int errors;157}158159160