diff --git a/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy/EnhancerImpl.java b/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy/EnhancerImpl.java
index f775607443..c0c0d20f86 100644
--- a/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy/EnhancerImpl.java
+++ b/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy/EnhancerImpl.java
@@ -6,10 +6,6 @@
*/
package org.hibernate.bytecode.enhance.internal.bytebuddy;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
@@ -61,10 +57,8 @@ import net.bytebuddy.implementation.FixedValue;
import net.bytebuddy.implementation.Implementation;
import net.bytebuddy.implementation.StubMethod;
import net.bytebuddy.pool.TypePool;
-import net.bytebuddy.utility.StreamDrainer;
import static net.bytebuddy.matcher.ElementMatchers.isGetter;
-import static net.bytebuddy.matcher.ElementMatchers.named;
public class EnhancerImpl implements Enhancer {
@@ -118,18 +112,6 @@ public class EnhancerImpl implements Enhancer {
}
}
- @Override
- public byte[] enhance(File javaClassFile) throws EnhancementException, IOException {
- String name = javaClassFile.getName().substring( 0, javaClassFile.getName().length() - ".class".length() );
- InputStream inputStream = new FileInputStream( javaClassFile );
- try {
- return enhance( name, StreamDrainer.DEFAULT.drain( inputStream ) );
- }
- finally {
- inputStream.close();
- }
- }
-
private TypePool buildClassPool(final ByteBuddyEnhancementContext enhancementContext) {
return TypePool.Default.WithLazyResolution.of( enhancementContext.getLoadingClassLoader() );
}
diff --git a/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/javassist/EnhancerImpl.java b/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/javassist/EnhancerImpl.java
index 1cdfe5ca1b..a7bdeeafb4 100644
--- a/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/javassist/EnhancerImpl.java
+++ b/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/javassist/EnhancerImpl.java
@@ -16,7 +16,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
-import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.LoaderClassPath;
@@ -86,18 +85,6 @@ public class EnhancerImpl implements Enhancer {
}
}
- @Override
- public byte[] enhance(File javaClassFile) throws EnhancementException, IOException {
- final CtClass ctClass = classPool.makeClass( new FileInputStream( javaClassFile ) );
- try {
- return enhance( ctClass.getName(), ctClass.toBytecode() );
- }
- catch (CannotCompileException e) {
- log.warn( "Unable to enhance class file [" + javaClassFile.getAbsolutePath() + "]", e );
- return null;
- }
- }
-
private ClassPool buildClassPool(final JavassistEnhancementContext enhancementContext) {
final ClassPool classPool = new ClassPool( false ) {
@Override
diff --git a/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/spi/Enhancer.java b/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/spi/Enhancer.java
index cd310d7240..dcd1182bb0 100644
--- a/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/spi/Enhancer.java
+++ b/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/spi/Enhancer.java
@@ -6,9 +6,6 @@
*/
package org.hibernate.bytecode.enhance.spi;
-import java.io.File;
-import java.io.IOException;
-
/**
* Class responsible for performing enhancement.
*
@@ -29,6 +26,4 @@ public interface Enhancer {
* @throws EnhancementException Indicates a problem performing the enhancement
*/
byte[] enhance(String className, byte[] originalBytes) throws EnhancementException;
-
- byte[] enhance(File javaClassFile) throws EnhancementException, IOException;
}
diff --git a/hibernate-core/src/main/java/org/hibernate/tool/enhance/EnhancementTask.java b/hibernate-core/src/main/java/org/hibernate/tool/enhance/EnhancementTask.java
index b98d8f60c2..7455c03465 100644
--- a/hibernate-core/src/main/java/org/hibernate/tool/enhance/EnhancementTask.java
+++ b/hibernate-core/src/main/java/org/hibernate/tool/enhance/EnhancementTask.java
@@ -6,6 +6,7 @@
*/
package org.hibernate.tool.enhance;
+import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
@@ -60,15 +61,27 @@ public class EnhancementTask extends Task {
continue;
}
- processClassFile( javaClassFile );
+ processClassFile( relativeIncludedFileName, javaClassFile );
}
}
-
}
- private void processClassFile(File javaClassFile) {
+ private void processClassFile(String relativeIncludedFileName, File javaClassFile) {
try {
- byte[] result = enhancer.enhance( javaClassFile );
+ String className = relativeIncludedFileName.substring( 0, ".class".length() ).replace( File.separatorChar, '.' );
+ ByteArrayOutputStream originalBytes = new ByteArrayOutputStream();
+ FileInputStream fileInputStream = new FileInputStream( javaClassFile );
+ try {
+ byte[] buffer = new byte[1024];
+ int length;
+ while ( ( length = fileInputStream.read( buffer ) ) != -1 ) {
+ originalBytes.write( buffer, 0, length );
+ }
+ }
+ finally {
+ fileInputStream.close();
+ }
+ byte[] result = enhancer.enhance( className, originalBytes.toByteArray() );
if ( result != null ) {
writeEnhancedClass( javaClassFile, result );
}
diff --git a/tooling/hibernate-enhance-maven-plugin/src/main/java/org/hibernate/orm/tooling/maven/MavenEnhancePlugin.java b/tooling/hibernate-enhance-maven-plugin/src/main/java/org/hibernate/orm/tooling/maven/MavenEnhancePlugin.java
index 327032f176..550b6e0785 100644
--- a/tooling/hibernate-enhance-maven-plugin/src/main/java/org/hibernate/orm/tooling/maven/MavenEnhancePlugin.java
+++ b/tooling/hibernate-enhance-maven-plugin/src/main/java/org/hibernate/orm/tooling/maven/MavenEnhancePlugin.java
@@ -6,9 +6,10 @@
*/
package org.hibernate.orm.tooling.maven;
+import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileFilter;
-import java.io.FileNotFoundException;
+import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
@@ -58,6 +59,9 @@ public class MavenEnhancePlugin extends AbstractMojo {
@Component
private BuildContext buildContext;
+ @Parameter(property = "base", defaultValue = "${project.build.outputDirectory}")
+ private String base;
+
@Parameter(property = "dir", defaultValue = "${project.build.outputDirectory}")
private String dir;
@@ -86,6 +90,10 @@ public class MavenEnhancePlugin extends AbstractMojo {
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() ) {
@@ -99,7 +107,7 @@ public class MavenEnhancePlugin extends AbstractMojo {
}
getLog().info( "Starting Hibernate enhancement for classes on " + dir );
- final ClassLoader classLoader = toClassLoader( Collections.singletonList( root ) );
+ final ClassLoader classLoader = toClassLoader( Collections.singletonList( new File( base ) ) );
EnhancementContext enhancementContext = new DefaultEnhancementContext() {
@Override
@@ -195,7 +203,23 @@ public class MavenEnhancePlugin extends AbstractMojo {
private byte[] doEnhancement(File javaClassFile, Enhancer enhancer) throws MojoExecutionException {
try {
- return enhancer.enhance(javaClassFile);
+ String className = javaClassFile.getAbsolutePath().substring(
+ base.length() + 1,
+ javaClassFile.getAbsolutePath().length() - ".class".length()
+ ).replace( File.separatorChar, '.' );
+ ByteArrayOutputStream originalBytes = new ByteArrayOutputStream();
+ FileInputStream fileInputStream = new FileInputStream( javaClassFile );
+ try {
+ byte[] buffer = new byte[1024];
+ int length;
+ while ( ( length = fileInputStream.read( buffer ) ) != -1 ) {
+ originalBytes.write( buffer, 0, length );
+ }
+ }
+ finally {
+ fileInputStream.close();
+ }
+ return enhancer.enhance( className, originalBytes.toByteArray() );
}
catch (Exception e) {
String msg = "Unable to enhance class: " + javaClassFile.getName();
@@ -267,7 +291,7 @@ public class MavenEnhancePlugin extends AbstractMojo {
}
finally {
try {
- if( outputStream != null ) {
+ if ( outputStream != null ) {
outputStream.close();
}
}
diff --git a/tooling/hibernate-enhance-maven-plugin/src/main/resources/META-INF/maven/org.hibernate.orm.tooling/hibernate-enhance-maven-plugin/plugin-help.xml b/tooling/hibernate-enhance-maven-plugin/src/main/resources/META-INF/maven/org.hibernate.orm.tooling/hibernate-enhance-maven-plugin/plugin-help.xml
index 551b36ace2..fa18073407 100644
--- a/tooling/hibernate-enhance-maven-plugin/src/main/resources/META-INF/maven/org.hibernate.orm.tooling/hibernate-enhance-maven-plugin/plugin-help.xml
+++ b/tooling/hibernate-enhance-maven-plugin/src/main/resources/META-INF/maven/org.hibernate.orm.tooling/hibernate-enhance-maven-plugin/plugin-help.xml
@@ -31,6 +31,13 @@
once-per-session
false
+
+ base
+ java.lang.String
+ false
+ true
+ The root folder for .class files
+
dir
java.lang.String
@@ -75,6 +82,7 @@
+ ${project.build.outputDirectory}
${project.build.outputDirectory}
true
false
diff --git a/tooling/hibernate-enhance-maven-plugin/src/main/resources/META-INF/maven/plugin.xml b/tooling/hibernate-enhance-maven-plugin/src/main/resources/META-INF/maven/plugin.xml
index 730c326848..8e2b9bb288 100644
--- a/tooling/hibernate-enhance-maven-plugin/src/main/resources/META-INF/maven/plugin.xml
+++ b/tooling/hibernate-enhance-maven-plugin/src/main/resources/META-INF/maven/plugin.xml
@@ -34,6 +34,13 @@
once-per-session
false
+
+ base
+ java.lang.String
+ false
+ true
+ The root folder for .class files
+
dir
java.lang.String
@@ -78,6 +85,7 @@
+ ${project.build.outputDirectory}
${project.build.outputDirectory}
true
false
diff --git a/tooling/hibernate-gradle-plugin/src/main/groovy/org/hibernate/orm/tooling/gradle/HibernatePlugin.java b/tooling/hibernate-gradle-plugin/src/main/groovy/org/hibernate/orm/tooling/gradle/HibernatePlugin.java
index c48d6f46cb..cb56a91453 100644
--- a/tooling/hibernate-gradle-plugin/src/main/groovy/org/hibernate/orm/tooling/gradle/HibernatePlugin.java
+++ b/tooling/hibernate-gradle-plugin/src/main/groovy/org/hibernate/orm/tooling/gradle/HibernatePlugin.java
@@ -6,7 +6,9 @@
*/
package org.hibernate.orm.tooling.gradle;
+import java.io.ByteArrayOutputStream;
import java.io.File;
+import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
@@ -126,7 +128,7 @@ public class HibernatePlugin implements Plugin {
continue;
}
- final byte[] enhancedBytecode = doEnhancement( file, enhancer );
+ final byte[] enhancedBytecode = doEnhancement( sourceSet.getOutput().getClassesDir(), file, enhancer );
if ( enhancedBytecode != null ) {
writeOutEnhancedClass( enhancedBytecode, file );
logger.info( "Successfully enhanced class [" + file + "]" );
@@ -155,9 +157,25 @@ public class HibernatePlugin implements Plugin {
return new URLClassLoader( urls.toArray( new URL[urls.size()] ), Enhancer.class.getClassLoader() );
}
- private byte[] doEnhancement(File javaClassFile, Enhancer enhancer) {
+ private byte[] doEnhancement(File root, File javaClassFile, Enhancer enhancer) {
try {
- return enhancer.enhance( javaClassFile );
+ String className = javaClassFile.getAbsolutePath().substring(
+ root.getAbsolutePath().length() + 1,
+ javaClassFile.getAbsolutePath().length() - ".class".length()
+ ).replace( File.separatorChar, '.' );
+ ByteArrayOutputStream originalBytes = new ByteArrayOutputStream();
+ FileInputStream fileInputStream = new FileInputStream( javaClassFile );
+ try {
+ byte[] buffer = new byte[1024];
+ int length;
+ while ( ( length = fileInputStream.read( buffer ) ) != -1 ) {
+ originalBytes.write( buffer, 0, length );
+ }
+ }
+ finally {
+ fileInputStream.close();
+ }
+ return enhancer.enhance( className, originalBytes.toByteArray() );
}
catch (Exception e) {
throw new GradleException( "Unable to enhance class : " + javaClassFile, e );