![JAR search and dependency download from the Maven repository](/logo.png)
org.mapstruct.factory.Mappers Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mapstruct-jdk8 Show documentation
Show all versions of mapstruct-jdk8 Show documentation
MapStruct annotations to be used with JDK 8 and later
/**
* 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;
import java.util.ArrayList;
import java.util.List;
import java.util.ServiceLoader;
import org.mapstruct.Mapper;
/**
* Factory for obtaining mapper instances if no explicit component model such as CDI is configured via
* {@link Mapper#componentModel()}.
*
* Mapper implementation types are expected to have the same fully qualified name as their interface type, with the
* suffix {@code Impl} appended. When using this factory, mapper types - and any mappers they use - are instantiated by
* invoking their public no-args constructor.
*
* By convention, a single instance of each mapper is retrieved from the factory and exposed on the mapper interface
* type by declaring a member named {@code INSTANCE} like this:
*
*
* @Mapper
* public interface CustomerMapper {
*
* CustomerMapper INSTANCE = Mappers.getMapper( CustomerMapper.class );
*
* // mapping methods...
* }
*
*
* @author Gunnar Morling
*/
public class Mappers {
private static final String IMPLEMENTATION_SUFFIX = "Impl";
private Mappers() {
}
/**
* Returns an instance of the given mapper type.
*
* @param clazz The type of the mapper to return.
* @param The type of the mapper to create.
*
* @return An instance of the given mapper type.
*/
public static T getMapper(Class clazz) {
try {
List classLoaders = new ArrayList( 3 );
classLoaders.add( clazz.getClassLoader() );
if ( Thread.currentThread().getContextClassLoader() != null ) {
classLoaders.add( Thread.currentThread().getContextClassLoader() );
}
classLoaders.add( Mappers.class.getClassLoader() );
return getMapper( clazz, classLoaders );
}
catch ( ClassNotFoundException e ) {
throw new RuntimeException( e );
}
}
private static T getMapper(
Class mapperType, Iterable classLoaders) throws ClassNotFoundException {
for ( ClassLoader classLoader : classLoaders ) {
T mapper = doGetMapper( mapperType, classLoader );
if ( mapper != null ) {
return mapper;
}
}
throw new ClassNotFoundException("Cannot find implementation for " + mapperType.getName() );
}
private static T doGetMapper(Class clazz, ClassLoader classLoader) {
try {
@SuppressWarnings("unchecked")
T mapper = (T) classLoader.loadClass( clazz.getName() + IMPLEMENTATION_SUFFIX ).newInstance();
return mapper;
}
catch (ClassNotFoundException e) {
ServiceLoader loader = ServiceLoader.load( clazz, classLoader );
if ( loader != null ) {
for ( T mapper : loader ) {
if ( mapper != null ) {
return mapper;
}
}
}
return null;
}
catch (InstantiationException e) {
throw new RuntimeException( e );
}
catch (IllegalAccessException e) {
throw new RuntimeException( e );
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy