Added container accessor to Castle.Core
[castle.git] / AspectSharp / AspectSharp.Lang / Steps / Types / ResolveTypesStep.cs
blob2e2a3c05bd4cf8aa0624daf6dac3366f745e9296
1 using AspectSharp.Lang.AST.Visitors;
2 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
16 namespace AspectSharp.Lang.Steps.Types
18 using System;
19 using System.IO;
20 using System.Reflection;
21 using System.Collections;
23 using AspectSharp.Lang.AST;
24 using AspectSharp.Lang.Steps.Types.Resolution;
26 /// <summary>
27 /// Summary description for ResolveTypesStep.
28 /// </summary>
29 public class ResolveTypesStep : AbstractVisitorStep
31 private TypeManager _typeManager;
32 private IDictionary _mixinKey2TypeReference;
33 private IDictionary _interceptorKey2TypeReference;
35 public ResolveTypesStep()
37 _typeManager = new TypeManager();
38 _typeManager.InspectAppDomainAssemblies();
40 _mixinKey2TypeReference = new Hashtable();
41 _interceptorKey2TypeReference = new Hashtable();
44 public override void Process(Context context, EngineConfiguration conf)
46 Init(context);
47 Visit(conf);
49 if (!context.HasErrors)
51 base.Process(context, conf);
55 #region AbstractVisitorStep overrides
57 public override void OnAssemblyReference(AssemblyReference assemblyReference)
59 try
61 Assembly assembly;
62 try
64 assembly = Assembly.Load(assemblyReference.AssemblyName);
66 catch(FileNotFoundException)
68 assembly = GacHelper.FindAssembly(assemblyReference.AssemblyName);
70 assemblyReference.ResolvedAssembly = assembly;
72 catch(Exception ex)
74 Context.RaiseErrorEvent(
75 assemblyReference.LexicalInfo,
76 String.Format("Could not load assembly '{0}'. Reason '{1}'",
77 assemblyReference.AssemblyName, ex.Message) );
81 public override void OnTypeReferenceDefinition(TypeReference type)
83 if (type.TargetType == TargetTypeEnum.Type)
85 base.OnTypeReferenceDefinition (type);
87 if (type.AssemblyReference == null)
89 type.ResolvedType = _typeManager.ResolveType( type.TypeName );
91 else
93 type.ResolvedType = LoadType(
94 type.TypeName,
95 type.AssemblyReference.ResolvedAssembly.FullName,
96 type.LexicalInfo );
101 public override void OnImportDirective(ImportDirective import)
103 base.OnImportDirective (import);
105 if (import.AssemblyReference != null)
107 _typeManager.AddAssembly( import.AssemblyReference.ResolvedAssembly );
111 public override void OnInterceptorEntryDefinition(InterceptorEntryDefinition interceptor)
113 base.OnInterceptorEntryDefinition (interceptor);
114 _interceptorKey2TypeReference[interceptor.Key] = interceptor.TypeReference;
117 public override void OnMixinEntryDefinition(MixinEntryDefinition mixin)
119 base.OnMixinEntryDefinition (mixin);
120 _mixinKey2TypeReference[mixin.Key] = mixin.TypeReference;
123 public override void OnMixinDefinition(MixinDefinition mixin)
125 if (mixin.TypeReference.TargetType == TargetTypeEnum.Type)
127 base.OnMixinDefinition(mixin);
129 else
131 mixin.TypeReference =
132 _mixinKey2TypeReference[ mixin.TypeReference.LinkRef ] as TypeReference;
136 public override void OnInterceptorDefinition(InterceptorDefinition interceptor)
138 if (interceptor.TypeReference.TargetType == TargetTypeEnum.Type)
140 base.OnInterceptorDefinition(interceptor);
142 else
144 interceptor.TypeReference =
145 _interceptorKey2TypeReference[ interceptor.TypeReference.LinkRef ] as TypeReference;
149 #endregion
151 private Type LoadType( String typeName, String assemblyName, LexicalInfo info )
153 String qualifiedName = String.Format("{0}, {1}", typeName, assemblyName);
157 return Type.GetType( qualifiedName, true, false );
159 catch(Exception)
161 Context.RaiseErrorEvent( info, "Could not load type specified " + qualifiedName );
164 return null;