Skip to content

[Xamarin.Android.Tools.Bytecode] Support RuntimeVisibleAnnotations #467

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

jonpryor
Copy link
Member

Context: #466

While investigating issue #466, I ran across an "oddity" within the
type kotlin/collections/AbstractCollection\$toString\$1.class:

<api api-source="class-parse">
  <package name="kotlin.collections" jni-name="kotlin/collections">
    <class
      name="AbstractCollection.toString.1"
      jni-signature="Lkotlin/collections/AbstractCollection$toString$1;">
      <implements
        name="kotlin.jvm.functions.Function1"
        name-generic-aware="kotlin.jvm.functions.Function1&lt;E, java.lang.CharSequence&gt;"
        jni-type="Lkotlin/jvm/functions/Function1&lt;TE;Ljava/lang/CharSequence;&gt;;" />
      <method
        name="invoke"
        jni-signature="(Ljava/lang/Object;)Ljava/lang/CharSequence;">
        <parameter
          name="it"
          type="E"
          jni-type="TE;" />
      </method>
    </class>
  </package>
</api>

(Abbreviated output).

What's bizarre is that the invoke() method references the type
E, but there is no declaration of what E is.

Normally, there'd be a <typeParameters/> element, which is
provided by the Signature annotation.

Note that the above does not contain <typeParameters/>!

On the vague thought that maybe RuntimeVisibleAnnotations were being
used to store generic type information, add support to class-parse
to extract RuntimeVisibleAnnotations and
RuntimeInvisibleAnnotations annotation blobs.

Context: dotnet#466

While investigating issue dotnet#466, I ran across an "oddity" within the
type `kotlin/collections/AbstractCollection\$toString\$1.class`:

	<api api-source="class-parse">
	  <package name="kotlin.collections" jni-name="kotlin/collections">
	    <class
	      name="AbstractCollection.toString.1"
	      jni-signature="Lkotlin/collections/AbstractCollection$toString$1;">
	      <implements
	        name="kotlin.jvm.functions.Function1"
	        name-generic-aware="kotlin.jvm.functions.Function1&lt;E, java.lang.CharSequence&gt;"
	        jni-type="Lkotlin/jvm/functions/Function1&lt;TE;Ljava/lang/CharSequence;&gt;;" />
	      <method
	        name="invoke"
	        jni-signature="(Ljava/lang/Object;)Ljava/lang/CharSequence;">
	        <parameter
	          name="it"
	          type="E"
	          jni-type="TE;" />
	      </method>
	    </class>
	  </package>
	</api>

(Abbreviated output).

What's *bizarre* is that the `invoke()` method references the type
`E`, but there is no declaration of what `E` *is*.

*Normally*, there'd be a `<typeParameters/>` element, which is
provided by the `Signature` annotation.

Note that the above does *not* contain `<typeParameters/>`!

On the vague thought that maybe `RuntimeVisibleAnnotations` were being
used to store generic type information, add support to `class-parse`
to extract `RuntimeVisibleAnnotations` and
`RuntimeInvisibleAnnotations` annotation blobs.
@jonpryor
Copy link
Member Author

TODO: add found annotations to XML output.

@jonpryor
Copy link
Member Author

Related:

jonpryor pushed a commit that referenced this pull request Sep 30, 2019
…495)

Context: #466
Context: #467

When attempting to bind [`kotlin-stdlib-1.3.41.jar`][0], `generator`
crashes with the following exception:

	System.NullReferenceException: Object reference not set to an instance of an object
	  at Xamarin.Android.Tools.ApiXmlAdjuster.JavaApiXmlGeneratorExtensions.Save (Xamarin.Android.Tools.ApiXmlAdjuster.JavaTypeParameters typeParameters, System.Xml.XmlWriter writer, System.String indent)
	  at Xamarin.Android.Tools.ApiXmlAdjuster.JavaApiXmlGeneratorExtensions.SaveCommon (Xamarin.Android.Tools.ApiXmlAdjuster.JavaMember m, System.Xml.XmlWriter writer, System.String elementName, System.String abs, System.String native, System.String ret, System.String sync, System.String transient, System.String type, System.String typeGeneric, System.String value, System.String volat, Xamarin.Android.Tools.ApiXmlAdjuster.JavaTypeParameters typeParameters, System.Collections.Generic.IEnumerable`1[T] parameters, System.Collections.Generic.IEnumerable`1[T] exceptions, System.Nullable`1[T] extBridge, System.String jniReturn, System.Nullable`1[T] extSynthetic)
	  at Xamarin.An/droid.Tools.ApiXmlAdjuster.JavaApiXmlGeneratorExtensions.Save (Xamarin.Android.Tools.ApiXmlAdjuster.JavaMethod method, System.Xml.XmlWriter writer)
	  at Xamarin.Android.Tools.ApiXmlAdjuster.JavaApiXmlGeneratorExtensions.SaveTypeCommon (Xamarin.Android.Tools.ApiXmlAdjuster.JavaType cls, System.Xml.XmlWriter writer, System.String elementName, System.String abs, System.String ext, System.String extgen, System.String jniExt)
	  at Xamarin.Android.Tools.ApiXmlAdjuster.JavaApiXmlGeneratorExtensions.Save (Xamarin.Android.Tools.ApiXmlAdjuster.JavaClass cls, System.Xml.XmlWriter writer)
	  at Xamarin.Android.Tools.ApiXmlAdjuster.JavaApiXmlGeneratorExtensions.Save (Xamarin.Android.Tools.ApiXmlAdjuster.JavaApi api, System.Xml.XmlWriter writer)
	  at Xamarin.Android.Tools.ApiXmlAdjuster.JavaApiXmlGeneratorExtensions.Save (Xamarin.Android.Tools.ApiXmlAdjuster.JavaApi api, System.String xmlfile)
	  at Xamarin.Android.Tools.ApiXmlAdjuster.Adjuster.Process (System.String inputXmlFile, MonoDroid.Generation.CodeGenerationOptions opt, MonoDroid.Generation.GenBase[] gens, System.String outputXmlFile, System.Int32 reportVerbosity)
	  at Xamarin.Android.Binder.CodeGenerator.Run (Xamarin.Android.Binder.CodeGeneratorOptions options, Java.Interop.Tools.Cecil.DirectoryAssemblyResolver resolver)
	  at Xamarin.Android.Binder.CodeGenerator.Run (Xamarin.Android.Binder.CodeGeneratorOptions options)
	  at Xamarin.Android.Binder.CodeGenerator.Main (System.String[] args)

We are still investigating *why* this happens and how it should be
appropriately fixed.

In the meantime, to "unblock" developers attempting to bind binding
Kotlin libraries in Visual Studio 16.4 Preview 2, simply prevent the
`NullReferenceException` from being generated generated.  This at
least lets people get past the crash so they can apply fixups in
`Metadata.xml` to continue.  (This `NullReferenceException` happens
before the `metadata` stage; there's nothing a user can currently do.)

I will be making our Kotlin bindings more "correct" in future sprints.

[0]: https://search.maven.org/remotecontent?filepath=org/jetbrains/kotlin/kotlin-stdlib/1.3.41/kotlin-stdlib-1.3.41.jar
jonpryor pushed a commit that referenced this pull request Sep 30, 2019
…495)

Context: #466
Context: #467

When attempting to bind [`kotlin-stdlib-1.3.41.jar`][0], `generator`
crashes with the following exception:

	System.NullReferenceException: Object reference not set to an instance of an object
	  at Xamarin.Android.Tools.ApiXmlAdjuster.JavaApiXmlGeneratorExtensions.Save (Xamarin.Android.Tools.ApiXmlAdjuster.JavaTypeParameters typeParameters, System.Xml.XmlWriter writer, System.String indent)
	  at Xamarin.Android.Tools.ApiXmlAdjuster.JavaApiXmlGeneratorExtensions.SaveCommon (Xamarin.Android.Tools.ApiXmlAdjuster.JavaMember m, System.Xml.XmlWriter writer, System.String elementName, System.String abs, System.String native, System.String ret, System.String sync, System.String transient, System.String type, System.String typeGeneric, System.String value, System.String volat, Xamarin.Android.Tools.ApiXmlAdjuster.JavaTypeParameters typeParameters, System.Collections.Generic.IEnumerable`1[T] parameters, System.Collections.Generic.IEnumerable`1[T] exceptions, System.Nullable`1[T] extBridge, System.String jniReturn, System.Nullable`1[T] extSynthetic)
	  at Xamarin.An/droid.Tools.ApiXmlAdjuster.JavaApiXmlGeneratorExtensions.Save (Xamarin.Android.Tools.ApiXmlAdjuster.JavaMethod method, System.Xml.XmlWriter writer)
	  at Xamarin.Android.Tools.ApiXmlAdjuster.JavaApiXmlGeneratorExtensions.SaveTypeCommon (Xamarin.Android.Tools.ApiXmlAdjuster.JavaType cls, System.Xml.XmlWriter writer, System.String elementName, System.String abs, System.String ext, System.String extgen, System.String jniExt)
	  at Xamarin.Android.Tools.ApiXmlAdjuster.JavaApiXmlGeneratorExtensions.Save (Xamarin.Android.Tools.ApiXmlAdjuster.JavaClass cls, System.Xml.XmlWriter writer)
	  at Xamarin.Android.Tools.ApiXmlAdjuster.JavaApiXmlGeneratorExtensions.Save (Xamarin.Android.Tools.ApiXmlAdjuster.JavaApi api, System.Xml.XmlWriter writer)
	  at Xamarin.Android.Tools.ApiXmlAdjuster.JavaApiXmlGeneratorExtensions.Save (Xamarin.Android.Tools.ApiXmlAdjuster.JavaApi api, System.String xmlfile)
	  at Xamarin.Android.Tools.ApiXmlAdjuster.Adjuster.Process (System.String inputXmlFile, MonoDroid.Generation.CodeGenerationOptions opt, MonoDroid.Generation.GenBase[] gens, System.String outputXmlFile, System.Int32 reportVerbosity)
	  at Xamarin.Android.Binder.CodeGenerator.Run (Xamarin.Android.Binder.CodeGeneratorOptions options, Java.Interop.Tools.Cecil.DirectoryAssemblyResolver resolver)
	  at Xamarin.Android.Binder.CodeGenerator.Run (Xamarin.Android.Binder.CodeGeneratorOptions options)
	  at Xamarin.Android.Binder.CodeGenerator.Main (System.String[] args)

We are still investigating *why* this happens and how it should be
appropriately fixed.

In the meantime, to "unblock" developers attempting to bind binding
Kotlin libraries in Visual Studio 16.4 Preview 2, simply prevent the
`NullReferenceException` from being generated generated.  This at
least lets people get past the crash so they can apply fixups in
`Metadata.xml` to continue.  (This `NullReferenceException` happens
before the `metadata` stage; there's nothing a user can currently do.)

I will be making our Kotlin bindings more "correct" in future sprints.

[0]: https://search.maven.org/remotecontent?filepath=org/jetbrains/kotlin/kotlin-stdlib/1.3.41/kotlin-stdlib-1.3.41.jar
jonpryor pushed a commit that referenced this pull request Oct 9, 2019
#502)

Context: #466
Context: #467
Context: #499

Add support for parsing the [`RuntimeVisibleAnnotations`][0] and
[`RuntimeInvisibleAnnotations`][1] attributes in Java bytecode.
These attributes store Java programming language [annotations][2]
placed on language constructs such as types, fields, and methods, and
are analogous with C# custom attributes.

Kotlin stores various bits of information in these attributes, and it
will be necessary to parse these attributes to read that information;
see also PR #499.

[0]: https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.7.16
[1]: https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.7.17
[2]: https://docs.oracle.com/javase/specs/jls/se7/html/jls-9.html#jls-9.7
@jonpryor
Copy link
Member Author

jonpryor commented Oct 9, 2019

Superseded by PR #502.

@jonpryor jonpryor closed this Oct 9, 2019
@jonpryor jonpryor deleted the jonp-runitme-visible-annotations branch October 9, 2019 19:01
@github-actions github-actions bot locked and limited conversation to collaborators Apr 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant