| 1 | package org.jtoolkit.essence.app.pojo.impl; |
| 2 | |
| 3 | import org.jetbrains.annotations.NotNull; |
| 4 | import static org.jtoolkit.essence.app.pojo.impl.DataValueClass.*; |
| 5 | import org.jtoolkit.essence.concurrency.Immutable; |
| 6 | import org.jtoolkit.essence.utils.Named; |
| 7 | import org.jtoolkit.essence.utils.StringUtils; |
| 8 | |
| 9 | import java.lang.reflect.Constructor; |
| 10 | import java.lang.reflect.Field; |
| 11 | import java.lang.reflect.InvocationTargetException; |
| 12 | import java.util.*; |
| 13 | import java.util.concurrent.ConcurrentHashMap; |
| 14 | |
| 15 | /* |
| 16 | Copyright 2006 Peter Lawrey |
| 17 | |
| 18 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 19 | you may not use this file except in compliance with the License. |
| 20 | You may obtain a copy of the License at |
| 21 | |
| 22 | http://www.apache.org/licenses/LICENSE-2.0 |
| 23 | |
| 24 | Unless required by applicable law or agreed to in writing, software |
| 25 | distributed under the License is distributed on an "AS IS" BASIS, |
| 26 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 27 | See the License for the specific language governing permissions and |
| 28 | limitations under the License. |
| 29 | */ |
| 30 | |
| 31 | /** |
| 32 | * Helper class for building components with references to other components. |
| 33 | * |
| 34 | * @author Peter Lawrey |
| 35 | */ |
| 36 | @Immutable |
| 37 | @SuppressWarnings("unchecked") |
| 38 | public class ComponentHelper<T> { |
| 39 | private static final Map<Class, ComponentHelper> componentClasses = new ConcurrentHashMap<Class, ComponentHelper>(); |
| 40 | private final Class<T> type; |
| 41 | private final Map<String, Field> fieldMap; |
| 42 | |
| 43 | private ComponentHelper(Class<T> clazz) { |
| 44 | type = clazz; |
| 45 | fieldMap = getFields(clazz); |
| 46 | } |
| 47 | |
| 48 | @NotNull public static <T> ComponentHelper<T> acquire(@NotNull Class<T> class2) { |
| 49 | ComponentHelper<T> ret = componentClasses.get(class2); |
| 50 | if (ret == null) |
| 51 | componentClasses.put(class2, ret = new ComponentHelper<T>(class2)); |
| 52 | return ret; |
| 53 | } |
| 54 | |
| 55 | @NotNull |
| 56 | public static <T> T build(@NotNull Class<T> clazz, @NotNull Named.Source<? extends Object> map, @NotNull Map<String, Object> defaults, @NotNull PojoContext pojoContext) { |
| 57 | return acquire(clazz).build(map, defaults, pojoContext); |
| 58 | } |
| 59 | |
| 60 | @SuppressWarnings({"OverlyCoupledMethod"}) |
| 61 | @NotNull |
| 62 | public T build(@NotNull Named.Source<? extends Object> map, @NotNull Map<String, Object> defaults, @NotNull PojoContext pojoContext) { |
| 63 | List<Class> targetCons = new ArrayList<Class>(); |
| 64 | List<Object> values = new ArrayList<Object>(); |
| 65 | List<String> matched = new ArrayList<String>(); |
| 66 | Map<String, Object> map2 = new LinkedHashMap<String, Object>(); |
| 67 | for (String key : map.getNames()) |
| 68 | map2.put(StringUtils.toCamelCase(key), map.getValue(key)); |
| 69 | |
| 70 | Set<String> unmatched = new LinkedHashSet<String>(map2.keySet()); |
| 71 | String objectName = (String) map.getValue("name"); |
| 72 | if (objectName == null) objectName = (String) defaults.get("name"); |
| 73 | |
| 74 | for (Field f : fieldMap.values()) { |
| 75 | String name = f.getName(); |
| 76 | Class type = f.getType(); |
| 77 | Object mapObj = map2.get(name); |
| 78 | if (map2.containsKey(name)) { |
| 79 | targetCons.add(type); |
| 80 | Object value = cast(type, mapObj, pojoContext); |
| 81 | if (isPrimative(type) && value == null) |
| 82 | return (T) DataValueClass.throwCannotBeNull(objectName, name); |
| 83 | values.add(value); |
| 84 | matched.add(name); |
| 85 | unmatched.remove(name); |
| 86 | } else { |
| 87 | // if there is a default which is the right type. |
| 88 | Object defObj = defaults.get(name); |
| 89 | if (defObj != null && isAssignableFrom(type, defObj.getClass())) { |
| 90 | targetCons.add(type); |
| 91 | values.add(defObj); |
| 92 | matched.add(name); |
| 93 | unmatched.remove(name); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | try { |
| 98 | Constructor<T> cons = type.getConstructor(targetCons.toArray(new Class[targetCons.size()])); |
| 99 | cons.setAccessible(true); |
| 100 | Object[] args = values.toArray(new Object[values.size()]); |
| 101 | return cons.newInstance(args); |
| 102 | } catch (NoSuchMethodException e) { |
| 103 | return (T) DataValueClass.throwUnmatched(objectName, matched, unmatched, e); |
| 104 | } catch (IllegalAccessException e) { |
| 105 | throw new IllegalStateException(e); |
| 106 | } catch (InvocationTargetException e) { |
| 107 | int arg = DataValueClass.getArgumentNumber(type, e); |
| 108 | return (T) DataValueClass.throwCannotBeNull(objectName, matched.get(arg)); |
| 109 | } catch (InstantiationException e) { |
| 110 | return (T) DataValueClass.throwUnmatched(objectName, matched, unmatched, e); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | @NotNull public Map<String, Class> getReferenceTypes() { |
| 115 | Map<String, Class> ret = new LinkedHashMap<String, Class>(); |
| 116 | for (Field f : fieldMap.values()) |
| 117 | ret.put(f.getName(), f.getType()); |
| 118 | return ret; |
| 119 | } |
| 120 | } |