Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
XMLBeanFactory |
|
| 5.0;5 |
1 | /** | |
2 | * Copyright 2005-2013 Dozer Project | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | package org.dozer.factory; | |
17 | ||
18 | import org.dozer.BeanFactory; | |
19 | import org.dozer.util.MappingUtils; | |
20 | import org.dozer.util.ReflectionUtils; | |
21 | ||
22 | import java.lang.reflect.Method; | |
23 | ||
24 | ||
25 | /** | |
26 | * Public custom bean factory that can be used by applition code when mapping XMLBean data objects | |
27 | * | |
28 | * @author garsombke.franz | |
29 | */ | |
30 | 1 | public class XMLBeanFactory implements BeanFactory { |
31 | 1 | private static final Class<?>[] emptyArglist = new Class[0]; |
32 | /** | |
33 | * Creat a bean implementation of a xml bean interface. | |
34 | * | |
35 | * @param srcObj | |
36 | * The source object | |
37 | * @param srcObjClass | |
38 | * The source object class | |
39 | * @param beanId | |
40 | * the name of the destination interface class | |
41 | * @return A implementation of the destination interface | |
42 | */ | |
43 | public Object createBean(Object srcObj, Class<?> srcObjClass, String beanId) { | |
44 | Object result; | |
45 | 14 | Class<?> destClass = MappingUtils.loadClass(beanId); |
46 | 14 | Class<?>[] innerClasses = destClass.getClasses(); |
47 | 14 | Class<?> factory = null; |
48 | 32 | for (Class<?> innerClass : innerClasses) { |
49 | 18 | if (innerClass.getName().endsWith("Factory")) { |
50 | 14 | factory = innerClass; |
51 | } | |
52 | } | |
53 | 14 | if (factory == null) { |
54 | 0 | MappingUtils.throwMappingException("Factory class of Bean of type " + beanId + " not found."); |
55 | } | |
56 | 14 | Method newInstanceMethod = null; |
57 | try { | |
58 | 14 | newInstanceMethod = ReflectionUtils.getMethod(factory, "newInstance", emptyArglist); |
59 | 0 | } catch (NoSuchMethodException e) { |
60 | 0 | MappingUtils.throwMappingException(e); |
61 | 14 | } |
62 | 14 | result = ReflectionUtils.invoke(newInstanceMethod, null, emptyArglist); |
63 | 14 | return result; |
64 | } | |
65 | } |