HHH-12545 ByteBuddy based enhancer not accepting special character in description names

This commit is contained in:
Sanne Grinovero 2018-05-08 14:20:32 +01:00
parent ed136d5f52
commit 2fa5d3ccb8
3 changed files with 79 additions and 6 deletions

View File

@ -98,10 +98,12 @@ public class EnhancerImpl implements Enhancer {
*/ */
@Override @Override
public synchronized byte[] enhance(String className, byte[] originalBytes) throws EnhancementException { public synchronized byte[] enhance(String className, byte[] originalBytes) throws EnhancementException {
//Classpool#describe does not accept '/' in the description name as it expects a class name. See HHH-12545
final String safeClassName = className.replace( '/', '.' );
try { try {
final TypeDescription managedCtClass = classPool.describe( className ).resolve(); final TypeDescription managedCtClass = classPool.describe( safeClassName ).resolve();
DynamicType.Builder<?> builder = doEnhance( DynamicType.Builder<?> builder = doEnhance(
bytebuddy.getCurrentyByteBuddy().ignore( isDefaultFinalizer() ).redefine( managedCtClass, ClassFileLocator.Simple.of( className, originalBytes ) ), bytebuddy.getCurrentyByteBuddy().ignore( isDefaultFinalizer() ).redefine( managedCtClass, ClassFileLocator.Simple.of( safeClassName, originalBytes ) ),
managedCtClass managedCtClass
); );
if ( builder == null ) { if ( builder == null ) {
@ -113,7 +115,7 @@ public class EnhancerImpl implements Enhancer {
} }
catch (RuntimeException e) { catch (RuntimeException e) {
e.printStackTrace(); e.printStackTrace();
log.unableToBuildEnhancementMetamodel( className ); log.unableToBuildEnhancementMetamodel( safeClassName );
return null; return null;
} }
} }

View File

@ -109,11 +109,13 @@ class FieldAccessEnhancer implements AsmVisitorWrapper.ForDeclaredMethods.Method
} }
private FieldDescription findField(String owner, String name, String desc) { private FieldDescription findField(String owner, String name, String desc) {
TypePool.Resolution resolution = classPool.describe( owner.replace( '/', '.' ) ); //Classpool#describe does not accept '/' in the description name as it expects a class name
final String cleanedOwner = owner.replace( '/', '.' );
final TypePool.Resolution resolution = classPool.describe( cleanedOwner );
if ( !resolution.isResolved() ) { if ( !resolution.isResolved() ) {
final String msg = String.format( final String msg = String.format(
"Unable to perform extended enhancement - Unable to locate [%s]", "Unable to perform extended enhancement - Unable to locate [%s]",
owner.replace( '/', '.' ) cleanedOwner
); );
throw new EnhancementException( msg ); throw new EnhancementException( msg );
} }
@ -122,7 +124,7 @@ class FieldAccessEnhancer implements AsmVisitorWrapper.ForDeclaredMethods.Method
final String msg = String.format( final String msg = String.format(
"Unable to perform extended enhancement - No unique field [%s] defined by [%s]", "Unable to perform extended enhancement - No unique field [%s] defined by [%s]",
name, name,
owner.replace( '/', '.' ) cleanedOwner
); );
throw new EnhancementException( msg ); throw new EnhancementException( msg );
} }

View File

@ -0,0 +1,69 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.bytecode.enhancement.bytebuddy;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.hibernate.bytecode.enhance.internal.bytebuddy.EnhancerImpl;
import org.hibernate.bytecode.enhance.spi.DefaultEnhancementContext;
import org.hibernate.bytecode.enhance.spi.Enhancer;
import org.hibernate.bytecode.internal.bytebuddy.ByteBuddyState;
import org.hibernate.testing.TestForIssue;
import org.hibernate.test.bytecode.Bean;
import org.junit.Assert;
import org.junit.Test;
/**
* WildFly will use class names in "internal JVM format" when invoking the enhancer,
* meaning the package separator is '/' rather than '.'.
* We need to make sure this is handled.
*/
public class EnhancerWildFlyNamesTest {
@Test
@TestForIssue( jiraKey = "HHH-12545" )
public void test() {
Enhancer enhancer = createByteBuddyEnhancer();
String internalName = Bean.class.getName().replace( '.', '/' );
String resourceName = internalName + ".class";
byte[] buffer = new byte[0];
try {
buffer = readResource( resourceName );
}
catch (IOException e) {
Assert.fail( "Should not have an IOException here" );
}
byte[] enhanced = enhancer.enhance( internalName, buffer );
Assert.assertNotNull( "This is null when there have been swallowed exceptions during enhancement. Check Logs!", enhanced );
}
private byte[] readResource(String resourceName) throws IOException {
final int BUF_SIZE = 256;
byte[] buffer = new byte[BUF_SIZE];
ByteArrayOutputStream os = new ByteArrayOutputStream();
int readSize = 0;
try ( InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream( resourceName ) ) {
while ( ( readSize = inputStream.read( buffer ) ) != -1 ) {
os.write( buffer, 0, readSize );
}
os.flush();
os.close();
}
return os.toByteArray();
}
private Enhancer createByteBuddyEnhancer() {
ByteBuddyState bytebuddy = new ByteBuddyState();
DefaultEnhancementContext enhancementContext = new DefaultEnhancementContext();
EnhancerImpl impl = new EnhancerImpl( enhancementContext, bytebuddy );
return impl;
}
}