666N/A/*
666N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
666N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
666N/A *
666N/A * This code is free software; you can redistribute it and/or modify it
666N/A * under the terms of the GNU General Public License version 2 only, as
666N/A * published by the Free Software Foundation.
666N/A *
666N/A * This code is distributed in the hope that it will be useful, but WITHOUT
666N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
666N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
666N/A * version 2 for more details (a copy is included in the LICENSE file that
666N/A * accompanied this code).
666N/A *
666N/A * You should have received a copy of the GNU General Public License version
666N/A * 2 along with this work; if not, write to the Free Software Foundation,
666N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
666N/A *
666N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
666N/A * or visit www.oracle.com if you need additional information or have any
666N/A * questions.
666N/A */
666N/A
666N/A/*
666N/A * @test
666N/A * @bug 6921495
666N/A * @summary spurious semicolons in class def cause empty NOPOS blocks
666N/A */
666N/A
666N/Aimport java.io.*;
666N/Aimport java.net.*;
666N/Aimport java.util.*;
666N/Aimport javax.tools.*;
666N/Aimport com.sun.source.util.*;
666N/A
666N/Apublic class ExtraSemiTest {
666N/A
666N/A static class JavaSource extends SimpleJavaFileObject {
666N/A
666N/A final static String source =
666N/A "class C {\n" +
666N/A " int x;;\n" +
666N/A " class X { int i;; };\n" +
666N/A "}";
666N/A
666N/A JavaSource() {
666N/A super(URI.create("myfo:/C.java"), JavaFileObject.Kind.SOURCE);
666N/A }
666N/A
666N/A @Override
666N/A public CharSequence getCharContent(boolean ignoreEncodingErrors) {
666N/A return source;
666N/A }
666N/A }
666N/A
666N/A public static void main(String... args) throws IOException {
666N/A new ExtraSemiTest().run();
666N/A }
666N/A
666N/A void run() throws IOException {
666N/A File destDir = new File("classes"); destDir.mkdir();
666N/A final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
666N/A JavaSource source = new JavaSource();
666N/A JavacTask ct = (JavacTask)tool.getTask(null, null, null,
666N/A Arrays.asList("-d", destDir.getPath(), "-XD-printsource"),
666N/A null,
666N/A Arrays.asList(source));
666N/A Boolean ok = ct.call();
666N/A if (!ok) throw new AssertionError("compilation failed");
666N/A
666N/A String text = readFile(new File(destDir, "C.java"));
666N/A System.out.println(text);
666N/A
666N/A // compress/canonicalize all whitespace
666N/A String canon = text.replaceAll("\\s+", " ");
666N/A System.out.println("canon: " + canon);
666N/A
666N/A // There are no empty blocks in the original text.
666N/A // C will be given a default constructor "C() { super(); }" which
666N/A // does not have any empty blocks.
666N/A // The bug is that spurious semicolons in the class defn are parsed
666N/A // into redundant empty blocks in the tree, so verify there are
666N/A // no empty blocks in the -printsource output
666N/A
666N/A if (canon.contains("{ }"))
666N/A throw new AssertionError("unexpected empty block found");
666N/A }
666N/A
666N/A String readFile(File f) throws IOException {
666N/A int len = (int) f.length();
666N/A byte[] data = new byte[len];
666N/A DataInputStream in = new DataInputStream(new FileInputStream(f));
666N/A try {
666N/A in.readFully(data);
666N/A return new String(data);
666N/A } finally {
666N/A in.close();
666N/A }
666N/A }
666N/A}