HHH-11321: fixes resolution of class names via build plugins
This commit is contained in:
parent
e10d87085d
commit
159bc99a36
|
@ -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() );
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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 );
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,6 +31,13 @@
|
|||
<executionStrategy>once-per-session</executionStrategy>
|
||||
<threadSafe>false</threadSafe>
|
||||
<parameters>
|
||||
<parameter>
|
||||
<name>base</name>
|
||||
<type>java.lang.String</type>
|
||||
<required>false</required>
|
||||
<editable>true</editable>
|
||||
<description>The root folder for .class files</description>
|
||||
</parameter>
|
||||
<parameter>
|
||||
<name>dir</name>
|
||||
<type>java.lang.String</type>
|
||||
|
@ -75,6 +82,7 @@
|
|||
</parameter>
|
||||
</parameters>
|
||||
<configuration>
|
||||
<base>${project.build.outputDirectory}</base>
|
||||
<dir>${project.build.outputDirectory}</dir>
|
||||
<failOnError>true</failOnError>
|
||||
<enableLazyInitialization>false</enableLazyInitialization>
|
||||
|
|
|
@ -34,6 +34,13 @@
|
|||
<executionStrategy>once-per-session</executionStrategy>
|
||||
<threadSafe>false</threadSafe>
|
||||
<parameters>
|
||||
<parameter>
|
||||
<name>base</name>
|
||||
<type>java.lang.String</type>
|
||||
<required>false</required>
|
||||
<editable>true</editable>
|
||||
<description>The root folder for .class files</description>
|
||||
</parameter>
|
||||
<parameter>
|
||||
<name>dir</name>
|
||||
<type>java.lang.String</type>
|
||||
|
@ -78,6 +85,7 @@
|
|||
</parameter>
|
||||
</parameters>
|
||||
<configuration>
|
||||
<base>${project.build.outputDirectory}</base>
|
||||
<dir>${project.build.outputDirectory}</dir>
|
||||
<failOnError>true</failOnError>
|
||||
<enableLazyInitialization>false</enableLazyInitialization>
|
||||
|
|
|
@ -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<Project> {
|
|||
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<Project> {
|
|||
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 );
|
||||
|
|
Loading…
Reference in New Issue