From 7180d68a18f8b7c18db7424b8755ae399a89b4b5 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Thu, 6 Jan 2022 08:47:54 +0100 Subject: [PATCH] Proposal to change abstract provider Changes: * validate input * do not modify loop invariant within loop body --- .../AbstractLifecycleMappingProvider.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/providers/packaging/AbstractLifecycleMappingProvider.java b/maven-core/src/main/java/org/apache/maven/lifecycle/providers/packaging/AbstractLifecycleMappingProvider.java index f58f9375ba..a1d818830d 100644 --- a/maven-core/src/main/java/org/apache/maven/lifecycle/providers/packaging/AbstractLifecycleMappingProvider.java +++ b/maven-core/src/main/java/org/apache/maven/lifecycle/providers/packaging/AbstractLifecycleMappingProvider.java @@ -29,6 +29,8 @@ import org.apache.maven.lifecycle.mapping.Lifecycle; import org.apache.maven.lifecycle.mapping.LifecycleMapping; import org.apache.maven.lifecycle.mapping.LifecyclePhase; +import static java.util.Objects.requireNonNull; + /** * Base lifecycle mapping provider, ie per-packaging plugin bindings for {@code default} lifecycle. */ @@ -39,11 +41,17 @@ public abstract class AbstractLifecycleMappingProvider protected AbstractLifecycleMappingProvider( String[] pluginBindings ) { - HashMap lifecyclePhases = new HashMap<>(); - int len = pluginBindings.length; - for ( int i = 0; i < len; i++ ) + requireNonNull( pluginBindings ); + final int len = pluginBindings.length; + if ( len < 1 || len % 2 != 0 ) { - lifecyclePhases.put( pluginBindings[i++], new LifecyclePhase( pluginBindings[i] ) ); + throw new IllegalArgumentException( "Plugin bindings must have more than 0, even count of elements" ); + } + + HashMap lifecyclePhases = new HashMap<>( len / 2 ); + for ( int i = 0; i < len; i = i + 2 ) + { + lifecyclePhases.put( pluginBindings[i], new LifecyclePhase( pluginBindings[i + 1] ) ); } Lifecycle lifecycle = new Lifecycle();