Path: blob/master/test/hotspot/jtreg/runtime/ClassFile/UnsupportedClassFileVersion.java
64474 views
/*1* Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @library /test/lib26* @modules java.base/jdk.internal.org.objectweb.asm27* java.base/jdk.internal.misc28* java.management29* @run driver UnsupportedClassFileVersion30*/3132import java.io.File;33import java.io.FileOutputStream;34import jdk.internal.org.objectweb.asm.ClassWriter;35import jdk.internal.org.objectweb.asm.MethodVisitor;36import jdk.internal.org.objectweb.asm.Opcodes;37import jdk.test.lib.process.ProcessTools;38import jdk.test.lib.process.OutputAnalyzer;3940public class UnsupportedClassFileVersion implements Opcodes {41public static void main(String... args) throws Exception {42writeClassFile();43ProcessBuilder pb = ProcessTools.createTestJvm("-cp", ".", "ClassFile");44OutputAnalyzer output = new OutputAnalyzer(pb.start());45output.shouldContain("ClassFile has been compiled by a more recent version of the " +46"Java Runtime (class file version 99.0), this version of " +47"the Java Runtime only recognizes class file versions up to " +48System.getProperty("java.class.version"));4950output.shouldHaveExitValue(1);51}5253public static void writeClassFile() throws Exception {54ClassWriter cw = new ClassWriter(0);55MethodVisitor mv;5657cw.visit(99, ACC_PUBLIC + ACC_SUPER, "ClassFile", null, "java/lang/Object", null);58mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);59mv.visitCode();60mv.visitVarInsn(ALOAD, 0);61mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);62mv.visitInsn(RETURN);63mv.visitMaxs(1, 1);64mv.visitEnd();65cw.visitEnd();6667try (FileOutputStream fos = new FileOutputStream(new File("ClassFile.class"))) {68fos.write(cw.toByteArray());69}70}71}727374