8311172: Classfile.PREVIEW_MINOR_VERSION doesn't match that read from class files

Reviewed-by: asotona
This commit is contained in:
Chen Liang 2023-07-17 11:53:32 +00:00 committed by Adam Sotona
parent afcf8e4751
commit 3fb9d117e3
2 changed files with 65 additions and 1 deletions
src/java.base/share/classes/jdk/internal/classfile
test/jdk/jdk/classfile

@ -690,7 +690,12 @@ public sealed interface Classfile
int JAVA_21_VERSION = 65;
int JAVA_22_VERSION = 66;
int PREVIEW_MINOR_VERSION = -1;
/**
* A minor version number indicating a class uses preview features
* of a Java SE version since 12, for major versions {@value
* #JAVA_12_VERSION} and above.
*/
int PREVIEW_MINOR_VERSION = 65535;
static int latestMajorVersion() {
return JAVA_22_VERSION;

@ -0,0 +1,59 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import jdk.internal.classfile.Classfile;
import org.junit.jupiter.api.Test;
import java.lang.constant.ClassDesc;
import static java.lang.constant.ConstantDescs.*;
import static jdk.internal.classfile.Classfile.*;
import static org.junit.jupiter.api.Assertions.*;
/*
* @test
* @bug 8311172
* @run junit PreviewMinorVersionTest
* @summary Ensures Classfile.PREVIEW_MINOR_VERSION equals that of classes with
* preview minor version from ClassModel::minorVersion
*/
public class PreviewMinorVersionTest {
@Test
public void testMinorVersionMatches() {
// compile a class with --enable-preview
// uses Record feature to trigger forcePreview
var cf = Classfile.of();
var cd = ClassDesc.of("Test");
var bytes = cf.build(cd, cb -> cb
.withSuperclass(CD_Object)
// old preview minor version,
// with all bits set to 1
.withVersion(JAVA_17_VERSION, -1)
);
var cm = Classfile.of().parse(bytes);
assertEquals(Classfile.PREVIEW_MINOR_VERSION, cm.minorVersion());
}
}