Skip to content

Commit 3495a7a

Browse files
jonathanpeppersjonpryor
authored andcommitted
[jcw-gen] Constructors should respect ExportAttribute (#170)
Context: https://github.com/mono/Embeddinator-4000 Embeddinator-4000 will bring a lot more developers to consume C# objects from their Java code. One pain point here is that all constructors are generating `throws java.lang.Throwable` regardless of the `[Export]` declaration. Looking into it further, there was also the issue if you declared an empty Type[], jcw-gen was not handling that case. Changes: - Added `ThrowsDeclaration` property for reuse, which also handles the case of empty `Type[]` - Constructors now use `ThrowsDeclaration` along with methods - New test cases: a class with constructors with plain `[Export]` and a class with `[Export]` declaring `Throws` - Expanded methods test case: method with `Throws` and method with `Throws` and an empty `Type[]`
1 parent cfc2ef3 commit 3495a7a

File tree

3 files changed

+178
-2
lines changed

3 files changed

+178
-2
lines changed

src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers/JavaCallableWrapperGenerator.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,10 @@ public string Retval {
738738
get { return retval; }
739739
}
740740

741+
public string ThrowsDeclaration {
742+
get { return ThrownTypeNames?.Length > 0 ? " throws " + String.Join (", ", ThrownTypeNames) : null; }
743+
}
744+
741745
public readonly string JavaAccess;
742746
public readonly string ManagedParameters;
743747
public readonly string JniSignature;
@@ -757,7 +761,7 @@ void GenerateConstructor (Signature ctor, TextWriter sw)
757761
sw.WriteLine ();
758762
if (ctor.Annotations != null)
759763
sw.WriteLine (ctor.Annotations);
760-
sw.WriteLine ("\tpublic {0} ({1}) throws java.lang.Throwable", name, ctor.Params);
764+
sw.WriteLine ("\tpublic {0} ({1}){2}", name, ctor.Params, ctor.ThrowsDeclaration);
761765
sw.WriteLine ("\t{");
762766
sw.WriteLine ("\t\tsuper ({0});", ctor.SuperCall);
763767
#if MONODROID_TIMING
@@ -796,7 +800,7 @@ void GenerateMethod (Signature method, TextWriter sw)
796800
sw.WriteLine ();
797801
if (method.Annotations != null)
798802
sw.WriteLine (method.Annotations);
799-
sw.WriteLine ("\t{0} {1}{2} {3} ({4}){5}", method.IsExport ? method.JavaAccess : "public", method.IsStatic ? "static " : null, method.Retval, method.JavaName, method.Params, method.ThrownTypeNames != null ? " throws " + String.Join (", ", method.ThrownTypeNames) : null);
803+
sw.WriteLine ("\t{0} {1}{2} {3} ({4}){5}", method.IsExport ? method.JavaAccess : "public", method.IsStatic ? "static " : null, method.Retval, method.JavaName, method.Params, method.ThrowsDeclaration);
800804
sw.WriteLine ("\t{");
801805
#if MONODROID_TIMING
802806
sw.WriteLine ("\t\tandroid.util.Log.i(\"MonoDroid-Timing\", \"{0}.{1}: time: \"+java.lang.System.currentTimeMillis());", name, method.Name);

src/Java.Interop.Tools.JavaCallableWrappers/Test/Java.Interop.Tools.JavaCallableWrappers/JavaCallableWrapperGeneratorTests.cs

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ extends java.lang.Object
160160
""n_GetValue:()Ljava/lang/String;:__export__\n"" +
161161
""n_methodNamesNotMangled:()V:__export__\n"" +
162162
""n_CompletelyDifferentName:(Ljava/lang/String;I)Ljava/lang/String;:__export__\n"" +
163+
""n_methodThatThrows:()V:__export__\n"" +
164+
""n_methodThatThrowsEmptyArray:()V:__export__\n"" +
163165
"""";
164166
mono.android.Runtime.register (""Xamarin.Android.ToolsTests.ExportsMembers, Java.Interop.Tools.JavaCallableWrappers-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"", ExportsMembers.class, __md_methods);
165167
}
@@ -200,6 +202,22 @@ public java.lang.String attributeOverridesNames (java.lang.String p0, int p1)
200202
201203
private native java.lang.String n_CompletelyDifferentName (java.lang.String p0, int p1);
202204
205+
206+
public void methodThatThrows () throws java.lang.Throwable
207+
{
208+
n_methodThatThrows ();
209+
}
210+
211+
private native void n_methodThatThrows ();
212+
213+
214+
public void methodThatThrowsEmptyArray ()
215+
{
216+
n_methodThatThrowsEmptyArray ();
217+
}
218+
219+
private native void n_methodThatThrowsEmptyArray ();
220+
203221
private java.util.ArrayList refList;
204222
public void monodroidAddReference (java.lang.Object obj)
205223
{
@@ -353,6 +371,122 @@ public void monodroidClearReferences ()
353371
refList.clear ();
354372
}
355373
}
374+
";
375+
Assert.AreEqual (expected, actual);
376+
}
377+
378+
[Test]
379+
public void GenerateConstructors ()
380+
{
381+
var actual = Generate (typeof (ExportsConstructors));
382+
var expected = @"package register;
383+
384+
385+
public class ExportsConstructors
386+
extends java.lang.Object
387+
implements
388+
mono.android.IGCUserPeer
389+
{
390+
/** @hide */
391+
public static final String __md_methods;
392+
static {
393+
__md_methods =
394+
"""";
395+
mono.android.Runtime.register (""Xamarin.Android.ToolsTests.ExportsConstructors, Java.Interop.Tools.JavaCallableWrappers-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"", ExportsConstructors.class, __md_methods);
396+
}
397+
398+
399+
public ExportsConstructors ()
400+
{
401+
super ();
402+
if (getClass () == ExportsConstructors.class)
403+
mono.android.TypeManager.Activate (""Xamarin.Android.ToolsTests.ExportsConstructors, Java.Interop.Tools.JavaCallableWrappers-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"", """", this, new java.lang.Object[] { });
404+
}
405+
406+
407+
public ExportsConstructors (int p0)
408+
{
409+
super (p0);
410+
if (getClass () == ExportsConstructors.class)
411+
mono.android.TypeManager.Activate (""Xamarin.Android.ToolsTests.ExportsConstructors, Java.Interop.Tools.JavaCallableWrappers-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"", """", this, new java.lang.Object[] { p0 });
412+
}
413+
414+
private java.util.ArrayList refList;
415+
public void monodroidAddReference (java.lang.Object obj)
416+
{
417+
if (refList == null)
418+
refList = new java.util.ArrayList ();
419+
refList.add (obj);
420+
}
421+
422+
public void monodroidClearReferences ()
423+
{
424+
if (refList != null)
425+
refList.clear ();
426+
}
427+
}
428+
";
429+
Assert.AreEqual (expected, actual);
430+
}
431+
432+
[Test]
433+
public void GenerateConstructors_WithThrows ()
434+
{
435+
var actual = Generate (typeof (ExportsThrowsConstructors));
436+
var expected = @"package register;
437+
438+
439+
public class ExportsThrowsConstructors
440+
extends java.lang.Object
441+
implements
442+
mono.android.IGCUserPeer
443+
{
444+
/** @hide */
445+
public static final String __md_methods;
446+
static {
447+
__md_methods =
448+
"""";
449+
mono.android.Runtime.register (""Xamarin.Android.ToolsTests.ExportsThrowsConstructors, Java.Interop.Tools.JavaCallableWrappers-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"", ExportsThrowsConstructors.class, __md_methods);
450+
}
451+
452+
453+
public ExportsThrowsConstructors () throws java.lang.Throwable
454+
{
455+
super ();
456+
if (getClass () == ExportsThrowsConstructors.class)
457+
mono.android.TypeManager.Activate (""Xamarin.Android.ToolsTests.ExportsThrowsConstructors, Java.Interop.Tools.JavaCallableWrappers-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"", """", this, new java.lang.Object[] { });
458+
}
459+
460+
461+
public ExportsThrowsConstructors (int p0) throws java.lang.Throwable
462+
{
463+
super (p0);
464+
if (getClass () == ExportsThrowsConstructors.class)
465+
mono.android.TypeManager.Activate (""Xamarin.Android.ToolsTests.ExportsThrowsConstructors, Java.Interop.Tools.JavaCallableWrappers-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"", """", this, new java.lang.Object[] { p0 });
466+
}
467+
468+
469+
public ExportsThrowsConstructors (java.lang.String p0)
470+
{
471+
super (p0);
472+
if (getClass () == ExportsThrowsConstructors.class)
473+
mono.android.TypeManager.Activate (""Xamarin.Android.ToolsTests.ExportsThrowsConstructors, Java.Interop.Tools.JavaCallableWrappers-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"", """", this, new java.lang.Object[] { p0 });
474+
}
475+
476+
private java.util.ArrayList refList;
477+
public void monodroidAddReference (java.lang.Object obj)
478+
{
479+
if (refList == null)
480+
refList = new java.util.ArrayList ();
481+
refList.add (obj);
482+
}
483+
484+
public void monodroidClearReferences ()
485+
{
486+
if (refList != null)
487+
refList.clear ();
488+
}
489+
}
356490
";
357491
Assert.AreEqual (expected, actual);
358492
}

src/Java.Interop.Tools.JavaCallableWrappers/Test/Java.Interop.Tools.JavaCallableWrappers/SupportDeclarations.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ namespace Java.Lang {
3636
class Object : Android.Runtime.IJavaObject
3737
{
3838
}
39+
40+
[Register ("java/lang/Throwable", DoNotGenerateAcw = true)]
41+
class Throwable : Exception, Android.Runtime.IJavaObject
42+
{
43+
}
3944
}
4045

4146
namespace Xamarin.Android.ToolsTests {
@@ -219,5 +224,38 @@ public string CompletelyDifferentName (string value, int count)
219224
{
220225
return value;
221226
}
227+
228+
[Export (Throws = new [] { typeof (Java.Lang.Throwable) })]
229+
public void methodThatThrows()
230+
{
231+
}
232+
233+
[Export (Throws = new Type [0])]
234+
public void methodThatThrowsEmptyArray ()
235+
{
236+
}
237+
}
238+
239+
[Register ("register.ExportsConstructors")]
240+
class ExportsConstructors : Java.Lang.Object
241+
{
242+
[Export]
243+
public ExportsConstructors () { }
244+
245+
[Export]
246+
public ExportsConstructors (int value) { }
247+
}
248+
249+
[Register ("register.ExportsThrowsConstructors")]
250+
class ExportsThrowsConstructors : Java.Lang.Object
251+
{
252+
[Export (Throws = new [] { typeof (Java.Lang.Throwable) })]
253+
public ExportsThrowsConstructors () { }
254+
255+
[Export (Throws = new [] { typeof (Java.Lang.Throwable) })]
256+
public ExportsThrowsConstructors (int value) { }
257+
258+
[Export (Throws = new Type [0])]
259+
public ExportsThrowsConstructors (string value) { }
222260
}
223261
}

0 commit comments

Comments
 (0)