mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
Allow package-private mapper
Use and make the default constructor accessible to create the mapper instance. fixes #1365
This commit is contained in:
parent
81f82a54a5
commit
c9bc1df132
@ -18,6 +18,8 @@
|
|||||||
*/
|
*/
|
||||||
package org.mapstruct.factory;
|
package org.mapstruct.factory;
|
||||||
|
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.ServiceLoader;
|
import java.util.ServiceLoader;
|
||||||
@ -78,10 +80,13 @@ public class Mappers {
|
|||||||
catch ( ClassNotFoundException e ) {
|
catch ( ClassNotFoundException e ) {
|
||||||
throw new RuntimeException( e );
|
throw new RuntimeException( e );
|
||||||
}
|
}
|
||||||
|
catch ( NoSuchMethodException e) {
|
||||||
|
throw new RuntimeException( e );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static <T> T getMapper(
|
private static <T> T getMapper(Class<T> mapperType, Iterable<ClassLoader> classLoaders)
|
||||||
Class<T> mapperType, Iterable<ClassLoader> classLoaders) throws ClassNotFoundException {
|
throws ClassNotFoundException, NoSuchMethodException {
|
||||||
|
|
||||||
for ( ClassLoader classLoader : classLoaders ) {
|
for ( ClassLoader classLoader : classLoaders ) {
|
||||||
T mapper = doGetMapper( mapperType, classLoader );
|
T mapper = doGetMapper( mapperType, classLoader );
|
||||||
@ -93,11 +98,13 @@ public class Mappers {
|
|||||||
throw new ClassNotFoundException("Cannot find implementation for " + mapperType.getName() );
|
throw new ClassNotFoundException("Cannot find implementation for " + mapperType.getName() );
|
||||||
}
|
}
|
||||||
|
|
||||||
private static <T> T doGetMapper(Class<T> clazz, ClassLoader classLoader) {
|
private static <T> T doGetMapper(Class<T> clazz, ClassLoader classLoader) throws NoSuchMethodException {
|
||||||
try {
|
try {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings( "unchecked" )
|
||||||
T mapper = (T) classLoader.loadClass( clazz.getName() + IMPLEMENTATION_SUFFIX ).newInstance();
|
Class<T> implementation = (Class<T>) classLoader.loadClass( clazz.getName() + IMPLEMENTATION_SUFFIX );
|
||||||
return mapper;
|
Constructor<T> constructor = implementation.getDeclaredConstructor();
|
||||||
|
constructor.setAccessible( true );
|
||||||
|
return constructor.newInstance();
|
||||||
}
|
}
|
||||||
catch (ClassNotFoundException e) {
|
catch (ClassNotFoundException e) {
|
||||||
ServiceLoader<T> loader = ServiceLoader.load( clazz, classLoader );
|
ServiceLoader<T> loader = ServiceLoader.load( clazz, classLoader );
|
||||||
@ -118,5 +125,8 @@ public class Mappers {
|
|||||||
catch (IllegalAccessException e) {
|
catch (IllegalAccessException e) {
|
||||||
throw new RuntimeException( e );
|
throw new RuntimeException( e );
|
||||||
}
|
}
|
||||||
|
catch (InvocationTargetException e) {
|
||||||
|
throw new RuntimeException( e );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -47,4 +47,9 @@ public class MappersTest {
|
|||||||
assertThat( Mappers.getMapper( SomeClass.Foo.class ) ).isNotNull();
|
assertThat( Mappers.getMapper( SomeClass.Foo.class ) ).isNotNull();
|
||||||
assertThat( Mappers.getMapper( SomeClass.NestedClass.Foo.class ) ).isNotNull();
|
assertThat( Mappers.getMapper( SomeClass.NestedClass.Foo.class ) ).isNotNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnPackagePrivateImplementationInstance() {
|
||||||
|
assertThat( Mappers.getMapper( PackagePrivateMapper.class ) ).isNotNull();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,23 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||||
|
* and/or other contributors as indicated by the @authors tag. See the
|
||||||
|
* copyright.txt file in the distribution for a full listing of all
|
||||||
|
* contributors.
|
||||||
|
*
|
||||||
|
* Licensed 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.mapstruct.factory;
|
||||||
|
|
||||||
|
interface PackagePrivateMapper {
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||||
|
* and/or other contributors as indicated by the @authors tag. See the
|
||||||
|
* copyright.txt file in the distribution for a full listing of all
|
||||||
|
* contributors.
|
||||||
|
*
|
||||||
|
* Licensed 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.mapstruct.factory;
|
||||||
|
|
||||||
|
class PackagePrivateMapperImpl implements PackagePrivateMapper {
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user