
org.hibernate.jpa.internal.instrument.InterceptFieldClassFileTransformer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hibernate-entitymanager
Show all versions of hibernate-entitymanager
A module of the Hibernate O/RM project
/*
* 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 .
*/
package org.hibernate.jpa.internal.instrument;
import java.lang.instrument.IllegalClassFormatException;
import java.security.ProtectionDomain;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.hibernate.bytecode.buildtime.spi.ClassFilter;
import org.hibernate.bytecode.buildtime.spi.FieldFilter;
import org.hibernate.bytecode.spi.ClassTransformer;
import org.hibernate.cfg.Environment;
/**
* Enhance the classes allowing them to implements InterceptFieldEnabled
* This interface is then used by Hibernate for some optimizations.
*
* @author Emmanuel Bernard
*/
public class InterceptFieldClassFileTransformer implements javax.persistence.spi.ClassTransformer {
private ClassTransformer classTransformer;
public InterceptFieldClassFileTransformer(Collection entities) {
final List copyEntities = new ArrayList( entities.size() );
copyEntities.addAll( entities );
classTransformer = Environment.getBytecodeProvider().getTransformer(
//TODO change it to a static class to make it faster?
new ClassFilter() {
public boolean shouldInstrumentClass(String className) {
return copyEntities.contains( className );
}
},
//TODO change it to a static class to make it faster?
new FieldFilter() {
@Override
public boolean shouldInstrumentField(String className, String fieldName) {
return true;
}
@Override
public boolean shouldTransformFieldAccess(
String transformingClassName, String fieldOwnerClassName, String fieldName
) {
return true;
}
}
);
}
@Override
public byte[] transform(
ClassLoader loader,
String className,
Class> classBeingRedefined,
ProtectionDomain protectionDomain,
byte[] classfileBuffer ) throws IllegalClassFormatException {
try {
return classTransformer.transform( loader, className, classBeingRedefined,
protectionDomain, classfileBuffer );
}
catch (Exception e) {
throw new IllegalClassFormatException( e.getMessage() );
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy