Skip to content
Closed
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public Class buildProxy(
}
Collections.addAll( key, interfaces );

final String proxyClassName = persistentClass.getTypeName() + "$" + PROXY_NAMING_SUFFIX;
final String proxyClassName = getClassNameWithSuffix( persistentClass, persistentClass.getTypeName(), PROXY_NAMING_SUFFIX );
return byteBuddyState.loadProxy( persistentClass, proxyClassName,
proxyBuilder( TypeDescription.ForLoadedType.of( persistentClass ), new TypeList.Generic.ForLoadedTypes( interfaces ) ) );
}
Expand Down Expand Up @@ -187,16 +187,24 @@ private static Method resolveIdSetterMethod(SerializableProxy serializableProxy)
}
}

private static String getClassNameWithSuffix(Class<?> clazz, String name, String suffix) {
return getClassNameWithCodeSourceLocationHashCode(clazz, name) + "$" + suffix;
}

public static String getClassNameWithSuffix(Class<?> clazz, String suffix) {
return getClassNameWithCodeSourceLocationHashCode(clazz) + "$" + suffix;
}

private static String getClassNameWithCodeSourceLocationHashCode(Class<?> clazz) {
return getClassNameWithCodeSourceLocationHashCode( clazz, clazz.getName() );
}

private static String getClassNameWithCodeSourceLocationHashCode(Class<?> clazz, String className) {
final java.security.CodeSource codeSource = clazz.getProtectionDomain().getCodeSource();
if ( codeSource == null ) {
return clazz.getName();
return className;
}
final URL url = codeSource.getLocation();
return url == null ? clazz.getName() : clazz.getName() + url.toString().hashCode();
return url == null ? className : className + url.toString().hashCode();
Copy link
Member

Choose a reason for hiding this comment

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

Just leaving a couple of notes here for the future:

  • hashCode() does not prevent possible collisions; I agree it would be extremely rare to have the same class multiple times, and also have conflicting hash-codes for their code source, but still could happen.
  • java.security.CodeSource access could be blocked via SecurityManager
  • bytecode enhanced classes return null here, though from some early testing we don't have any CCEs in that case; it would be great to understand why that works (my hypothesis would be we're using the byte code-enhanced class from byte-buddy in both PUs)

Copy link
Member

Choose a reason for hiding this comment

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

Regarding SecurityManager, I think ORM would be trusted by the SecurityManager and if not, would need changes to ensure it is trusted.

}
}