[MNG-6164] Collections inconsistently immutable

Make non-empty collections returned immutable just like those returned by
java.util.Collections.
This commit is contained in:
Christian Schulte 2015-12-14 04:57:47 +01:00 committed by Michael Osipov
parent 27864904fe
commit 44826ab446
20 changed files with 91 additions and 51 deletions

View File

@ -273,7 +273,7 @@ public Collection<ArtifactMetadata> getMetadataList()
return Collections.emptyList(); return Collections.emptyList();
} }
return metadataMap.values(); return Collections.unmodifiableCollection( metadataMap.values() );
} }
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------

View File

@ -262,7 +262,7 @@ public VersionRange restrict( VersionRange restriction )
} }
else else
{ {
restrictions = intersection( r1, r2 ); restrictions = Collections.unmodifiableList( intersection( r1, r2 ) );
} }
ArtifactVersion version = null; ArtifactVersion version = null;

View File

@ -256,7 +256,7 @@ public void setMirroredRepositories( List<ArtifactRepository> mirroredRepositori
{ {
if ( mirroredRepositories != null ) if ( mirroredRepositories != null )
{ {
this.mirroredRepositories = mirroredRepositories; this.mirroredRepositories = Collections.unmodifiableList( mirroredRepositories );
} }
else else
{ {

View File

@ -118,7 +118,10 @@ public boolean hasMissingArtifacts()
public List<Artifact> getMissingArtifacts() public List<Artifact> getMissingArtifacts()
{ {
return missingArtifacts == null ? Collections.<Artifact>emptyList() : missingArtifacts; return missingArtifacts == null
? Collections.<Artifact>emptyList()
: Collections.unmodifiableList( missingArtifacts );
} }
public MetadataResolutionResult addMissingArtifact( Artifact artifact ) public MetadataResolutionResult addMissingArtifact( Artifact artifact )
@ -148,7 +151,10 @@ public boolean hasExceptions()
public List<Exception> getExceptions() public List<Exception> getExceptions()
{ {
return exceptions == null ? Collections.<Exception>emptyList() : exceptions; return exceptions == null
? Collections.<Exception>emptyList()
: Collections.unmodifiableList( exceptions );
} }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
@ -185,7 +191,10 @@ public OverConstrainedVersionException getVersionRangeViolation( int i )
public List<Exception> getVersionRangeViolations() public List<Exception> getVersionRangeViolations()
{ {
return versionRangeViolations == null ? Collections.<Exception>emptyList() : versionRangeViolations; return versionRangeViolations == null
? Collections.<Exception>emptyList()
: Collections.unmodifiableList( versionRangeViolations );
} }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
@ -217,8 +226,10 @@ public ArtifactResolutionException getMetadataResolutionException( int i )
public List<ArtifactResolutionException> getMetadataResolutionExceptions() public List<ArtifactResolutionException> getMetadataResolutionExceptions()
{ {
return metadataResolutionExceptions == null ? Collections.<ArtifactResolutionException>emptyList() return metadataResolutionExceptions == null
: metadataResolutionExceptions; ? Collections.<ArtifactResolutionException>emptyList()
: Collections.unmodifiableList( metadataResolutionExceptions );
} }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
@ -246,7 +257,7 @@ public List<ArtifactResolutionException> getErrorArtifactExceptions()
return Collections.emptyList(); return Collections.emptyList();
} }
return errorArtifactExceptions; return Collections.unmodifiableList( errorArtifactExceptions );
} }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
@ -283,7 +294,7 @@ public List<CyclicDependencyException> getCircularDependencyExceptions()
return Collections.emptyList(); return Collections.emptyList();
} }
return circularDependencyExceptions; return Collections.unmodifiableList( circularDependencyExceptions );
} }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
@ -297,7 +308,7 @@ public List<ArtifactRepository> getRepositories()
return Collections.emptyList(); return Collections.emptyList();
} }
return repositories; return Collections.unmodifiableList( repositories );
} }
public MetadataResolutionResult setRepositories( final List<ArtifactRepository> repositories ) public MetadataResolutionResult setRepositories( final List<ArtifactRepository> repositories )

View File

@ -405,7 +405,7 @@ public void setMirroredRepositories( List<ArtifactRepository> mirroredRepositori
{ {
if ( mirroredRepositories != null ) if ( mirroredRepositories != null )
{ {
this.mirroredRepositories = mirroredRepositories; this.mirroredRepositories = Collections.unmodifiableList( mirroredRepositories );
} }
else else
{ {

View File

@ -130,7 +130,10 @@ public boolean hasMissingArtifacts()
public List<Artifact> getMissingArtifacts() public List<Artifact> getMissingArtifacts()
{ {
return missingArtifacts == null ? Collections.<Artifact>emptyList() : missingArtifacts; return missingArtifacts == null
? Collections.<Artifact>emptyList()
: Collections.unmodifiableList( missingArtifacts );
} }
public ArtifactResolutionResult addMissingArtifact( Artifact artifact ) public ArtifactResolutionResult addMissingArtifact( Artifact artifact )
@ -165,7 +168,10 @@ public boolean hasExceptions()
public List<Exception> getExceptions() public List<Exception> getExceptions()
{ {
return exceptions == null ? Collections.<Exception>emptyList() : exceptions; return exceptions == null
? Collections.<Exception>emptyList()
: Collections.unmodifiableList( exceptions );
} }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
@ -202,7 +208,10 @@ public OverConstrainedVersionException getVersionRangeViolation( int i )
public List<Exception> getVersionRangeViolations() public List<Exception> getVersionRangeViolations()
{ {
return versionRangeViolations == null ? Collections.<Exception>emptyList() : versionRangeViolations; return versionRangeViolations == null
? Collections.<Exception>emptyList()
: Collections.unmodifiableList( versionRangeViolations );
} }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
@ -234,8 +243,10 @@ public ArtifactResolutionException getMetadataResolutionException( int i )
public List<ArtifactResolutionException> getMetadataResolutionExceptions() public List<ArtifactResolutionException> getMetadataResolutionExceptions()
{ {
return metadataResolutionExceptions == null ? Collections.<ArtifactResolutionException>emptyList() return metadataResolutionExceptions == null
: metadataResolutionExceptions; ? Collections.<ArtifactResolutionException>emptyList()
: Collections.unmodifiableList( metadataResolutionExceptions );
} }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
@ -267,7 +278,7 @@ public List<ArtifactResolutionException> getErrorArtifactExceptions()
return Collections.emptyList(); return Collections.emptyList();
} }
return errorArtifactExceptions; return Collections.unmodifiableList( errorArtifactExceptions );
} }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
@ -304,7 +315,7 @@ public List<CyclicDependencyException> getCircularDependencyExceptions()
return Collections.emptyList(); return Collections.emptyList();
} }
return circularDependencyExceptions; return Collections.unmodifiableList( circularDependencyExceptions );
} }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
@ -318,7 +329,7 @@ public List<ArtifactRepository> getRepositories()
return Collections.emptyList(); return Collections.emptyList();
} }
return repositories; return Collections.unmodifiableList( repositories );
} }
public ArtifactResolutionResult setRepositories( final List<ArtifactRepository> repositories ) public ArtifactResolutionResult setRepositories( final List<ArtifactRepository> repositories )

View File

@ -102,6 +102,7 @@ public void addDependencies( Set<Artifact> artifacts, List<ArtifactRepository> r
children.add( new ResolutionNode( a, remoteRepositories, this ) ); children.add( new ResolutionNode( a, remoteRepositories, this ) );
} }
children = Collections.unmodifiableList( children );
} }
else else
{ {

View File

@ -53,7 +53,10 @@ public ExceptionSummary( Throwable exception, String message, String reference,
this.exception = exception; this.exception = exception;
this.message = ( message != null ) ? message : ""; this.message = ( message != null ) ? message : "";
this.reference = ( reference != null ) ? reference : ""; this.reference = ( reference != null ) ? reference : "";
this.children = ( children != null ) ? children : Collections.<ExceptionSummary>emptyList(); this.children = ( children != null )
? Collections.unmodifiableList( children )
: Collections.<ExceptionSummary>emptyList();
} }
public Throwable getException() public Throwable getException()

View File

@ -64,8 +64,10 @@ public MavenExecutionResult setTopologicallySortedProjects( List<MavenProject> t
public List<MavenProject> getTopologicallySortedProjects() public List<MavenProject> getTopologicallySortedProjects()
{ {
return null == topologicallySortedProjects ? Collections.<MavenProject>emptyList() return null == topologicallySortedProjects
: topologicallySortedProjects; ? Collections.<MavenProject>emptyList()
: Collections.unmodifiableList( topologicallySortedProjects );
} }
public DependencyResolutionResult getDependencyResolutionResult() public DependencyResolutionResult getDependencyResolutionResult()

View File

@ -105,32 +105,34 @@ private void collectDependencyRequirements( Set<String> scopesToResolve, Set<Str
private Collection<String> toScopes( String classpath ) private Collection<String> toScopes( String classpath )
{ {
Collection<String> scopes = Collections.emptyList();
if ( StringUtils.isNotEmpty( classpath ) ) if ( StringUtils.isNotEmpty( classpath ) )
{ {
if ( Artifact.SCOPE_COMPILE.equals( classpath ) ) if ( Artifact.SCOPE_COMPILE.equals( classpath ) )
{ {
return Arrays.asList( Artifact.SCOPE_COMPILE, Artifact.SCOPE_SYSTEM, Artifact.SCOPE_PROVIDED ); scopes = Arrays.asList( Artifact.SCOPE_COMPILE, Artifact.SCOPE_SYSTEM, Artifact.SCOPE_PROVIDED );
} }
else if ( Artifact.SCOPE_RUNTIME.equals( classpath ) ) else if ( Artifact.SCOPE_RUNTIME.equals( classpath ) )
{ {
return Arrays.asList( Artifact.SCOPE_COMPILE, Artifact.SCOPE_RUNTIME ); scopes = Arrays.asList( Artifact.SCOPE_COMPILE, Artifact.SCOPE_RUNTIME );
} }
else if ( Artifact.SCOPE_COMPILE_PLUS_RUNTIME.equals( classpath ) ) else if ( Artifact.SCOPE_COMPILE_PLUS_RUNTIME.equals( classpath ) )
{ {
return Arrays.asList( Artifact.SCOPE_COMPILE, Artifact.SCOPE_SYSTEM, Artifact.SCOPE_PROVIDED, scopes = Arrays.asList( Artifact.SCOPE_COMPILE, Artifact.SCOPE_SYSTEM, Artifact.SCOPE_PROVIDED,
Artifact.SCOPE_RUNTIME ); Artifact.SCOPE_RUNTIME );
} }
else if ( Artifact.SCOPE_RUNTIME_PLUS_SYSTEM.equals( classpath ) ) else if ( Artifact.SCOPE_RUNTIME_PLUS_SYSTEM.equals( classpath ) )
{ {
return Arrays.asList( Artifact.SCOPE_COMPILE, Artifact.SCOPE_SYSTEM, Artifact.SCOPE_RUNTIME ); scopes = Arrays.asList( Artifact.SCOPE_COMPILE, Artifact.SCOPE_SYSTEM, Artifact.SCOPE_RUNTIME );
} }
else if ( Artifact.SCOPE_TEST.equals( classpath ) ) else if ( Artifact.SCOPE_TEST.equals( classpath ) )
{ {
return Arrays.asList( Artifact.SCOPE_COMPILE, Artifact.SCOPE_SYSTEM, Artifact.SCOPE_PROVIDED, scopes = Arrays.asList( Artifact.SCOPE_COMPILE, Artifact.SCOPE_SYSTEM, Artifact.SCOPE_PROVIDED,
Artifact.SCOPE_RUNTIME, Artifact.SCOPE_TEST ); Artifact.SCOPE_RUNTIME, Artifact.SCOPE_TEST );
} }
} }
return Collections.emptyList(); return Collections.unmodifiableCollection( scopes );
} }
public void execute( MavenSession session, List<MojoExecution> mojoExecutions, ProjectIndex projectIndex ) public void execute( MavenSession session, List<MojoExecution> mojoExecutions, ProjectIndex projectIndex )

View File

@ -461,7 +461,7 @@ private List<Artifact> toMavenArtifacts( DependencyNode root, PreorderNodeListGe
it.remove(); it.remove();
} }
} }
return artifacts; return Collections.unmodifiableList( artifacts );
} }
private Map<String, ClassLoader> calcImports( MavenProject project, ClassLoader parent, List<String> imports ) private Map<String, ClassLoader> calcImports( MavenProject project, ClassLoader parent, List<String> imports )

View File

@ -100,7 +100,7 @@ public DefaultPluginPrefixRequest setPluginGroups( List<String> pluginGroups )
{ {
if ( pluginGroups != null ) if ( pluginGroups != null )
{ {
this.pluginGroups = pluginGroups; this.pluginGroups = Collections.unmodifiableList( pluginGroups );
} }
else else
{ {
@ -131,7 +131,7 @@ public DefaultPluginPrefixRequest setRepositories( List<RemoteRepository> reposi
{ {
if ( repositories != null ) if ( repositories != null )
{ {
this.repositories = repositories; this.repositories = Collections.unmodifiableList( repositories );
} }
else else
{ {

View File

@ -140,7 +140,7 @@ public DefaultPluginVersionRequest setRepositories( List<RemoteRepository> repos
{ {
if ( repositories != null ) if ( repositories != null )
{ {
this.repositories = repositories; this.repositories = Collections.unmodifiableList( repositories );
} }
else else
{ {

View File

@ -98,7 +98,10 @@ public void setCollectionErrors( List<Exception> exceptions )
public List<Exception> getResolutionErrors( Dependency dependency ) public List<Exception> getResolutionErrors( Dependency dependency )
{ {
List<Exception> errors = resolutionErrors.get( dependency ); List<Exception> errors = resolutionErrors.get( dependency );
return ( errors != null ) ? errors : Collections.<Exception>emptyList(); return ( errors != null )
? Collections.unmodifiableList( errors )
: Collections.<Exception>emptyList();
} }
public void setResolutionErrors( Dependency dependency, List<Exception> errors ) public void setResolutionErrors( Dependency dependency, List<Exception> errors )

View File

@ -809,6 +809,7 @@ private void initProject( MavenProject project, Map<String, MavenProject> projec
map.put( d.getManagementKey(), artifact ); map.put( d.getManagementKey(), artifact );
} }
} }
map = Collections.unmodifiableMap( map );
} }
else else
{ {

View File

@ -51,7 +51,9 @@ protected static class CacheKey
public CacheKey( List<? extends ClassRealm> extensionRealms ) public CacheKey( List<? extends ClassRealm> extensionRealms )
{ {
this.extensionRealms = ( extensionRealms != null ) ? extensionRealms : Collections.<ClassRealm>emptyList(); this.extensionRealms = ( extensionRealms != null )
? Collections.unmodifiableList( extensionRealms )
: Collections.<ClassRealm>emptyList();
this.hashCode = this.extensionRealms.hashCode(); this.hashCode = this.extensionRealms.hashCode();
} }

View File

@ -778,7 +778,7 @@ public List<Plugin> getBuildPlugins()
{ {
return Collections.emptyList(); return Collections.emptyList();
} }
return getModel().getBuild().getPlugins(); return Collections.unmodifiableList( getModel().getBuild().getPlugins() );
} }
public List<String> getModules() public List<String> getModules()
@ -1079,7 +1079,7 @@ public List<Extension> getBuildExtensions()
} }
else else
{ {
return build.getExtensions(); return Collections.unmodifiableList( build.getExtensions() );
} }
} }
@ -1604,7 +1604,7 @@ public List<Dependency> getCompileDependencies()
{ {
// TODO let the scope handler deal with this // TODO let the scope handler deal with this
if ( Artifact.SCOPE_COMPILE.equals( a.getScope() ) || Artifact.SCOPE_PROVIDED.equals( a.getScope() ) if ( Artifact.SCOPE_COMPILE.equals( a.getScope() ) || Artifact.SCOPE_PROVIDED.equals( a.getScope() )
|| Artifact.SCOPE_SYSTEM.equals( a.getScope() ) ) || Artifact.SCOPE_SYSTEM.equals( a.getScope() ) )
{ {
Dependency dependency = new Dependency(); Dependency dependency = new Dependency();
@ -1618,7 +1618,7 @@ public List<Dependency> getCompileDependencies()
list.add( dependency ); list.add( dependency );
} }
} }
return list; return Collections.unmodifiableList( list );
} }
@Deprecated @Deprecated
@ -1662,7 +1662,7 @@ public List<Dependency> getTestDependencies()
list.add( dependency ); list.add( dependency );
} }
return list; return Collections.unmodifiableList( list );
} }
@Deprecated // used by the Maven ITs @Deprecated // used by the Maven ITs
@ -1677,7 +1677,7 @@ public List<Dependency> getRuntimeDependencies()
List<Dependency> list = new ArrayList<>( artifacts.size() ); List<Dependency> list = new ArrayList<>( artifacts.size() );
for ( Artifact a : getArtifacts() ) for ( Artifact a : getArtifacts() )
{ {
// TODO let the scope handler deal with this // TODO let the scope handler deal with this
if ( Artifact.SCOPE_COMPILE.equals( a.getScope() ) || Artifact.SCOPE_RUNTIME.equals( a.getScope() ) ) if ( Artifact.SCOPE_COMPILE.equals( a.getScope() ) || Artifact.SCOPE_RUNTIME.equals( a.getScope() ) )
@ -1694,7 +1694,7 @@ public List<Dependency> getRuntimeDependencies()
list.add( dependency ); list.add( dependency );
} }
} }
return list; return Collections.unmodifiableList( list );
} }
@Deprecated @Deprecated
@ -1790,7 +1790,7 @@ public List<Dependency> getSystemDependencies()
list.add( dependency ); list.add( dependency );
} }
} }
return list; return Collections.unmodifiableList( list );
} }
@Deprecated @Deprecated
@ -1862,8 +1862,7 @@ public List<ReportPlugin> getReportPlugins()
{ {
return Collections.emptyList(); return Collections.emptyList();
} }
return getModel().getReporting().getPlugins(); return Collections.unmodifiableList( getModel().getReporting().getPlugins() );
} }
@Deprecated @Deprecated

View File

@ -58,8 +58,11 @@ public List<Dependency> getDependencies()
public List<Dependency> getManagedDependencies() public List<Dependency> getManagedDependencies()
{ {
DependencyManagement depMgmt = project.getDependencyManagement(); DependencyManagement depMngt = project.getDependencyManagement();
return ( depMgmt != null ) ? depMgmt.getDependencies() : Collections.<Dependency>emptyList(); return ( depMngt != null )
? Collections.unmodifiableList( depMngt.getDependencies() )
: Collections.<Dependency>emptyList();
} }
static class PomArtifactHandler static class PomArtifactHandler

View File

@ -750,7 +750,9 @@ protected void configure()
BootstrapCoreExtensionManager resolver = container.lookup( BootstrapCoreExtensionManager.class ); BootstrapCoreExtensionManager resolver = container.lookup( BootstrapCoreExtensionManager.class );
return resolver.loadCoreExtensions( request, providedArtifacts, extensions ); return Collections.unmodifiableList( resolver.loadCoreExtensions( request, providedArtifacts,
extensions ) );
} }
finally finally
{ {

View File

@ -136,7 +136,7 @@ public List<ModelProblem> getProblems()
{ {
return Collections.emptyList(); return Collections.emptyList();
} }
return result.getProblems(); return Collections.unmodifiableList( result.getProblems() );
} }
private static String toMessage( ModelBuildingResult result ) private static String toMessage( ModelBuildingResult result )