Skip to content

Commit

Permalink
Simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Jun 12, 2023
1 parent 56ee6a8 commit 35a9d54
Showing 1 changed file with 13 additions and 24 deletions.
37 changes: 13 additions & 24 deletions maven-core/src/main/java/org/apache/maven/DefaultMaven.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<MavenProject> 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);
Expand Down

0 comments on commit 35a9d54

Please sign in to comment.