From 5fdb2ae2fd2f5fc2875d694f18cbeb61098b373e Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 16 Dec 2020 09:50:48 +0000 Subject: [PATCH] Protect PluginApplicationActions against absent plugin classes Closes gh-24526 --- .../boot/gradle/plugin/KotlinPluginAction.java | 11 ++--------- .../gradle/plugin/PluginApplicationAction.java | 10 ++++++---- .../boot/gradle/plugin/SpringBootPlugin.java | 17 +++++++++++++---- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/KotlinPluginAction.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/KotlinPluginAction.java index ccf0d0cb4d6a..896cc7e9290f 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/KotlinPluginAction.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/KotlinPluginAction.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -47,15 +47,8 @@ private void enableJavaParametersOption(Project project) { } @Override - @SuppressWarnings("unchecked") public Class> getPluginClass() { - try { - return (Class>) Class - .forName("org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper"); - } - catch (Throwable ex) { - return null; - } + return KotlinPluginWrapper.class; } } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/PluginApplicationAction.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/PluginApplicationAction.java index 96bccf32a37f..f3584f3943dd 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/PluginApplicationAction.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/PluginApplicationAction.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,9 +30,11 @@ interface PluginApplicationAction extends Action { /** * The class of the {@code Plugin} that, when applied, will trigger the execution of - * this action. May return {@code null} if the plugin class is not on the classpath. - * @return the plugin class or {@code null} + * this action. + * @return the plugin class + * @throws ClassNotFoundException if the plugin class cannot be found + * @throws NoClassDefFoundError if an error occurs when defining the plugin class */ - Class> getPluginClass(); + Class> getPluginClass() throws ClassNotFoundException, NoClassDefFoundError; } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/SpringBootPlugin.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/SpringBootPlugin.java index 75b960507ac7..26178f9c0b24 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/SpringBootPlugin.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/SpringBootPlugin.java @@ -18,6 +18,7 @@ import java.util.Arrays; import java.util.List; +import java.util.function.Consumer; import org.gradle.api.GradleException; import org.gradle.api.Plugin; @@ -119,10 +120,18 @@ private void registerPluginActions(Project project, Configuration bootArchives) new WarPluginAction(singlePublishedArtifact), new MavenPluginAction(bootArchives.getUploadTaskName()), new DependencyManagementPluginAction(), new ApplicationPluginAction(), new KotlinPluginAction()); for (PluginApplicationAction action : actions) { - Class> pluginClass = action.getPluginClass(); - if (pluginClass != null) { - project.getPlugins().withType(pluginClass, (plugin) -> action.execute(project)); - } + withPluginClassOfAction(action, + (pluginClass) -> project.getPlugins().withType(pluginClass, (plugin) -> action.execute(project))); + } + } + + private void withPluginClassOfAction(PluginApplicationAction action, + Consumer>> consumer) { + try { + consumer.accept(action.getPluginClass()); + } + catch (Throwable ex) { + // Plugin class unavailable. Continue. } }