1/*
2 * Copyright (c) 2014, 2016, 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 */
23
24/*
25 * @test
26 * @library /test/lib
27 * @modules java.base/jdk.internal.org.objectweb.asm
28 *          java.base/jdk.internal.misc
29 *          java.management
30 * @compile -XDignore.symbol.file UnsupportedClassFileVersion.java
31 * @run main UnsupportedClassFileVersion
32 */
33
34import java.io.File;
35import java.io.FileOutputStream;
36import jdk.internal.org.objectweb.asm.ClassWriter;
37import jdk.internal.org.objectweb.asm.MethodVisitor;
38import jdk.internal.org.objectweb.asm.Opcodes;
39import jdk.test.lib.process.ProcessTools;
40import jdk.test.lib.process.OutputAnalyzer;
41
42public class UnsupportedClassFileVersion implements Opcodes {
43    public static void main(String... args) throws Exception {
44        writeClassFile();
45        ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true, "-cp", ".",  "ClassFile");
46        OutputAnalyzer output = new OutputAnalyzer(pb.start());
47        output.shouldContain("ClassFile has been compiled by a more recent version of the " +
48                            "Java Runtime (class file version 99.0), this version of " +
49                            "the Java Runtime only recognizes class file versions up to " +
50                            System.getProperty("java.class.version"));
51
52        output.shouldHaveExitValue(1);
53    }
54
55    public static void writeClassFile() throws Exception {
56        ClassWriter cw = new ClassWriter(0);
57        MethodVisitor mv;
58
59        cw.visit(99, ACC_PUBLIC + ACC_SUPER, "ClassFile", null, "java/lang/Object", null);
60        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
61        mv.visitCode();
62        mv.visitVarInsn(ALOAD, 0);
63        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
64        mv.visitInsn(RETURN);
65        mv.visitMaxs(1, 1);
66        mv.visitEnd();
67        cw.visitEnd();
68
69        try (FileOutputStream fos = new FileOutputStream(new File("ClassFile.class"))) {
70             fos.write(cw.toByteArray());
71        }
72    }
73}
74