-
-
Notifications
You must be signed in to change notification settings - Fork 946
/
Mappers.java
122 lines (107 loc) · 3.99 KB
/
Mappers.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* 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()}.
* <p>
* 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.
* <p>
* 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:
*
* <pre>
* @Mapper
* public interface CustomerMapper {
*
* CustomerMapper INSTANCE = Mappers.getMapper( CustomerMapper.class );
*
* // mapping methods...
* }
* </pre>
*
* @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 <T> The type of the mapper to create.
*
* @return An instance of the given mapper type.
*/
public static <T> T getMapper(Class<T> clazz) {
try {
List<ClassLoader> classLoaders = new ArrayList<ClassLoader>( 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> T getMapper(
Class<T> mapperType, Iterable<ClassLoader> 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> T doGetMapper(Class<T> clazz, ClassLoader classLoader) {
try {
@SuppressWarnings("unchecked")
T mapper = (T) classLoader.loadClass( clazz.getName() + IMPLEMENTATION_SUFFIX ).newInstance();
return mapper;
}
catch (ClassNotFoundException e) {
ServiceLoader<T> 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 );
}
}
}