Skip to content

Java Callable Wrappers via custom attributes #833

@jonpryor

Description

@jonpryor

Java Callable Wrappers are (currently) generated by parsing an IL Type Definition for a Java.Lang.Object subclass (though see also Issue #540), and contain Java source which "mirror" the managed type, "forwarding" the base class, implemented interfaces, overridden methods, and [Export] methods.

It may be useful if the Java generation process could be extended via custom attributes, overriding the defaults.

Add the following two custom attributes:

namespace Java.Interop.Generation {

    [AttributeUsage (AttributeTargets.Class, AllowMultiple=false)]
    public class JcwExtendsAttribute : Attribute {
        public JcwExtendsAttribute(string javaTypeName);
        public string JavaTypeName {get;}
    }

    [AttributeUsage (AttributeTargets.Class, AllowMultiple=true)]
    public class JcwImplementsAttribute : Attribute {
        public JcwImplementsAttribute(string javaTypeName);
        public string JavaTypeName {get;}
    }
}

and update https://github.com/xamarin/java.interop/tree/main/src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers so that if the above attributes are present, they override the default behavior. This would allow:

[JcwExtends("some.random.Type")]
[Register("mypackage.MyType")]
[JcwImplements("some.random.Interface")]
[JcwImplements("another.random.Interface<Integer>")]
class MyExample : Java.Lang.Object, ISomeOtherInterface {
    [Export] public void forSomeRandomInterface() {}
    [Export] public void forAnotherRandomInterface(Integer v) {}
}

which would generate the Java Callable Wrapper:

package mypackage; // via JcwNameAttribute

class MyType // JcwNameAttribute
  extends some.random.Type // JcwExtends
  implements mono.android.IGCUserPeer
    , some.random.Interface // JcwImplements
    , another.random.Interface<Integer> // JcwImplements
{
    public MyType() {…}
    public void forSomeRandomInterface() {…}
    public void forAnotherRandomInterface(java.lang.Integer v) {…}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    callable-wrappersIssues with Java Callable Wrappers

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions