775N/A/*
775N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
775N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
775N/A *
775N/A * This code is free software; you can redistribute it and/or modify it
775N/A * under the terms of the GNU General Public License version 2 only, as
775N/A * published by the Free Software Foundation.
775N/A *
775N/A * This code is distributed in the hope that it will be useful, but WITHOUT
775N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
775N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
775N/A * version 2 for more details (a copy is included in the LICENSE file that
775N/A * accompanied this code).
775N/A *
775N/A * You should have received a copy of the GNU General Public License version
775N/A * 2 along with this work; if not, write to the Free Software Foundation,
775N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
775N/A *
775N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
775N/A * or visit www.oracle.com if you need additional information or have any
775N/A * questions.
775N/A */
775N/A
775N/A/*
775N/A * @test
775N/A * @bug 6999210
775N/A * @summary javac should be able to warn of anomalous conditions in classfiles
775N/A */
775N/A
775N/Aimport java.io.*;
775N/Aimport java.util.*;
775N/A
775N/Apublic class T6999210 {
775N/A public static void main(String... args) throws Exception {
775N/A new T6999210().run();
775N/A }
775N/A
775N/A void run() throws Exception {
775N/A File srcDir = new File("src");
775N/A File classesDir = new File("classes");
775N/A classesDir.mkdirs();
775N/A
775N/A File c_java = writeFile(srcDir, "C.java", "class C<T> { }");
775N/A compile("-d", classesDir.getPath(), c_java.getPath());
775N/A File c_class = new File(classesDir, "C.class");
775N/A setMajorVersion(c_class, 48);
775N/A File d_java = writeFile(srcDir, "D.java", "class D { C c; }");
775N/A
775N/A // verify no warning if -Xlint:classfile not enabled
775N/A String out1 = compile(
775N/A "-d", classesDir.getPath(),
775N/A "-classpath", classesDir.getPath(),
775N/A d_java.getPath());
775N/A if (out1.length() > 0)
775N/A error("unexpected output from javac");
775N/A
775N/A // sanity check of warning when -XDrawDiagnostics not used
775N/A String out2 = compile(
775N/A "-d", classesDir.getPath(),
775N/A "-classpath", classesDir.getPath(),
775N/A "-Xlint:classfile",
775N/A d_java.getPath());
775N/A if (!out2.contains("[classfile]"))
775N/A error("expected output \"[classfile]\" not found");
775N/A
775N/A // check specific details, using -XDrawDiagnostics
775N/A String out3 = compile(
775N/A "-d", classesDir.getPath(),
775N/A "-classpath", classesDir.getPath(),
775N/A "-Xlint:classfile", "-XDrawDiagnostics",
775N/A d_java.getPath());
775N/A String expect = "C.class:-:-: compiler.warn.future.attr: Signature, 49, 0, 48, 0";
775N/A if (!out3.contains(expect))
775N/A error("expected output \"" + expect + "\" not found");
775N/A
775N/A if (errors > 0)
775N/A throw new Exception(errors + " errors occurred");
775N/A }
775N/A
775N/A String compile(String... args) throws Exception {
775N/A System.err.println("compile: " + Arrays.asList(args));
775N/A StringWriter sw = new StringWriter();
775N/A PrintWriter pw = new PrintWriter(sw);
775N/A int rc = com.sun.tools.javac.Main.compile(args, pw);
775N/A pw.close();
775N/A String out = sw.toString();
775N/A if (out.length() > 0)
775N/A System.err.println(out);
775N/A if (rc != 0)
775N/A throw new Exception("compilation failed, rc=" + rc);
775N/A return out;
775N/A }
775N/A
775N/A void setMajorVersion(File f, int major) throws IOException {
775N/A int len = (int) f.length();
775N/A byte[] data = new byte[len];
775N/A try (DataInputStream in = new DataInputStream(new FileInputStream(f))) {
775N/A in.readFully(data);
775N/A }
775N/A // u4 magic
775N/A // u2 minor
775N/A data[6] = (byte) (major >> 8);
775N/A data[7] = (byte) (major & 0xff);
775N/A try (FileOutputStream out = new FileOutputStream(f)) {
775N/A out.write(data);
775N/A }
775N/A }
775N/A
775N/A File writeFile(File dir, String path, String body) throws IOException {
775N/A File f = new File(dir, path);
775N/A f.getParentFile().mkdirs();
775N/A try (FileWriter out = new FileWriter(f)) {
775N/A out.write(body);
775N/A }
775N/A return f;
775N/A }
775N/A
775N/A void error(String msg) {
775N/A System.err.println("Error: " + msg);
775N/A errors++;
775N/A }
775N/A
775N/A int errors;
775N/A}