Skip to content
This repository was archived by the owner on Aug 27, 2026. It is now read-only.

Commit 59a944a

Browse files
committed
[JCW] Throw error if user tries to override a Kotlin name-mangled method. (#534)
1 parent 0065de4 commit 59a944a

3 files changed

Lines changed: 54 additions & 0 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,10 @@ void AddMethod (MethodDefinition registeredMethod, MethodDefinition implementedM
362362
{
363363
if (registeredMethod != null)
364364
foreach (RegisterAttribute attr in GetRegisterAttributes (registeredMethod)) {
365+
// Check for Kotlin-mangled methods that cannot be overridden
366+
if (attr.Name.Contains ("-impl") || (attr.Name.Contains ('-') && attr.Name.Length - attr.Name.LastIndexOf ('-') == 8))
367+
Diagnostic.Error (4213, LookupSource (implementedMethod), $"Cannot override Kotlin-generated method '{attr.Name}' because it is not a valid Java method name. This method can only be overridden from Kotlin.");
368+
365369
var msig = new Signature (implementedMethod, attr);
366370
if (!registeredMethod.IsConstructor && !methods.Any (m => m.Name == msig.Name && m.Params == msig.Params))
367371
methods.Add (msig);

‎tests/Java.Interop.Tools.JavaCallableWrappers-Tests/Java.Interop.Tools.JavaCallableWrappers/JavaCallableWrapperGeneratorTests.cs‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,28 @@ public void ConstructorExceptions ()
3030
Assert.AreEqual (4200, e.Code);
3131
}
3232

33+
[Test]
34+
public void KotlinInvalidImplRegisterName ()
35+
{
36+
Action<string, object []> logger = (f, o) => { };
37+
38+
// Contains invalid [Register] name of "foo-impl"
39+
var td = SupportDeclarations.GetTypeDefinition (typeof (KotlinInvalidImplRegisterName));
40+
var e = Assert.Throws<XamarinAndroidException> (() => new JavaCallableWrapperGenerator (td, logger));
41+
Assert.AreEqual (4209, e.Code);
42+
}
43+
44+
[Test]
45+
public void KotlinInvalidHashRegisterName ()
46+
{
47+
Action<string, object []> logger = (f, o) => { };
48+
49+
// Contains invalid [Register] name of "KotlinInvalidHashRegisterName"
50+
var td = SupportDeclarations.GetTypeDefinition (typeof (KotlinInvalidHashRegisterName));
51+
var e = Assert.Throws<XamarinAndroidException> (() => new JavaCallableWrapperGenerator (td, logger));
52+
Assert.AreEqual (4209, e.Code);
53+
}
54+
3355
[Test]
3456
public void GenerateApplication (
3557
[Values (null, "android.app.Application", "android.support.multidex.MultiDexApplication")] string applicationJavaClass

‎tests/Java.Interop.Tools.JavaCallableWrappers-Tests/Java.Interop.Tools.JavaCallableWrappers/SupportDeclarations.cs‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,34 @@ public class OverrideNestedName : Java.Lang.Object
227227
}
228228
}
229229

230+
[Register ("Kotlin.InvalidRegisterName")]
231+
class KotlinInvalidRegisterName : Java.Lang.Object
232+
{
233+
[Register ("foo-impl")]
234+
public virtual void Foo ()
235+
{
236+
}
237+
238+
[Register ("foo-f8k2a13")]
239+
public virtual void Bar ()
240+
{
241+
}
242+
}
243+
244+
[Register ("Kotlin.InvalidRegisterNameSubclass")]
245+
class KotlinInvalidImplRegisterName : KotlinInvalidRegisterName
246+
{
247+
[Register ("foo-impl")]
248+
public override void Foo () => base.Foo ();
249+
}
250+
251+
[Register ("Kotlin.InvalidRegisterNameSubclass")]
252+
class KotlinInvalidHashRegisterName : KotlinInvalidRegisterName
253+
{
254+
[Register ("foo-f8k2a13")]
255+
public override void Bar () => base.Foo ();
256+
}
257+
230258
[Service (Name = "service.Name")]
231259
class ServiceName : Java.Lang.Object
232260
{

0 commit comments

Comments
 (0)