|
| 1 | +package org.jboss.shamrock.jpa; |
| 2 | + |
| 3 | +import java.util.Set; |
| 4 | +import java.util.function.Function; |
| 5 | + |
| 6 | +import org.hibernate.bytecode.enhance.spi.DefaultEnhancementContext; |
| 7 | +import org.hibernate.bytecode.enhance.spi.Enhancer; |
| 8 | +import org.hibernate.bytecode.spi.BytecodeProvider; |
| 9 | + |
| 10 | +import org.objectweb.asm.ClassReader; |
| 11 | +import org.objectweb.asm.ClassVisitor; |
| 12 | +import org.objectweb.asm.ClassWriter; |
| 13 | +import org.objectweb.asm.Opcodes; |
| 14 | + |
| 15 | +/** |
| 16 | + * Used to transform bytecode by registering to org.jboss.shamrock.deployment.ProcessorContext#addByteCodeTransformer(java.util.function.Function) |
| 17 | + * this function adapts the Shamrock bytecode transformer API - which uses ASM - to use the Entity Enhancement API of |
| 18 | + * Hibernate ORM, which exposes a simple byte array. |
| 19 | + * |
| 20 | + * @author Sanne Grinovero <sanne@hibernate.org> |
| 21 | + */ |
| 22 | +public final class HibernateEntityEnhancer implements Function<String, Function<ClassVisitor, ClassVisitor>> { |
| 23 | + |
| 24 | + private final Enhancer enhancer; |
| 25 | + private final Set<String> classnameWhitelist; |
| 26 | + |
| 27 | + @Deprecated //shouldn't use this version, but will do for a while |
| 28 | + public HibernateEntityEnhancer() { |
| 29 | + this(null); |
| 30 | + } |
| 31 | + |
| 32 | + public HibernateEntityEnhancer(Set<String> classnameWhitelist) { |
| 33 | + this.classnameWhitelist = classnameWhitelist; |
| 34 | + BytecodeProvider provider = new org.hibernate.bytecode.internal.bytebuddy.BytecodeProviderImpl(); |
| 35 | + this.enhancer = provider.getEnhancer(new DefaultEnhancementContext()); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public Function<ClassVisitor, ClassVisitor> apply(String classname) { |
| 40 | + if (classnameWhitelist == null || classnameWhitelist.contains(classname)) |
| 41 | + return new HibernateTransformingVisitorFunction(classname); |
| 42 | + else |
| 43 | + return null; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Having to convert a ClassVisitor into another, this allows visitor chaining: the returned ClassVisitor needs to |
| 48 | + * refer to the previous ClassVisitor in the chain to forward input events (optionally transformed). |
| 49 | + */ |
| 50 | + private class HibernateTransformingVisitorFunction implements Function<ClassVisitor, ClassVisitor> { |
| 51 | + |
| 52 | + private final String className; |
| 53 | + |
| 54 | + public HibernateTransformingVisitorFunction(String className) { |
| 55 | + this.className = className; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public ClassVisitor apply(ClassVisitor outputClassVisitor) { |
| 60 | + return new HibernateEnhancingClassVisitor(className, outputClassVisitor); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private class HibernateEnhancingClassVisitor extends ClassVisitor { |
| 65 | + |
| 66 | + private final String className; |
| 67 | + private final ClassVisitor outputClassVisitor; |
| 68 | + |
| 69 | + public HibernateEnhancingClassVisitor(String className, ClassVisitor outputClassVisitor) { |
| 70 | + super(Opcodes.ASM6, new ClassWriter(0)); |
| 71 | + this.className = className; |
| 72 | + this.outputClassVisitor = outputClassVisitor; |
| 73 | + } |
| 74 | + |
| 75 | + public void visitEnd() { |
| 76 | + super.visitEnd(); |
| 77 | + final ClassWriter writer = (ClassWriter) this.cv; //safe cast: cv is the the ClassWriter instance we passed to the super constructor |
| 78 | + //We need to convert the nice Visitor chain into a plain byte array to adapt to the Hibernate ORM |
| 79 | + //enhancement API: |
| 80 | + final byte[] inputBytes = writer.toByteArray(); |
| 81 | + final byte[] transformedBytes = hibernateEnhancement(className, inputBytes); |
| 82 | + //Then re-convert the transformed bytecode to not interrupt the visitor chain: |
| 83 | + ClassReader cr = new ClassReader(transformedBytes); |
| 84 | + cr.accept(outputClassVisitor, 0); |
| 85 | + } |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | + private byte[] hibernateEnhancement(final String className, final byte[] originalBytes) { |
| 90 | + final byte[] enhanced = enhancer.enhance(className, originalBytes); |
| 91 | + return enhanced == null ? originalBytes : enhanced; |
| 92 | + } |
| 93 | + |
| 94 | +} |
0 commit comments