Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MNG-7678] Settings (v3) and Settings.Builder() cannot unset a non-null field #983

Merged
merged 2 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,11 @@ private static class MapN<K, V> extends AbstractImmutableMap<K, V> {
private final Object[] entries;

private MapN(Map<K, V> map) {
entries = map != null ? map.entrySet().toArray() : new Object[0];
entries = map != null
? map.entrySet().stream()
.map(e -> new SimpleImmutableEntry<>(e.getKey(), e.getValue()))
.toArray()
: new Object[0];
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,11 @@ private static class MapN<K, V> extends AbstractImmutableMap<K, V> {
private final Object[] entries;

private MapN(Map<K, V> map) {
entries = map != null ? map.entrySet().toArray() : new Object[0];
entries = map != null
? map.entrySet().stream()
.map(e -> new SimpleImmutableEntry<>(e.getKey(), e.getValue()))
.toArray()
: new Object[0];
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.api.settings;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

public class SettingsTest {

@Test
void testSetLocalRepository() {
Settings s = Settings.newInstance();

s = s.withLocalRepository("xxx");
assertEquals("xxx", s.getLocalRepository());

s = s.withLocalRepository("yyy");
assertEquals("yyy", s.getLocalRepository());

s = s.withLocalRepository(null);
assertNull(s.getLocalRepository());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,11 @@ private static class MapN<K, V> extends AbstractImmutableMap<K, V> {
private final Object[] entries;

private MapN(Map<K, V> map) {
entries = map != null ? map.entrySet().toArray() : new Object[0];
entries = map != null
? map.entrySet().stream()
.map(e -> new SimpleImmutableEntry<>(e.getKey(), e.getValue()))
.toArray()
: new Object[0];
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,11 @@ private static class MapN<K, V> extends AbstractImmutableMap<K, V> {
private final Object[] entries;

private MapN(Map<K, V> map) {
entries = map != null ? map.entrySet().toArray() : new Object[0];
entries = map != null
? map.entrySet().stream()
.map(e -> new SimpleImmutableEntry<>(e.getKey(), e.getValue()))
.toArray()
: new Object[0];
}

@Override
Expand Down
71 changes: 11 additions & 60 deletions src/mdo/model.vm
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,7 @@ public class ${class.name}
#end
#if ( $locationTracking )
#if ( ! $class.superClass )
/** Location of the xml element for this object. */
final InputLocation location;
#end
#foreach ( $field in $class.getFields($version) )
/** Location of the xml element for the field ${field.name}. */
final InputLocation ${field.name}Location;
#end
#if ( ! $class.superClass )
/** Other locations */
/** Locations */
final Map<Object, InputLocation> locations;
#end
#end
Expand All @@ -155,13 +147,7 @@ public class ${class.name}
$type $field.name${sep}
#end
#if ( $locationTracking )
Map<Object, InputLocation> locations,
#set ( $sep = "#if(${allFields.size()}>0),#end" )
InputLocation location${sep}
#foreach ( $field in $allFields )
#set ( $sep = "#if(${locationTracking}&&$field!=${allFields[${allFields.size()} - 1]}),#end" )
InputLocation ${field.name}Location${sep}
#end
Map<Object, InputLocation> locations
#end
)
{
Expand All @@ -172,13 +158,7 @@ public class ${class.name}
${field.name}${sep}
#end
#if ( $locationTracking )
locations,
#set ( $sep = "#if(${inheritedFields.size()}>0),#end" )
location${sep}
#foreach ( $field in $inheritedFields )
#set ( $sep = "#if(${locationTracking}&&$field!=${inheritedFields[${inheritedFields.size()} - 1]}),#end" )
${field.name}Location${sep}
#end
locations
#end
);
#end
Expand All @@ -195,10 +175,6 @@ public class ${class.name}
#if ( $locationTracking )
#if ( ! $class.superClass )
this.locations = ImmutableCollections.copy( locations );
this.location = location;
#end
#foreach ( $field in $class.getFields($version) )
this.${field.name}Location = ${field.name}Location;
#end
#end
}
Expand Down Expand Up @@ -258,31 +234,13 @@ public class ${class.name}
}

#end
#if ( $locationTracking )
#if ( $locationTracking && !$class.superClass )
/**
* Gets the location of the specified field in the input source.
*/
public InputLocation getLocation( Object key )
{
if ( key instanceof String )
{
switch ( ( String ) key )
{
#if ( ! $class.superClass )
case "":
return location;
#end
#foreach ( $field in $class.getFields($version) )
case "${field.name}":
return ${field.name}Location;
#end
}
}
#if ( $class.superClass )
return super.getLocation( key );
#else
return locations != null ? locations.get( key ) : null;
#end
}

#end
Expand Down Expand Up @@ -311,7 +269,7 @@ public class ${class.name}
@Nonnull
public ${class.name} with${cap}( $type $field.name )
{
return with().${field.name}( $field.name ).build();
return newBuilder(this, true).${field.name}( $field.name ).build();
}
#end

Expand Down Expand Up @@ -453,6 +411,9 @@ public class ${class.name}
{
#foreach ( $field in $class.getFields($version) )
this.${field.name} = base.${field.name};
#end
#if ( $locationTracking )
this.locations = base.locations;
#end
}
else
Expand Down Expand Up @@ -490,9 +451,9 @@ public class ${class.name}
{
if ( location != null )
{
if ( this.locations == null )
if ( !(this.locations instanceof HashMap) )
{
this.locations = new HashMap<>();
this.locations = this.locations != null ? new HashMap<>( this.locations ) : new HashMap<>();
}
this.locations.put( key, location );
}
Expand Down Expand Up @@ -520,10 +481,6 @@ public class ${class.name}
if ( this.locations != null )
{
locations = this.locations;
location = locations.remove( "" );
#foreach ( $field in $allFields )
${field.name}Location = locations.remove( "${field.name}" );
#end
}
#end
return new ${class.name}(
Expand All @@ -539,13 +496,7 @@ public class ${class.name}
#end
#end
#if ( $locationTracking )
locations != null ? locations : ( base != null ? base.locations : null ),
#set ( $sep = "#if(${allFields.size()}>0),#end" )
location != null ? location : ( base != null ? base.location : null )${sep}
#foreach ( $field in $allFields )
#set ( $sep = "#if(${locationTracking}&&$field!=${allFields[${allFields.size()} - 1]}),#end" )
${field.name}Location != null ? ${field.name}Location : ( base != null ? base.${field.name}Location : null )${sep}
#end
locations != null ? locations : ( base != null ? base.locations : null )
#end
);
}
Expand Down