[3742] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16565] | 3 | * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3742] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
[1623] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
[1705] | 25 | using System.Reflection;
|
---|
[4068] | 26 | using System.Reflection.Emit;
|
---|
[3025] | 27 | using System.Text;
|
---|
[16565] | 28 | using HEAL.Attic;
|
---|
[4068] | 29 | using HeuristicLab.Persistence.Core;
|
---|
| 30 | using HeuristicLab.Persistence.Interfaces;
|
---|
[1623] | 31 |
|
---|
[1823] | 32 | namespace HeuristicLab.Persistence.Default.CompositeSerializers.Storable {
|
---|
[1623] | 33 |
|
---|
[2994] | 34 | /// <summary>
|
---|
| 35 | /// Intended for serialization of all custom classes. Classes should have the
|
---|
[3029] | 36 | /// <c>[StorableClass]</c> attribute set. The default mode is to serialize
|
---|
| 37 | /// members with the <c>[Storable]</c> attribute set. Alternatively the
|
---|
| 38 | /// storable mode can be set to <c>AllFields</c>, <c>AllProperties</c>
|
---|
| 39 | /// or <c>AllFieldsAndAllProperties</c>.
|
---|
[2994] | 40 | /// </summary>
|
---|
[16571] | 41 | [StorableType("F60343E8-4337-4171-A50A-6A57D09267ED")]
|
---|
[3036] | 42 | public sealed class StorableSerializer : ICompositeSerializer {
|
---|
[1623] | 43 |
|
---|
[4175] | 44 | public StorableSerializer() {
|
---|
| 45 | accessorListCache = new AccessorListCache();
|
---|
| 46 | accessorCache = new AccessorCache();
|
---|
| 47 | constructorCache = new Dictionary<Type, Constructor>();
|
---|
| 48 | hookCache = new Dictionary<HookDesignator, List<StorableReflection.Hook>>();
|
---|
| 49 | }
|
---|
[10958] | 50 |
|
---|
[4806] | 51 | [StorableConstructor]
|
---|
[16565] | 52 | private StorableSerializer(StorableConstructorFlag _) { }
|
---|
[4175] | 53 |
|
---|
[3025] | 54 | #region ICompositeSerializer implementation
|
---|
| 55 |
|
---|
[3036] | 56 | /// <summary>
|
---|
| 57 | /// Priority 200, one of the first default composite serializers to try.
|
---|
| 58 | /// </summary>
|
---|
| 59 | /// <value></value>
|
---|
[1623] | 60 | public int Priority {
|
---|
| 61 | get { return 200; }
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[3036] | 64 | /// <summary>
|
---|
| 65 | /// Determines for every type whether the composite serializer is applicable.
|
---|
| 66 | /// </summary>
|
---|
| 67 | /// <param name="type">The type.</param>
|
---|
| 68 | /// <returns>
|
---|
| 69 | /// <c>true</c> if this instance can serialize the specified type; otherwise, <c>false</c>.
|
---|
| 70 | /// </returns>
|
---|
[1823] | 71 | public bool CanSerialize(Type type) {
|
---|
[16565] | 72 | if (type.IsEnum || type.IsValueType) return false; // there are other more specific serializers for enums and structs
|
---|
| 73 | var markedStorable = StorableReflection.HasStorableTypeAttribute(type);
|
---|
[3553] | 74 | if (GetConstructor(type) == null)
|
---|
[16565] | 75 | if (markedStorable && !type.IsInterface)
|
---|
[3577] | 76 | throw new Exception("[Storable] type has no default constructor and no [StorableConstructor]");
|
---|
| 77 | else
|
---|
| 78 | return false;
|
---|
| 79 | if (!StorableReflection.IsEmptyOrStorableType(type, true))
|
---|
[16565] | 80 | if (markedStorable && !type.IsInterface)
|
---|
[3577] | 81 | throw new Exception("[Storable] type has non emtpy, non [Storable] base classes");
|
---|
| 82 | else
|
---|
| 83 | return false;
|
---|
| 84 | return true;
|
---|
[1623] | 85 | }
|
---|
| 86 |
|
---|
[3036] | 87 | /// <summary>
|
---|
| 88 | /// Give a reason if possibly why the given type cannot be serialized by this
|
---|
| 89 | /// ICompositeSerializer.
|
---|
| 90 | /// </summary>
|
---|
| 91 | /// <param name="type">The type.</param>
|
---|
| 92 | /// <returns>
|
---|
| 93 | /// A string justifying why type cannot be serialized.
|
---|
| 94 | /// </returns>
|
---|
[2993] | 95 | public string JustifyRejection(Type type) {
|
---|
[10958] | 96 | var sb = new StringBuilder();
|
---|
[3553] | 97 | if (GetConstructor(type) == null)
|
---|
[3577] | 98 | sb.Append("class has no default constructor and no [StorableConstructor]");
|
---|
[3029] | 99 | if (!StorableReflection.IsEmptyOrStorableType(type, true))
|
---|
[4068] | 100 | sb.Append("class (or one of its bases) is not empty and not marked [Storable]; ");
|
---|
[3577] | 101 | return sb.ToString();
|
---|
[2993] | 102 | }
|
---|
| 103 |
|
---|
[3036] | 104 | /// <summary>
|
---|
| 105 | /// Creates the meta info.
|
---|
| 106 | /// </summary>
|
---|
| 107 | /// <param name="o">The object.</param>
|
---|
| 108 | /// <returns>A list of storable components.</returns>
|
---|
[1623] | 109 | public IEnumerable<Tag> CreateMetaInfo(object o) {
|
---|
[3031] | 110 | InvokeHook(HookType.BeforeSerialization, o);
|
---|
[1623] | 111 | return new Tag[] { };
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[3036] | 114 | /// <summary>
|
---|
| 115 | /// Decompose an object into <see cref="Tag"/>s, the tag name can be null,
|
---|
| 116 | /// the order in which elements are generated is guaranteed to be
|
---|
| 117 | /// the same as they will be supplied to the Populate method.
|
---|
| 118 | /// </summary>
|
---|
| 119 | /// <param name="obj">An object.</param>
|
---|
| 120 | /// <returns>An enumerable of <see cref="Tag"/>s.</returns>
|
---|
[1623] | 121 | public IEnumerable<Tag> Decompose(object obj) {
|
---|
[10958] | 122 | return from accessor in GetStorableAccessors(obj.GetType())
|
---|
| 123 | where accessor.Get != null
|
---|
| 124 | select new Tag(accessor.Name, accessor.Get(obj));
|
---|
[1623] | 125 | }
|
---|
| 126 |
|
---|
[3036] | 127 | /// <summary>
|
---|
| 128 | /// Create an instance of the object using the provided meta information.
|
---|
| 129 | /// </summary>
|
---|
| 130 | /// <param name="type">A type.</param>
|
---|
| 131 | /// <param name="metaInfo">The meta information.</param>
|
---|
| 132 | /// <returns>A fresh instance of the provided type.</returns>
|
---|
[1623] | 133 | public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
|
---|
[2990] | 134 | try {
|
---|
[3553] | 135 | return GetConstructor(type)();
|
---|
[5292] | 136 | } catch (TargetInvocationException x) {
|
---|
[2990] | 137 | throw new PersistenceException(
|
---|
| 138 | "Could not instantiate storable object: Encountered exception during constructor call",
|
---|
| 139 | x.InnerException);
|
---|
| 140 | }
|
---|
[1623] | 141 | }
|
---|
| 142 |
|
---|
[3036] | 143 | /// <summary>
|
---|
| 144 | /// Populates the specified instance.
|
---|
| 145 | /// </summary>
|
---|
| 146 | /// <param name="instance">The instance.</param>
|
---|
| 147 | /// <param name="objects">The objects.</param>
|
---|
| 148 | /// <param name="type">The type.</param>
|
---|
[1623] | 149 | public void Populate(object instance, IEnumerable<Tag> objects, Type type) {
|
---|
| 150 | var memberDict = new Dictionary<string, Tag>();
|
---|
[10958] | 151 | var iter = objects.GetEnumerator();
|
---|
[1623] | 152 | while (iter.MoveNext()) {
|
---|
| 153 | memberDict.Add(iter.Current.Name, iter.Current);
|
---|
[3029] | 154 | }
|
---|
[3913] | 155 | foreach (var accessor in GetStorableAccessors(instance.GetType())) {
|
---|
[5324] | 156 | if (accessor.Set != null) {
|
---|
| 157 | if (memberDict.ContainsKey(accessor.Name)) {
|
---|
| 158 | accessor.Set(instance, memberDict[accessor.Name].Value);
|
---|
| 159 | } else if (accessor.DefaultValue != null) {
|
---|
| 160 | accessor.Set(instance, accessor.DefaultValue);
|
---|
| 161 | }
|
---|
[1623] | 162 | }
|
---|
| 163 | }
|
---|
[3031] | 164 | InvokeHook(HookType.AfterDeserialization, instance);
|
---|
[1623] | 165 | }
|
---|
[3025] | 166 |
|
---|
| 167 | #endregion
|
---|
| 168 |
|
---|
[3029] | 169 | #region constants & private data types
|
---|
[3025] | 170 |
|
---|
| 171 | private const BindingFlags ALL_CONSTRUCTORS =
|
---|
| 172 | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
|
---|
| 173 |
|
---|
[10958] | 174 | private sealed class HookDesignator : Tuple<Type, HookType> {
|
---|
| 175 | public HookDesignator(Type type, HookType hookType) : base(type, hookType) { }
|
---|
[3031] | 176 | }
|
---|
| 177 |
|
---|
[3913] | 178 | private sealed class AccessorListCache : Dictionary<Type, IEnumerable<DataMemberAccessor>> { }
|
---|
| 179 | private sealed class AccessorCache : Dictionary<MemberInfo, DataMemberAccessor> { }
|
---|
[4175] | 180 | private delegate object Constructor();
|
---|
[3025] | 181 |
|
---|
| 182 | #endregion
|
---|
| 183 |
|
---|
| 184 | #region caches
|
---|
| 185 |
|
---|
[10958] | 186 | private readonly AccessorListCache accessorListCache;
|
---|
| 187 | private readonly AccessorCache accessorCache;
|
---|
| 188 | private readonly Dictionary<Type, Constructor> constructorCache;
|
---|
| 189 | private readonly Dictionary<HookDesignator, List<StorableReflection.Hook>> hookCache;
|
---|
[3025] | 190 |
|
---|
| 191 | #endregion
|
---|
| 192 |
|
---|
[3029] | 193 | #region attribute access
|
---|
[3025] | 194 |
|
---|
[3913] | 195 | private IEnumerable<DataMemberAccessor> GetStorableAccessors(Type type) {
|
---|
| 196 | lock (accessorListCache) {
|
---|
| 197 | if (accessorListCache.ContainsKey(type))
|
---|
| 198 | return accessorListCache[type];
|
---|
| 199 | var storableMembers = StorableReflection
|
---|
| 200 | .GenerateStorableMembers(type)
|
---|
[10958] | 201 | .Select(GetMemberAccessor)
|
---|
| 202 | .ToList();
|
---|
[3913] | 203 | accessorListCache[type] = storableMembers;
|
---|
| 204 | return storableMembers;
|
---|
[4068] | 205 | }
|
---|
[3913] | 206 | }
|
---|
| 207 |
|
---|
| 208 | private DataMemberAccessor GetMemberAccessor(StorableMemberInfo mi) {
|
---|
| 209 | lock (accessorCache) {
|
---|
| 210 | if (accessorCache.ContainsKey(mi.MemberInfo))
|
---|
| 211 | return new DataMemberAccessor(accessorCache[mi.MemberInfo], mi.DisentangledName, mi.DefaultValue);
|
---|
[10958] | 212 | var dma = new DataMemberAccessor(mi.MemberInfo, mi.DisentangledName, mi.DefaultValue);
|
---|
[3913] | 213 | accessorCache[mi.MemberInfo] = dma;
|
---|
| 214 | return dma;
|
---|
[3025] | 215 | }
|
---|
[3553] | 216 | }
|
---|
[3025] | 217 |
|
---|
[3553] | 218 | private Constructor GetConstructor(Type type) {
|
---|
[3025] | 219 | lock (constructorCache) {
|
---|
| 220 | if (constructorCache.ContainsKey(type))
|
---|
| 221 | return constructorCache[type];
|
---|
[10958] | 222 | var c = FindStorableConstructor(type) ?? GetDefaultConstructor(type);
|
---|
[3553] | 223 | constructorCache.Add(type, c);
|
---|
| 224 | return c;
|
---|
| 225 | }
|
---|
[4068] | 226 | }
|
---|
[3553] | 227 |
|
---|
| 228 | private Constructor GetDefaultConstructor(Type type) {
|
---|
[10958] | 229 | var ci = type.GetConstructor(ALL_CONSTRUCTORS, null, Type.EmptyTypes, null);
|
---|
[3553] | 230 | if (ci == null)
|
---|
[4068] | 231 | return null;
|
---|
[10958] | 232 | var dm = new DynamicMethod("", typeof(object), null, type, true);
|
---|
| 233 | var ilgen = dm.GetILGenerator();
|
---|
[3553] | 234 | ilgen.Emit(OpCodes.Newobj, ci);
|
---|
| 235 | ilgen.Emit(OpCodes.Ret);
|
---|
| 236 | return (Constructor)dm.CreateDelegate(typeof(Constructor));
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | private Constructor FindStorableConstructor(Type type) {
|
---|
[10958] | 240 | foreach (var ci in type
|
---|
| 241 | .GetConstructors(ALL_CONSTRUCTORS)
|
---|
| 242 | .Where(ci => ci.GetCustomAttributes(typeof(StorableConstructorAttribute), false).Length > 0)) {
|
---|
| 243 | if (ci.GetParameters().Length != 1 ||
|
---|
[16565] | 244 | ci.GetParameters()[0].ParameterType != typeof(StorableConstructorFlag))
|
---|
| 245 | throw new PersistenceException("StorableConstructor must have exactly one argument of type StorableConstructorFlag");
|
---|
[10958] | 246 | var dm = new DynamicMethod("", typeof(object), null, type, true);
|
---|
| 247 | var ilgen = dm.GetILGenerator();
|
---|
[16565] | 248 | var defaultFlagFieldInfo = typeof(StorableConstructorFlag).GetField("Default", BindingFlags.Static | BindingFlags.Public);
|
---|
| 249 | ilgen.Emit(OpCodes.Ldsfld, defaultFlagFieldInfo); // load the object
|
---|
[10958] | 250 | ilgen.Emit(OpCodes.Newobj, ci);
|
---|
| 251 | ilgen.Emit(OpCodes.Ret);
|
---|
| 252 | return (Constructor)dm.CreateDelegate(typeof(Constructor));
|
---|
[3025] | 253 | }
|
---|
[3553] | 254 | return null;
|
---|
[4068] | 255 | }
|
---|
[3025] | 256 |
|
---|
[3031] | 257 | private void InvokeHook(HookType hookType, object obj) {
|
---|
| 258 | if (obj == null)
|
---|
[10958] | 259 | throw new ArgumentNullException("obj");
|
---|
| 260 | foreach (var hook in GetHooks(hookType, obj.GetType())) {
|
---|
[3553] | 261 | hook(obj);
|
---|
[3031] | 262 | }
|
---|
| 263 | }
|
---|
| 264 |
|
---|
[3553] | 265 | private IEnumerable<StorableReflection.Hook> GetHooks(HookType hookType, Type type) {
|
---|
[3031] | 266 | lock (hookCache) {
|
---|
[3553] | 267 | List<StorableReflection.Hook> hooks;
|
---|
[3031] | 268 | var designator = new HookDesignator(type, hookType);
|
---|
| 269 | hookCache.TryGetValue(designator, out hooks);
|
---|
| 270 | if (hooks != null)
|
---|
| 271 | return hooks;
|
---|
[3553] | 272 | hooks = new List<StorableReflection.Hook>(StorableReflection.CollectHooks(hookType, type));
|
---|
[3031] | 273 | hookCache.Add(designator, hooks);
|
---|
| 274 | return hooks;
|
---|
| 275 | }
|
---|
| 276 | }
|
---|
| 277 |
|
---|
[3025] | 278 | #endregion
|
---|
[3031] | 279 |
|
---|
[3553] | 280 |
|
---|
| 281 |
|
---|
[1623] | 282 | }
|
---|
[3553] | 283 |
|
---|
[2980] | 284 | } |
---|