Skip to content

Commit

Permalink
[maven-release-plugin] prepare for next development iteration
Browse files Browse the repository at this point in the history
Support named empty type map

Add UuidConverter

Add error message while skip conflict

Throw error if skip conflict

Update asm-tree lib version

[maven-release-plugin] prepare release modelmapper-parent-3.1.1

[maven-release-plugin] prepare for next development iteration

add support for Permazen proxies

change configs equals method to check for namingConventions

change != to equals call

add NamingConventions to hashcode of InheritingConfig

Fix the order of actual value and expected value in the test code

update .gitignore file

Bump asm and bytebuddy for java 21 support (modelmapper#729)

[maven-release-plugin] prepare release modelmapper-parent-3.2.0

[maven-release-plugin] prepare for next development iteration

Update CHANGES.md for 3.2.0 release
  • Loading branch information
chhsiao90 authored and WeiWuDavid committed Jan 24, 2024
1 parent 251772e commit 50491be
Show file tree
Hide file tree
Showing 35 changed files with 410 additions and 60 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
build:
strategy:
matrix:
java: [ '8', '11', '17' ]
java: [ '8', '11', '17', '21' ]
os: [ 'ubuntu-latest' ]
runs-on: ${{ matrix.os }}
steps:
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ release.properties
.settings/
_site/
user-manual/
*.iml
*.iml
.idea/
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 3.2.0

* Bump asm and byte-buddy for JDK 21 support

# 3.1.0

* Migrates optional converters from modelmapper-module-java8
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>modelmapper-parent</artifactId>
<groupId>org.modelmapper</groupId>
<version>3.1.0</version>
<version>3.2.1-SNAPSHOT</version>
</parent>

<artifactId>benchmarks</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper-parent</artifactId>
<version>3.1.0</version>
<version>3.2.1-SNAPSHOT</version>
</parent>

<artifactId>modelmapper</artifactId>
Expand Down
16 changes: 16 additions & 0 deletions core/src/main/java/org/modelmapper/ModelMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,22 @@ public <S, D> TypeMap<S, D> emptyTypeMap(Class<S> sourceType, Class<D> destinati
return config.typeMapStore.createEmptyTypeMap(sourceType, destinationType, null, config, engine);
}

/**
* Creates an empty TypeMap for the {@code sourceType}, {@code destinationType}.
*
* @param <S> source type
* @param <D> destination type
* @throws IllegalArgumentException is {@code sourceType} or {@code destinationType} are null, or {@code TypeMap<Source Type, DestinationType}
* already defined in the TypeMapStore
*/
public <S, D> TypeMap<S, D> emptyTypeMap(Class<S> sourceType, Class<D> destinationType, String typeMapName) {
Assert.notNull(sourceType, "sourceType");
Assert.notNull(destinationType, "destinationType");
Assert.notNull(typeMapName, "typeMapName");
Assert.isNull(config.typeMapStore.get(sourceType, destinationType, typeMapName), "TypeMap already defined");
return config.typeMapStore.createEmptyTypeMap(sourceType, destinationType, typeMapName, config, engine);
}

/**
* Returns all TypeMaps for the ModelMapper.
*/
Expand Down
8 changes: 7 additions & 1 deletion core/src/main/java/org/modelmapper/internal/Errors.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.Collections;
import java.util.Formatter;
import java.util.List;

import org.modelmapper.ConfigurationException;
import org.modelmapper.MappingException;
import org.modelmapper.TypeMap;
Expand Down Expand Up @@ -375,6 +374,13 @@ Errors sourceOutsideOfMap() {
return addMessage("'source' cannot be used outside of a map statement.");
}

Errors skipConflict(String skip, List<String> paths) {
return addMessage("Not able to skip %s, because there are already nested properties are mapped: [%s]. "
+ "Do you skip the property after the implicit mappings mapped? "
+ "We recommended you to create an empty type map, and followed by addMappings and implicitMappings calls",
skip, String.join(" ", paths));
}

void throwMappingExceptionIfErrorsExist() {
if (hasErrors())
throw new MappingException(getMessages());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ public boolean equals(Object obj) {
return false;
if (isFieldMatchingEnabled() != other.isFieldMatchingEnabled())
return false;
if (!getSourceNamingConvention().equals(other.getSourceNamingConvention()))
return false;
if (!getDestinationNamingConvention().equals(other.getDestinationNamingConvention()))
return false;
return true;
}

Expand Down Expand Up @@ -283,6 +287,8 @@ public int hashCode() {
result = prime * result + getDestinationNameTransformer().hashCode();
result = prime * result + getFieldAccessLevel().hashCode();
result = prime * result + getMethodAccessLevel().hashCode();
result = prime * result + getSourceNamingConvention().hashCode();
result = prime * result + getDestinationNamingConvention().hashCode();
result = prime * result + (isFieldMatchingEnabled() ? 1231 : 1237);
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
*/
package org.modelmapper.internal;

import static java.util.stream.Collectors.toList;
import static org.modelmapper.internal.ExplicitMappingBuilder.MappingOptions;
import static org.modelmapper.internal.util.Assert.notNull;

import java.util.List;
import net.jodah.typetools.TypeResolver;
import org.modelmapper.builder.ReferenceMapExpression;
import org.modelmapper.internal.util.Primitives;
import org.modelmapper.spi.DestinationSetter;
import org.modelmapper.spi.Mapping;
import org.modelmapper.spi.SourceGetter;

/**
Expand Down Expand Up @@ -62,24 +65,38 @@ class ReferenceMapExpressionImpl<S, D> implements ReferenceMapExpression<S, D> {
public <V> void map(SourceGetter<S> sourceGetter, DestinationSetter<D, V> destinationSetter) {
visitSource(sourceGetter);
visitDestination(destinationSetter);
typeMap.addMapping(collector.collect());
skipMapping(collector.collect());
collector.reset();
}

@Override
public <V> void skip(DestinationSetter<D, V> destinationSetter) {
options.skipType = 1;
visitDestination(destinationSetter);
typeMap.addMapping(collector.collect());
skipMapping(collector.collect());
collector.reset();
}

private void skipMapping(MappingImpl skipMapping) {
String prefix = skipMapping.getPath();
List<String> conflictPaths = typeMap.getMappings().stream()
.map(Mapping::getPath)
.filter(path -> path.startsWith(prefix) && !path.equals(prefix))
.collect(toList());
if (conflictPaths.isEmpty()) {
typeMap.addMapping(skipMapping);
} else {
collector.getErrors().skipConflict(skipMapping.getPath(), conflictPaths);
collector.getErrors().throwConfigurationExceptionIfErrorsExist();
}
}

@Override
public <V> void skip(SourceGetter<S> sourceGetter, DestinationSetter<D, V> destinationSetter) {
options.skipType = 1;
visitSource(sourceGetter);
visitDestination(destinationSetter);
typeMap.addMapping(collector.collect());
skipMapping(collector.collect());
collector.reset();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public final class ConverterStore {
new FromOptionalConverter(),new OptionalConverter(), new ToOptionalConverter(),
new AssignableConverter(), new StringConverter(), new EnumConverter(), new NumberConverter(),
new BooleanConverter(), new CharacterConverter(), new DateConverter(),
new CalendarConverter(),
new CalendarConverter(), new UuidConverter(),
};

private final List<ConditionalConverter<?, ?>> converters;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.modelmapper.internal.converter;

import java.util.UUID;
import org.modelmapper.spi.ConditionalConverter;
import org.modelmapper.spi.MappingContext;

/**
* Converts objects to UUID.
*/
class UuidConverter implements ConditionalConverter<Object, UUID> {

public UUID convert(MappingContext<Object, UUID> context) {
Object source = context.getSource();
if (source == null) {
return null;
}

Class<?> sourceType = context.getSourceType();
if (isCharArray(sourceType)) {
return UUID.fromString(new String((char[]) source));
}
return UUID.fromString(source.toString());
}

public MatchResult match(Class<?> sourceType, Class<?> destinationType) {
boolean destMatch = destinationType == UUID.class;
return destMatch ? isCharArray(sourceType) || sourceType == String.class
? MatchResult.FULL
: MatchResult.PARTIAL
: MatchResult.NONE;
}

private boolean isCharArray(Class<?> sourceType) {
return sourceType.isArray() && (sourceType.getComponentType() == Character.TYPE
|| sourceType.getComponentType() == Character.class);
}
}
2 changes: 2 additions & 0 deletions core/src/main/java/org/modelmapper/internal/util/Types.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public static boolean isProxied(Class<?> type) {
return true;
if (type.getName().contains("$MockitoMock$"))
return true;
if (type.getName().contains("$$Permazen"))
return true;
if (Proxy.isProxyClass(type))
return true;
return isProxiedByJavassist(type);
Expand Down
16 changes: 14 additions & 2 deletions core/src/test/java/org/modelmapper/ModelMapperTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package org.modelmapper;

import static org.testng.Assert.*;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNotSame;
import static org.testng.Assert.assertSame;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

import java.util.Map;

import org.modelmapper.config.Configuration.AccessLevel;
import org.modelmapper.spi.Mapping;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -130,4 +134,12 @@ public void shouldTypeMapCreateOrGet() {
assertNotSame(typeMapWithDifferentName, typeMapWithName);
assertSame(modelMapper.typeMap(Person.class, PersonDTO.class, "bar"), typeMapWithDifferentName);
}

public void shouldCreateNamedEmptyTypeMap() {
TypeMap<Person, PersonDTO> unnamed = modelMapper.emptyTypeMap(Person.class, PersonDTO.class);
TypeMap<Person, PersonDTO> foo = modelMapper.emptyTypeMap(Person.class, PersonDTO.class, "foo");
TypeMap<Person, PersonDTO> bar = modelMapper.emptyTypeMap(Person.class, PersonDTO.class, "bar");
assertNotSame(unnamed, foo);
assertNotSame(foo, bar);
}
}
2 changes: 1 addition & 1 deletion core/src/test/java/org/modelmapper/bugs/GH109.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ protected void configure() {
Source s = new Source();
s.value = Integer.valueOf(42);
Destination dest = modelMapper.map(s, Destination.class);
assertEquals(42, dest.value);
assertEquals(dest.value, 42);
}
}
18 changes: 6 additions & 12 deletions core/src/test/java/org/modelmapper/bugs/GH204.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,8 @@ protected void configure() {

modelMapper.validate();

assertEquals("foo",
modelMapper.map(new SomeDto("foo"), SomeEntity.class).otherValue);
assertEquals("bar",
modelMapper.map(new SomeEntity("bar"), SomeDto.class).someValue);
assertEquals(modelMapper.map(new SomeDto("foo"), SomeEntity.class).otherValue, "foo");
assertEquals(modelMapper.map(new SomeEntity("bar"), SomeDto.class).someValue, "bar");
}

public void shouldMappingMethod2() {
Expand All @@ -86,10 +84,8 @@ protected void configure() {

modelMapper.validate();

assertEquals("foo",
modelMapper.map(new SomeDto("foo"), SomeEntity.class).otherValue);
assertEquals("bar",
modelMapper.map(new SomeEntity("bar"), SomeDto.class).someValue);
assertEquals(modelMapper.map(new SomeDto("foo"), SomeEntity.class).otherValue, "foo");
assertEquals(modelMapper.map(new SomeEntity("bar"), SomeDto.class).someValue, "bar");
}

public void shouldMappingMethod3() {
Expand All @@ -108,9 +104,7 @@ protected void configure() {

modelMapper.validate();

assertEquals("foo",
modelMapper.map(new SomeDto("foo"), SomeEntity.class).otherValue);
assertEquals("bar",
modelMapper.map(new SomeEntity("bar"), SomeDto.class).someValue);
assertEquals(modelMapper.map(new SomeDto("foo"), SomeEntity.class).otherValue, "foo");
assertEquals(modelMapper.map(new SomeEntity("bar"), SomeDto.class).someValue, "bar");
}
}
4 changes: 2 additions & 2 deletions core/src/test/java/org/modelmapper/bugs/GH249.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void shouldMap() {
pojoAgent.setRoles(new HashSet<Role>(Arrays.asList(new Role("foo"), new Role("bar"))));

DomainAgent domainAgent = modelMapper.map(pojoAgent, DomainAgent.class);
assertEquals(2, domainAgent.getRoles().size());
assertEquals(domainAgent.getRoles().size(), 2);
}

public void shouldMapExistDestination() {
Expand All @@ -83,6 +83,6 @@ public void shouldMapExistDestination() {

modelMapper.map(pojoAgent, domainAgent);

assertEquals(2, domainAgent.getRoles().size());
assertEquals(domainAgent.getRoles().size(), 2);
}
}
2 changes: 1 addition & 1 deletion core/src/test/java/org/modelmapper/bugs/GH379.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void shouldMapGeneric() {

Type destinationType = new TypeToken<PageModel<SubjectModel>>(){}.getType();
PageModel<SubjectModel> destination = modelMapper.map(page, destinationType);
assertEquals(2, destination.items.size());
assertEquals(destination.items.size(), 2);
assertEquals(SubjectModel.class, destination.items.get(0).getClass());
}
}
2 changes: 1 addition & 1 deletion core/src/test/java/org/modelmapper/bugs/GH38.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ protected void configure() {
Mockito.when(a.getName()).thenReturn("hello");
B b = modelMapper.map(a, B.class);

Assert.assertEquals("hello", b.getNameProp());
Assert.assertEquals(b.getNameProp(), "hello");
}
}
10 changes: 5 additions & 5 deletions core/src/test/java/org/modelmapper/bugs/GH386.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ public void map() {
OuterDto outerDto = new OuterDto(2L,"dto", Arrays.asList(innerDto));

modelMapper.map(outer, outerDto);
assertEquals(2L, (long) outerDto.id);
assertEquals(1, outerDto.inners.size());
assertEquals(1L, (long) outerDto.inners.get(0).id);
assertEquals("domain", outerDto.inners.get(0).description);
assertEquals("domain", outerDto.inners.get(0).name);
assertEquals((long) outerDto.id, 2L);
assertEquals(outerDto.inners.size(), 1);
assertEquals((long) outerDto.inners.get(0).id, 1L);
assertEquals(outerDto.inners.get(0).description, "domain");
assertEquals(outerDto.inners.get(0).name, "domain");
}
}
Loading

0 comments on commit 50491be

Please sign in to comment.