HHH-11242 - [enhancer] load class on isolated class pool to avoid it being frozen
This commit is contained in:
parent
c1093d533e
commit
4a3dee1588
|
@ -9,8 +9,6 @@ package org.hibernate.bytecode.enhance.internal.javassist;
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
@ -70,30 +68,31 @@ 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 {
|
||||||
|
CtClass managedCtClass = null;
|
||||||
try {
|
try {
|
||||||
final CtClass managedCtClass = classPool.makeClassIfNew( new ByteArrayInputStream( originalBytes ) );
|
managedCtClass = classPool.makeClassIfNew( new ByteArrayInputStream( originalBytes ) );
|
||||||
if ( enhance( managedCtClass ) ) {
|
return enhance( managedCtClass ) ? getByteCode( managedCtClass ) : null;
|
||||||
return getByteCode( managedCtClass );
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (IOException e) {
|
catch (IOException e) {
|
||||||
log.unableToBuildEnhancementMetamodel( className );
|
log.unableToBuildEnhancementMetamodel( className );
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
finally {
|
||||||
|
if ( managedCtClass != null ) {
|
||||||
|
managedCtClass.detach();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private ClassPool buildClassPool(final JavassistEnhancementContext enhancementContext) {
|
private ClassPool buildClassPool(JavassistEnhancementContext enhancementContext) {
|
||||||
final ClassPool classPool = new ClassPool( false ) {
|
ClassPool classPool = new ClassPool( false ) {
|
||||||
@Override
|
@Override
|
||||||
public ClassLoader getClassLoader() {
|
public ClassLoader getClassLoader() {
|
||||||
return enhancementContext.getLoadingClassLoader();
|
return enhancementContext.getLoadingClassLoader();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
final ClassLoader loadingClassLoader = enhancementContext.getLoadingClassLoader();
|
ClassLoader loadingClassLoader = enhancementContext.getLoadingClassLoader();
|
||||||
if ( loadingClassLoader != null ) {
|
if ( loadingClassLoader != null ) {
|
||||||
classPool.appendClassPath( new LoaderClassPath( loadingClassLoader ) );
|
classPool.appendClassPath( new LoaderClassPath( loadingClassLoader ) );
|
||||||
}
|
}
|
||||||
|
@ -177,15 +176,15 @@ public class EnhancerImpl implements Enhancer {
|
||||||
}
|
}
|
||||||
|
|
||||||
private byte[] getByteCode(CtClass managedCtClass) {
|
private byte[] getByteCode(CtClass managedCtClass) {
|
||||||
final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
|
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
|
||||||
final DataOutputStream out = new DataOutputStream( byteStream );
|
DataOutputStream out = new DataOutputStream( byteStream );
|
||||||
try {
|
try {
|
||||||
managedCtClass.toBytecode( out );
|
managedCtClass.toBytecode( out );
|
||||||
return byteStream.toByteArray();
|
return byteStream.toByteArray();
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
log.unableToTransformClass( e.getMessage() );
|
log.unableToTransformClass( e.getMessage() );
|
||||||
throw new HibernateException( "Unable to transform class: " + e.getMessage() );
|
throw new HibernateException( "Unable to transform class: " + e.getMessage() , e );
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
try {
|
try {
|
||||||
|
|
Loading…
Reference in New Issue