1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.Reflection;
|
---|
6 | using HeuristicLab.PluginInfrastructure;
|
---|
7 |
|
---|
8 | namespace HeuristicLab.Hive.JobBase {
|
---|
9 | public static class PluginUtil {
|
---|
10 | #region Required Plugin Search
|
---|
11 |
|
---|
12 | /// <summary>
|
---|
13 | /// Returns a list of plugins in which the type itself and all members
|
---|
14 | /// of the type are declared. Objectgraph is searched recursively.
|
---|
15 | /// </summary>
|
---|
16 | public static IEnumerable<IPluginDescription> GetDeclaringPlugins(Type type) {
|
---|
17 | HashSet<Type> types = new HashSet<Type>();
|
---|
18 | FindTypes(type, types, "HeuristicLab.");
|
---|
19 | return GetDeclaringPlugins(types);
|
---|
20 | }
|
---|
21 |
|
---|
22 | /// <summary>
|
---|
23 | /// Returns the plugins (including dependencies) in which the given types are declared
|
---|
24 | /// </summary>
|
---|
25 | public static IEnumerable<IPluginDescription> GetDeclaringPlugins(IEnumerable<Type> types) {
|
---|
26 | HashSet<IPluginDescription> plugins = new HashSet<IPluginDescription>();
|
---|
27 | foreach (Type t in types) {
|
---|
28 | FindDeclaringPlugins(ApplicationManager.Manager.GetDeclaringPlugin(t), plugins);
|
---|
29 | }
|
---|
30 | return plugins;
|
---|
31 | }
|
---|
32 |
|
---|
33 | /// <summary>
|
---|
34 | /// Finds the dependencies of the given plugin and adds it to the plugins hashset.
|
---|
35 | /// Also searches the dependencies recursively.
|
---|
36 | /// </summary>
|
---|
37 | public static void FindDeclaringPlugins(IPluginDescription plugin, HashSet<IPluginDescription> plugins) {
|
---|
38 | if (!plugins.Contains(plugin)) {
|
---|
39 | plugins.Add(plugin);
|
---|
40 | foreach (IPluginDescription dependency in plugin.Dependencies) {
|
---|
41 | FindDeclaringPlugins(dependency, plugins);
|
---|
42 | }
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | /// <summary>
|
---|
47 | /// Recursively finds all types used in type which are in a namespace which starts with namespaceStart
|
---|
48 | /// Be aware that search is not performed on attributes
|
---|
49 | /// </summary>
|
---|
50 | /// <param name="type">the type to be searched</param>
|
---|
51 | /// <param name="types">found types will be stored there, needed in order to avoid duplicates</param>
|
---|
52 | /// <param name="namespaceStart">only types from namespaces which start with this will be searched and added</param>
|
---|
53 | public static void FindTypes(Type type, HashSet<Type> types, string namespaceStart) {
|
---|
54 | if (!types.Contains(type) && type.Namespace.StartsWith(namespaceStart)) {
|
---|
55 | types.Add(type);
|
---|
56 |
|
---|
57 | // constructors
|
---|
58 | foreach (ConstructorInfo info in type.GetConstructors()) {
|
---|
59 | foreach (ParameterInfo paramInfo in info.GetParameters()) {
|
---|
60 | FindTypes(paramInfo.ParameterType, types, namespaceStart);
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | // interfaces
|
---|
65 | foreach (Type t in type.GetInterfaces()) {
|
---|
66 | FindTypes(t, types, namespaceStart);
|
---|
67 | }
|
---|
68 |
|
---|
69 | // events
|
---|
70 | foreach (EventInfo info in type.GetEvents()) {
|
---|
71 | FindTypes(info.EventHandlerType, types, namespaceStart);
|
---|
72 | FindTypes(info.DeclaringType, types, namespaceStart);
|
---|
73 | }
|
---|
74 |
|
---|
75 | // properties
|
---|
76 | foreach (PropertyInfo info in type.GetProperties()) {
|
---|
77 | FindTypes(info.PropertyType, types, namespaceStart);
|
---|
78 | }
|
---|
79 |
|
---|
80 | // fields
|
---|
81 | foreach (FieldInfo info in type.GetFields()) {
|
---|
82 | FindTypes(info.FieldType, types, namespaceStart);
|
---|
83 | }
|
---|
84 |
|
---|
85 | // methods
|
---|
86 | foreach (MethodInfo info in type.GetMethods()) {
|
---|
87 | foreach (ParameterInfo paramInfo in info.GetParameters()) {
|
---|
88 | FindTypes(paramInfo.ParameterType, types, namespaceStart);
|
---|
89 | }
|
---|
90 | FindTypes(info.ReturnType, types, namespaceStart);
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 | #endregion
|
---|
95 | }
|
---|
96 | }
|
---|