test/tools/javac/T6999210.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 6999210
    27  * @summary javac should be able to warn of anomalous conditions in classfiles
    28  */
    30 import java.io.*;
    31 import java.util.*;
    33 public class T6999210 {
    34     public static void main(String... args) throws Exception {
    35         new T6999210().run();
    36     }
    38     void run() throws Exception {
    39         File srcDir = new File("src");
    40         File classesDir = new File("classes");
    41         classesDir.mkdirs();
    43         File c_java = writeFile(srcDir, "C.java", "class C<T> { }");
    44         compile("-d", classesDir.getPath(), c_java.getPath());
    45         File c_class = new File(classesDir, "C.class");
    46         setMajorVersion(c_class, 48);
    47         File d_java = writeFile(srcDir, "D.java", "class D { C c; }");
    49         // verify no warning if -Xlint:classfile not enabled
    50         String out1 = compile(
    51             "-d", classesDir.getPath(),
    52             "-classpath", classesDir.getPath(),
    53             d_java.getPath());
    54         if (out1.length() > 0)
    55             error("unexpected output from javac");
    57         // sanity check of warning when -XDrawDiagnostics not used
    58         String out2 = compile(
    59             "-d", classesDir.getPath(),
    60             "-classpath", classesDir.getPath(),
    61             "-Xlint:classfile",
    62             d_java.getPath());
    63         if (!out2.contains("[classfile]"))
    64             error("expected output \"[classfile]\" not found");
    66         // check specific details, using -XDrawDiagnostics
    67         String out3 = compile(
    68             "-d", classesDir.getPath(),
    69             "-classpath", classesDir.getPath(),
    70             "-Xlint:classfile", "-XDrawDiagnostics",
    71             d_java.getPath());
    72         String expect = "C.class:-:-: compiler.warn.future.attr: Signature, 49, 0, 48, 0";
    73         if (!out3.contains(expect))
    74             error("expected output \"" + expect + "\" not found");
    76         if (errors > 0)
    77             throw new Exception(errors + " errors occurred");
    78     }
    80     String compile(String... args) throws Exception {
    81         System.err.println("compile: " + Arrays.asList(args));
    82         StringWriter sw = new StringWriter();
    83         PrintWriter pw = new PrintWriter(sw);
    84         int rc = com.sun.tools.javac.Main.compile(args, pw);
    85         pw.close();
    86         String out = sw.toString();
    87         if (out.length() > 0)
    88             System.err.println(out);
    89         if (rc != 0)
    90             throw new Exception("compilation failed, rc=" + rc);
    91         return out;
    92     }
    94     void setMajorVersion(File f, int major) throws IOException {
    95         int len = (int) f.length();
    96         byte[] data = new byte[len];
    97         try (DataInputStream in = new DataInputStream(new FileInputStream(f))) {
    98             in.readFully(data);
    99         }
   100         // u4 magic
   101         // u2 minor
   102         data[6] = (byte) (major >> 8);
   103         data[7] = (byte) (major & 0xff);
   104         try (FileOutputStream out = new FileOutputStream(f)) {
   105             out.write(data);
   106         }
   107     }
   109     File writeFile(File dir, String path, String body) throws IOException {
   110         File f = new File(dir, path);
   111         f.getParentFile().mkdirs();
   112         try (FileWriter out = new FileWriter(f)) {
   113             out.write(body);
   114         }
   115         return f;
   116     }
   118     void error(String msg) {
   119         System.err.println("Error: " + msg);
   120         errors++;
   121     }
   123     int errors;
   124 }

mercurial