[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 class DefaultArtifact
return Collections.emptyList();
}
return metadataMap.values();
return Collections.unmodifiableCollection( metadataMap.values() );
}
// ----------------------------------------------------------------------

View File

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

View File

@ -256,7 +256,7 @@ public class DefaultArtifactRepository
{
if ( mirroredRepositories != null )
{
this.mirroredRepositories = mirroredRepositories;
this.mirroredRepositories = Collections.unmodifiableList( mirroredRepositories );
}
else
{

View File

@ -118,7 +118,10 @@ public class MetadataResolutionResult
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 )
@ -148,7 +151,10 @@ public class MetadataResolutionResult
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 class MetadataResolutionResult
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 class MetadataResolutionResult
public List<ArtifactResolutionException> getMetadataResolutionExceptions()
{
return metadataResolutionExceptions == null ? Collections.<ArtifactResolutionException>emptyList()
: metadataResolutionExceptions;
return metadataResolutionExceptions == null
? Collections.<ArtifactResolutionException>emptyList()
: Collections.unmodifiableList( metadataResolutionExceptions );
}
// ------------------------------------------------------------------------
@ -246,7 +257,7 @@ public class MetadataResolutionResult
return Collections.emptyList();
}
return errorArtifactExceptions;
return Collections.unmodifiableList( errorArtifactExceptions );
}
// ------------------------------------------------------------------------
@ -283,7 +294,7 @@ public class MetadataResolutionResult
return Collections.emptyList();
}
return circularDependencyExceptions;
return Collections.unmodifiableList( circularDependencyExceptions );
}
// ------------------------------------------------------------------------
@ -297,7 +308,7 @@ public class MetadataResolutionResult
return Collections.emptyList();
}
return repositories;
return Collections.unmodifiableList( repositories );
}
public MetadataResolutionResult setRepositories( final List<ArtifactRepository> repositories )

View File

@ -405,7 +405,7 @@ public class MavenArtifactRepository
{
if ( mirroredRepositories != null )
{
this.mirroredRepositories = mirroredRepositories;
this.mirroredRepositories = Collections.unmodifiableList( mirroredRepositories );
}
else
{

View File

@ -130,7 +130,10 @@ public class ArtifactResolutionResult
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 )
@ -165,7 +168,10 @@ public class ArtifactResolutionResult
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 class ArtifactResolutionResult
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 class ArtifactResolutionResult
public List<ArtifactResolutionException> getMetadataResolutionExceptions()
{
return metadataResolutionExceptions == null ? Collections.<ArtifactResolutionException>emptyList()
: metadataResolutionExceptions;
return metadataResolutionExceptions == null
? Collections.<ArtifactResolutionException>emptyList()
: Collections.unmodifiableList( metadataResolutionExceptions );
}
// ------------------------------------------------------------------------
@ -267,7 +278,7 @@ public class ArtifactResolutionResult
return Collections.emptyList();
}
return errorArtifactExceptions;
return Collections.unmodifiableList( errorArtifactExceptions );
}
// ------------------------------------------------------------------------
@ -304,7 +315,7 @@ public class ArtifactResolutionResult
return Collections.emptyList();
}
return circularDependencyExceptions;
return Collections.unmodifiableList( circularDependencyExceptions );
}
// ------------------------------------------------------------------------
@ -318,7 +329,7 @@ public class ArtifactResolutionResult
return Collections.emptyList();
}
return repositories;
return Collections.unmodifiableList( repositories );
}
public ArtifactResolutionResult setRepositories( final List<ArtifactRepository> repositories )

View File

@ -102,6 +102,7 @@ public class ResolutionNode
children.add( new ResolutionNode( a, remoteRepositories, this ) );
}
children = Collections.unmodifiableList( children );
}
else
{

View File

@ -53,7 +53,10 @@ public class ExceptionSummary
this.exception = exception;
this.message = ( message != null ) ? message : "";
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()

View File

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

View File

@ -105,32 +105,34 @@ public class MojoExecutor
private Collection<String> toScopes( String classpath )
{
Collection<String> scopes = Collections.emptyList();
if ( StringUtils.isNotEmpty( 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 ) )
{
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 ) )
{
return Arrays.asList( Artifact.SCOPE_COMPILE, Artifact.SCOPE_SYSTEM, Artifact.SCOPE_PROVIDED,
Artifact.SCOPE_RUNTIME );
scopes = Arrays.asList( Artifact.SCOPE_COMPILE, Artifact.SCOPE_SYSTEM, Artifact.SCOPE_PROVIDED,
Artifact.SCOPE_RUNTIME );
}
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 ) )
{
return Arrays.asList( Artifact.SCOPE_COMPILE, Artifact.SCOPE_SYSTEM, Artifact.SCOPE_PROVIDED,
Artifact.SCOPE_RUNTIME, Artifact.SCOPE_TEST );
scopes = Arrays.asList( Artifact.SCOPE_COMPILE, Artifact.SCOPE_SYSTEM, Artifact.SCOPE_PROVIDED,
Artifact.SCOPE_RUNTIME, Artifact.SCOPE_TEST );
}
}
return Collections.emptyList();
return Collections.unmodifiableCollection( scopes );
}
public void execute( MavenSession session, List<MojoExecution> mojoExecutions, ProjectIndex projectIndex )

View File

@ -461,7 +461,7 @@ public class DefaultMavenPluginManager
it.remove();
}
}
return artifacts;
return Collections.unmodifiableList( artifacts );
}
private Map<String, ClassLoader> calcImports( MavenProject project, ClassLoader parent, List<String> imports )

View File

@ -100,7 +100,7 @@ public class DefaultPluginPrefixRequest
{
if ( pluginGroups != null )
{
this.pluginGroups = pluginGroups;
this.pluginGroups = Collections.unmodifiableList( pluginGroups );
}
else
{
@ -131,7 +131,7 @@ public class DefaultPluginPrefixRequest
{
if ( repositories != null )
{
this.repositories = repositories;
this.repositories = Collections.unmodifiableList( repositories );
}
else
{

View File

@ -140,7 +140,7 @@ public class DefaultPluginVersionRequest
{
if ( repositories != null )
{
this.repositories = repositories;
this.repositories = Collections.unmodifiableList( repositories );
}
else
{

View File

@ -98,7 +98,10 @@ class DefaultDependencyResolutionResult
public List<Exception> getResolutionErrors( Dependency 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 )

View File

@ -809,6 +809,7 @@ public class DefaultProjectBuilder
map.put( d.getManagementKey(), artifact );
}
}
map = Collections.unmodifiableMap( map );
}
else
{

View File

@ -51,7 +51,9 @@ public class DefaultProjectRealmCache
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();
}

View File

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

View File

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

View File

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

View File

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