Implemented better attribute support for C# output.
[mono.git] / mcs / tools / type-reflector / TypeLoader.cs
1 //
2 // TypeLoader.cs: Loads types from a list of Assemblies
3 //
4 // Author: Jonathan Pryor (jonpryor@vt.edu)
5 //
6 // (C) 2002 Jonathan Pryor
7 //
8 // Permission is hereby granted, free of charge, to any           
9 // person obtaining a copy of this software and associated        
10 // documentation files (the "Software"), to deal in the           
11 // Software without restriction, including without limitation     
12 // the rights to use, copy, modify, merge, publish,               
13 // distribute, sublicense, and/or sell copies of the Software,    
14 // and to permit persons to whom the Software is furnished to     
15 // do so, subject to the following conditions:                    
16 //                                                                 
17 // The above copyright notice and this permission notice          
18 // shall be included in all copies or substantial portions        
19 // of the Software.                                               
20 //                                                                 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY      
22 // KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO         
23 // THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A               
24 // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL      
25 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,      
26 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  
27 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION       
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Collections;
33 using System.IO;
34 using System.Diagnostics;
35 using System.Reflection;
36 using System.Text;
37 using System.Text.RegularExpressions;
38
39 namespace Mono.TypeReflector
40 {
41         public class TypeLoader {
42
43                 // String collection
44                 private ICollection assemblies = null;
45
46                 private bool matchFullName = true;
47                 private bool matchName = false;
48                 private bool matchBase = false;
49                 private bool matchMethodReturnType = false;
50                 private bool matchNamespace = false;
51
52                 public bool MatchFullName {
53                         get {return matchFullName;}
54                         set {matchFullName = value;}
55                 }
56
57                 public bool MatchClassName {
58                         get {return matchName;}
59                         set {matchName = value;}
60                 }
61
62                 public bool MatchBase {
63                         get {return matchBase;}
64                         set {matchBase = value;}
65                 }
66
67                 public bool MatchMethodReturnType {
68                         get {return matchMethodReturnType;}
69                         set {matchMethodReturnType = value;}
70                 }
71
72                 public bool MatchNamespace {
73                         get {return matchNamespace;}
74                         set {matchNamespace = value;}
75                 }
76
77                 public TypeLoader ()
78                 {
79                 }
80
81                 public TypeLoader (ICollection assemblies)
82                 {
83                         this.assemblies = assemblies;
84                 }
85
86                 public ICollection Assemblies {
87                         get {return assemblies;}
88                         set {assemblies = value;}
89                 }
90
91                 public ICollection LoadTypes (string match)
92                 {
93                         if (assemblies == null)
94                                 throw new ArgumentNullException ("Assemblies");
95
96                         IList found = new ArrayList ();
97
98                         foreach (string a in assemblies) {
99                                 LoadMatchingTypesFrom (a, match, found);
100                         }
101
102                         return found;
103                 }
104
105                 private void LoadMatchingTypesFrom (string where, string match, IList types)
106                 {
107                         Regex re = new Regex (match);
108                         try {
109                                 Assembly a = Assembly.LoadFrom (where);
110                                 Type[] _types = a.GetTypes();
111                                 foreach (Type t in _types) {
112                                         if (Matches (re, t))
113                                                 types.Add (t);
114                                 }
115                         } catch (Exception e) {
116                                 Trace.WriteLine (String.Format (
117                                         "Unable to load type regex `{0}' from `{1}'.",
118                                         match, where));
119                                 Trace.WriteLine (e.ToString());
120                         }
121                 }
122
123                 private bool Matches (Regex r, Type t)
124                 {
125                         bool f, c, b, rt, n;
126                         f = c = b = rt = n = false;
127                         if (MatchFullName)
128                                 f = r.Match(t.FullName).Success;
129                         if (MatchClassName)
130                                 c = r.Match(t.Name).Success;
131                         if (MatchNamespace)
132                                 n = r.Match(t.Namespace).Success;
133                         if (MatchBase) {
134                                 b = (!MatchFullName ? false : r.Match (t.BaseType.FullName).Success) ||
135                                     (!MatchClassName ? false : r.Match (t.BaseType.Name).Success) ||
136                                     (!MatchNamespace ? false : r.Match (t.BaseType.Namespace).Success);
137                         }
138                         // TODO: MatchMethodReturnType
139                         Trace.WriteLine (String.Format("TypeLoader.Matches: c={0}, b={1}, rt={2}, n={3}", c, b, rt, n));
140                         return f || c || b || rt || n;
141                 }
142         }
143 }
144