Skip to content

Commit b991bb8

Browse files
authored
[generator] Revert change to use auto-properties in EventArgs classes (#736)
In ee7afee, properties in generated EventArgs classes were changed from regular properties: int param1; public int Param1 { get { return param1; } } to instead emit auto-properties: public int Param1 { get; } However, this is technically a source breaking change, since users could have hand written code that references the backing field. Although the usage is likely rare, we have decided to revert the auto-property change for compatibility reasons.
1 parent ee50d89 commit b991bb8

File tree

3 files changed

+90
-34
lines changed

3 files changed

+90
-34
lines changed

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/Common/WriteDuplicateInterfaceEventArgs.txt

+22-9
Original file line numberDiff line numberDiff line change
@@ -123,24 +123,37 @@ internal partial class AnimatorListenerInvoker : global::Java.Lang.Object, Anima
123123

124124
// event args for java.code.AnimatorListener.OnAnimationEnd
125125
public partial class AnimationEndEventArgs : global::System.EventArgs {
126+
bool handled;
127+
128+
public bool Handled {
129+
get { return handled; }
130+
set { handled = value; }
131+
}
132+
126133
public AnimationEndEventArgs (bool handled, int param1)
127134
{
128-
this.Handled = handled;
129-
this.Param1 = param1;
135+
this.handled = handled;
136+
this.param1 = param1;
137+
}
138+
139+
int param1;
140+
141+
public int Param1 {
142+
get { return param1; }
130143
}
131144

132145
public AnimationEndEventArgs (bool handled, int param1, int param2)
133146
{
134-
this.Handled = handled;
135-
this.Param1 = param1;
136-
this.Param2 = param2;
147+
this.handled = handled;
148+
this.param1 = param1;
149+
this.param2 = param2;
137150
}
138151

139-
public bool Handled { get; set; }
152+
int param2;
140153

141-
public int Param1 { get; private set; }
142-
143-
public int Param2 { get; private set; }
154+
public int Param2 {
155+
get { return param2; }
156+
}
144157

145158
}
146159

tests/generator-Tests/expected.ji/GenericArguments/Com.Google.Android.Exoplayer.Drm.IExoMediaDrm.cs

+30-10
Original file line numberDiff line numberDiff line change
@@ -123,22 +123,42 @@ public unsafe void OnEvent (global::Com.Google.Android.Exoplayer.Drm.IExoMediaDr
123123
public partial class ExoMediaDrmOnEventEventArgs : global::System.EventArgs {
124124
public ExoMediaDrmOnEventEventArgs (global::Com.Google.Android.Exoplayer.Drm.IExoMediaDrm p0, byte[] p1, int p2, int p3, byte[] p4)
125125
{
126-
this.P0 = p0;
127-
this.P1 = p1;
128-
this.P2 = p2;
129-
this.P3 = p3;
130-
this.P4 = p4;
126+
this.p0 = p0;
127+
this.p1 = p1;
128+
this.p2 = p2;
129+
this.p3 = p3;
130+
this.p4 = p4;
131131
}
132132

133-
public global::Com.Google.Android.Exoplayer.Drm.IExoMediaDrm P0 { get; private set; }
133+
global::Com.Google.Android.Exoplayer.Drm.IExoMediaDrm p0;
134134

135-
public byte[] P1 { get; private set; }
135+
public global::Com.Google.Android.Exoplayer.Drm.IExoMediaDrm P0 {
136+
get { return p0; }
137+
}
138+
139+
byte[] p1;
140+
141+
public byte[] P1 {
142+
get { return p1; }
143+
}
144+
145+
int p2;
136146

137-
public int P2 { get; private set; }
147+
public int P2 {
148+
get { return p2; }
149+
}
150+
151+
int p3;
138152

139-
public int P3 { get; private set; }
153+
public int P3 {
154+
get { return p3; }
155+
}
140156

141-
public byte[] P4 { get; private set; }
157+
byte[] p4;
158+
159+
public byte[] P4 {
160+
get { return p4; }
161+
}
142162

143163
}
144164

tools/generator/SourceWriters/InterfaceEventArgsClass.cs

+38-15
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,12 @@ public InterfaceEventArgsClass (InterfaceGen iface, Method method)
1818
IsPublic = true;
1919
IsPartial = true;
2020

21+
UsePriorityOrder = true;
22+
2123
Comments.Add ($"// event args for {iface.JavaName}.{method.JavaName}");
2224

23-
// Add: public bool Handled { get; set; }
2425
if (method.IsEventHandlerWithHandledProperty)
25-
Properties.Add (new PropertyWriter {
26-
Name = "Handled",
27-
PropertyType = TypeReferenceWriter.Bool,
28-
IsPublic = true,
29-
HasGet = true,
30-
HasSet = true,
31-
IsAutoProperty = true
32-
});
26+
Properties.Add (new HandledProperty ());
3327
}
3428

3529
public void AddMembersFromMethod (InterfaceGen iface, Method method, CodeGenerationOptions opt)
@@ -47,15 +41,15 @@ void AddConstructor (InterfaceGen iface, Method method, CodeGenerationOptions op
4741

4842
if (method.IsEventHandlerWithHandledProperty) {
4943
ctor.Parameters.Add (new MethodParameterWriter ("handled", TypeReferenceWriter.Bool));
50-
ctor.Body.Add ("this.Handled = handled;");
44+
ctor.Body.Add ("this.handled = handled;");
5145
}
5246

5347
foreach (var p in method.Parameters) {
5448
if (p.IsSender)
5549
continue;
5650

5751
ctor.Parameters.Add (new MethodParameterWriter (p.Name, new TypeReferenceWriter (opt.GetTypeReferenceName (p))));
58-
ctor.Body.Add ($"this.{p.PropertyName} = {opt.GetSafeIdentifier (p.Name)};");
52+
ctor.Body.Add ($"this.{opt.GetSafeIdentifier (p.Name)} = {opt.GetSafeIdentifier (p.Name)};");
5953
}
6054

6155
Constructors.Add (ctor);
@@ -71,18 +65,47 @@ void AddProperties (Method method, CodeGenerationOptions opt)
7165
if (Properties.Any (prop => prop.Name == p.PropertyName))
7266
continue;
7367

68+
Fields.Add (new FieldWriter {
69+
Name = opt.GetSafeIdentifier (p.Name),
70+
Type = new TypeReferenceWriter (opt.GetTypeReferenceName (p))
71+
});
72+
7473
var prop = new PropertyWriter {
7574
Name = p.PropertyName,
7675
PropertyType = new TypeReferenceWriter (opt.GetTypeReferenceName (p)),
7776
IsPublic = true,
78-
HasGet = true,
79-
HasSet = true,
80-
IsAutoProperty = true,
81-
AutoSetterVisibility = Visibility.Private
77+
HasGet = true
8278
};
8379

80+
prop.GetBody.Add ($"return {opt.GetSafeIdentifier (p.Name)};");
81+
8482
Properties.Add (prop);
8583
}
8684
}
8785
}
86+
87+
public class HandledProperty : PropertyWriter
88+
{
89+
public HandledProperty ()
90+
{
91+
Name = "Handled";
92+
PropertyType = TypeReferenceWriter.Bool;
93+
94+
IsPublic = true;
95+
96+
HasGet = true;
97+
GetBody.Add ("return handled;");
98+
99+
HasSet = true;
100+
SetBody.Add ("handled = value;");
101+
}
102+
103+
public override void Write (CodeWriter writer)
104+
{
105+
writer.WriteLine ("bool handled;");
106+
writer.WriteLine ();
107+
108+
base.Write (writer);
109+
}
110+
}
88111
}

0 commit comments

Comments
 (0)