HHH-13097 Only rewrite class if enhanced in the ByteBuddy enhancer

This makes the behavior of the ByteBuddy enhancer consistent with the
behavior of the Javassist enhancer.

Currently, the Maven plugin rewrites every class provided.
This commit is contained in:
Guillaume Smet 2018-11-13 11:07:47 +01:00 committed by Sanne Grinovero
parent 5cdf56774d
commit d125349c07
3 changed files with 8 additions and 6 deletions

View File

@ -123,7 +123,7 @@ public class EnhancerImpl implements Enhancer {
try { try {
final TypeDescription typeDescription = typePool.describe( safeClassName ).resolve(); final TypeDescription typeDescription = typePool.describe( safeClassName ).resolve();
return byteBuddyState.rewrite( typePool, safeClassName, originalBytes, byteBuddy -> doEnhance( return byteBuddyState.rewrite( typePool, safeClassName, byteBuddy -> doEnhance(
byteBuddy.ignore( isDefaultFinalizer() ).redefine( typeDescription, ClassFileLocator.Simple.of( safeClassName, originalBytes ) ), byteBuddy.ignore( isDefaultFinalizer() ).redefine( typeDescription, ClassFileLocator.Simple.of( safeClassName, originalBytes ) ),
typeDescription typeDescription
) ); ) );

View File

@ -130,18 +130,20 @@ public final class ByteBuddyState {
/** /**
* Rewrite a class, used by the enhancer. * Rewrite a class, used by the enhancer.
* <p>
* WARNING: Returns null if rewriteClassFunction returns a null builder. Do not use if you expect the original
* content.
* *
* @param typePool the ByteBuddy TypePool * @param typePool the ByteBuddy TypePool
* @param className The original class name. * @param className The original class name.
* @param originalBytes The original content of the class.
* @param rewriteClassFunction The function used to rewrite the class. * @param rewriteClassFunction The function used to rewrite the class.
* @return The rewritten content of the class. * @return The rewritten content of the class or null if rewriteClassFunction returns a null builder.
*/ */
public byte[] rewrite(TypePool typePool, String className, byte[] originalBytes, public byte[] rewrite(TypePool typePool, String className,
Function<ByteBuddy, DynamicType.Builder<?>> rewriteClassFunction) { Function<ByteBuddy, DynamicType.Builder<?>> rewriteClassFunction) {
DynamicType.Builder<?> builder = rewriteClassFunction.apply( byteBuddy ); DynamicType.Builder<?> builder = rewriteClassFunction.apply( byteBuddy );
if ( builder == null ) { if ( builder == null ) {
return originalBytes; return null;
} }
return make( typePool, builder ).getBytes(); return make( typePool, builder ).getBytes();

View File

@ -31,7 +31,7 @@ public class EnhancerWildFlyNamesTest {
@TestForIssue( jiraKey = "HHH-12545" ) @TestForIssue( jiraKey = "HHH-12545" )
public void test() { public void test() {
Enhancer enhancer = createByteBuddyEnhancer(); Enhancer enhancer = createByteBuddyEnhancer();
String internalName = Bean.class.getName().replace( '.', '/' ); String internalName = SimpleEntity.class.getName().replace( '.', '/' );
String resourceName = internalName + ".class"; String resourceName = internalName + ".class";
byte[] buffer = new byte[0]; byte[] buffer = new byte[0];
try { try {