Path: blob/master/test/langtools/tools/javac/6330997/T6330997.java
41149 views
/*1* Copyright (c) 2006, 2019, 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* @bug 6330997 7025789 8000961 8188870 819329026* @summary javac should accept class files with major version of the next release27* @author Wei Tao28* @modules jdk.compiler/com.sun.tools.javac.api29* jdk.compiler/com.sun.tools.javac.code30* jdk.compiler/com.sun.tools.javac.comp31* jdk.compiler/com.sun.tools.javac.main32* jdk.compiler/com.sun.tools.javac.util33* @clean T1 T234* @compile T1.java35* @compile T2.java36* @run main/othervm T633099737*/3839import java.nio.*;40import java.io.*;41import java.nio.channels.*;4243import com.sun.tools.javac.api.JavacTaskImpl;44import com.sun.tools.javac.code.ClassFinder.BadClassFile;45import com.sun.tools.javac.code.Symtab;46import com.sun.tools.javac.util.Names;47import javax.tools.ToolProvider;4849public class T6330997 {50public static void main(String... args) {51increaseMajor("T1.class", 1);52increaseMajor("T2.class", 2);53javax.tools.JavaCompiler tool = ToolProvider.getSystemJavaCompiler();54JavacTaskImpl task = (JavacTaskImpl)tool.getTask(null, null, null, null, null, null);55Symtab syms = Symtab.instance(task.getContext());56Names names = Names.instance(task.getContext());57task.ensureEntered();58try {59syms.enterClass(syms.unnamedModule, names.fromString("T1")).complete();60} catch (Exception e) {61e.printStackTrace();62throw new RuntimeException("Failed: unexpected exception while reading class T1");63}64try {65syms.enterClass(syms.unnamedModule, names.fromString("T2")).complete();66} catch (BadClassFile e) {67System.err.println("Passed: expected completion failure " + e.getClass().getName());68return;69} catch (Exception e) {70e.printStackTrace();71throw new RuntimeException("Failed: unexpected exception while reading class T2");72}73throw new RuntimeException("Failed: no error reported");74}7576// Increase class file cfile's major version by delta77static void increaseMajor(String cfile, int delta) {78try (RandomAccessFile cls =79new RandomAccessFile(new File(System.getProperty("test.classes", "."), cfile), "rw");80FileChannel fc = cls.getChannel()) {81ByteBuffer rbuf = ByteBuffer.allocate(2);82fc.read(rbuf, 6);83ByteBuffer wbuf = ByteBuffer.allocate(2);84wbuf.putShort(0, (short)(rbuf.getShort(0) + delta));85fc.write(wbuf, 6);86fc.force(false);87} catch (Exception e){88throw new RuntimeException("Failed: unexpected exception");89}90}91}929394