HHH-15597 - Gradle plugin should use full compilation classpath for enhancement

This commit is contained in:
Steve Ebersole 2022-10-18 20:14:07 -05:00
parent 5cf5f5adbd
commit d3dafe255c
2 changed files with 29 additions and 47 deletions

View File

@ -12,10 +12,15 @@ import java.net.URI;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Set; import java.util.Set;
import org.gradle.api.GradleException; import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.file.FileCollection; import org.gradle.api.file.FileCollection;
import org.gradle.api.tasks.SourceSet;
import org.hibernate.bytecode.enhance.spi.Enhancer; import org.hibernate.bytecode.enhance.spi.Enhancer;
@ -24,57 +29,34 @@ import org.hibernate.bytecode.enhance.spi.Enhancer;
*/ */
public class Helper { public class Helper {
public static String determineCompileSourceSetName(String name) { public static ClassLoader toClassLoader(SourceSet sourceSet, Project project) {
return determineCompileNameParts( name )[0]; final List<URL> urls = new ArrayList<>();
final FileCollection classesDirs = sourceSet.getOutput().getClassesDirs();
for ( File classesDir : classesDirs ) {
urls.add( toUrl( classesDir ) );
} }
public static String[] determineCompileNameParts(String name) { final Configuration compileConfig = project
StringBuilder firstPart = null; .getConfigurations()
StringBuilder secondPart = null; .getByName( sourceSet.getCompileClasspathConfigurationName() );
final Set<File> dependencyFiles = compileConfig.getResolvedConfiguration().getFiles();
boolean processingFirstPart = false; for ( File dependencyFile : dependencyFiles ) {
boolean processingSecondPart = false; urls.add( toUrl( dependencyFile ) );
final char[] nameChars = name.toCharArray();
for ( int i = 0; i < nameChars.length; i++ ) {
final char nameChar = nameChars[ i ];
if ( processingFirstPart ) {
if ( Character.isUpperCase( nameChar ) ) {
// this is the start of the second-part
processingFirstPart = false;
processingSecondPart = true;
secondPart = new StringBuilder( String.valueOf( Character.toLowerCase( nameChar ) ) );
}
else {
firstPart.append( nameChar );
}
}
else if ( processingSecondPart ) {
if ( Character.isUpperCase( nameChar ) ) {
throw new RuntimeException( "Unexpected compilation task name : " + name );
}
else {
secondPart.append( nameChar );
}
}
else {
if ( Character.isUpperCase( nameChar ) ) {
processingFirstPart = true;
firstPart = new StringBuilder( String.valueOf( Character.toLowerCase( nameChar ) ) );
}
}
} }
if ( firstPart == null ) { return new URLClassLoader( urls.toArray(new URL[0]), Enhancer.class.getClassLoader() );
throw new RuntimeException( "Unexpected compilation task name : " + name );
} }
if ( secondPart == null ) { private static URL toUrl(File file) {
return new String[] { "main", firstPart.toString() }; final URI classesDirUri = file.toURI();
try {
return classesDirUri.toURL();
}
catch (MalformedURLException e) {
throw new GradleException( "Unable to resolve classpath entry to URL : " + file.getAbsolutePath(), e );
} }
return new String[] { firstPart.toString(), secondPart.toString() };
} }
public static ClassLoader toClassLoader(FileCollection directories) { public static ClassLoader toClassLoader(FileCollection directories) {
final Set<File> files = directories.getFiles(); final Set<File> files = directories.getFiles();

View File

@ -79,7 +79,7 @@ public class HibernateOrmPlugin implements Plugin<Project> {
@Override @Override
public void execute(Task t) { public void execute(Task t) {
final DirectoryProperty classesDirectory = languageCompileTask.getDestinationDirectory(); final DirectoryProperty classesDirectory = languageCompileTask.getDestinationDirectory();
final ClassLoader classLoader = Helper.toClassLoader( sourceSet.getOutput().getClassesDirs() ); final ClassLoader classLoader = Helper.toClassLoader( sourceSet, project );
EnhancementHelper.enhance( classesDirectory, classLoader, ormDsl, project ); EnhancementHelper.enhance( classesDirectory, classLoader, ormDsl, project );
} }