Skip to content
Open
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
16 changes: 12 additions & 4 deletions core/src/main/java/org/mapstruct/factory/Mappers.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.ServiceLoader;

Expand Down Expand Up @@ -75,14 +76,21 @@ private static <T> T getMapper(Class<T> mapperType, Iterable<ClassLoader> classL
throw new ClassNotFoundException("Cannot find implementation for " + mapperType.getName() );
}

private static <T> T doGetMapper(Class<T> clazz, ClassLoader classLoader) throws NoSuchMethodException {
private static <T> T doGetMapper(Class<T> clazz, ClassLoader classLoader) {
try {
@SuppressWarnings( "unchecked" )
Class<T> implementation = (Class<T>) classLoader.loadClass( clazz.getName() + IMPLEMENTATION_SUFFIX );
Constructor<T> constructor = implementation.getDeclaredConstructor();
constructor.setAccessible( true );

return constructor.newInstance();
Constructor<?>[] constructors = implementation.getDeclaredConstructors();
Constructor<T> constructor = (Constructor<T>) constructors[0];
constructor.setAccessible(true);
// the parameters are mappers
Class<?>[] parameters = constructor.getParameterTypes();
Object[] args = Arrays.stream(parameters)
.map(paramClass -> doGetMapper(paramClass, classLoader))
.toArray();

return constructor.newInstance(args);
}
catch (ClassNotFoundException e) {
return getMapperFromServiceLoader( clazz, classLoader );
Expand Down
17 changes: 17 additions & 0 deletions core/src/test/java/org/mapstruct/factory/MapperWhichIsUsed.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.mapstruct.factory;

import org.mapstruct.Mapper;

@Mapper
public interface MapperWhichIsUsed {

Foo toModel(FooDto dto);

class Foo {
public String name;
}

class FooDto {
public String name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.mapstruct.factory;

class MapperWhichIsUsedImpl implements MapperWhichIsUsed {

@Override
public Foo toModel(MapperWhichIsUsed.FooDto dto) {
if ( dto == null ) {
return null;
}

Foo foo = new Foo();

return foo;
}
}
40 changes: 40 additions & 0 deletions core/src/test/java/org/mapstruct/factory/MapperWithUses.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.mapstruct.factory;

import org.mapstruct.InjectionStrategy;
import org.mapstruct.Mapper;

@Mapper(
uses = MapperWhichIsUsed.class,
injectionStrategy = InjectionStrategy.CONSTRUCTOR
)
public interface MapperWithUses {
Bar toModel(BarDto dto);

class Bar {
private final String name;
private final MapperWhichIsUsed.Foo foo;

public Bar(String name, MapperWhichIsUsed.Foo foo) {
this.name = name;
this.foo = foo;
}
}

class BarDto {
private final String name;
private final MapperWhichIsUsed.FooDto foo;

BarDto(String name, MapperWhichIsUsed.FooDto foo) {
this.name = name;
this.foo = foo;
}

public String getName() {
return this.name;
}

public MapperWhichIsUsed.FooDto getFoo() {
return this.foo;
}
}
}
27 changes: 27 additions & 0 deletions core/src/test/java/org/mapstruct/factory/MapperWithUsesImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.mapstruct.factory;

public class MapperWithUsesImpl implements MapperWithUses {

private final MapperWhichIsUsed mapperWhichIsUsed;

public MapperWithUsesImpl(MapperWhichIsUsed mapperWhichIsUsed) {
this.mapperWhichIsUsed = mapperWhichIsUsed;
}

@Override
public Bar toModel(BarDto dto) {
if (dto == null) {
return null;
}

String name = null;
MapperWhichIsUsed.Foo foo = null;

name = dto.getName();
foo = mapperWhichIsUsed.toModel(dto.getFoo());

Bar bar = new Bar(name, foo);

return bar;
}
}
5 changes: 5 additions & 0 deletions core/src/test/java/org/mapstruct/factory/MappersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,9 @@ public void shouldReturnPackagePrivateImplementationInstance() {
public void shouldReturnPackagePrivateImplementationClass() {
assertThat( Mappers.getMapperClass( PackagePrivateMapper.class ) ).isNotNull();
}

@Test
public void shouldReturnImplementationClassOfMapperWhichDependsOnAnotherOnes() {
assertThat( Mappers.getMapper( MapperWithUses.class ) ).isNotNull();
}
}