Skip to content

Commit 7a06b94

Browse files
authored
Merge pull request #713 from stakx/refactor/member-and-type-emitters
Remove some unnecessary code in member and type emitters
2 parents efff684 + 0efd7a5 commit 7a06b94

16 files changed

+393
-709
lines changed

src/Castle.Core.Tests/DynamicProxy.Tests/ClassEmitterTestCase.cs

Lines changed: 1 addition & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2004-2021 Castle Project - http://www.castleproject.org/
1+
// Copyright 2004-2025 Castle Project - http://www.castleproject.org/
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -106,73 +106,5 @@ public void CreateStaticFieldWithAttributes()
106106
Assert.IsNotNull(field);
107107
Assert.AreEqual(FieldAttributes.Static | FieldAttributes.FamANDAssem | FieldAttributes.InitOnly, field.Attributes);
108108
}
109-
110-
[Test]
111-
public void UsingClassEmitterForInterfaces()
112-
{
113-
ClassEmitter emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "IFoo", null, Type.EmptyTypes,
114-
TypeAttributes.Interface | TypeAttributes.Abstract | TypeAttributes.Public, false);
115-
emitter.CreateMethod("MyMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual,
116-
typeof(void), Type.EmptyTypes);
117-
Type t = emitter.BuildType();
118-
Assert.IsTrue(t.IsInterface);
119-
MethodInfo method = t.GetMethod("MyMethod");
120-
Assert.IsNotNull(method);
121-
}
122-
123-
[Test]
124-
public void NoBaseTypeForInterfaces()
125-
{
126-
DisableVerification();
127-
ClassEmitter emitter = new ClassEmitter (generator.ProxyBuilder.ModuleScope, "IFoo", null, Type.EmptyTypes,
128-
TypeAttributes.Interface | TypeAttributes.Abstract | TypeAttributes.Public, false);
129-
130-
Assert.Throws<InvalidOperationException>(delegate {
131-
#pragma warning disable 219
132-
Type t = emitter.BaseType;
133-
#pragma warning restore 219
134-
});
135-
}
136-
137-
[Test]
138-
public void NoDefaultCtorForInterfaces()
139-
{
140-
DisableVerification();
141-
ClassEmitter emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "IFoo", null, Type.EmptyTypes,
142-
TypeAttributes.Interface | TypeAttributes.Abstract | TypeAttributes.Public, false);
143-
144-
Assert.Throws<InvalidOperationException>(delegate {
145-
emitter.CreateDefaultConstructor();
146-
});
147-
}
148-
149-
[Test]
150-
public void NoCustomCtorForInterfaces()
151-
{
152-
DisableVerification();
153-
ClassEmitter emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "IFoo", null, Type.EmptyTypes,
154-
TypeAttributes.Interface | TypeAttributes.Abstract | TypeAttributes.Public, false);
155-
156-
Assert.Throws<InvalidOperationException>(delegate {
157-
emitter.CreateConstructor();
158-
});
159-
}
160-
161-
[Test]
162-
public void NestedInterface()
163-
{
164-
ClassEmitter outerEmitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "IOuter", null, Type.EmptyTypes,
165-
TypeAttributes.Interface | TypeAttributes.Abstract | TypeAttributes.Public, false);
166-
NestedClassEmitter innerEmitter = new NestedClassEmitter(outerEmitter, "IInner",
167-
TypeAttributes.Interface | TypeAttributes.Abstract | TypeAttributes.NestedPublic, null, Type.EmptyTypes);
168-
innerEmitter.CreateMethod("MyMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual,
169-
typeof(void), Type.EmptyTypes);
170-
Type inner = innerEmitter.BuildType();
171-
Type outer = outerEmitter.BuildType();
172-
Assert.IsTrue(inner.IsInterface);
173-
MethodInfo method = inner.GetMethod("MyMethod");
174-
Assert.IsNotNull(method);
175-
Assert.AreSame(inner, outer.GetNestedType("IInner", BindingFlags.Public));
176-
}
177109
}
178110
}

src/Castle.Core/DynamicProxy/Contributors/IInvocationCreationContributor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2004-2021 Castle Project - http://www.castleproject.org/
1+
// Copyright 2004-2025 Castle Project - http://www.castleproject.org/
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -21,11 +21,11 @@ namespace Castle.DynamicProxy.Contributors
2121

2222
internal interface IInvocationCreationContributor
2323
{
24-
ConstructorEmitter CreateConstructor(ArgumentReference[] baseCtorArguments, AbstractTypeEmitter invocation);
24+
ConstructorEmitter CreateConstructor(ArgumentReference[] baseCtorArguments, ClassEmitter invocation);
2525

2626
MethodInfo GetCallbackMethod();
2727

28-
MethodInvocationExpression GetCallbackMethodInvocation(AbstractTypeEmitter invocation, IExpression[] args,
28+
MethodInvocationExpression GetCallbackMethodInvocation(ClassEmitter invocation, IExpression[] args,
2929
Reference targetField, MethodEmitter invokeMethodOnTarget);
3030

3131
IExpression[] GetConstructorInvocationArguments(IExpression[] arguments, ClassEmitter proxy);

src/Castle.Core/DynamicProxy/Contributors/InvocationWithDelegateContributor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2004-2021 Castle Project - http://www.castleproject.org/
1+
// Copyright 2004-2025 Castle Project - http://www.castleproject.org/
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -40,7 +40,7 @@ public InvocationWithDelegateContributor(Type delegateType, Type targetType, Met
4040
this.namingScope = namingScope;
4141
}
4242

43-
public ConstructorEmitter CreateConstructor(ArgumentReference[] baseCtorArguments, AbstractTypeEmitter invocation)
43+
public ConstructorEmitter CreateConstructor(ArgumentReference[] baseCtorArguments, ClassEmitter invocation)
4444
{
4545
var arguments = GetArguments(baseCtorArguments);
4646
var constructor = invocation.CreateConstructor(arguments);
@@ -55,7 +55,7 @@ public MethodInfo GetCallbackMethod()
5555
return delegateType.GetMethod("Invoke");
5656
}
5757

58-
public MethodInvocationExpression GetCallbackMethodInvocation(AbstractTypeEmitter invocation, IExpression[] args,
58+
public MethodInvocationExpression GetCallbackMethodInvocation(ClassEmitter invocation, IExpression[] args,
5959
Reference targetField,
6060
MethodEmitter invokeMethodOnTarget)
6161
{

src/Castle.Core/DynamicProxy/Contributors/InvocationWithGenericDelegateContributor.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2004-2021 Castle Project - http://www.castleproject.org/
1+
// Copyright 2004-2025 Castle Project - http://www.castleproject.org/
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -39,7 +39,7 @@ public InvocationWithGenericDelegateContributor(Type delegateType, MetaMethod me
3939
this.targetReference = targetReference;
4040
}
4141

42-
public ConstructorEmitter CreateConstructor(ArgumentReference[] baseCtorArguments, AbstractTypeEmitter invocation)
42+
public ConstructorEmitter CreateConstructor(ArgumentReference[] baseCtorArguments, ClassEmitter invocation)
4343
{
4444
return invocation.CreateConstructor(baseCtorArguments);
4545
}
@@ -49,7 +49,7 @@ public MethodInfo GetCallbackMethod()
4949
return delegateType.GetMethod("Invoke");
5050
}
5151

52-
public MethodInvocationExpression GetCallbackMethodInvocation(AbstractTypeEmitter invocation, IExpression[] args,
52+
public MethodInvocationExpression GetCallbackMethodInvocation(ClassEmitter invocation, IExpression[] args,
5353
Reference targetField,
5454
MethodEmitter invokeMethodOnTarget)
5555
{
@@ -62,7 +62,7 @@ public IExpression[] GetConstructorInvocationArguments(IExpression[] arguments,
6262
return arguments;
6363
}
6464

65-
private Reference GetDelegate(AbstractTypeEmitter invocation, MethodEmitter invokeMethodOnTarget)
65+
private Reference GetDelegate(ClassEmitter invocation, MethodEmitter invokeMethodOnTarget)
6666
{
6767
var genericTypeParameters = invocation.GenericTypeParams.AsTypeArray();
6868
var closedDelegateType = delegateType.MakeGenericType(genericTypeParameters);

src/Castle.Core/DynamicProxy/Generators/CompositionInvocationTypeGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ protected override FieldReference GetTargetReference()
5757
return new FieldReference(InvocationMethods.CompositionInvocationTarget);
5858
}
5959

60-
protected override void ImplementInvokeMethodOnTarget(AbstractTypeEmitter invocation, ParameterInfo[] parameters,
60+
protected override void ImplementInvokeMethodOnTarget(ClassEmitter invocation, ParameterInfo[] parameters,
6161
MethodEmitter invokeMethodOnTarget, Reference targetField)
6262
{
6363
invokeMethodOnTarget.CodeBuilder.AddStatement(

src/Castle.Core/DynamicProxy/Generators/DelegateTypeGenerator.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2004-2021 Castle Project - http://www.castleproject.org/
1+
// Copyright 2004-2025 Castle Project - http://www.castleproject.org/
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -21,7 +21,7 @@ namespace Castle.DynamicProxy.Generators
2121
using Castle.DynamicProxy.Generators.Emitters.SimpleAST;
2222
using Castle.DynamicProxy.Internal;
2323

24-
internal class DelegateTypeGenerator : IGenerator<AbstractTypeEmitter>
24+
internal class DelegateTypeGenerator : IGenerator<ClassEmitter>
2525
{
2626
private const TypeAttributes DelegateFlags = TypeAttributes.Class |
2727
TypeAttributes.Public |
@@ -38,22 +38,22 @@ public DelegateTypeGenerator(MetaMethod method, Type targetType)
3838
this.targetType = targetType;
3939
}
4040

41-
public AbstractTypeEmitter Generate(ClassEmitter @class, INamingScope namingScope)
41+
public ClassEmitter Generate(ClassEmitter @class, INamingScope namingScope)
4242
{
4343
var emitter = GetEmitter(@class, namingScope);
4444
BuildConstructor(emitter);
4545
BuildInvokeMethod(emitter);
4646
return emitter;
4747
}
4848

49-
private void BuildConstructor(AbstractTypeEmitter emitter)
49+
private void BuildConstructor(ClassEmitter emitter)
5050
{
5151
var constructor = emitter.CreateConstructor(new ArgumentReference(typeof(object)),
5252
new ArgumentReference(typeof(IntPtr)));
5353
constructor.ConstructorBuilder.SetImplementationFlags(MethodImplAttributes.Runtime | MethodImplAttributes.Managed);
5454
}
5555

56-
private void BuildInvokeMethod(AbstractTypeEmitter @delegate)
56+
private void BuildInvokeMethod(ClassEmitter @delegate)
5757
{
5858
var paramTypes = GetParamTypes(@delegate);
5959
var invoke = @delegate.CreateMethod("Invoke",
@@ -66,7 +66,7 @@ private void BuildInvokeMethod(AbstractTypeEmitter @delegate)
6666
invoke.MethodBuilder.SetImplementationFlags(MethodImplAttributes.Runtime | MethodImplAttributes.Managed);
6767
}
6868

69-
private AbstractTypeEmitter GetEmitter(ClassEmitter @class, INamingScope namingScope)
69+
private ClassEmitter GetEmitter(ClassEmitter @class, INamingScope namingScope)
7070
{
7171
var methodInfo = method.MethodOnTarget;
7272
var suggestedName = string.Format("Castle.Proxies.Delegates.{0}_{1}",
@@ -84,7 +84,7 @@ private AbstractTypeEmitter GetEmitter(ClassEmitter @class, INamingScope namingS
8484
return @delegate;
8585
}
8686

87-
private Type[] GetParamTypes(AbstractTypeEmitter @delegate)
87+
private Type[] GetParamTypes(ClassEmitter @delegate)
8888
{
8989
var parameters = method.MethodOnTarget.GetParameters();
9090
if (@delegate.TypeBuilder.IsGenericType)

0 commit comments

Comments
 (0)