Skip to content

Commit

Permalink
Remove system property for stable lambda class names. Remove the test.
Browse files Browse the repository at this point in the history
  • Loading branch information
sstanoje authored and zapster committed Nov 22, 2023
1 parent b57925e commit 28b5d2a
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 385 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,11 @@
import sun.invoke.util.VerifyAccess;
import sun.security.action.GetBooleanAction;

import java.nio.charset.StandardCharsets;
import java.io.Serializable;
import java.lang.constant.ConstantDescs;
import java.lang.reflect.Modifier;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.StringJoiner;
import java.util.zip.CRC32;

import static java.lang.invoke.MethodHandleStatics.CLASSFILE_VERSION;
import static java.lang.invoke.MethodHandles.Lookup.ClassOption.NESTMATE;
Expand Down Expand Up @@ -87,11 +84,6 @@

private static final boolean disableEagerInitialization;

private static final boolean generateStableLambdaNames;

private static final int mask1 = 0b10101010;
private static final int mask2 = 0b01010101;

// condy to load implMethod from class data
private static final ConstantDynamic implMethodCondy;

Expand All @@ -105,9 +97,6 @@
final String disableEagerInitializationKey = "jdk.internal.lambda.disableEagerInitialization";
disableEagerInitialization = GetBooleanAction.privilegedGetProperty(disableEagerInitializationKey);

final String generateStableLambdaNamesKey = "jdk.internal.lambda.generateStableLambdaNames";
generateStableLambdaNames = GetBooleanAction.privilegedGetProperty(generateStableLambdaNamesKey);

// condy to load implMethod from class data
MethodType classDataMType = methodType(Object.class, MethodHandles.Lookup.class, String.class, Class.class);
Handle classDataBsm = new Handle(H_INVOKESTATIC, Type.getInternalName(MethodHandles.class), "classData",
Expand Down Expand Up @@ -183,7 +172,7 @@ public InnerClassLambdaMetafactory(MethodHandles.Lookup caller,
implMethodName = implInfo.getName();
implMethodDesc = implInfo.getMethodType().toMethodDescriptorString();
constructorType = factoryType.changeReturnType(Void.TYPE);
lambdaClassName = generateStableLambdaNames ? stableLambdaClassName(targetClass) : lambdaClassName(targetClass);
lambdaClassName = lambdaClassName(targetClass);
// If the target class invokes a protected method inherited from a
// superclass in a different package, or does 'invokespecial', the
// lambda class has no access to the resolved method. Instead, we need
Expand All @@ -208,10 +197,6 @@ public InnerClassLambdaMetafactory(MethodHandles.Lookup caller,
}

private static String lambdaClassName(Class<?> targetClass) {
return createNameFromTargetClass(targetClass);
}

private static String createNameFromTargetClass(Class<?> targetClass) {
String name = targetClass.getName();
if (targetClass.isHidden()) {
// use the original class name
Expand All @@ -220,78 +205,6 @@ private static String createNameFromTargetClass(Class<?> targetClass) {
return name.replace('.', '/') + "$$Lambda";
}

/**
* Create a stable name for the lambda class.
* When CDS archiving is enabled, lambda classes
* are stored in the archive using some parameters from
* the InnerClassLambdaMetafactory. To distinguish between
* two lambdas, even when CDS archiving is disabled,
* use a superset of those parameters to create a stable name.
*
* Concatenate all the parameters chosen for the stable name,
* and hash them into 64-bit hash value.
* Any additional changes to this method will result in unstable
* hash values across different versions. Thus, every change
* to this method should be regarded as a backward incompatible change.
*
* No matter what hash function we use, there is a possibility of
* collisions in names. We expect a relatively low number of lambdas
* per class. Thus, we don't expect to have collisions using the described
* hash function. Every tool that uses this feature should handle potential
* collisions on its own. There is no guarantee that names will be unique,
* only that they will be stable (identical in every run).
*
* @return a stable name for the created lambda class.
*/
private String stableLambdaClassName(Class<?> targetClass) {
String name = createNameFromTargetClass(targetClass);

StringBuilder hashData1 = new StringBuilder(), hashData2 = new StringBuilder();
appendData(hashData1, hashData2, interfaceMethodName);
appendData(hashData1, hashData2, getQualifiedSignature(factoryType));
appendData(hashData1, hashData2, getQualifiedSignature(interfaceMethodType));
appendData(hashData1, hashData2, implementation.internalMemberName().toString());
appendData(hashData1, hashData2, getQualifiedSignature(dynamicMethodType));

for (Class<?> clazz : altInterfaces) {
appendData(hashData1, hashData2, clazz.getName());
}

for (MethodType method : altMethods) {
appendData(hashData1, hashData2, getQualifiedSignature(method));
}

return name + hashToHexString(hashData1.toString(), hashData2.toString());
}

private void appendData(StringBuilder hashData1, StringBuilder hashData2, String data) {
for (int i = 0; i < data.length(); i++) {
hashData1.append((char)(data.charAt(i) & mask1));
hashData2.append((char)(data.charAt(i) & mask2));
}
}

private long hashStringToLong(String hashData) {
CRC32 crc32 = new CRC32();
crc32.update(hashData.getBytes(StandardCharsets.UTF_8));
return crc32.getValue();
}

private String hashToHexString(String hashData1, String hashData2) {
long hashValueData1 = hashStringToLong(hashData1);
long hashValueData2 = hashStringToLong(hashData2);
return Long.toHexString(hashValueData1 | (hashValueData2 << 32));
}

private String getQualifiedSignature(MethodType type) {
StringJoiner sj = new StringJoiner(",", "(", ")" + type.returnType().getName());
Class<?>[] ptypes = type.ptypes();
for (int i = 0; i < ptypes.length; i++) {
sj.add(ptypes[i].getName());
}
return sj.toString();
}

/**
* Build the CallSite. Generate a class file which implements the functional
* interface, define the class, if there are no parameters create an instance
Expand Down
Loading

0 comments on commit 28b5d2a

Please sign in to comment.