From 07529c309dcd8f9292a3f3140abb3fd7b13f128e Mon Sep 17 00:00:00 2001 From: Tomas Cerskus Date: Sat, 4 Feb 2023 19:17:32 +0000 Subject: [PATCH] HHH-15707 - Fix Gradle plugin with Kotlin 1.7.0 or higher Since Kotlin version 1.7.0 the KotlinCompile task no longer extends Gradle's AbstractCompile. This commit updates Hibernate Gradle enhancement plugin to not cast to AbstractCompile and instead use reflection to invoke the "getDestinationDirectory" method. It also updates the Kotlin version on used to test the Gradle enhancement (but remains backwards compatible with previous Kotlin versions). --- .../tooling/gradle/HibernateOrmPlugin.java | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/tooling/hibernate-gradle-plugin/src/main/java/org/hibernate/orm/tooling/gradle/HibernateOrmPlugin.java b/tooling/hibernate-gradle-plugin/src/main/java/org/hibernate/orm/tooling/gradle/HibernateOrmPlugin.java index c5f68dac7d..7b1ec99322 100644 --- a/tooling/hibernate-gradle-plugin/src/main/java/org/hibernate/orm/tooling/gradle/HibernateOrmPlugin.java +++ b/tooling/hibernate-gradle-plugin/src/main/java/org/hibernate/orm/tooling/gradle/HibernateOrmPlugin.java @@ -6,6 +6,7 @@ */ package org.hibernate.orm.tooling.gradle; +import java.lang.reflect.Method; import java.util.Set; import org.gradle.api.Action; @@ -69,21 +70,26 @@ public class HibernateOrmPlugin implements Plugin { for ( String language : languages ) { final String languageCompileTaskName = sourceSet.getCompileTaskName( language ); - final AbstractCompile languageCompileTask = (AbstractCompile) project.getTasks().findByName( languageCompileTaskName ); + final Task languageCompileTask = project.getTasks().findByName( languageCompileTaskName ); if ( languageCompileTask == null ) { continue; } //noinspection Convert2Lambda - languageCompileTask.doLast( new Action<>() { + languageCompileTask.doLast(new Action<>() { @Override public void execute(Task t) { - final DirectoryProperty classesDirectory = languageCompileTask.getDestinationDirectory(); - final ClassLoader classLoader = Helper.toClassLoader( sourceSet, project ); - - EnhancementHelper.enhance( classesDirectory, classLoader, ormDsl, project ); + try { + final Method getDestinationDirectory = languageCompileTask.getClass().getMethod("getDestinationDirectory"); + final DirectoryProperty classesDirectory = (DirectoryProperty) getDestinationDirectory.invoke(languageCompileTask); + final ClassLoader classLoader = Helper.toClassLoader(sourceSet, project); + EnhancementHelper.enhance(classesDirectory, classLoader, ormDsl, project); + } + catch (Exception e) { + throw new RuntimeException(e); + } } - } ); + }); } } ); }