Skip to content

Commit 542be58

Browse files
jpobstSteve Pfister
authored andcommitted
[generator] Use [Obsolete(error:true)] for *InterfaceConsts classes (#490)
Context: #436 The `*InterfaceConsts` classes we've been generating for ages have been marked as `[Obsolete]` for at least 3 years: [Obsolete ("Use the 'MyInterface' type. This type will be removed in a future release.")] public abstract class MyInterfaceConsts : MyInterface { } It's time to tighten the screws a little bit and make this an error: [Obsolete ("Use the 'MyInterface' type. This type will be removed in a future release.", error: true)] public abstract class MyInterfaceConsts : MyInterface { }
1 parent f462752 commit 542be58

File tree

8 files changed

+208
-6
lines changed

8 files changed

+208
-6
lines changed

tools/generator/Java.Interop.Tools.Generator.CodeGeneration/CodeGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ public void WriteInterfaceImplementedMembersAlternative (InterfaceGen @interface
747747

748748
if (!@interface.HasManagedName) {
749749
writer.WriteLine ("{0}[Register (\"{1}\"{2}, DoNotGenerateAcw=true)]", indent, @interface.RawJniName, @interface.AdditionalAttributeString ());
750-
writer.WriteLine ("{0}[global::System.Obsolete (\"Use the '{1}' type. This type will be removed in a future release.\")]", indent, name);
750+
writer.WriteLine ("{0}[global::System.Obsolete (\"Use the '{1}' type. This type will be removed in a future release.\", error: true)]", indent, name);
751751
writer.WriteLine ("{0}public abstract class {1}Consts : {1} {{", indent, name);
752752
writer.WriteLine ();
753753
writer.WriteLine ("{0}\tprivate {1}Consts ()", indent, name);

tools/generator/Tests/Unit-Tests/CodeGeneratorExpectedResults/JavaInterop1/WriteInterface.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public abstract class MyInterface : Java.Lang.Object {
2020
}
2121

2222
[Register ("java/code/IMyInterface", DoNotGenerateAcw=true)]
23-
[global::System.Obsolete ("Use the 'MyInterface' type. This type will be removed in a future release.")]
23+
[global::System.Obsolete ("Use the 'MyInterface' type. This type will be removed in a future release.", error: true)]
2424
public abstract class MyInterfaceConsts : MyInterface {
2525

2626
private MyInterfaceConsts ()
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
[Register ("java/code/IMyInterface", DoNotGenerateAcw=true)]
2+
public abstract class MyInterface : Java.Lang.Object {
3+
4+
internal MyInterface ()
5+
{
6+
}
7+
// Metadata.xml XPath method reference: path="/api/package[@name='java.code']/interface[@name='IMyInterface']/method[@name='DoSomething' and count(parameter)=0]"
8+
[Register ("DoSomething", "()V", "")]
9+
public static unsafe void DoSomething ()
10+
{
11+
const string __id = "DoSomething.()V";
12+
try {
13+
_members.StaticMethods.InvokeVoidMethod (__id, null);
14+
} finally {
15+
}
16+
}
17+
18+
19+
static new readonly JniPeerMembers _members = new JniPeerMembers ("java/code/IMyInterface", typeof (MyInterface));
20+
}
21+
22+
[Register ("java/code/IMyInterface", DoNotGenerateAcw=true)]
23+
[global::System.Obsolete ("Use the 'MyInterface' type. This type will be removed in a future release.", error: true)]
24+
public abstract class MyInterfaceConsts : MyInterface {
25+
26+
private MyInterfaceConsts ()
27+
{
28+
}
29+
}
30+
31+
// Metadata.xml XPath interface reference: path="/api/package[@name='java.code']/interface[@name='IMyInterface']"
32+
[Register ("java/code/IMyInterface", "", "java.code.IMyInterfaceInvoker")]
33+
public partial interface IMyInterface : IJavaObject, IJavaPeerable {
34+
static new readonly JniPeerMembers _members = new JniPeerMembers ("java/code/IMyInterface", typeof (IMyInterface), isInterface: true);
35+
36+
// Metadata.xml XPath method reference: path="/api/package[@name='java.code']/interface[@name='IMyInterface']/method[@name='DoSomething' and count(parameter)=0]"
37+
[Register ("DoSomething", "()V", "")]
38+
public static unsafe void DoSomething ()
39+
{
40+
const string __id = "DoSomething.()V";
41+
try {
42+
_members.StaticMethods.InvokeVoidMethod (__id, null);
43+
} finally {
44+
}
45+
}
46+
47+
}
48+
49+
[global::Android.Runtime.Register ("java/code/IMyInterface", DoNotGenerateAcw=true)]
50+
internal partial class IMyInterfaceInvoker : global::Java.Lang.Object, IMyInterface {
51+
52+
internal static new readonly JniPeerMembers _members = new JniPeerMembers ("java/code/IMyInterface", typeof (IMyInterfaceInvoker));
53+
54+
static IntPtr java_class_ref {
55+
get { return _members.JniPeerType.PeerReference.Handle; }
56+
}
57+
58+
public override global::Java.Interop.JniPeerMembers JniPeerMembers {
59+
get { return _members; }
60+
}
61+
62+
protected override IntPtr ThresholdClass {
63+
get { return class_ref; }
64+
}
65+
66+
protected override global::System.Type ThresholdType {
67+
get { return _members.ManagedPeerType; }
68+
}
69+
70+
IntPtr class_ref;
71+
72+
public static IMyInterface GetObject (IntPtr handle, JniHandleOwnership transfer)
73+
{
74+
return global::Java.Lang.Object.GetObject<IMyInterface> (handle, transfer);
75+
}
76+
77+
static IntPtr Validate (IntPtr handle)
78+
{
79+
if (!JNIEnv.IsInstanceOf (handle, java_class_ref))
80+
throw new InvalidCastException (string.Format ("Unable to convert instance of type '{0}' to type '{1}'.",
81+
JNIEnv.GetClassNameFromInstance (handle), "java.code.IMyInterface"));
82+
return handle;
83+
}
84+
85+
protected override void Dispose (bool disposing)
86+
{
87+
if (this.class_ref != IntPtr.Zero)
88+
JNIEnv.DeleteGlobalRef (this.class_ref);
89+
this.class_ref = IntPtr.Zero;
90+
base.Dispose (disposing);
91+
}
92+
93+
public IMyInterfaceInvoker (IntPtr handle, JniHandleOwnership transfer) : base (Validate (handle), transfer)
94+
{
95+
IntPtr local_ref = JNIEnv.GetObjectClass (((global::Java.Lang.Object) this).Handle);
96+
this.class_ref = JNIEnv.NewGlobalRef (local_ref);
97+
JNIEnv.DeleteLocalRef (local_ref);
98+
}
99+
100+
}
101+

tools/generator/Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1/WriteInterface.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public abstract class MyInterface : Java.Lang.Object {
2020
}
2121

2222
[Register ("java/code/IMyInterface", DoNotGenerateAcw=true)]
23-
[global::System.Obsolete ("Use the 'MyInterface' type. This type will be removed in a future release.")]
23+
[global::System.Obsolete ("Use the 'MyInterface' type. This type will be removed in a future release.", error: true)]
2424
public abstract class MyInterfaceConsts : MyInterface {
2525

2626
private MyInterfaceConsts ()
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
[Register ("java/code/IMyInterface", DoNotGenerateAcw=true)]
2+
public abstract class MyInterface : Java.Lang.Object {
3+
4+
internal MyInterface ()
5+
{
6+
}
7+
// Metadata.xml XPath method reference: path="/api/package[@name='java.code']/interface[@name='IMyInterface']/method[@name='DoSomething' and count(parameter)=0]"
8+
[Register ("DoSomething", "()V", "")]
9+
public static unsafe void DoSomething ()
10+
{
11+
const string __id = "DoSomething.()V";
12+
try {
13+
_members.StaticMethods.InvokeVoidMethod (__id, null);
14+
} finally {
15+
}
16+
}
17+
18+
19+
static new readonly JniPeerMembers _members = new XAPeerMembers ("java/code/IMyInterface", typeof (MyInterface));
20+
}
21+
22+
[Register ("java/code/IMyInterface", DoNotGenerateAcw=true)]
23+
[global::System.Obsolete ("Use the 'MyInterface' type. This type will be removed in a future release.", error: true)]
24+
public abstract class MyInterfaceConsts : MyInterface {
25+
26+
private MyInterfaceConsts ()
27+
{
28+
}
29+
}
30+
31+
// Metadata.xml XPath interface reference: path="/api/package[@name='java.code']/interface[@name='IMyInterface']"
32+
[Register ("java/code/IMyInterface", "", "java.code.IMyInterfaceInvoker")]
33+
public partial interface IMyInterface : IJavaObject, IJavaPeerable {
34+
static new readonly JniPeerMembers _members = new XAPeerMembers ("java/code/IMyInterface", typeof (IMyInterface), isInterface: true);
35+
36+
// Metadata.xml XPath method reference: path="/api/package[@name='java.code']/interface[@name='IMyInterface']/method[@name='DoSomething' and count(parameter)=0]"
37+
[Register ("DoSomething", "()V", "")]
38+
public static unsafe void DoSomething ()
39+
{
40+
const string __id = "DoSomething.()V";
41+
try {
42+
_members.StaticMethods.InvokeVoidMethod (__id, null);
43+
} finally {
44+
}
45+
}
46+
47+
}
48+
49+
[global::Android.Runtime.Register ("java/code/IMyInterface", DoNotGenerateAcw=true)]
50+
internal partial class IMyInterfaceInvoker : global::Java.Lang.Object, IMyInterface {
51+
52+
internal static new readonly JniPeerMembers _members = new XAPeerMembers ("java/code/IMyInterface", typeof (IMyInterfaceInvoker));
53+
54+
static IntPtr java_class_ref {
55+
get { return _members.JniPeerType.PeerReference.Handle; }
56+
}
57+
58+
public override global::Java.Interop.JniPeerMembers JniPeerMembers {
59+
get { return _members; }
60+
}
61+
62+
protected override IntPtr ThresholdClass {
63+
get { return class_ref; }
64+
}
65+
66+
protected override global::System.Type ThresholdType {
67+
get { return _members.ManagedPeerType; }
68+
}
69+
70+
IntPtr class_ref;
71+
72+
public static IMyInterface GetObject (IntPtr handle, JniHandleOwnership transfer)
73+
{
74+
return global::Java.Lang.Object.GetObject<IMyInterface> (handle, transfer);
75+
}
76+
77+
static IntPtr Validate (IntPtr handle)
78+
{
79+
if (!JNIEnv.IsInstanceOf (handle, java_class_ref))
80+
throw new InvalidCastException (string.Format ("Unable to convert instance of type '{0}' to type '{1}'.",
81+
JNIEnv.GetClassNameFromInstance (handle), "java.code.IMyInterface"));
82+
return handle;
83+
}
84+
85+
protected override void Dispose (bool disposing)
86+
{
87+
if (this.class_ref != IntPtr.Zero)
88+
JNIEnv.DeleteGlobalRef (this.class_ref);
89+
this.class_ref = IntPtr.Zero;
90+
base.Dispose (disposing);
91+
}
92+
93+
public IMyInterfaceInvoker (IntPtr handle, JniHandleOwnership transfer) : base (Validate (handle), transfer)
94+
{
95+
IntPtr local_ref = JNIEnv.GetObjectClass (((global::Java.Lang.Object) this).Handle);
96+
this.class_ref = JNIEnv.NewGlobalRef (local_ref);
97+
JNIEnv.DeleteLocalRef (local_ref);
98+
}
99+
100+
}
101+

tools/generator/Tests/Unit-Tests/CodeGeneratorExpectedResults/XamarinAndroid/WriteInterface.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public abstract class MyInterface : Java.Lang.Object {
2222
}
2323

2424
[Register ("java/code/IMyInterface", DoNotGenerateAcw=true)]
25-
[global::System.Obsolete ("Use the 'MyInterface' type. This type will be removed in a future release.")]
25+
[global::System.Obsolete ("Use the 'MyInterface' type. This type will be removed in a future release.", error: true)]
2626
public abstract class MyInterfaceConsts : MyInterface {
2727

2828
private MyInterfaceConsts ()

tools/generator/Tests/expected.ji/TestInterface/Test.ME.ITestInterface.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ internal TestInterface ()
3232
}
3333

3434
[Register ("test/me/TestInterface", DoNotGenerateAcw=true)]
35-
[global::System.Obsolete ("Use the 'TestInterface' type. This type will be removed in a future release.")]
35+
[global::System.Obsolete ("Use the 'TestInterface' type. This type will be removed in a future release.", error: true)]
3636
public abstract class TestInterfaceConsts : TestInterface {
3737

3838
private TestInterfaceConsts ()

tools/generator/Tests/expected/TestInterface/Test.ME.ITestInterface.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ internal TestInterface ()
3232
}
3333

3434
[Register ("test/me/TestInterface", DoNotGenerateAcw=true)]
35-
[global::System.Obsolete ("Use the 'TestInterface' type. This type will be removed in a future release.")]
35+
[global::System.Obsolete ("Use the 'TestInterface' type. This type will be removed in a future release.", error: true)]
3636
public abstract class TestInterfaceConsts : TestInterface {
3737

3838
private TestInterfaceConsts ()

0 commit comments

Comments
 (0)