test/tools/javac/parser/ExtraSemiTest.java

Tue, 28 Dec 2010 15:54:52 -0800

author
ohair
date
Tue, 28 Dec 2010 15:54:52 -0800
changeset 798
4868a36f6fd8
parent 0
959103a6100f
permissions
-rw-r--r--

6962318: Update copyright year
Reviewed-by: xdono

     1 /*
     2  * Copyright (c) 2010, 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  */
    24 /*
    25  * @test
    26  * @bug 6921495
    27  * @summary spurious semicolons in class def cause empty NOPOS blocks
    28  */
    30 import java.io.*;
    31 import java.net.*;
    32 import java.util.*;
    33 import javax.tools.*;
    34 import com.sun.source.util.*;
    36 public class ExtraSemiTest {
    38     static class JavaSource extends SimpleJavaFileObject {
    40         final static String source =
    41                         "class C {\n" +
    42                         "    int x;;\n" +
    43                         "    class X { int i;; };\n" +
    44                         "}";
    46         JavaSource() {
    47             super(URI.create("myfo:/C.java"), JavaFileObject.Kind.SOURCE);
    48         }
    50         @Override
    51         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
    52             return source;
    53         }
    54     }
    56     public static void main(String... args) throws IOException {
    57         new ExtraSemiTest().run();
    58     }
    60     void run() throws IOException {
    61         File destDir = new File("classes"); destDir.mkdir();
    62         final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    63         JavaSource source = new JavaSource();
    64         JavacTask ct = (JavacTask)tool.getTask(null, null, null,
    65                 Arrays.asList("-d", destDir.getPath(), "-XD-printsource"),
    66                 null,
    67                 Arrays.asList(source));
    68         Boolean ok = ct.call();
    69         if (!ok) throw new AssertionError("compilation failed");
    71         String text = readFile(new File(destDir, "C.java"));
    72         System.out.println(text);
    74         // compress/canonicalize all whitespace
    75         String canon = text.replaceAll("\\s+", " ");
    76         System.out.println("canon: " + canon);
    78         // There are no empty blocks in the original text.
    79         // C will be given a default constructor "C() { super(); }" which
    80         // does not have any empty blocks.
    81         // The bug is that spurious semicolons in the class defn are parsed
    82         // into redundant empty blocks in the tree, so verify there are
    83         // no empty blocks in the -printsource output
    85         if (canon.contains("{ }"))
    86             throw new AssertionError("unexpected empty block found");
    87     }
    89     String readFile(File f) throws IOException {
    90         int len = (int) f.length();
    91         byte[] data = new byte[len];
    92         DataInputStream in = new DataInputStream(new FileInputStream(f));
    93         try {
    94             in.readFully(data);
    95             return new String(data);
    96         } finally {
    97             in.close();
    98         }
    99     }
   100 }

mercurial