Skip to content

Generic members of package-private base class not properly bound in public derived class #1083

@jonpryor

Description

@jonpryor

Context: #1080

Consider the following Java code:

package p;

class ExampleBase<V> {
    public void m1(V value) {}
    public <T> void m2(T value) {}
}

public class Example extends ExampleBase<String> {
}

Build it, bind it:

mkdir bin
javac -d bin Example.java
mono path/to/class-parse.exe $(find bin -iname \*.class) > api.xml
mkdir gsrc
mono path/to/generator.exe api.xml \
    -o gsrc --codegen-target=XAJavaInterop1 \
    -r /Library/Frameworks/Xamarin.Android.framework/Versions/Current//lib/xamarin.android/xbuild-frameworks/MonoAndroid/v13.0/Mono.Android.dll \
    -L /Library/Frameworks/Xamarin.Android.framework/Versions/Current//lib/xamarin.android/xbuild-frameworks/MonoAndroid/v1.0

…elicits lots of warnings; of interest to #1080 is:

api.xml(11,10): warning BG8800: Unknown parameter type 'V' for member 'P.Example.M1 (V)'.

Of interest is gsrc/P.Example.cs, which binds ExampleBase.m2() but not ExampleBase.m1():

namespace P {
	// Metadata.xml XPath class reference: path="/api/package[@name='p']/class[@name='Example']"
	[global::Android.Runtime.Register ("p/Example", DoNotGenerateAcw=true)]
	public partial class Example : Java.Lang.Object {
		// …
		[Register ("m2", "(Ljava/lang/Object;)V", "GetM2_Ljava_lang_Object_Handler")]
		[global::Java.Interop.JavaTypeParameters (new string [] {"T"})]
		public virtual unsafe void M2 (Java.Lang.Object p0) =>}
}

Thus, the bug/request: ExampleBase.m1() should also be bound in Example, using the type parameters used in the extends clause to resolve type parameters:

namespace P {
	public partial class Example : Java.Lang.Object {
		[Register ("m1", "(Ljava/lang/Object;)V", "GetM1_Ljava_lang_Object_Handler")]
		public virtual unsafe void M1 (string p0) =>}
}

Problem: This is apparently another case where Java type erasure is "fun".

Specifically, consider javap:

% javap -classpath bin p.Example
Compiled from "Example.java"
public class p.Example extends p.ExampleBase<java.lang.String> {
  public p.Example();
  public static void main(java.lang.String[]);
  public void m2(java.lang.Object);
  public void m1(java.lang.Object);
}

This matches class-parse.exe --dump and api.xml: Example.class contains an m1 method, but the parameter type for m1 is java.lang.Object, not java.lang.String! (Aside: when generator runs, it creates api.xml.adjusted, which removes the <method/> elements from api.xml…)

This suggests that I should be able to call new Example().m1(new Object()) in Java, but that doesn't compile!

error: incompatible types: Object cannot be converted to String

So how is it that Example.class has m1(java.lang.Object), and does not have m1(java.lang.String), and yet the Java compiler requires that a String parameter be passed to m1()?

The answer is that the Java compiler requires that the entire inheritance chain be present at compile-time; Example.class is not a "stand-alone" type! For example, if I "muck around" with things and remove bin/p/ExampleBase.class while leaving bin/p/Example.class behind, types attempting to use p.Example do not build as p.ExampleBase is still required! (Though the resulting Java bytecode doesn't reference ExampleBase, so if Example changes or removes it's base class in the future, existing code won't break. @jonpryor finds this fascinating.)

Thus, Example.class itself doesn't have enough information to tell the Java compiler that Example.m1() only accepts String parameters; it's Example.class in combination with ExampleBase.class which allows the compiler to know that only String parameters are allowed.

What this means in terms of binding is that while the parameter type needs to be a String (System.String for bindings), the JNI signature will instead be invoking Object, a'la:

namespace P {
	public partial class Example : Java.Lang.Object {
		[Register ("m1", "(Ljava/lang/Object;)V", "GetM1_Ljava_lang_Object_Handler")]
		public virtual unsafe void m1 (string p0)
		{
			const string __id = "m1.(Ljava/lang/Object;)I";
			IntPtr native_p0 = JNIEnv.NewString((string?) p0);
			try {
				JniArgumentValue* __args = stackalloc JniArgumentValue [1];
				__args[0] = new JniArgumentValue (native_p0);
				_members.InstanceMethods.InvokeVirtualVoidMethod (__id, this, __args);
			} finally {
				JNIEnv.DeleteLocalRef (native_p0);
				global::System.GC.KeepAlive (p0);
			}
		}
	}
}

Problem Then we have the problem of subclassing (which is probably a separate bug somewhere, which I'm not bothering to look up right now); consider this C# subclass:

public class MyExample : Example {
    public override void M1(string value) {}
}

This will eventually require a Java Callable Wrapper, which uses the [Register] custom attribute information, which is all Object, not String, which means we'll emit:

public class MyExample extends Example {
    public void m1(Object value) {…}
}

…which won't compile.

Question/suggestion: does the [Register] custom attribute need to match the __id value within the binding method? If not, we can "fake" things and have [Register] use the "generic parameter replaced" version, while __id uses the actual correct version:

namespace P {
	public partial class Example : Java.Lang.Object {
		[Register ("m1", "(Ljava/lang/String;)V", "GetM1_Ljava_lang_String_Handler")]
		public virtual unsafe void m1 (string p0)
		{
			const string __id = "m1.(Ljava/lang/Object;)I";

This would allow Java Callable Wrapper generation to work as-is, but I'm not sure offhand if generator can have separate values for [Register] vs. __id.

Metadata

Metadata

Assignees

No one assigned

    Labels

    generatorIssues binding a Java library (generator, class-parse, etc.)

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions