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,58 +29,35 @@ 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 ) );
}
final Configuration compileConfig = project
.getConfigurations()
.getByName( sourceSet.getCompileClasspathConfigurationName() );
final Set<File> dependencyFiles = compileConfig.getResolvedConfiguration().getFiles();
for ( File dependencyFile : dependencyFiles ) {
urls.add( toUrl( dependencyFile ) );
}
return new URLClassLoader( urls.toArray(new URL[0]), Enhancer.class.getClassLoader() );
} }
public static String[] determineCompileNameParts(String name) { private static URL toUrl(File file) {
StringBuilder firstPart = null; final URI classesDirUri = file.toURI();
StringBuilder secondPart = null; try {
return classesDirUri.toURL();
boolean processingFirstPart = false;
boolean processingSecondPart = false;
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 ) ) );
}
}
} }
catch (MalformedURLException e) {
if ( firstPart == null ) { throw new GradleException( "Unable to resolve classpath entry to URL : " + file.getAbsolutePath(), e );
throw new RuntimeException( "Unexpected compilation task name : " + name );
} }
if ( secondPart == null ) {
return new String[] { "main", firstPart.toString() };
}
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();
final URL[] urls = new URL[ files.size() ]; final URL[] urls = new URL[ files.size() ];

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 );
} }