Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/tools/FxCop/HeuristicLab.FxCop/EnforceOneStorableHookPerClass.cs @ 7787

Last change on this file since 7787 was 7350, checked in by abeham, 13 years ago

#1759

  • Changed storable hook name rule to correctly distinguish between BeforeSerialization and AfterDeserialization hooks
  • Added a new rule that checks if more than one storable hook is defined in a class
File size: 2.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
22using System.Linq;
23using Microsoft.FxCop.Sdk;
24
25namespace HeuristicLab.FxCop {
26  internal sealed class EnforceOneStorableHookPerClass : HeuristicLabFxCopRuleBase {
27    public EnforceOneStorableHookPerClass()
28      : base("EnforceOneStorableHookPerClass") { }
29
30    public override ProblemCollection Check(TypeNode type) {
31      var storableHooks = type.Members.Where(x => x.Attributes.Any(y => y.Type.FullName == "HeuristicLab.Persistence.Default.CompositeSerializers.Storable.StorableHookAttribute")).ToArray();
32      if (storableHooks.Length < 2) return null;
33      else {
34        int afterDeserialization = 0;
35        int beforeSerialization = 0;
36        foreach (var hook in storableHooks) {
37          var attribute = hook.Attributes.FirstOrDefault(x => x.Type.FullName == "HeuristicLab.Persistence.Default.CompositeSerializers.Storable.StorableHookAttribute");
38          if (attribute != null && attribute.Expressions.Count > 0) {
39            var literal = (Literal)attribute.Expressions[0];
40            if (literal != null && literal.Value is int) {
41              int hookType = (int)literal.Value;
42              if (hookType == 1) afterDeserialization++;
43              else if (hookType == 0) beforeSerialization++;
44            }
45          }
46        }
47        if (afterDeserialization > 1)
48          Problems.Add(new Problem(GetResolution(type.Name.Name, afterDeserialization, "AfterDeserialization")));
49        if (beforeSerialization > 1)
50          Problems.Add(new Problem(GetResolution(type.Name.Name, beforeSerialization, "BeforeSerialization")));
51      }
52      return Problems;
53    }
54  }
55}
Note: See TracBrowser for help on using the repository browser.