use of org.hibernate.bytecode.enhance.spi.Enhancer in project hibernate-orm by hibernate.
the class MavenEnhancePlugin method execute.
public void execute() throws MojoExecutionException, MojoFailureException {
if (!shouldApply()) {
getLog().warn("Skipping Hibernate bytecode enhancement plugin execution since no feature is enabled");
return;
}
if (!dir.startsWith(base)) {
throw new MojoExecutionException("The enhancement directory 'dir' (" + dir + ") is no subdirectory of 'base' (" + base + ")");
}
// Perform a depth first search for sourceSet
File root = new File(this.dir);
if (!root.exists()) {
getLog().info("Skipping Hibernate enhancement plugin execution since there is no classes dir " + dir);
return;
}
walkDir(root);
if (sourceSet.isEmpty()) {
getLog().info("Skipping Hibernate enhancement plugin execution since there are no classes to enhance on " + dir);
return;
}
getLog().info("Starting Hibernate enhancement for classes on " + dir);
final ClassLoader classLoader = toClassLoader(Collections.singletonList(new File(base)));
EnhancementContext enhancementContext = new DefaultEnhancementContext() {
@Override
public ClassLoader getLoadingClassLoader() {
return classLoader;
}
@Override
public boolean doBiDirectionalAssociationManagement(UnloadedField field) {
return enableAssociationManagement;
}
@Override
public boolean doDirtyCheckingInline(UnloadedClass classDescriptor) {
return enableDirtyTracking;
}
@Override
public boolean hasLazyLoadableAttributes(UnloadedClass classDescriptor) {
return enableLazyInitialization;
}
@Override
public boolean isLazyLoadable(UnloadedField field) {
return enableLazyInitialization;
}
@Override
public boolean doExtendedEnhancement(UnloadedClass classDescriptor) {
return enableExtendedEnhancement;
}
};
if (enableExtendedEnhancement) {
getLog().warn("Extended enhancement is enabled. Classes other than entities may be modified. You should consider access the entities using getter/setter methods and disable this property. Use at your own risk.");
}
final Enhancer enhancer = Environment.getBytecodeProvider().getEnhancer(enhancementContext);
for (File file : sourceSet) {
final byte[] enhancedBytecode = doEnhancement(file, enhancer);
if (enhancedBytecode == null) {
continue;
}
writeOutEnhancedClass(enhancedBytecode, file);
getLog().info("Successfully enhanced class [" + file + "]");
}
}
use of org.hibernate.bytecode.enhance.spi.Enhancer in project hibernate-orm by hibernate.
the class HibernatePlugin method applyEnhancement.
private void applyEnhancement(final Project project, final HibernateExtension hibernateExtension) {
if (!hibernateExtension.enhance.shouldApply()) {
project.getLogger().warn("Skipping Hibernate bytecode enhancement since no feature is enabled");
return;
}
for (final SourceSet sourceSet : hibernateExtension.getSourceSets()) {
project.getLogger().debug("Applying Hibernate enhancement action to SourceSet.{}", sourceSet.getName());
final Task compileTask = project.getTasks().findByName(sourceSet.getCompileJavaTaskName());
compileTask.doLast(new Action<Task>() {
@Override
public void execute(Task task) {
project.getLogger().debug("Starting Hibernate enhancement on SourceSet.{}", sourceSet.getName());
final ClassLoader classLoader = toClassLoader(sourceSet.getRuntimeClasspath());
EnhancementContext enhancementContext = new DefaultEnhancementContext() {
@Override
public ClassLoader getLoadingClassLoader() {
return classLoader;
}
@Override
public boolean doBiDirectionalAssociationManagement(UnloadedField field) {
return hibernateExtension.enhance.getEnableAssociationManagement();
}
@Override
public boolean doDirtyCheckingInline(UnloadedClass classDescriptor) {
return hibernateExtension.enhance.getEnableDirtyTracking();
}
@Override
public boolean hasLazyLoadableAttributes(UnloadedClass classDescriptor) {
return hibernateExtension.enhance.getEnableLazyInitialization();
}
@Override
public boolean isLazyLoadable(UnloadedField field) {
return hibernateExtension.enhance.getEnableLazyInitialization();
}
@Override
public boolean doExtendedEnhancement(UnloadedClass classDescriptor) {
return hibernateExtension.enhance.getEnableExtendedEnhancement();
}
};
if (hibernateExtension.enhance.getEnableExtendedEnhancement()) {
logger.warn("Extended enhancement is enabled. Classes other than entities may be modified. You should consider access the entities using getter/setter methods and disable this property. Use at your own risk.");
}
final Enhancer enhancer = Environment.getBytecodeProvider().getEnhancer(enhancementContext);
final FileTree fileTree = project.fileTree(sourceSet.getOutput().getClassesDir());
for (File file : fileTree) {
if (!file.getName().endsWith(".class")) {
continue;
}
final byte[] enhancedBytecode = doEnhancement(sourceSet.getOutput().getClassesDir(), file, enhancer);
if (enhancedBytecode != null) {
writeOutEnhancedClass(enhancedBytecode, file);
logger.info("Successfully enhanced class [" + file + "]");
} else {
logger.info("Skipping class [" + file.getAbsolutePath() + "], not an entity nor embeddable");
}
}
}
});
}
}
use of org.hibernate.bytecode.enhance.spi.Enhancer in project hibernate-orm by hibernate.
the class EnhancerTestUtils method getEnhancerClassLoader.
private static ClassLoader getEnhancerClassLoader(EnhancementContext context, String packageName) {
return new ClassLoader() {
private Enhancer enhancer = Environment.getBytecodeProvider().getEnhancer(context);
@SuppressWarnings("ResultOfMethodCallIgnored")
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
if (!name.startsWith(packageName)) {
return getParent().loadClass(name);
}
Class c = findLoadedClass(name);
if (c != null) {
return c;
}
InputStream is = getResourceAsStream(name.replace('.', '/') + ".class");
if (is == null) {
throw new ClassNotFoundException(name + " not found");
}
try {
byte[] original = new byte[is.available()];
new BufferedInputStream(is).read(original);
byte[] enhanced = enhancer.enhance(name, original);
if (enhanced != null) {
File f = new File(workingDir + File.separator + name.replace(".", File.separator) + ".class");
f.getParentFile().mkdirs();
f.createNewFile();
FileOutputStream out = new FileOutputStream(f);
out.write(enhanced);
out.close();
} else {
enhanced = original;
}
return defineClass(name, enhanced, 0, enhanced.length);
} catch (Throwable t) {
throw new ClassNotFoundException(name + " not found", t);
} finally {
try {
is.close();
} catch (IOException ignore) {
}
}
}
};
}
Aggregations