Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8270949: Make dynamically generated classes with the class file versi…
…on of the current release

Reviewed-by: alanb
  • Loading branch information
Mandy Chung committed Jul 21, 2021
1 parent b7245c6 commit ddce47c
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 7 deletions.
Expand Up @@ -621,7 +621,7 @@ byte[] generateConcreteSpeciesCodeFile(String className0, ClassSpecializer<T,K,S

final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
final int NOT_ACC_PUBLIC = 0; // not ACC_PUBLIC
cw.visit(V1_6, NOT_ACC_PUBLIC + ACC_FINAL + ACC_SUPER, className, null, superClassName, null);
cw.visit(CLASSFILE_VERSION, NOT_ACC_PUBLIC + ACC_FINAL + ACC_SUPER, className, null, superClassName, null);

final String sourceFile = className.substring(className.lastIndexOf('.')+1);
cw.visitSource(sourceFile, null);
Expand Down
Expand Up @@ -39,6 +39,7 @@
import java.util.stream.Stream;

import static java.lang.invoke.LambdaForm.BasicType.*;
import static java.lang.invoke.MethodHandleStatics.CLASSFILE_VERSION;
import static java.lang.invoke.MethodTypeForm.*;
import static java.lang.invoke.LambdaForm.Kind.*;

Expand Down Expand Up @@ -504,7 +505,7 @@ static byte[] generateInvokersHolderClassBytes(String className,
*/
private static byte[] generateCodeBytesForLFs(String className, String[] names, LambdaForm[] forms) {
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
cw.visit(Opcodes.V1_8, Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL + Opcodes.ACC_SUPER,
cw.visit(CLASSFILE_VERSION, Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL + Opcodes.ACC_SUPER,
className, null, InvokerBytecodeGenerator.INVOKER_SUPER_NAME, null);
cw.visitSource(className.substring(className.lastIndexOf('/') + 1), null);

Expand Down
Expand Up @@ -26,6 +26,7 @@
package java.lang.invoke;

import jdk.internal.misc.CDS;
import jdk.internal.misc.VM;
import jdk.internal.org.objectweb.asm.*;
import sun.invoke.util.BytecodeDescriptor;
import sun.invoke.util.VerifyAccess;
Expand Down Expand Up @@ -56,7 +57,7 @@
* @see LambdaMetafactory
*/
/* package */ final class InnerClassLambdaMetafactory extends AbstractValidatingLambdaMetafactory {
private static final int CLASSFILE_VERSION = 59;
private static final int CLASSFILE_VERSION = VM.classFileVersion();
private static final String METHOD_DESCRIPTOR_VOID = Type.getMethodDescriptor(Type.VOID_TYPE);
private static final String JAVA_LANG_OBJECT = "java/lang/Object";
private static final String NAME_CTOR = "<init>";
Expand Down
Expand Up @@ -340,7 +340,7 @@ private ClassWriter classFilePrologue() {
final int NOT_ACC_PUBLIC = 0; // not ACC_PUBLIC
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
setClassWriter(cw);
cw.visit(Opcodes.V1_8, NOT_ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_SUPER,
cw.visit(CLASSFILE_VERSION, NOT_ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_SUPER,
className, null, INVOKER_SUPER_NAME, null);
cw.visitSource(SOURCE_PREFIX + name, null);
return cw;
Expand Down
Expand Up @@ -1156,7 +1156,7 @@ private static byte[] generateInvokerTemplate() {
// return vamh.invokeExact(args);
// }
// }
cw.visit(52, ACC_PRIVATE | ACC_SUPER, "InjectedInvoker", null, "java/lang/Object", null);
cw.visit(CLASSFILE_VERSION, ACC_PRIVATE | ACC_SUPER, "InjectedInvoker", null, "java/lang/Object", null);

MethodVisitor mv = cw.visitMethod(ACC_STATIC, "invoke_V",
"(Ljava/lang/invoke/MethodHandle;[Ljava/lang/Object;)Ljava/lang/Object;",
Expand Down
Expand Up @@ -27,6 +27,7 @@

import jdk.internal.misc.CDS;
import jdk.internal.misc.Unsafe;
import jdk.internal.misc.VM;
import sun.security.action.GetPropertyAction;

import java.util.Properties;
Expand All @@ -46,6 +47,7 @@ class MethodHandleStatics {
private MethodHandleStatics() { } // do not instantiate

static final Unsafe UNSAFE = Unsafe.getUnsafe();
static final int CLASSFILE_VERSION = VM.classFileVersion();

static final boolean DEBUG_METHOD_HANDLE_NAMES;
static final boolean DUMP_CLASS_FILES;
Expand Down
Expand Up @@ -25,6 +25,7 @@

package java.lang.reflect;

import jdk.internal.misc.VM;
import jdk.internal.org.objectweb.asm.ClassWriter;
import jdk.internal.org.objectweb.asm.Label;
import jdk.internal.org.objectweb.asm.MethodVisitor;
Expand Down Expand Up @@ -55,7 +56,7 @@
* "generateProxyClass" method.
*/
final class ProxyGenerator extends ClassWriter {

private static final int CLASSFILE_VERSION = VM.classFileVersion();
private static final String JL_CLASS = "java/lang/Class";
private static final String JL_OBJECT = "java/lang/Object";
private static final String JL_THROWABLE = "java/lang/Throwable";
Expand Down Expand Up @@ -455,7 +456,7 @@ protected ClassLoader getClassLoader() {
* class file generation process.
*/
private byte[] generateClassFile() {
visit(V14, accessFlags, dotToSlash(className), null,
visit(CLASSFILE_VERSION, accessFlags, dotToSlash(className), null,
JLR_PROXY, typeNames(interfaces));

/*
Expand Down
8 changes: 8 additions & 0 deletions src/java.base/share/classes/jdk/internal/misc/VM.java
Expand Up @@ -151,6 +151,14 @@ public static boolean isDirectMemoryPageAligned() {
private static int classFileMinorVersion;
private static final int PREVIEW_MINOR_VERSION = 65535;

/**
* Returns the class file version of the current release.
* @jvms 4.1 Table 4.1-A. class file format major versions
*/
public static int classFileVersion() {
return classFileMajorVersion;
}

/**
* Tests if the given version is a supported {@code class}
* file version.
Expand Down

1 comment on commit ddce47c

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.