From d59ef49ead31f7d85c24f98ca6747f9b3a1906a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Boutemy?= Date: Fri, 1 Mar 2024 18:09:06 +0100 Subject: [PATCH] extract Maven 3.3.1 specific method call --- .../plugin/compiler/AbstractCompilerMojo.java | 43 +++++++++++-------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java b/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java index 365a649d..538b7ffc 100644 --- a/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java +++ b/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java @@ -1500,26 +1500,11 @@ protected final Toolchain getToolchain() { Toolchain tc = null; if (jdkToolchain != null) { - // require Maven 3.3.1, that has plugin execution scoped Toolchain support MNG-5755 - try { - // TODO use direct method invocation when prerequisite upgraded to Maven 3.3.1 - Method getToolchainsMethod = toolchainManager - .getClass() - .getMethod("getToolchains", MavenSession.class, String.class, Map.class); - - @SuppressWarnings("unchecked") - List tcs = - (List) getToolchainsMethod.invoke(toolchainManager, session, "jdk", jdkToolchain); + // require Maven 3.3.1, that has plugin execution scoped Toolchain support: MNG-5755 + List tcs = getToolchains(); - if (tcs != null && !tcs.isEmpty()) { - tc = tcs.get(0); - } - } catch (NoSuchMethodException - | SecurityException - | IllegalAccessException - | IllegalArgumentException - | InvocationTargetException e) { - // ignore + if (tcs != null && !tcs.isEmpty()) { + tc = tcs.get(0); } } @@ -1530,6 +1515,26 @@ protected final Toolchain getToolchain() { return tc; } + // TODO use direct method invocation when prerequisite upgraded to Maven 3.3.1 + private List getToolchains() { + try { + Method getToolchainsMethod = + toolchainManager.getClass().getMethod("getToolchains", MavenSession.class, String.class, Map.class); + + @SuppressWarnings("unchecked") + List tcs = + (List) getToolchainsMethod.invoke(toolchainManager, session, "jdk", jdkToolchain); + return tcs; + } catch (NoSuchMethodException + | SecurityException + | IllegalAccessException + | IllegalArgumentException + | InvocationTargetException e) { + // ignore + } + return null; + } + private boolean isDigits(String string) { for (int i = 0; i < string.length(); i++) { if (!Character.isDigit(string.charAt(i))) {