Skip to content

Commit

Permalink
Simplify code (#1156)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet authored Jun 13, 2023
1 parent 973b1b9 commit 898e4e4
Showing 1 changed file with 17 additions and 29 deletions.
46 changes: 17 additions & 29 deletions maven-core/src/main/java/org/apache/maven/DefaultMaven.java
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ private MavenExecutionResult doExecute(
}
} finally {
try {
afterSessionEnd(session.getProjects(), session);
afterSessionEnd(session);
} catch (MavenExecutionException e) {
return addExceptionToResult(result, e);
}
Expand All @@ -354,44 +354,29 @@ 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());
callListeners(session, AbstractMavenLifecycleParticipant::afterProjectsRead);
}

listener.afterProjectsRead(session);
}
} finally {
Thread.currentThread().setContextClassLoader(originalClassLoader);
}
private void afterSessionEnd(MavenSession session) throws MavenExecutionException {
callListeners(session, AbstractMavenLifecycleParticipant::afterSessionEnd);
}

private void afterSessionEnd(Collection<MavenProject> projects, MavenSession session)
throws MavenExecutionException {
@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);
Expand Down Expand Up @@ -451,6 +436,9 @@ private <T> Collection<T> getExtensionComponents(Collection<MavenProject> projec
}

protected <T> Collection<T> getProjectScopedExtensionComponents(Collection<MavenProject> projects, Class<T> role) {
if (projects == null) {
return Collections.emptyList();
}

Collection<T> foundComponents = new LinkedHashSet<>();
Collection<ClassLoader> scannedRealms = new HashSet<>();
Expand Down

0 comments on commit 898e4e4

Please sign in to comment.