HHH-13343 : Bytecode enhancement using ByteBuddy fails when the class is not available from the provided ClassLoader

This commit is contained in:
Gail Badner 2019-04-04 22:44:02 -07:00 committed by gbadner
parent a2a5799a13
commit 0506b4aa46
1 changed files with 13 additions and 10 deletions

View File

@ -14,7 +14,6 @@ import java.lang.reflect.Modifier;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
@ -126,7 +125,7 @@ public class EnhancerImpl implements Enhancer {
public byte[] enhance(String className, byte[] originalBytes) throws EnhancementException { public 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 //Classpool#describe does not accept '/' in the description name as it expects a class name. See HHH-12545
final String safeClassName = className.replace( '/', '.' ); final String safeClassName = className.replace( '/', '.' );
classFileLocator.addNameAndBytes( safeClassName, originalBytes ); classFileLocator.setClassNameAndBytes( safeClassName, originalBytes );
try { try {
final TypeDescription typeDescription = typePool.describe( safeClassName ).resolve(); final TypeDescription typeDescription = typePool.describe( safeClassName ).resolve();
@ -539,7 +538,10 @@ public class EnhancerImpl implements Enhancer {
private class EnhancerClassFileLocator extends ClassFileLocator.ForClassLoader { private class EnhancerClassFileLocator extends ClassFileLocator.ForClassLoader {
private Map<String,Resolution> nameToResolutionMap = new HashMap<>(); // The name of the class to (possibly be) transformed.
private String className;
// The explicitly resolved Resolution for the class to (possibly be) transformed.
private Resolution resolution;
/** /**
* Creates a new class file locator for the given class loader. * Creates a new class file locator for the given class loader.
@ -551,20 +553,21 @@ public class EnhancerImpl implements Enhancer {
} }
@Override @Override
public Resolution locate(String name) throws IOException { public Resolution locate(String className) throws IOException {
Resolution resolution = nameToResolutionMap.get( name ); assert className != null;
if ( resolution != null ) { if ( className.equals( this.className ) ) {
return resolution; return resolution;
} }
else { else {
return super.locate( name ); return super.locate( className );
} }
} }
void addNameAndBytes(String name, byte[] bytes ) { void setClassNameAndBytes(String className, byte[] bytes) {
assert name != null; assert className != null;
assert bytes != null; assert bytes != null;
nameToResolutionMap.put( name, new Resolution.Explicit( bytes) ); this.className = className;
this.resolution = new Resolution.Explicit( bytes);
} }
} }
} }