Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/16/10 14:31:52 (13 years ago)
Author:
cneumuel
Message:

#1260

  • changed dependency discovery machanism: now all locally loaded plugins will be dependencies for a job.
  • fixed logging of slaveconsole by limiting the maximum log-messages
  • minor bug fixes.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Hive/sources/HeuristicLab.Hive/HeuristicLab.Hive.JobBase/3.3/PluginUtil.cs

    r4424 r4810  
    2424using System.Reflection;
    2525using HeuristicLab.PluginInfrastructure;
     26using System.Linq;
     27using HeuristicLab.Common;
    2628
    2729namespace HeuristicLab.Hive.JobBase {
    2830  public static class PluginUtil {
    2931    #region Required Plugin Search
    30    
     32
    3133    /// <summary>
    3234    /// Returns a list of plugins in which the type itself and all members
    3335    /// of the type are declared. Objectgraph is searched recursively.
    3436    /// </summary>
    35     public static IEnumerable<IPluginDescription> GetDeclaringPlugins(Type type) {
     37    public static IEnumerable<IPluginDescription> GetDeclaringPlugins(IDeepCloneable obj) {
     38      object clone = obj.Clone();
    3639      HashSet<Type> types = new HashSet<Type>();
    37       FindTypes(type, types, "HeuristicLab.");
    38       return GetDeclaringPlugins(types);
     40      HashSet<object> objects = new HashSet<object>();
     41      string namespaceStart = "HeuristicLab.";
     42      FindTypesInObject(clone, objects, types, namespaceStart, 10);
     43      return GetDeclaringPlugins(types, namespaceStart);
    3944    }
    4045
     
    4247    /// Returns the plugins (including dependencies) in which the given types are declared
    4348    /// </summary>
    44     public static IEnumerable<IPluginDescription> GetDeclaringPlugins(IEnumerable<Type> types) {
     49    public static IEnumerable<IPluginDescription> GetDeclaringPlugins(IEnumerable<Type> types, string namespaceStart) {
    4550      HashSet<IPluginDescription> plugins = new HashSet<IPluginDescription>();
    46       foreach (Type t in types) {
     51      var hlTypes = types.Where(x => x.Namespace != null && x.Namespace.StartsWith(namespaceStart));
     52      foreach (Type t in hlTypes) {
    4753        FindDeclaringPlugins(ApplicationManager.Manager.GetDeclaringPlugin(t), plugins);
    4854      }
     
    6874    /// </summary>
    6975    /// <param name="type">the type to be searched</param>
    70     /// <param name="types">found types will be stored there, needed in order to avoid duplicates</param>
    71     /// <param name="namespaceStart">only types from namespaces which start with this will be searched and added</param>
    72     public static void FindTypes(Type type, HashSet<Type> types, string namespaceStart) {
    73       if (!types.Contains(type) && type.Namespace.StartsWith(namespaceStart)) {
    74         types.Add(type);
     76    /// <param name="foundTypes">found types will be stored there, needed in order to avoid duplicates</param>
     77    public static void FindTypes(Type type, HashSet<Type> foundTypes, string namespaceStart, int nonNamespaceSearchDepth) {
     78      if (type.Namespace != null && type.Namespace.StartsWith(namespaceStart)) {
     79        nonNamespaceSearchDepth = 10;
     80      } else {
     81        nonNamespaceSearchDepth--;
     82      }
     83
     84      if (!foundTypes.Contains(type) && nonNamespaceSearchDepth > 0) {
     85        foundTypes.Add(type);
     86
     87        if (type.IsGenericType) {
     88          foreach (Type t in type.GetGenericArguments()) {
     89            FindTypes(t, foundTypes, namespaceStart, nonNamespaceSearchDepth);
     90          }
     91        }
    7592
    7693        // constructors
    7794        foreach (ConstructorInfo info in type.GetConstructors()) {
    7895          foreach (ParameterInfo paramInfo in info.GetParameters()) {
    79             FindTypes(paramInfo.ParameterType, types, namespaceStart);
     96            FindTypes(paramInfo.ParameterType, foundTypes, namespaceStart, nonNamespaceSearchDepth);
    8097          }
    8198        }
     
    83100        // interfaces
    84101        foreach (Type t in type.GetInterfaces()) {
    85           FindTypes(t, types, namespaceStart);
     102          FindTypes(t, foundTypes, namespaceStart, nonNamespaceSearchDepth);
    86103        }
    87104
    88105        // events
    89106        foreach (EventInfo info in type.GetEvents()) {
    90           FindTypes(info.EventHandlerType, types, namespaceStart);
    91           FindTypes(info.DeclaringType, types, namespaceStart);
    92         }
    93 
    94         // properties
    95         foreach (PropertyInfo info in type.GetProperties()) {
    96           FindTypes(info.PropertyType, types, namespaceStart);
    97         }
    98 
    99         // fields
    100         foreach (FieldInfo info in type.GetFields()) {
    101           FindTypes(info.FieldType, types, namespaceStart);
     107          FindTypes(info.EventHandlerType, foundTypes, namespaceStart, nonNamespaceSearchDepth);
     108          FindTypes(info.DeclaringType, foundTypes, namespaceStart, nonNamespaceSearchDepth);
    102109        }
    103110
     
    105112        foreach (MethodInfo info in type.GetMethods()) {
    106113          foreach (ParameterInfo paramInfo in info.GetParameters()) {
    107             FindTypes(paramInfo.ParameterType, types, namespaceStart);
     114            FindTypes(paramInfo.ParameterType, foundTypes, namespaceStart, nonNamespaceSearchDepth);
    108115          }
    109           FindTypes(info.ReturnType, types, namespaceStart);
     116          FindTypes(info.ReturnType, foundTypes, namespaceStart, nonNamespaceSearchDepth);
     117        }
     118
     119        // base type
     120        if (type.BaseType != null) {
     121          FindTypes(type.BaseType, foundTypes, namespaceStart, nonNamespaceSearchDepth);
    110122        }
    111123      }
     124    }
     125
     126    public static void FindTypesInObject(object obj, HashSet<object> visitedObjects, HashSet<Type> types, string namespaceStart, int nonNamespaceSearchDepth) {
     127      Type type = obj.GetType();
     128      FindTypes(type, types, namespaceStart, nonNamespaceSearchDepth);
     129
     130      if (type.Namespace != null && type.Namespace.StartsWith(namespaceStart)) {
     131        nonNamespaceSearchDepth = 10;
     132      } else {
     133        nonNamespaceSearchDepth--;
     134      }
     135
     136      if (!visitedObjects.Contains(obj) && nonNamespaceSearchDepth > 0) {
     137        visitedObjects.Add(obj);
     138
     139        // fields
     140        foreach (FieldInfo info in GetAllFields(type)) {
     141          var value = info.GetValue(obj);
     142          if (value == null) {
     143            FindTypes(info.FieldType, types, namespaceStart, nonNamespaceSearchDepth);
     144          } else {
     145            FindTypesInObject(value, visitedObjects, types, namespaceStart, nonNamespaceSearchDepth);
     146          }
     147        }
     148      }
     149    }
     150
     151    public static IEnumerable<FieldInfo> GetAllFields(Type t) {
     152      BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy;
     153      return t.GetFields(flags);
    112154    }
    113155    #endregion
Note: See TracChangeset for help on using the changeset viewer.