8252725: Refactor jlink GenerateJLIClassesPlugin code
Cleanup code for jlink classes generation, move parsing file work to java.lang.invoke and add a new API in interface JavaLangInvokeAccess to generate holder classes, remove old APIs. The new API is used both by JLI and CDS. Reviewed-by: mchung, sundar
This commit is contained in:
parent
9b5a9b6189
commit
8f36580594
@ -46,6 +46,7 @@ import java.util.Objects;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static java.lang.invoke.GenerateJLIClassesHelper.traceSpeciesType;
|
||||
import static java.lang.invoke.LambdaForm.*;
|
||||
import static java.lang.invoke.MethodHandleNatives.Constants.REF_getStatic;
|
||||
import static java.lang.invoke.MethodHandleNatives.Constants.REF_putStatic;
|
||||
@ -475,15 +476,8 @@ abstract class ClassSpecializer<T,K,S extends ClassSpecializer<T,K,S>.SpeciesDat
|
||||
Class<?> salvage = null;
|
||||
try {
|
||||
salvage = BootLoader.loadClassOrNull(className);
|
||||
if (TRACE_RESOLVE && salvage != null) {
|
||||
// Used by jlink species pregeneration plugin, see
|
||||
// jdk.tools.jlink.internal.plugins.GenerateJLIClassesPlugin
|
||||
System.out.println("[SPECIES_RESOLVE] " + className + " (salvaged)");
|
||||
}
|
||||
traceSpeciesType(className, salvage);
|
||||
} catch (Error ex) {
|
||||
if (TRACE_RESOLVE) {
|
||||
System.out.println("[SPECIES_FRESOLVE] " + className + " (Error) " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
final Class<? extends T> speciesCode;
|
||||
if (salvage != null) {
|
||||
@ -494,19 +488,12 @@ abstract class ClassSpecializer<T,K,S extends ClassSpecializer<T,K,S>.SpeciesDat
|
||||
// Not pregenerated, generate the class
|
||||
try {
|
||||
speciesCode = generateConcreteSpeciesCode(className, speciesData);
|
||||
if (TRACE_RESOLVE) {
|
||||
// Used by jlink species pregeneration plugin, see
|
||||
// jdk.tools.jlink.internal.plugins.GenerateJLIClassesPlugin
|
||||
System.out.println("[SPECIES_RESOLVE] " + className + " (generated)");
|
||||
}
|
||||
traceSpeciesType(className, salvage);
|
||||
// This operation causes a lot of churn:
|
||||
linkSpeciesDataToCode(speciesData, speciesCode);
|
||||
// This operation commits the relation, but causes little churn:
|
||||
linkCodeToSpeciesData(speciesCode, speciesData, false);
|
||||
} catch (Error ex) {
|
||||
if (TRACE_RESOLVE) {
|
||||
System.out.println("[SPECIES_RESOLVE] " + className + " (Error #2)" );
|
||||
}
|
||||
// We can get here if there is a race condition loading a class.
|
||||
// Or maybe we are out of resources. Back out of the CHM.get and retry.
|
||||
throw ex;
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2016, 2020, 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
|
||||
@ -32,16 +32,328 @@ import sun.invoke.util.Wrapper;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeMap;
|
||||
import java.util.TreeSet;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static java.lang.invoke.MethodTypeForm.LF_INVINTERFACE;
|
||||
import static java.lang.invoke.MethodTypeForm.LF_INVVIRTUAL;
|
||||
import static java.lang.invoke.LambdaForm.basicTypeSignature;
|
||||
import static java.lang.invoke.LambdaForm.shortenSignature;
|
||||
import static java.lang.invoke.LambdaForm.BasicType.*;
|
||||
import static java.lang.invoke.MethodHandleStatics.TRACE_RESOLVE;
|
||||
import static java.lang.invoke.MethodTypeForm.*;
|
||||
import static java.lang.invoke.LambdaForm.Kind.*;
|
||||
|
||||
/**
|
||||
* Helper class to assist the GenerateJLIClassesPlugin to get access to
|
||||
* generate classes ahead of time.
|
||||
*/
|
||||
class GenerateJLIClassesHelper {
|
||||
private static final String LF_RESOLVE = "[LF_RESOLVE]";
|
||||
private static final String SPECIES_RESOLVE = "[SPECIES_RESOLVE]";
|
||||
|
||||
static void traceLambdaForm(String name, MethodType type, Class<?> holder, MemberName resolvedMember) {
|
||||
if (TRACE_RESOLVE) {
|
||||
System.out.println(LF_RESOLVE + " " + holder.getName() + " " + name + " " +
|
||||
shortenSignature(basicTypeSignature(type)) +
|
||||
(resolvedMember != null ? " (success)" : " (fail)"));
|
||||
}
|
||||
}
|
||||
|
||||
static void traceSpeciesType(String cn, Class<?> salvage) {
|
||||
if (TRACE_RESOLVE) {
|
||||
System.out.println(SPECIES_RESOLVE + " " + cn + (salvage != null ? " (salvaged)" : " (generated)"));
|
||||
}
|
||||
}
|
||||
|
||||
// Map from DirectMethodHandle method type name to index to LambdForms
|
||||
static final Map<String, Integer> DMH_METHOD_TYPE_MAP =
|
||||
Map.of(
|
||||
DIRECT_INVOKE_VIRTUAL.methodName, LF_INVVIRTUAL,
|
||||
DIRECT_INVOKE_STATIC.methodName, LF_INVSTATIC,
|
||||
DIRECT_INVOKE_SPECIAL.methodName, LF_INVSPECIAL,
|
||||
DIRECT_NEW_INVOKE_SPECIAL.methodName, LF_NEWINVSPECIAL,
|
||||
DIRECT_INVOKE_INTERFACE.methodName, LF_INVINTERFACE,
|
||||
DIRECT_INVOKE_STATIC_INIT.methodName, LF_INVSTATIC_INIT,
|
||||
DIRECT_INVOKE_SPECIAL_IFC.methodName, LF_INVSPECIAL_IFC
|
||||
);
|
||||
|
||||
static final String DIRECT_HOLDER = "java/lang/invoke/DirectMethodHandle$Holder";
|
||||
static final String DELEGATING_HOLDER = "java/lang/invoke/DelegatingMethodHandle$Holder";
|
||||
static final String BASIC_FORMS_HOLDER = "java/lang/invoke/LambdaForm$Holder";
|
||||
static final String INVOKERS_HOLDER = "java/lang/invoke/Invokers$Holder";
|
||||
static final String INVOKERS_HOLDER_CLASS_NAME = INVOKERS_HOLDER.replace('/', '.');
|
||||
static final String BMH_SPECIES_PREFIX = "java.lang.invoke.BoundMethodHandle$Species_";
|
||||
|
||||
static class HolderClassBuilder {
|
||||
|
||||
|
||||
private final TreeSet<String> speciesTypes = new TreeSet<>();
|
||||
private final TreeSet<String> invokerTypes = new TreeSet<>();
|
||||
private final TreeSet<String> callSiteTypes = new TreeSet<>();
|
||||
private final Map<String, Set<String>> dmhMethods = new TreeMap<>();
|
||||
|
||||
HolderClassBuilder addSpeciesType(String type) {
|
||||
speciesTypes.add(expandSignature(type));
|
||||
return this;
|
||||
}
|
||||
|
||||
HolderClassBuilder addInvokerType(String methodType) {
|
||||
validateMethodType(methodType);
|
||||
invokerTypes.add(methodType);
|
||||
return this;
|
||||
}
|
||||
|
||||
HolderClassBuilder addCallSiteType(String csType) {
|
||||
validateMethodType(csType);
|
||||
callSiteTypes.add(csType);
|
||||
return this;
|
||||
}
|
||||
|
||||
Map<String, byte[]> build() {
|
||||
int count = 0;
|
||||
for (Set<String> entry : dmhMethods.values()) {
|
||||
count += entry.size();
|
||||
}
|
||||
MethodType[] directMethodTypes = new MethodType[count];
|
||||
int[] dmhTypes = new int[count];
|
||||
int index = 0;
|
||||
for (Map.Entry<String, Set<String>> entry : dmhMethods.entrySet()) {
|
||||
String dmhType = entry.getKey();
|
||||
for (String type : entry.getValue()) {
|
||||
// The DMH type to actually ask for is retrieved by removing
|
||||
// the first argument, which needs to be of Object.class
|
||||
MethodType mt = asMethodType(type);
|
||||
if (mt.parameterCount() < 1 ||
|
||||
mt.parameterType(0) != Object.class) {
|
||||
throw new RuntimeException(
|
||||
"DMH type parameter must start with L: " + dmhType + " " + type);
|
||||
}
|
||||
|
||||
// Adapt the method type of the LF to retrieve
|
||||
directMethodTypes[index] = mt.dropParameterTypes(0, 1);
|
||||
|
||||
// invokeVirtual and invokeInterface must have a leading Object
|
||||
// parameter, i.e., the receiver
|
||||
dmhTypes[index] = DMH_METHOD_TYPE_MAP.get(dmhType);
|
||||
if (dmhTypes[index] == LF_INVINTERFACE || dmhTypes[index] == LF_INVVIRTUAL) {
|
||||
if (mt.parameterCount() < 2 ||
|
||||
mt.parameterType(1) != Object.class) {
|
||||
throw new RuntimeException(
|
||||
"DMH type parameter must start with LL: " + dmhType + " " + type);
|
||||
}
|
||||
}
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
// The invoker type to ask for is retrieved by removing the first
|
||||
// and the last argument, which needs to be of Object.class
|
||||
MethodType[] invokerMethodTypes = new MethodType[invokerTypes.size()];
|
||||
index = 0;
|
||||
for (String invokerType : invokerTypes) {
|
||||
MethodType mt = asMethodType(invokerType);
|
||||
final int lastParam = mt.parameterCount() - 1;
|
||||
if (mt.parameterCount() < 2 ||
|
||||
mt.parameterType(0) != Object.class ||
|
||||
mt.parameterType(lastParam) != Object.class) {
|
||||
throw new RuntimeException(
|
||||
"Invoker type parameter must start and end with Object: " + invokerType);
|
||||
}
|
||||
mt = mt.dropParameterTypes(lastParam, lastParam + 1);
|
||||
invokerMethodTypes[index] = mt.dropParameterTypes(0, 1);
|
||||
index++;
|
||||
}
|
||||
|
||||
// The callSite type to ask for is retrieved by removing the last
|
||||
// argument, which needs to be of Object.class
|
||||
MethodType[] callSiteMethodTypes = new MethodType[callSiteTypes.size()];
|
||||
index = 0;
|
||||
for (String callSiteType : callSiteTypes) {
|
||||
MethodType mt = asMethodType(callSiteType);
|
||||
final int lastParam = mt.parameterCount() - 1;
|
||||
if (mt.parameterCount() < 1 ||
|
||||
mt.parameterType(lastParam) != Object.class) {
|
||||
throw new RuntimeException(
|
||||
"CallSite type parameter must end with Object: " + callSiteType);
|
||||
}
|
||||
callSiteMethodTypes[index] = mt.dropParameterTypes(lastParam, lastParam + 1);
|
||||
index++;
|
||||
}
|
||||
|
||||
Map<String, byte[]> result = new TreeMap<>();
|
||||
result.put(DIRECT_HOLDER,
|
||||
generateDirectMethodHandleHolderClassBytes(
|
||||
DIRECT_HOLDER, directMethodTypes, dmhTypes));
|
||||
result.put(DELEGATING_HOLDER,
|
||||
generateDelegatingMethodHandleHolderClassBytes(
|
||||
DELEGATING_HOLDER, directMethodTypes));
|
||||
result.put(INVOKERS_HOLDER,
|
||||
generateInvokersHolderClassBytes(INVOKERS_HOLDER,
|
||||
invokerMethodTypes, callSiteMethodTypes));
|
||||
result.put(BASIC_FORMS_HOLDER,
|
||||
generateBasicFormsClassBytes(BASIC_FORMS_HOLDER));
|
||||
|
||||
speciesTypes.forEach(types -> {
|
||||
Map.Entry<String, byte[]> entry = generateConcreteBMHClassBytes(types);
|
||||
result.put(entry.getKey(), entry.getValue());
|
||||
});
|
||||
|
||||
// clear builder
|
||||
speciesTypes.clear();
|
||||
invokerTypes.clear();
|
||||
callSiteTypes.clear();
|
||||
dmhMethods.clear();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static MethodType asMethodType(String basicSignatureString) {
|
||||
String[] parts = basicSignatureString.split("_");
|
||||
assert (parts.length == 2);
|
||||
assert (parts[1].length() == 1);
|
||||
String parameters = expandSignature(parts[0]);
|
||||
Class<?> rtype = simpleType(parts[1].charAt(0));
|
||||
if (parameters.isEmpty()) {
|
||||
return MethodType.methodType(rtype);
|
||||
} else {
|
||||
Class<?>[] ptypes = new Class<?>[parameters.length()];
|
||||
for (int i = 0; i < ptypes.length; i++) {
|
||||
ptypes[i] = simpleType(parameters.charAt(i));
|
||||
}
|
||||
return MethodType.methodType(rtype, ptypes);
|
||||
}
|
||||
}
|
||||
|
||||
private void addDMHMethodType(String dmh, String methodType) {
|
||||
validateMethodType(methodType);
|
||||
Set<String> methodTypes = dmhMethods.get(dmh);
|
||||
if (methodTypes == null) {
|
||||
methodTypes = new TreeSet<>();
|
||||
dmhMethods.put(dmh, methodTypes);
|
||||
}
|
||||
methodTypes.add(methodType);
|
||||
}
|
||||
|
||||
private static void validateMethodType(String type) {
|
||||
String[] typeParts = type.split("_");
|
||||
// check return type (second part)
|
||||
if (typeParts.length != 2 || typeParts[1].length() != 1
|
||||
|| !isBasicTypeChar(typeParts[1].charAt(0))) {
|
||||
throw new RuntimeException(
|
||||
"Method type signature must be of form [LJIFD]*_[LJIFDV]");
|
||||
}
|
||||
// expand and check arguments (first part)
|
||||
expandSignature(typeParts[0]);
|
||||
}
|
||||
|
||||
// Convert LL -> LL, L3 -> LLL
|
||||
private static String expandSignature(String signature) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
char last = 'X';
|
||||
int count = 0;
|
||||
for (int i = 0; i < signature.length(); i++) {
|
||||
char c = signature.charAt(i);
|
||||
if (c >= '0' && c <= '9') {
|
||||
count *= 10;
|
||||
count += (c - '0');
|
||||
} else {
|
||||
requireBasicType(c);
|
||||
for (int j = 1; j < count; j++) {
|
||||
sb.append(last);
|
||||
}
|
||||
sb.append(c);
|
||||
last = c;
|
||||
count = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// ended with a number, e.g., "L2": append last char count - 1 times
|
||||
if (count > 1) {
|
||||
requireBasicType(last);
|
||||
for (int j = 1; j < count; j++) {
|
||||
sb.append(last);
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private static void requireBasicType(char c) {
|
||||
if (!isArgBasicTypeChar(c)) {
|
||||
throw new RuntimeException(
|
||||
"Character " + c + " must correspond to a basic field type: LIJFD");
|
||||
}
|
||||
}
|
||||
|
||||
private static Class<?> simpleType(char c) {
|
||||
if (isBasicTypeChar(c)) {
|
||||
return LambdaForm.BasicType.basicType(c).basicTypeClass();
|
||||
}
|
||||
switch (c) {
|
||||
case 'Z':
|
||||
case 'B':
|
||||
case 'S':
|
||||
case 'C':
|
||||
throw new IllegalArgumentException("Not a valid primitive: " + c +
|
||||
" (use I instead)");
|
||||
default:
|
||||
throw new IllegalArgumentException("Not a primitive: " + c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns a map of class name in internal form to the corresponding class bytes
|
||||
* per the given stream of SPECIES_RESOLVE and LF_RESOLVE trace logs.
|
||||
*
|
||||
* Used by GenerateJLIClassesPlugin to pre-generate holder classes during
|
||||
* jlink phase.
|
||||
*/
|
||||
static Map<String, byte[]> generateHolderClasses(Stream<String> traces) {
|
||||
HolderClassBuilder builder = new HolderClassBuilder();
|
||||
traces.map(line -> line.split(" "))
|
||||
.forEach(parts -> {
|
||||
switch (parts[0]) {
|
||||
case SPECIES_RESOLVE:
|
||||
// Allow for new types of species data classes being resolved here
|
||||
assert parts.length == 3;
|
||||
if (parts[1].startsWith(BMH_SPECIES_PREFIX)) {
|
||||
String species = parts[1].substring(BMH_SPECIES_PREFIX.length());
|
||||
if (!"L".equals(species)) {
|
||||
builder.addSpeciesType(species);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case LF_RESOLVE:
|
||||
assert parts.length > 3;
|
||||
String methodType = parts[3];
|
||||
if (parts[1].equals(INVOKERS_HOLDER_CLASS_NAME)) {
|
||||
if ("linkToTargetMethod".equals(parts[2]) ||
|
||||
"linkToCallSite".equals(parts[2])) {
|
||||
builder.addCallSiteType(methodType);
|
||||
} else {
|
||||
builder.addInvokerType(methodType);
|
||||
}
|
||||
} else if (parts[1].contains("DirectMethodHandle")) {
|
||||
String dmh = parts[2];
|
||||
// ignore getObject etc for now (generated by default)
|
||||
if (DMH_METHOD_TYPE_MAP.containsKey(dmh)) {
|
||||
builder.addDMHMethodType(dmh, methodType);
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break; // ignore
|
||||
}
|
||||
});
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a {@code byte[]} representation of a class implementing
|
||||
* the zero and identity forms of all {@code LambdaForm.BasicType}s.
|
||||
*/
|
||||
static byte[] generateBasicFormsClassBytes(String className) {
|
||||
ArrayList<LambdaForm> forms = new ArrayList<>();
|
||||
ArrayList<String> names = new ArrayList<>();
|
||||
@ -68,6 +380,11 @@ class GenerateJLIClassesHelper {
|
||||
forms.toArray(new LambdaForm[0]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a {@code byte[]} representation of a class implementing
|
||||
* DirectMethodHandle of each pairwise combination of {@code MethodType} and
|
||||
* an {@code int} representing method type.
|
||||
*/
|
||||
static byte[] generateDirectMethodHandleHolderClassBytes(String className,
|
||||
MethodType[] methodTypes, int[] types) {
|
||||
ArrayList<LambdaForm> forms = new ArrayList<>();
|
||||
@ -115,6 +432,11 @@ class GenerateJLIClassesHelper {
|
||||
forms.toArray(new LambdaForm[0]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a {@code byte[]} representation of a class implementing
|
||||
* DelegatingMethodHandles of each {@code MethodType} kind in the
|
||||
* {@code methodTypes} argument.
|
||||
*/
|
||||
static byte[] generateDelegatingMethodHandleHolderClassBytes(String className,
|
||||
MethodType[] methodTypes) {
|
||||
|
||||
@ -145,6 +467,11 @@ class GenerateJLIClassesHelper {
|
||||
forms.toArray(new LambdaForm[0]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a {@code byte[]} representation of a class implementing
|
||||
* the invoker forms for the set of supplied {@code invokerMethodTypes}
|
||||
* and {@code callSiteMethodTypes}.
|
||||
*/
|
||||
static byte[] generateInvokersHolderClassBytes(String className,
|
||||
MethodType[] invokerMethodTypes, MethodType[] callSiteMethodTypes) {
|
||||
|
||||
@ -193,10 +520,7 @@ class GenerateJLIClassesHelper {
|
||||
* Generate customized code for a set of LambdaForms of specified types into
|
||||
* a class with a specified name.
|
||||
*/
|
||||
private static byte[] generateCodeBytesForLFs(String className,
|
||||
String[] names, LambdaForm[] forms) {
|
||||
|
||||
|
||||
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,
|
||||
className, null, InvokerBytecodeGenerator.INVOKER_SUPER_NAME, null);
|
||||
@ -229,10 +553,14 @@ class GenerateJLIClassesHelper {
|
||||
DelegatingMethodHandle.NF_getTarget);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a {@code byte[]} representation of {@code BoundMethodHandle}
|
||||
* species class implementing the signature defined by {@code types}.
|
||||
*/
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
static Map.Entry<String, byte[]> generateConcreteBMHClassBytes(final String types) {
|
||||
for (char c : types.toCharArray()) {
|
||||
if ("LIJFD".indexOf(c) < 0) {
|
||||
if (!isArgBasicTypeChar(c)) {
|
||||
throw new IllegalArgumentException("All characters must "
|
||||
+ "correspond to a basic field type: LIJFD");
|
||||
}
|
||||
|
@ -46,6 +46,7 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static java.lang.invoke.GenerateJLIClassesHelper.traceLambdaForm;
|
||||
import static java.lang.invoke.LambdaForm.BasicType;
|
||||
import static java.lang.invoke.LambdaForm.BasicType.*;
|
||||
import static java.lang.invoke.LambdaForm.*;
|
||||
@ -696,10 +697,7 @@ class InvokerBytecodeGenerator {
|
||||
private static MemberName resolveFrom(String name, MethodType type, Class<?> holder) {
|
||||
MemberName member = new MemberName(holder, name, type, REF_invokeStatic);
|
||||
MemberName resolvedMember = MemberName.getFactory().resolveOrNull(REF_invokeStatic, member, holder, LM_TRUSTED);
|
||||
if (TRACE_RESOLVE) {
|
||||
System.out.println("[LF_RESOLVE] " + holder.getName() + " " + name + " " +
|
||||
shortenSignature(basicTypeSignature(type)) + (resolvedMember != null ? " (success)" : " (fail)") );
|
||||
}
|
||||
traceLambdaForm(name, type, holder, resolvedMember);
|
||||
return resolvedMember;
|
||||
}
|
||||
|
||||
|
@ -1764,41 +1764,8 @@ abstract class MethodHandleImpl {
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] generateDirectMethodHandleHolderClassBytes(
|
||||
String className, MethodType[] methodTypes, int[] types) {
|
||||
return GenerateJLIClassesHelper
|
||||
.generateDirectMethodHandleHolderClassBytes(
|
||||
className, methodTypes, types);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] generateDelegatingMethodHandleHolderClassBytes(
|
||||
String className, MethodType[] methodTypes) {
|
||||
return GenerateJLIClassesHelper
|
||||
.generateDelegatingMethodHandleHolderClassBytes(
|
||||
className, methodTypes);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map.Entry<String, byte[]> generateConcreteBMHClassBytes(
|
||||
final String types) {
|
||||
return GenerateJLIClassesHelper
|
||||
.generateConcreteBMHClassBytes(types);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] generateBasicFormsClassBytes(final String className) {
|
||||
return GenerateJLIClassesHelper
|
||||
.generateBasicFormsClassBytes(className);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] generateInvokersHolderClassBytes(final String className,
|
||||
MethodType[] invokerMethodTypes,
|
||||
MethodType[] callSiteMethodTypes) {
|
||||
return GenerateJLIClassesHelper
|
||||
.generateInvokersHolderClassBytes(className,
|
||||
invokerMethodTypes, callSiteMethodTypes);
|
||||
public Map<String, byte[]> generateHolderClasses(Stream<String> traces) {
|
||||
return GenerateJLIClassesHelper.generateHolderClasses(traces);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -31,6 +31,7 @@ import java.lang.invoke.VarHandle;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public interface JavaLangInvokeAccess {
|
||||
/**
|
||||
@ -68,47 +69,12 @@ public interface JavaLangInvokeAccess {
|
||||
Class<?> getDeclaringClass(Object mname);
|
||||
|
||||
/**
|
||||
* Returns a {@code byte[]} representation of a class implementing
|
||||
* DirectMethodHandle of each pairwise combination of {@code MethodType} and
|
||||
* an {@code int} representing method type. Used by
|
||||
* GenerateJLIClassesPlugin to generate such a class during the jlink phase.
|
||||
* Returns a map of class name in internal forms to its corresponding
|
||||
* class bytes per the given stream of LF_RESOLVE and SPECIES_RESOLVE
|
||||
* trace logs. Used by GenerateJLIClassesPlugin to enable generation
|
||||
* of such classes during the jlink phase.
|
||||
*/
|
||||
byte[] generateDirectMethodHandleHolderClassBytes(String className,
|
||||
MethodType[] methodTypes, int[] types);
|
||||
|
||||
/**
|
||||
* Returns a {@code byte[]} representation of a class implementing
|
||||
* DelegatingMethodHandles of each {@code MethodType} kind in the
|
||||
* {@code methodTypes} argument. Used by GenerateJLIClassesPlugin to
|
||||
* generate such a class during the jlink phase.
|
||||
*/
|
||||
byte[] generateDelegatingMethodHandleHolderClassBytes(String className,
|
||||
MethodType[] methodTypes);
|
||||
|
||||
/**
|
||||
* Returns a {@code byte[]} representation of {@code BoundMethodHandle}
|
||||
* species class implementing the signature defined by {@code types}. Used
|
||||
* by GenerateJLIClassesPlugin to enable generation of such classes during
|
||||
* the jlink phase. Should do some added validation since this string may be
|
||||
* user provided.
|
||||
*/
|
||||
Map.Entry<String, byte[]> generateConcreteBMHClassBytes(
|
||||
final String types);
|
||||
|
||||
/**
|
||||
* Returns a {@code byte[]} representation of a class implementing
|
||||
* the zero and identity forms of all {@code LambdaForm.BasicType}s.
|
||||
*/
|
||||
byte[] generateBasicFormsClassBytes(final String className);
|
||||
|
||||
/**
|
||||
* Returns a {@code byte[]} representation of a class implementing
|
||||
* the invoker forms for the set of supplied {@code invokerMethodTypes}
|
||||
* and {@code callSiteMethodTypes}.
|
||||
*/
|
||||
byte[] generateInvokersHolderClassBytes(String className,
|
||||
MethodType[] invokerMethodTypes,
|
||||
MethodType[] callSiteMethodTypes);
|
||||
Map<String, byte[]> generateHolderClasses(Stream<String> traces);
|
||||
|
||||
/**
|
||||
* Returns a var handle view of a given memory address.
|
||||
|
@ -29,13 +29,10 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.lang.invoke.MethodType;
|
||||
import java.nio.file.Files;
|
||||
import java.util.EnumSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeMap;
|
||||
import java.util.TreeSet;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import jdk.internal.access.JavaLangInvokeAccess;
|
||||
@ -72,33 +69,11 @@ public final class GenerateJLIClassesPlugin implements Plugin {
|
||||
|
||||
private static final String DEFAULT_TRACE_FILE = "default_jli_trace.txt";
|
||||
|
||||
private static final String DIRECT_HOLDER = "java/lang/invoke/DirectMethodHandle$Holder";
|
||||
private static final String DMH_INVOKE_VIRTUAL = "invokeVirtual";
|
||||
private static final String DMH_INVOKE_STATIC = "invokeStatic";
|
||||
private static final String DMH_INVOKE_SPECIAL = "invokeSpecial";
|
||||
private static final String DMH_NEW_INVOKE_SPECIAL = "newInvokeSpecial";
|
||||
private static final String DMH_INVOKE_INTERFACE = "invokeInterface";
|
||||
private static final String DMH_INVOKE_STATIC_INIT = "invokeStaticInit";
|
||||
private static final String DMH_INVOKE_SPECIAL_IFC = "invokeSpecialIFC";
|
||||
|
||||
private static final String DELEGATING_HOLDER = "java/lang/invoke/DelegatingMethodHandle$Holder";
|
||||
private static final String BASIC_FORMS_HOLDER = "java/lang/invoke/LambdaForm$Holder";
|
||||
|
||||
private static final String INVOKERS_HOLDER_NAME = "java.lang.invoke.Invokers$Holder";
|
||||
private static final String INVOKERS_HOLDER_INTERNAL_NAME = INVOKERS_HOLDER_NAME.replace('.', '/');
|
||||
|
||||
private static final JavaLangInvokeAccess JLIA
|
||||
= SharedSecrets.getJavaLangInvokeAccess();
|
||||
|
||||
private final TreeSet<String> speciesTypes = new TreeSet<>();
|
||||
|
||||
private final TreeSet<String> invokerTypes = new TreeSet<>();
|
||||
|
||||
private final TreeSet<String> callSiteTypes = new TreeSet<>();
|
||||
|
||||
private final Map<String, Set<String>> dmhMethods = new TreeMap<>();
|
||||
|
||||
String mainArgument;
|
||||
private String mainArgument;
|
||||
private Stream<String> traceFileStream;
|
||||
|
||||
public GenerateJLIClassesPlugin() {
|
||||
}
|
||||
@ -128,41 +103,11 @@ public final class GenerateJLIClassesPlugin implements Plugin {
|
||||
return PluginsResourceBundle.getArgument(NAME);
|
||||
}
|
||||
|
||||
private static int DMH_INVOKE_VIRTUAL_TYPE = 0;
|
||||
private static int DMH_INVOKE_INTERFACE_TYPE = 4;
|
||||
|
||||
// Map from DirectMethodHandle method type to internal ID, matching values
|
||||
// of the corresponding constants in java.lang.invoke.MethodTypeForm
|
||||
private static final Map<String, Integer> DMH_METHOD_TYPE_MAP =
|
||||
Map.of(
|
||||
DMH_INVOKE_VIRTUAL, DMH_INVOKE_VIRTUAL_TYPE,
|
||||
DMH_INVOKE_STATIC, 1,
|
||||
DMH_INVOKE_SPECIAL, 2,
|
||||
DMH_NEW_INVOKE_SPECIAL, 3,
|
||||
DMH_INVOKE_INTERFACE, DMH_INVOKE_INTERFACE_TYPE,
|
||||
DMH_INVOKE_STATIC_INIT, 5,
|
||||
DMH_INVOKE_SPECIAL_IFC, 20
|
||||
);
|
||||
|
||||
@Override
|
||||
public void configure(Map<String, String> config) {
|
||||
mainArgument = config.get(NAME);
|
||||
}
|
||||
|
||||
private void addSpeciesType(String type) {
|
||||
speciesTypes.add(expandSignature(type));
|
||||
}
|
||||
|
||||
private void addInvokerType(String methodType) {
|
||||
validateMethodType(methodType);
|
||||
invokerTypes.add(methodType);
|
||||
}
|
||||
|
||||
private void addCallSiteType(String csType) {
|
||||
validateMethodType(csType);
|
||||
callSiteTypes.add(csType);
|
||||
}
|
||||
|
||||
public void initialize(ResourcePool in) {
|
||||
// Load configuration from the contents in the supplied input file
|
||||
// - if none was supplied we look for the default file
|
||||
@ -170,9 +115,7 @@ public final class GenerateJLIClassesPlugin implements Plugin {
|
||||
try (InputStream traceFile =
|
||||
this.getClass().getResourceAsStream(DEFAULT_TRACE_FILE)) {
|
||||
if (traceFile != null) {
|
||||
readTraceConfig(
|
||||
new BufferedReader(
|
||||
new InputStreamReader(traceFile)).lines());
|
||||
traceFileStream = new BufferedReader(new InputStreamReader(traceFile)).lines();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new PluginException("Couldn't read " + DEFAULT_TRACE_FILE, e);
|
||||
@ -180,57 +123,11 @@ public final class GenerateJLIClassesPlugin implements Plugin {
|
||||
} else {
|
||||
File file = new File(mainArgument.substring(1));
|
||||
if (file.exists()) {
|
||||
readTraceConfig(fileLines(file));
|
||||
traceFileStream = fileLines(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void readTraceConfig(Stream<String> lines) {
|
||||
lines.map(line -> line.split(" "))
|
||||
.forEach(parts -> {
|
||||
switch (parts[0]) {
|
||||
case "[SPECIES_RESOLVE]":
|
||||
// Allow for new types of species data classes being resolved here
|
||||
if (parts.length == 3 && parts[1].startsWith("java.lang.invoke.BoundMethodHandle$Species_")) {
|
||||
String species = parts[1].substring("java.lang.invoke.BoundMethodHandle$Species_".length());
|
||||
if (!"L".equals(species)) {
|
||||
addSpeciesType(species);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case "[LF_RESOLVE]":
|
||||
String methodType = parts[3];
|
||||
if (parts[1].equals(INVOKERS_HOLDER_NAME)) {
|
||||
if ("linkToTargetMethod".equals(parts[2]) ||
|
||||
"linkToCallSite".equals(parts[2])) {
|
||||
addCallSiteType(methodType);
|
||||
} else {
|
||||
addInvokerType(methodType);
|
||||
}
|
||||
} else if (parts[1].contains("DirectMethodHandle")) {
|
||||
String dmh = parts[2];
|
||||
// ignore getObject etc for now (generated
|
||||
// by default)
|
||||
if (DMH_METHOD_TYPE_MAP.containsKey(dmh)) {
|
||||
addDMHMethodType(dmh, methodType);
|
||||
}
|
||||
}
|
||||
break;
|
||||
default: break; // ignore
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void addDMHMethodType(String dmh, String methodType) {
|
||||
validateMethodType(methodType);
|
||||
Set<String> methodTypes = dmhMethods.get(dmh);
|
||||
if (methodTypes == null) {
|
||||
methodTypes = new TreeSet<>();
|
||||
dmhMethods.put(dmh, methodTypes);
|
||||
}
|
||||
methodTypes.add(methodType);
|
||||
}
|
||||
|
||||
private Stream<String> fileLines(File file) {
|
||||
try {
|
||||
return Files.lines(file.toPath());
|
||||
@ -239,25 +136,6 @@ public final class GenerateJLIClassesPlugin implements Plugin {
|
||||
}
|
||||
}
|
||||
|
||||
private void validateMethodType(String type) {
|
||||
String[] typeParts = type.split("_");
|
||||
// check return type (second part)
|
||||
if (typeParts.length != 2 || typeParts[1].length() != 1
|
||||
|| "LJIFDV".indexOf(typeParts[1].charAt(0)) == -1) {
|
||||
throw new PluginException(
|
||||
"Method type signature must be of form [LJIFD]*_[LJIFDV]");
|
||||
}
|
||||
// expand and check arguments (first part)
|
||||
expandSignature(typeParts[0]);
|
||||
}
|
||||
|
||||
private static void requireBasicType(char c) {
|
||||
if ("LIJFD".indexOf(c) < 0) {
|
||||
throw new PluginException(
|
||||
"Character " + c + " must correspond to a basic field type: LIJFD");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResourcePool transform(ResourcePool in, ResourcePoolBuilder out) {
|
||||
initialize(in);
|
||||
@ -275,213 +153,28 @@ public final class GenerateJLIClassesPlugin implements Plugin {
|
||||
}
|
||||
}, out);
|
||||
|
||||
// Generate BMH Species classes
|
||||
speciesTypes.forEach(types -> generateBMHClass(types, out));
|
||||
|
||||
// Generate LambdaForm Holder classes
|
||||
generateHolderClasses(out);
|
||||
|
||||
// Let it go
|
||||
speciesTypes.clear();
|
||||
invokerTypes.clear();
|
||||
callSiteTypes.clear();
|
||||
dmhMethods.clear();
|
||||
|
||||
// Generate Holder classes
|
||||
if (traceFileStream != null) {
|
||||
try {
|
||||
JLIA.generateHolderClasses(traceFileStream)
|
||||
.forEach((cn, bytes) -> {
|
||||
String entryName = "/java.base/" + cn + ".class";
|
||||
ResourcePoolEntry ndata = ResourcePoolEntry.create(entryName, bytes);
|
||||
out.add(ndata);
|
||||
});
|
||||
} catch (Exception ex) {
|
||||
throw new PluginException(ex);
|
||||
}
|
||||
}
|
||||
return out.build();
|
||||
}
|
||||
|
||||
private void generateBMHClass(String types, ResourcePoolBuilder out) {
|
||||
try {
|
||||
// Generate class
|
||||
Map.Entry<String, byte[]> result =
|
||||
JLIA.generateConcreteBMHClassBytes(types);
|
||||
String className = result.getKey();
|
||||
byte[] bytes = result.getValue();
|
||||
|
||||
// Add class to pool
|
||||
ResourcePoolEntry ndata = ResourcePoolEntry.create(
|
||||
"/java.base/" + className + ".class",
|
||||
bytes);
|
||||
out.add(ndata);
|
||||
} catch (Exception ex) {
|
||||
throw new PluginException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private void generateHolderClasses(ResourcePoolBuilder out) {
|
||||
int count = 0;
|
||||
for (Set<String> entry : dmhMethods.values()) {
|
||||
count += entry.size();
|
||||
}
|
||||
MethodType[] directMethodTypes = new MethodType[count];
|
||||
int[] dmhTypes = new int[count];
|
||||
int index = 0;
|
||||
for (Map.Entry<String, Set<String>> entry : dmhMethods.entrySet()) {
|
||||
String dmhType = entry.getKey();
|
||||
for (String type : entry.getValue()) {
|
||||
// The DMH type to actually ask for is retrieved by removing
|
||||
// the first argument, which needs to be of Object.class
|
||||
MethodType mt = asMethodType(type);
|
||||
if (mt.parameterCount() < 1 ||
|
||||
mt.parameterType(0) != Object.class) {
|
||||
throw new PluginException(
|
||||
"DMH type parameter must start with L: " + dmhType + " " + type);
|
||||
}
|
||||
|
||||
// Adapt the method type of the LF to retrieve
|
||||
directMethodTypes[index] = mt.dropParameterTypes(0, 1);
|
||||
|
||||
// invokeVirtual and invokeInterface must have a leading Object
|
||||
// parameter, i.e., the receiver
|
||||
dmhTypes[index] = DMH_METHOD_TYPE_MAP.get(dmhType);
|
||||
if (dmhTypes[index] == DMH_INVOKE_INTERFACE_TYPE ||
|
||||
dmhTypes[index] == DMH_INVOKE_VIRTUAL_TYPE) {
|
||||
if (mt.parameterCount() < 2 ||
|
||||
mt.parameterType(1) != Object.class) {
|
||||
throw new PluginException(
|
||||
"DMH type parameter must start with LL: " + dmhType + " " + type);
|
||||
}
|
||||
}
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
// The invoker type to ask for is retrieved by removing the first
|
||||
// and the last argument, which needs to be of Object.class
|
||||
MethodType[] invokerMethodTypes = new MethodType[this.invokerTypes.size()];
|
||||
int i = 0;
|
||||
for (String invokerType : invokerTypes) {
|
||||
MethodType mt = asMethodType(invokerType);
|
||||
final int lastParam = mt.parameterCount() - 1;
|
||||
if (mt.parameterCount() < 2 ||
|
||||
mt.parameterType(0) != Object.class ||
|
||||
mt.parameterType(lastParam) != Object.class) {
|
||||
throw new PluginException(
|
||||
"Invoker type parameter must start and end with Object: " + invokerType);
|
||||
}
|
||||
mt = mt.dropParameterTypes(lastParam, lastParam + 1);
|
||||
invokerMethodTypes[i] = mt.dropParameterTypes(0, 1);
|
||||
i++;
|
||||
}
|
||||
|
||||
// The callSite type to ask for is retrieved by removing the last
|
||||
// argument, which needs to be of Object.class
|
||||
MethodType[] callSiteMethodTypes = new MethodType[this.callSiteTypes.size()];
|
||||
i = 0;
|
||||
for (String callSiteType : callSiteTypes) {
|
||||
MethodType mt = asMethodType(callSiteType);
|
||||
final int lastParam = mt.parameterCount() - 1;
|
||||
if (mt.parameterCount() < 1 ||
|
||||
mt.parameterType(lastParam) != Object.class) {
|
||||
throw new PluginException(
|
||||
"CallSite type parameter must end with Object: " + callSiteType);
|
||||
}
|
||||
callSiteMethodTypes[i] = mt.dropParameterTypes(lastParam, lastParam + 1);
|
||||
i++;
|
||||
}
|
||||
try {
|
||||
byte[] bytes = JLIA.generateDirectMethodHandleHolderClassBytes(
|
||||
DIRECT_HOLDER, directMethodTypes, dmhTypes);
|
||||
ResourcePoolEntry ndata = ResourcePoolEntry
|
||||
.create(DIRECT_METHOD_HOLDER_ENTRY, bytes);
|
||||
out.add(ndata);
|
||||
|
||||
bytes = JLIA.generateDelegatingMethodHandleHolderClassBytes(
|
||||
DELEGATING_HOLDER, directMethodTypes);
|
||||
ndata = ResourcePoolEntry.create(DELEGATING_METHOD_HOLDER_ENTRY, bytes);
|
||||
out.add(ndata);
|
||||
|
||||
bytes = JLIA.generateInvokersHolderClassBytes(INVOKERS_HOLDER_INTERNAL_NAME,
|
||||
invokerMethodTypes, callSiteMethodTypes);
|
||||
ndata = ResourcePoolEntry.create(INVOKERS_HOLDER_ENTRY, bytes);
|
||||
out.add(ndata);
|
||||
|
||||
bytes = JLIA.generateBasicFormsClassBytes(BASIC_FORMS_HOLDER);
|
||||
ndata = ResourcePoolEntry.create(BASIC_FORMS_HOLDER_ENTRY, bytes);
|
||||
out.add(ndata);
|
||||
} catch (Exception ex) {
|
||||
throw new PluginException(ex);
|
||||
}
|
||||
}
|
||||
private static final String DIRECT_METHOD_HOLDER_ENTRY =
|
||||
"/java.base/" + DIRECT_HOLDER + ".class";
|
||||
"/java.base/java/lang/invoke/DirectMethodHandle$Holder.class";
|
||||
private static final String DELEGATING_METHOD_HOLDER_ENTRY =
|
||||
"/java.base/" + DELEGATING_HOLDER + ".class";
|
||||
"/java.base/java/lang/invoke/DelegatingMethodHandle$Holder.class";
|
||||
private static final String BASIC_FORMS_HOLDER_ENTRY =
|
||||
"/java.base/" + BASIC_FORMS_HOLDER + ".class";
|
||||
"/java.base/java/lang/invoke/LambdaForm$Holder.class";
|
||||
private static final String INVOKERS_HOLDER_ENTRY =
|
||||
"/java.base/" + INVOKERS_HOLDER_INTERNAL_NAME + ".class";
|
||||
|
||||
// Convert LL -> LL, L3 -> LLL
|
||||
public static String expandSignature(String signature) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
char last = 'X';
|
||||
int count = 0;
|
||||
for (int i = 0; i < signature.length(); i++) {
|
||||
char c = signature.charAt(i);
|
||||
if (c >= '0' && c <= '9') {
|
||||
count *= 10;
|
||||
count += (c - '0');
|
||||
} else {
|
||||
requireBasicType(c);
|
||||
for (int j = 1; j < count; j++) {
|
||||
sb.append(last);
|
||||
}
|
||||
sb.append(c);
|
||||
last = c;
|
||||
count = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// ended with a number, e.g., "L2": append last char count - 1 times
|
||||
if (count > 1) {
|
||||
requireBasicType(last);
|
||||
for (int j = 1; j < count; j++) {
|
||||
sb.append(last);
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private static MethodType asMethodType(String basicSignatureString) {
|
||||
String[] parts = basicSignatureString.split("_");
|
||||
assert(parts.length == 2);
|
||||
assert(parts[1].length() == 1);
|
||||
String parameters = expandSignature(parts[0]);
|
||||
Class<?> rtype = simpleType(parts[1].charAt(0));
|
||||
if (parameters.isEmpty()) {
|
||||
return MethodType.methodType(rtype);
|
||||
} else {
|
||||
Class<?>[] ptypes = new Class<?>[parameters.length()];
|
||||
for (int i = 0; i < ptypes.length; i++) {
|
||||
ptypes[i] = simpleType(parameters.charAt(i));
|
||||
}
|
||||
return MethodType.methodType(rtype, ptypes);
|
||||
}
|
||||
}
|
||||
|
||||
private static Class<?> simpleType(char c) {
|
||||
switch (c) {
|
||||
case 'F':
|
||||
return float.class;
|
||||
case 'D':
|
||||
return double.class;
|
||||
case 'I':
|
||||
return int.class;
|
||||
case 'L':
|
||||
return Object.class;
|
||||
case 'J':
|
||||
return long.class;
|
||||
case 'V':
|
||||
return void.class;
|
||||
case 'Z':
|
||||
case 'B':
|
||||
case 'S':
|
||||
case 'C':
|
||||
throw new IllegalArgumentException("Not a valid primitive: " + c +
|
||||
" (use I instead)");
|
||||
default:
|
||||
throw new IllegalArgumentException("Not a primitive: " + c);
|
||||
}
|
||||
}
|
||||
"/java.base/java/lang/invoke/Invokers$Holder.class";
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user