Skip to content

Commit c9bc1df

Browse files
gervaisbfiliphr
authored andcommitted
Allow package-private mapper
Use and make the default constructor accessible to create the mapper instance. fixes mapstruct#1365
1 parent 81f82a5 commit c9bc1df

4 files changed

Lines changed: 66 additions & 6 deletions

File tree

core-common/src/main/java/org/mapstruct/factory/Mappers.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919
package org.mapstruct.factory;
2020

21+
import java.lang.reflect.Constructor;
22+
import java.lang.reflect.InvocationTargetException;
2123
import java.util.ArrayList;
2224
import java.util.List;
2325
import java.util.ServiceLoader;
@@ -78,10 +80,13 @@ public static <T> T getMapper(Class<T> clazz) {
7880
catch ( ClassNotFoundException e ) {
7981
throw new RuntimeException( e );
8082
}
83+
catch ( NoSuchMethodException e) {
84+
throw new RuntimeException( e );
85+
}
8186
}
8287

83-
private static <T> T getMapper(
84-
Class<T> mapperType, Iterable<ClassLoader> classLoaders) throws ClassNotFoundException {
88+
private static <T> T getMapper(Class<T> mapperType, Iterable<ClassLoader> classLoaders)
89+
throws ClassNotFoundException, NoSuchMethodException {
8590

8691
for ( ClassLoader classLoader : classLoaders ) {
8792
T mapper = doGetMapper( mapperType, classLoader );
@@ -93,11 +98,13 @@ private static <T> T getMapper(
9398
throw new ClassNotFoundException("Cannot find implementation for " + mapperType.getName() );
9499
}
95100

96-
private static <T> T doGetMapper(Class<T> clazz, ClassLoader classLoader) {
101+
private static <T> T doGetMapper(Class<T> clazz, ClassLoader classLoader) throws NoSuchMethodException {
97102
try {
98-
@SuppressWarnings("unchecked")
99-
T mapper = (T) classLoader.loadClass( clazz.getName() + IMPLEMENTATION_SUFFIX ).newInstance();
100-
return mapper;
103+
@SuppressWarnings( "unchecked" )
104+
Class<T> implementation = (Class<T>) classLoader.loadClass( clazz.getName() + IMPLEMENTATION_SUFFIX );
105+
Constructor<T> constructor = implementation.getDeclaredConstructor();
106+
constructor.setAccessible( true );
107+
return constructor.newInstance();
101108
}
102109
catch (ClassNotFoundException e) {
103110
ServiceLoader<T> loader = ServiceLoader.load( clazz, classLoader );
@@ -118,5 +125,8 @@ private static <T> T doGetMapper(Class<T> clazz, ClassLoader classLoader) {
118125
catch (IllegalAccessException e) {
119126
throw new RuntimeException( e );
120127
}
128+
catch (InvocationTargetException e) {
129+
throw new RuntimeException( e );
130+
}
121131
}
122132
}

core-common/src/test/java/org/mapstruct/factory/MappersTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,9 @@ public void findsNestedMapperImpl() throws Exception {
4747
assertThat( Mappers.getMapper( SomeClass.Foo.class ) ).isNotNull();
4848
assertThat( Mappers.getMapper( SomeClass.NestedClass.Foo.class ) ).isNotNull();
4949
}
50+
51+
@Test
52+
public void shouldReturnPackagePrivateImplementationInstance() {
53+
assertThat( Mappers.getMapper( PackagePrivateMapper.class ) ).isNotNull();
54+
}
5055
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.factory;
20+
21+
interface PackagePrivateMapper {
22+
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.factory;
20+
21+
class PackagePrivateMapperImpl implements PackagePrivateMapper {
22+
}

0 commit comments

Comments
 (0)