From 898e4e4b3ca57d674759fe608bea2023ca550093 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Tue, 13 Jun 2023 22:45:33 +0200 Subject: [PATCH] Simplify code (#1156) --- .../java/org/apache/maven/DefaultMaven.java | 48 +++++++------------ 1 file changed, 18 insertions(+), 30 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 1b32dfe048..d1312fe0d4 100644 --- a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java +++ b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java @@ -327,7 +327,7 @@ public class DefaultMaven implements Maven { } } finally { try { - afterSessionEnd(session.getProjects(), session); + afterSessionEnd(session); } catch (MavenExecutionException e) { return addExceptionToResult(result, e); } @@ -354,44 +354,29 @@ public class DefaultMaven implements Maven { } 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 { + private void afterSessionEnd(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)) - // CHECKSTYLE_ON: LineLength - { + getExtensionComponents(session.getProjects(), AbstractMavenLifecycleParticipant.class)) { Thread.currentThread().setContextClassLoader(listener.getClass().getClassLoader()); - - listener.afterSessionEnd(session); + method.run(listener, session); } } finally { Thread.currentThread().setContextClassLoader(originalClassLoader); @@ -451,6 +436,9 @@ public class DefaultMaven implements Maven { } protected Collection getProjectScopedExtensionComponents(Collection projects, Class role) { + if (projects == null) { + return Collections.emptyList(); + } Collection foundComponents = new LinkedHashSet<>(); Collection scannedRealms = new HashSet<>();