Author: hammett Date: Sun Sep 19 00:09:31 2004 New Revision: 46323 Added: avalon/trunk/central/laboratory/avalon-net/DynamicProxy/Builder/GeneratorContext.cs (contents, props changed) Modified: avalon/trunk/central/laboratory/avalon-net/DynamicProxy/Apache.Avalon.DynamicProxy.csproj avalon/trunk/central/laboratory/avalon-net/DynamicProxy/Builder/CodeGenerators/BaseCodeGenerator.cs avalon/trunk/central/laboratory/avalon-net/DynamicProxy/Builder/CodeGenerators/ClassProxyGenerator.cs avalon/trunk/central/laboratory/avalon-net/DynamicProxy/Builder/CodeGenerators/InterfaceProxyGenerator.cs avalon/trunk/central/laboratory/avalon-net/DynamicProxy/Builder/DefaultProxyBuilder.cs avalon/trunk/central/laboratory/avalon-net/DynamicProxy/Builder/IProxyBuilder.cs avalon/trunk/central/laboratory/avalon-net/DynamicProxy/DynamicProxyTest/CustomProxyGeneratorTestCase.cs avalon/trunk/central/laboratory/avalon-net/DynamicProxy/ProxyGenerator.cs Log: Bug fixes.
Modified: avalon/trunk/central/laboratory/avalon-net/DynamicProxy/Apache.Avalon.DynamicProxy.csproj ============================================================================== --- avalon/trunk/central/laboratory/avalon-net/DynamicProxy/Apache.Avalon.DynamicProxy.csproj (original) +++ avalon/trunk/central/laboratory/avalon-net/DynamicProxy/Apache.Avalon.DynamicProxy.csproj Sun Sep 19 00:09:31 2004 @@ -109,6 +109,11 @@ BuildAction = "Compile" /> <File + RelPath = "Builder\GeneratorContext.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File RelPath = "Builder\IProxyBuilder.cs" SubType = "Code" BuildAction = "Compile" Modified: avalon/trunk/central/laboratory/avalon-net/DynamicProxy/Builder/CodeGenerators/BaseCodeGenerator.cs ============================================================================== --- avalon/trunk/central/laboratory/avalon-net/DynamicProxy/Builder/CodeGenerators/BaseCodeGenerator.cs (original) +++ avalon/trunk/central/laboratory/avalon-net/DynamicProxy/Builder/CodeGenerators/BaseCodeGenerator.cs Sun Sep 19 00:09:31 2004 @@ -24,33 +24,29 @@ /// </summary> public abstract class BaseCodeGenerator { + private static readonly String FILE_NAME = "GeneratedAssembly.dll"; + + private Type m_baseType = typeof(Object); + private AssemblyBuilder m_assemblyBuilder; private TypeBuilder m_typeBuilder; private FieldBuilder m_handlerField; private ConstructorBuilder m_constBuilder; private IList m_generated = new ArrayList(); - private EnhanceTypeDelegate m_enhanceDelegate; - private ScreenInterfacesDelegate m_screenInterfacesDelegate; - - protected BaseCodeGenerator() - { - } + private GeneratorContext m_context; - protected BaseCodeGenerator(EnhanceTypeDelegate enhance, - ScreenInterfacesDelegate screenInterfaces) + protected BaseCodeGenerator(GeneratorContext context) { - m_enhanceDelegate = enhance; - m_screenInterfacesDelegate = screenInterfaces; + m_context = context; } - protected EnhanceTypeDelegate EnhanceTypeDelegate + protected BaseCodeGenerator() : this(new GeneratorContext()) { - get { return m_enhanceDelegate; } } - protected ScreenInterfacesDelegate ScreenInterfacesDelegate + protected GeneratorContext Context { - get { return m_screenInterfacesDelegate; } + get { return m_context; } } protected TypeBuilder MainTypeBuilder @@ -73,18 +69,30 @@ AssemblyName assemblyName = new AssemblyName(); assemblyName.Name = "DynamicAssemblyProxyGen"; - AssemblyBuilder assemblyBuilder = + ModuleBuilder moduleBuilder = null; + +#if (DEBUG) + m_assemblyBuilder = + AppDomain.CurrentDomain.DefineDynamicAssembly( + assemblyName, + AssemblyBuilderAccess.RunAndSave); + moduleBuilder = m_assemblyBuilder.DefineDynamicModule(assemblyName.Name, FILE_NAME); +#else + m_assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly( assemblyName, AssemblyBuilderAccess.Run); + moduleBuilder = m_assemblyBuilder.DefineDynamicModule(assemblyName.Name, true); +#endif - return assemblyBuilder.DefineDynamicModule(assemblyName.Name, true); + return moduleBuilder; } protected virtual TypeBuilder CreateTypeBuilder(Type baseType, Type[] interfaces) { ModuleBuilder moduleBuilder = CreateDynamicModule(); + m_baseType = baseType; m_typeBuilder = moduleBuilder.DefineType( "ProxyType", TypeAttributes.Public | TypeAttributes.Class, baseType, interfaces); @@ -94,14 +102,52 @@ return m_typeBuilder; } + protected virtual void EnhanceType() + { + if (Context.EnhanceType != null) + { + Context.EnhanceType(MainTypeBuilder, HandlerFieldBuilder, DefaultConstructorBuilder); + } + } + + protected virtual Type[] ScreenInterfaces(Type[] interfaces) + { + if (Context.ScreenInterfaces != null) + { + interfaces = Context.ScreenInterfaces(interfaces); + } + + return interfaces; + } + + protected virtual Type CreateType() + { + Type newType = MainTypeBuilder.CreateType(); +#if (DEBUG) + m_assemblyBuilder.Save(FILE_NAME); +#endif + return newType; + } + /// <summary> /// Generates a public field holding the <see cref="IInvocationHandler"/> /// </summary> /// <returns><see cref="FieldBuilder"/> instance</returns> protected FieldBuilder GenerateField() { - return m_typeBuilder.DefineField("handler", - typeof (IInvocationHandler), FieldAttributes.Public); + return GenerateField("handler", typeof (IInvocationHandler) ); + } + + /// <summary> + /// Generates a public field + /// </summary> + /// <param name="name">Field's name</param> + /// <param name="type">Field's type</param> + /// <returns></returns> + protected FieldBuilder GenerateField( String name, Type type ) + { + return m_typeBuilder.DefineField(name, + typeof (IInvocationHandler), FieldAttributes.Public); } /// <summary> @@ -118,7 +164,7 @@ ILGenerator ilGenerator = consBuilder.GetILGenerator(); ilGenerator.Emit(OpCodes.Ldarg_0); - ilGenerator.Emit(OpCodes.Call, typeof (Object).GetConstructor(new Type[0])); + ilGenerator.Emit(OpCodes.Call, m_baseType.GetConstructor(new Type[0])); ilGenerator.Emit(OpCodes.Ldarg_0); ilGenerator.Emit(OpCodes.Ldarg_1); ilGenerator.Emit(OpCodes.Stfld, m_handlerField); @@ -135,7 +181,10 @@ { foreach(Type inter in interfaces) { - GenerateTypeImplementation(inter, false); + if (!Context.ShouldSkip( inter )) + { + GenerateTypeImplementation(inter, false); + } } } Modified: avalon/trunk/central/laboratory/avalon-net/DynamicProxy/Builder/CodeGenerators/ClassProxyGenerator.cs ============================================================================== --- avalon/trunk/central/laboratory/avalon-net/DynamicProxy/Builder/CodeGenerators/ClassProxyGenerator.cs (original) +++ avalon/trunk/central/laboratory/avalon-net/DynamicProxy/Builder/CodeGenerators/ClassProxyGenerator.cs Sun Sep 19 00:09:31 2004 @@ -25,29 +25,21 @@ { } - public ClassProxyGenerator(EnhanceTypeDelegate enhance, ScreenInterfacesDelegate screenInterfaces) : - base(enhance, screenInterfaces) + public ClassProxyGenerator(GeneratorContext context) : base(context) { } public Type GenerateCode(Type baseClass) { Type[] interfaces = new Type[0]; - - if (ScreenInterfacesDelegate != null) - { - interfaces = ScreenInterfacesDelegate(interfaces); - } + interfaces = ScreenInterfaces(interfaces); CreateTypeBuilder( baseClass, interfaces ); GenerateTypeImplementation( baseClass, true ); - if (EnhanceTypeDelegate != null) - { - EnhanceTypeDelegate(MainTypeBuilder, HandlerFieldBuilder, DefaultConstructorBuilder); - } + EnhanceType(); - return MainTypeBuilder.CreateType(); + return CreateType(); } } } Modified: avalon/trunk/central/laboratory/avalon-net/DynamicProxy/Builder/CodeGenerators/InterfaceProxyGenerator.cs ============================================================================== --- avalon/trunk/central/laboratory/avalon-net/DynamicProxy/Builder/CodeGenerators/InterfaceProxyGenerator.cs (original) +++ avalon/trunk/central/laboratory/avalon-net/DynamicProxy/Builder/CodeGenerators/InterfaceProxyGenerator.cs Sun Sep 19 00:09:31 2004 @@ -15,8 +15,6 @@ namespace Apache.Avalon.DynamicProxy.Builder.CodeGenerators { using System; - using System.Reflection; - using System.Reflection.Emit; /// <summary> /// Summary description for InterfaceProxyGenerator. @@ -27,28 +25,21 @@ { } - public InterfaceProxyGenerator(EnhanceTypeDelegate enhance, ScreenInterfacesDelegate screenInterfaces) : - base(enhance, screenInterfaces) + public InterfaceProxyGenerator(GeneratorContext context) : base(context) { - } - public Type GenerateCode(Type[] interfaces) + public virtual Type GenerateCode(Type[] interfaces) { - if (ScreenInterfacesDelegate != null) - { - interfaces = ScreenInterfacesDelegate(interfaces); - } + interfaces = ScreenInterfaces(interfaces); - CreateTypeBuilder( null, interfaces ); + CreateTypeBuilder( typeof(Object), interfaces ); GenerateInterfaceImplementation( interfaces ); - if (EnhanceTypeDelegate != null) - { - EnhanceTypeDelegate(MainTypeBuilder, HandlerFieldBuilder, DefaultConstructorBuilder); - } + EnhanceType(); - return MainTypeBuilder.CreateType(); + return CreateType(); } + } } Modified: avalon/trunk/central/laboratory/avalon-net/DynamicProxy/Builder/DefaultProxyBuilder.cs ============================================================================== --- avalon/trunk/central/laboratory/avalon-net/DynamicProxy/Builder/DefaultProxyBuilder.cs (original) +++ avalon/trunk/central/laboratory/avalon-net/DynamicProxy/Builder/DefaultProxyBuilder.cs Sun Sep 19 00:09:31 2004 @@ -24,29 +24,27 @@ { #region IProxyBuilder Members - public Type CreateInterfaceProxy(Type[] interfaces) + public virtual Type CreateInterfaceProxy(Type[] interfaces) { InterfaceProxyGenerator generator = new InterfaceProxyGenerator(); return generator.GenerateCode( interfaces ); } - public Type CreateClassProxy(Type theClass) + public virtual Type CreateClassProxy(Type theClass) { ClassProxyGenerator generator = new ClassProxyGenerator(); return generator.GenerateCode( theClass ); } - public Type CreateCustomInterfaceProxy(Type[] interfaces, EnhanceTypeDelegate enhance, - ScreenInterfacesDelegate screenInterfaces) + public virtual Type CreateCustomInterfaceProxy(Type[] interfaces, GeneratorContext context) { - InterfaceProxyGenerator generator = new InterfaceProxyGenerator(enhance, screenInterfaces); + InterfaceProxyGenerator generator = new InterfaceProxyGenerator(context); return generator.GenerateCode( interfaces ); } - public Type CreateCustomClassProxy(Type theClass, EnhanceTypeDelegate enhance, - ScreenInterfacesDelegate screenInterfaces) + public virtual Type CreateCustomClassProxy(Type theClass, GeneratorContext context) { - ClassProxyGenerator generator = new ClassProxyGenerator(enhance, screenInterfaces); + ClassProxyGenerator generator = new ClassProxyGenerator(context); return generator.GenerateCode( theClass ); } Added: avalon/trunk/central/laboratory/avalon-net/DynamicProxy/Builder/GeneratorContext.cs ============================================================================== --- (empty file) +++ avalon/trunk/central/laboratory/avalon-net/DynamicProxy/Builder/GeneratorContext.cs Sun Sep 19 00:09:31 2004 @@ -0,0 +1,68 @@ +// Copyright 2004 The Apache Software Foundation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace Apache.Avalon.DynamicProxy +{ + using System; + using System.Collections; + + /// <summary> + /// Summary description for GeneratorContext. + /// </summary> + public sealed class GeneratorContext : DictionaryBase + { + private EnhanceTypeDelegate m_enhance; + private ScreenInterfacesDelegate m_screenInterfaces; + private IList m_skipInterfaces = new ArrayList(); + + public GeneratorContext() + { + } + + public GeneratorContext(EnhanceTypeDelegate enhanceDelegate, + ScreenInterfacesDelegate screenDelegate) + { + m_enhance = enhanceDelegate; + m_screenInterfaces = screenDelegate; + } + + public EnhanceTypeDelegate EnhanceType + { + get { return m_enhance; } + set { m_enhance = value; } + } + + public ScreenInterfacesDelegate ScreenInterfaces + { + get { return m_screenInterfaces; } + set { m_screenInterfaces = value; } + } + + public bool ShouldSkip( Type interfaceType ) + { + return m_skipInterfaces.Contains( interfaceType ); + } + + public void AddInterfaceToSkip( Type interfaceType ) + { + m_skipInterfaces.Add( interfaceType ); + } + + public object this[ String key ] + { + get { return Dictionary[key]; } + set { Dictionary[key] = value; } + } + } +} Modified: avalon/trunk/central/laboratory/avalon-net/DynamicProxy/Builder/IProxyBuilder.cs ============================================================================== --- avalon/trunk/central/laboratory/avalon-net/DynamicProxy/Builder/IProxyBuilder.cs (original) +++ avalon/trunk/central/laboratory/avalon-net/DynamicProxy/Builder/IProxyBuilder.cs Sun Sep 19 00:09:31 2004 @@ -23,12 +23,10 @@ { Type CreateInterfaceProxy( Type[] interfaces ); - Type CreateCustomInterfaceProxy( Type[] interfaces, EnhanceTypeDelegate enhance, - ScreenInterfacesDelegate screenInterfaces ); + Type CreateCustomInterfaceProxy( Type[] interfaces, GeneratorContext context ); Type CreateClassProxy( Type theClass ); - Type CreateCustomClassProxy( Type theClass, EnhanceTypeDelegate enhance, - ScreenInterfacesDelegate screenInterfaces ); + Type CreateCustomClassProxy( Type theClass, GeneratorContext context ); } } Modified: avalon/trunk/central/laboratory/avalon-net/DynamicProxy/DynamicProxyTest/CustomProxyGeneratorTestCase.cs ============================================================================== --- avalon/trunk/central/laboratory/avalon-net/DynamicProxy/DynamicProxyTest/CustomProxyGeneratorTestCase.cs (original) +++ avalon/trunk/central/laboratory/avalon-net/DynamicProxy/DynamicProxyTest/CustomProxyGeneratorTestCase.cs Sun Sep 19 00:09:31 2004 @@ -31,7 +31,6 @@ private ProxyGenerator m_generator; private bool m_enhanceInvoked; private bool m_screenInvoked; - private bool m_constructorInvoked; [SetUp] public void Init() @@ -39,37 +38,37 @@ m_generator = new ProxyGenerator(); m_enhanceInvoked = false; m_screenInvoked = false; - m_constructorInvoked = false; } [Test] public void CreateCustomProxy() { + GeneratorContext context = new GeneratorContext( + new EnhanceTypeDelegate(EnhanceType), + new ScreenInterfacesDelegate(ScreenInterfaces)); + object proxy = m_generator.CreateCustomProxy( typeof (IMyInterface), - new StandardInvocationHandler(new MyInterfaceImpl()), - new EnhanceTypeDelegate(EnhanceType), - new ScreenInterfacesDelegate(ScreenInterfaces), - new ConstructorArgumentsDelegate(BuildConstructorArguments)); + new StandardInvocationHandler(new MyInterfaceImpl()), context); Assert( m_enhanceInvoked ); Assert( m_screenInvoked ); - Assert( m_constructorInvoked ); } [Test] public void CreateCustomClassProxy() { + GeneratorContext context = new GeneratorContext( + new EnhanceTypeDelegate(EnhanceType), + new ScreenInterfacesDelegate(ScreenInterfaces)); + object proxy = m_generator.CreateCustomClassProxy( typeof (ServiceClass), new StandardInvocationHandler(new ServiceClass()), - new EnhanceTypeDelegate(EnhanceType), - new ScreenInterfacesDelegate(ScreenInterfaces), - new ConstructorArgumentsDelegate(BuildConstructorArguments)); + context); Assert( m_enhanceInvoked ); Assert( m_screenInvoked ); - Assert( m_constructorInvoked ); } private void EnhanceType(TypeBuilder mainType, FieldBuilder handlerFieldBuilder, ConstructorBuilder constructorBuilder) @@ -92,18 +91,6 @@ m_screenInvoked = true; return interfaces; - } - - private object[] BuildConstructorArguments(Type generatedType, IInvocationHandler handler) - { - Assert( !m_constructorInvoked ); - - AssertNotNull( generatedType ); - AssertNotNull( handler ); - - m_constructorInvoked = true; - - return new object[] { handler }; } } } Modified: avalon/trunk/central/laboratory/avalon-net/DynamicProxy/ProxyGenerator.cs ============================================================================== --- avalon/trunk/central/laboratory/avalon-net/DynamicProxy/ProxyGenerator.cs (original) +++ avalon/trunk/central/laboratory/avalon-net/DynamicProxy/ProxyGenerator.cs Sun Sep 19 00:09:31 2004 @@ -23,8 +23,6 @@ public delegate Type[] ScreenInterfacesDelegate( Type[] interfaces ); - public delegate object[] ConstructorArgumentsDelegate( Type generatedType, IInvocationHandler handler ); - /// <summary> /// Generates a Java style proxy. This overrides the .Net proxy requirements /// that forces one to extend MarshalByRefObject or (for a different purpose) @@ -56,7 +54,7 @@ m_builder = builder; } - public ProxyGenerator() : this( new ProxyBuilderImpl() ) + public ProxyGenerator() : this( new DefaultProxyBuilder() ) { } @@ -74,15 +72,13 @@ return CreateProxyInstance( newType, handler ); } - public virtual object CreateCustomClassProxy(Type baseClass, IInvocationHandler handler, - EnhanceTypeDelegate enhance, - ScreenInterfacesDelegate screenInterfaces, - ConstructorArgumentsDelegate constructorArguments) + public virtual object CreateCustomClassProxy(Type baseClass, + IInvocationHandler handler, GeneratorContext context) { - AssertCreateClassProxyArguments(baseClass, handler); + AssertCreateClassProxyArguments(baseClass, handler, context); - Type newType = ProxyBuilder.CreateCustomClassProxy(baseClass, enhance, screenInterfaces); - return CreateCustomProxyInstance( newType, handler, constructorArguments ); + Type newType = ProxyBuilder.CreateCustomClassProxy(baseClass, context); + return CreateProxyInstance( newType, handler, context ); } /// <summary> @@ -117,17 +113,13 @@ /// </summary> /// <param name="theInterface"></param> /// <param name="handler"></param> - /// <param name="enhance"></param> - /// <param name="screenInterfaces"></param> - /// <param name="constructorArguments"></param> + /// <param name="context"></param> /// <returns></returns> public virtual object CreateCustomProxy(Type theInterface, - IInvocationHandler handler, EnhanceTypeDelegate enhance, - ScreenInterfacesDelegate screenInterfaces, - ConstructorArgumentsDelegate constructorArguments ) + IInvocationHandler handler, + GeneratorContext context ) { - return CreateCustomProxy( new Type[] { theInterface }, handler, - enhance, screenInterfaces, constructorArguments ); + return CreateCustomProxy( new Type[] { theInterface }, handler, context ); } /// <summary> @@ -135,18 +127,14 @@ /// </summary> /// <param name="interfaces"></param> /// <param name="handler"></param> - /// <param name="enhance"></param> - /// <param name="screenInterfaces"></param> - /// <param name="constructorArguments"></param> + /// <param name="context"></param> /// <returns></returns> public virtual object CreateCustomProxy(Type[] interfaces, - IInvocationHandler handler, EnhanceTypeDelegate enhance, - ScreenInterfacesDelegate screenInterfaces, - ConstructorArgumentsDelegate constructorArguments ) - { - AssertCreateProxyArguments( interfaces, handler); - Type newType = ProxyBuilder.CreateCustomInterfaceProxy(interfaces, enhance, screenInterfaces); - return CreateCustomProxyInstance( newType, handler, constructorArguments ); + IInvocationHandler handler, GeneratorContext context ) + { + AssertCreateProxyArguments( interfaces, handler, context ); + Type newType = ProxyBuilder.CreateCustomInterfaceProxy(interfaces, context); + return CreateProxyInstance( newType, handler, context ); } protected virtual object CreateProxyInstance(Type type, IInvocationHandler handler) @@ -154,17 +142,9 @@ return Activator.CreateInstance(type, new object[] {handler}); } - protected virtual object CreateCustomProxyInstance(Type type, IInvocationHandler handler, ConstructorArgumentsDelegate constructorArguments) + protected virtual object CreateProxyInstance(Type type, IInvocationHandler handler, GeneratorContext context) { - if (constructorArguments != null) - { - object[] arguments = constructorArguments( type, handler ); - return Activator.CreateInstance(type, arguments); - } - else - { - return CreateProxyInstance( type, handler ); - } + return CreateProxyInstance( type, handler ); } protected static void AssertCreateProxyArguments(Type[] interfaces, IInvocationHandler handler) @@ -183,6 +163,16 @@ } } + protected static void AssertCreateProxyArguments(Type[] interfaces, IInvocationHandler handler, GeneratorContext context) + { + AssertCreateProxyArguments(interfaces, handler); + + if (context == null) + { + throw new ArgumentNullException("context"); + } + } + protected static void AssertCreateClassProxyArguments(Type baseClass, IInvocationHandler handler) { if (baseClass == null) @@ -196,6 +186,15 @@ if (handler == null) { throw new ArgumentNullException("handler"); + } + } + + protected static void AssertCreateClassProxyArguments(Type baseClass, IInvocationHandler handler, GeneratorContext context) + { + AssertCreateClassProxyArguments(baseClass, handler); + if (context == null) + { + throw new ArgumentNullException("context"); } } } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]