From 35a9d54dbe3492aaa6414804504b0542ab0e627e Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Mon, 12 Jun 2023 21:50:16 +0200 Subject: [PATCH] Simplify code --- .../java/org/apache/maven/DefaultMaven.java | 37 +++++++------------ 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java index 1b32dfe04822..8b2d3c3d5767 100644 --- a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java +++ b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java @@ -26,7 +26,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.Collections; import java.util.Date; import java.util.HashSet; import java.util.LinkedHashMap; @@ -354,44 +353,34 @@ private void setupWorkspaceReader(MavenSession session, DefaultRepositorySystemS } private void afterSessionStart(MavenSession session) throws MavenExecutionException { - // CHECKSTYLE_OFF: LineLength - for (AbstractMavenLifecycleParticipant listener : - getExtensionComponents(Collections.emptyList(), AbstractMavenLifecycleParticipant.class)) - // CHECKSTYLE_ON: LineLength - { - listener.afterSessionStart(session); - } + callListeners(session, AbstractMavenLifecycleParticipant::afterSessionStart); } private void afterProjectsRead(MavenSession session) throws MavenExecutionException { - ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader(); - try { - // CHECKSTYLE_OFF: LineLength - for (AbstractMavenLifecycleParticipant listener : - getExtensionComponents(session.getProjects(), AbstractMavenLifecycleParticipant.class)) - // CHECKSTYLE_ON: LineLength - { - Thread.currentThread().setContextClassLoader(listener.getClass().getClassLoader()); - - listener.afterProjectsRead(session); - } - } finally { - Thread.currentThread().setContextClassLoader(originalClassLoader); - } + callListeners(session, AbstractMavenLifecycleParticipant::afterProjectsRead); } private void afterSessionEnd(Collection projects, MavenSession session) throws MavenExecutionException { + callListeners(session, AbstractMavenLifecycleParticipant::afterSessionEnd); + } + + @FunctionalInterface + interface ListenerMethod { + void run(AbstractMavenLifecycleParticipant listener, MavenSession session) throws MavenExecutionException; + } + + private void callListeners(MavenSession session, ListenerMethod method) throws MavenExecutionException { ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader(); try { // CHECKSTYLE_OFF: LineLength for (AbstractMavenLifecycleParticipant listener : - getExtensionComponents(projects, AbstractMavenLifecycleParticipant.class)) + getExtensionComponents(session.getProjects(), AbstractMavenLifecycleParticipant.class)) // CHECKSTYLE_ON: LineLength { Thread.currentThread().setContextClassLoader(listener.getClass().getClassLoader()); - listener.afterSessionEnd(session); + method.run(listener, session); } } finally { Thread.currentThread().setContextClassLoader(originalClassLoader);