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

Commit 5c085cf

Browse files
[release/10.0.1xx] Support JSpecify nullness annotations (#1509)
1 parent 567e6b6 commit 5c085cf

16 files changed

Lines changed: 903 additions & 19 deletions

File tree

‎src/Xamarin.Android.Tools.Bytecode/AttributeInfo.cs‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ public class AttributeInfo {
5151
public const string StackMapTable = "StackMapTable";
5252
public const string RuntimeVisibleAnnotations = "RuntimeVisibleAnnotations";
5353
public const string RuntimeInvisibleAnnotations = "RuntimeInvisibleAnnotations";
54+
public const string RuntimeVisibleParameterAnnotations = "RuntimeVisibleParameterAnnotations";
5455
public const string RuntimeInvisibleParameterAnnotations = "RuntimeInvisibleParameterAnnotations";
56+
public const string RuntimeVisibleTypeAnnotations = "RuntimeVisibleTypeAnnotations";
57+
public const string RuntimeInvisibleTypeAnnotations = "RuntimeInvisibleTypeAnnotations";
5558

5659
ushort nameIndex;
5760

@@ -85,6 +88,10 @@ public string Name {
8588
{ typeof (ModulePackagesAttribute), ModulePackages },
8689
{ typeof (RuntimeVisibleAnnotationsAttribute), RuntimeVisibleAnnotations },
8790
{ typeof (RuntimeInvisibleAnnotationsAttribute), RuntimeInvisibleAnnotations },
91+
{ typeof (RuntimeVisibleParameterAnnotationsAttribute), RuntimeVisibleParameterAnnotations },
92+
{ typeof (RuntimeInvisibleParameterAnnotationsAttribute), RuntimeInvisibleParameterAnnotations },
93+
{ typeof (RuntimeVisibleTypeAnnotationsAttribute), RuntimeVisibleTypeAnnotations },
94+
{ typeof (RuntimeInvisibleTypeAnnotationsAttribute), RuntimeInvisibleTypeAnnotations },
8895
{ typeof (SignatureAttribute), Signature },
8996
{ typeof (SourceFileAttribute), SourceFile },
9097
{ typeof (StackMapTableAttribute), StackMapTable },
@@ -123,7 +130,10 @@ static AttributeInfo CreateAttribute (string name, ConstantPool constantPool, us
123130
case ModulePackages: return new ModulePackagesAttribute (constantPool, nameIndex, stream);
124131
case RuntimeVisibleAnnotations: return new RuntimeVisibleAnnotationsAttribute (constantPool, nameIndex, stream);
125132
case RuntimeInvisibleAnnotations: return new RuntimeInvisibleAnnotationsAttribute (constantPool, nameIndex, stream);
133+
case RuntimeVisibleParameterAnnotations: return new RuntimeVisibleParameterAnnotationsAttribute (constantPool, nameIndex, stream);
126134
case RuntimeInvisibleParameterAnnotations: return new RuntimeInvisibleParameterAnnotationsAttribute (constantPool, nameIndex, stream);
135+
case RuntimeVisibleTypeAnnotations: return new RuntimeVisibleTypeAnnotationsAttribute (constantPool, nameIndex, stream);
136+
case RuntimeInvisibleTypeAnnotations: return new RuntimeInvisibleTypeAnnotationsAttribute (constantPool, nameIndex, stream);
127137
case Signature: return new SignatureAttribute (constantPool, nameIndex, stream);
128138
case SourceFile: return new SourceFileAttribute (constantPool, nameIndex, stream);
129139
case StackMapTable: return new StackMapTableAttribute (constantPool, nameIndex, stream);
@@ -671,6 +681,31 @@ public override string ToString ()
671681
}
672682

673683

684+
// https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.18
685+
public sealed class RuntimeVisibleParameterAnnotationsAttribute : AttributeInfo
686+
{
687+
public IList<ParameterAnnotation> Annotations { get; } = new List<ParameterAnnotation> ();
688+
689+
public RuntimeVisibleParameterAnnotationsAttribute (ConstantPool constantPool, ushort nameIndex, Stream stream)
690+
: base (constantPool, nameIndex, stream)
691+
{
692+
var length = stream.ReadNetworkUInt32 ();
693+
var param_count = stream.ReadNetworkByte ();
694+
695+
for (var i = 0; i < param_count; ++i) {
696+
var a = new ParameterAnnotation (constantPool, stream, i);
697+
Annotations.Add (a);
698+
}
699+
}
700+
701+
public override string ToString ()
702+
{
703+
var annotations = string.Join (", ", Annotations.Select (v => v.ToString ()));
704+
return $"RuntimeVisibleParameterAnnotationsAttribute({annotations})";
705+
}
706+
}
707+
708+
674709
// https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.7.19
675710
public sealed class RuntimeInvisibleParameterAnnotationsAttribute : AttributeInfo
676711
{
@@ -695,6 +730,52 @@ public override string ToString ()
695730
}
696731
}
697732

733+
// https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.20
734+
public sealed class RuntimeVisibleTypeAnnotationsAttribute : AttributeInfo
735+
{
736+
public IList<TypeAnnotation> Annotations { get; } = new List<TypeAnnotation> ();
737+
738+
public RuntimeVisibleTypeAnnotationsAttribute (ConstantPool constantPool, ushort nameIndex, Stream stream)
739+
: base (constantPool, nameIndex, stream)
740+
{
741+
var length = stream.ReadNetworkUInt32 ();
742+
var count = stream.ReadNetworkUInt16 ();
743+
744+
for (int i = 0; i < count; ++i) {
745+
Annotations.Add (new TypeAnnotation (constantPool, stream));
746+
}
747+
}
748+
749+
public override string ToString ()
750+
{
751+
var annotations = string.Join (", ", Annotations.Select (v => v.ToString ()));
752+
return $"RuntimeVisibleTypeAnnotations({annotations})";
753+
}
754+
}
755+
756+
// https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.21
757+
public sealed class RuntimeInvisibleTypeAnnotationsAttribute : AttributeInfo
758+
{
759+
public IList<TypeAnnotation> Annotations { get; } = new List<TypeAnnotation> ();
760+
761+
public RuntimeInvisibleTypeAnnotationsAttribute (ConstantPool constantPool, ushort nameIndex, Stream stream)
762+
: base (constantPool, nameIndex, stream)
763+
{
764+
var length = stream.ReadNetworkUInt32 ();
765+
var count = stream.ReadNetworkUInt16 ();
766+
767+
for (int i = 0; i < count; ++i) {
768+
Annotations.Add (new TypeAnnotation (constantPool, stream));
769+
}
770+
}
771+
772+
public override string ToString ()
773+
{
774+
var annotations = string.Join (", ", Annotations.Select (v => v.ToString ()));
775+
return $"RuntimeInvisibleTypeAnnotations({annotations})";
776+
}
777+
}
778+
698779
// http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.7.9
699780
public sealed class SignatureAttribute : AttributeInfo {
700781

‎src/Xamarin.Android.Tools.Bytecode/ClassFile.cs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,18 @@ public string PackageName {
108108

109109
public string FullJniName => "L" + ThisClass.Name.Value + ";";
110110

111+
// `package-info.class` is a synthetic class that carries
112+
// package-level annotations (e.g. JSpecify's `@NullMarked`).
113+
// Its simple name is `package-info`.
114+
public bool IsPackageInfo {
115+
get {
116+
var name = ThisClass.Name.Value;
117+
var slash = name.LastIndexOf ('/');
118+
var simple = slash < 0 ? name : name.Substring (slash + 1);
119+
return simple == "package-info";
120+
}
121+
}
122+
111123
public string? SourceFileName {
112124
get {
113125
var sourceFile = Attributes.Get<SourceFileAttribute> ();

‎src/Xamarin.Android.Tools.Bytecode/ClassPath.cs‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,16 @@ public ReadOnlyDictionary<string, List<ClassFile>> GetPackages ()
125125
x => classFiles.Where (p => p.PackageName == x).ToList ()));
126126
}
127127

128+
// Returns the `package-info.class` for the given package, if any.
129+
public ClassFile? GetPackageInfo (string packageName)
130+
{
131+
foreach (var c in classFiles) {
132+
if (c.IsPackageInfo && c.PackageName == packageName)
133+
return c;
134+
}
135+
return null;
136+
}
137+
128138
public static bool IsJarFile (string jarFile)
129139
{
130140
if (jarFile == null)
@@ -368,15 +378,19 @@ public XElement ToXElement ()
368378
FixupModuleVisibility (removeModules: true);
369379

370380
var packagesDictionary = GetPackages ();
381+
var packageInfos = packagesDictionary.ToDictionary (
382+
kv => kv.Key,
383+
kv => kv.Value.FirstOrDefault (c => c.IsPackageInfo));
371384
var api = new XElement ("api",
372385
GetApiSource (),
373386
GetPlatform (),
374387
packagesDictionary.Keys.OrderBy (p => p, StringComparer.OrdinalIgnoreCase)
375388
.Select (p => new XElement ("package",
376389
new XAttribute ("name", p),
377390
new XAttribute ("jni-name", p.Replace ('.', '/')),
378-
packagesDictionary [p].OrderBy (c => c.ThisClass.Name.Value, StringComparer.OrdinalIgnoreCase)
379-
.Select (c => new XmlClassDeclarationBuilder (c).ToXElement ()))));
391+
packagesDictionary [p].Where (c => !c.IsPackageInfo)
392+
.OrderBy (c => c.ThisClass.Name.Value, StringComparer.OrdinalIgnoreCase)
393+
.Select (c => new XmlClassDeclarationBuilder (c, packageInfos [p]).ToXElement ()))));
380394
FixupParametersFromDocs (api);
381395
return api;
382396
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
using System;
2+
using System.IO;
3+
4+
namespace Xamarin.Android.Tools.Bytecode
5+
{
6+
// https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.20
7+
public enum TypeAnnotationTargetType : byte
8+
{
9+
ClassTypeParameter = 0x00,
10+
MethodTypeParameter = 0x01,
11+
ClassExtends = 0x10,
12+
ClassTypeParameterBound = 0x11,
13+
MethodTypeParameterBound = 0x12,
14+
Field = 0x13,
15+
MethodReturn = 0x14,
16+
MethodReceiver = 0x15,
17+
MethodFormalParameter = 0x16,
18+
Throws = 0x17,
19+
LocalVariable = 0x40,
20+
ResourceVariable = 0x41,
21+
ExceptionParameter = 0x42,
22+
Instanceof = 0x43,
23+
New = 0x44,
24+
ConstructorReference = 0x45,
25+
MethodReference = 0x46,
26+
Cast = 0x47,
27+
ConstructorInvocationTypeArgument = 0x48,
28+
MethodInvocationTypeArgument = 0x49,
29+
ConstructorReferenceTypeArgument = 0x4A,
30+
MethodReferenceTypeArgument = 0x4B,
31+
}
32+
33+
// https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.20
34+
public sealed class TypeAnnotation
35+
{
36+
public TypeAnnotationTargetType TargetType { get; }
37+
38+
// `formal_parameter_index` for MethodFormalParameter; otherwise 0.
39+
public int FormalParameterIndex { get; }
40+
41+
// `path_length` from `type_path`; we don't retain the individual
42+
// path entries — see AppliesToTopLevelType.
43+
public int TypePathLength { get; }
44+
45+
public Annotation Annotation { get; }
46+
47+
// JSpecify-style nullness annotations only apply to the top-level
48+
// type when there is no `type_path`. Annotations with a non-empty
49+
// path describe inner types (e.g. `Map<@Nullable K, V>`) which the
50+
// API XML schema cannot currently represent.
51+
public bool AppliesToTopLevelType => TypePathLength == 0;
52+
53+
public TypeAnnotation (ConstantPool constantPool, Stream stream)
54+
{
55+
TargetType = (TypeAnnotationTargetType) stream.ReadNetworkByte ();
56+
57+
// target_info — size depends on target_type.
58+
switch (TargetType) {
59+
case TypeAnnotationTargetType.ClassTypeParameter:
60+
case TypeAnnotationTargetType.MethodTypeParameter:
61+
stream.ReadNetworkByte (); // type_parameter_index
62+
break;
63+
case TypeAnnotationTargetType.ClassExtends:
64+
stream.ReadNetworkUInt16 (); // supertype_index
65+
break;
66+
case TypeAnnotationTargetType.ClassTypeParameterBound:
67+
case TypeAnnotationTargetType.MethodTypeParameterBound:
68+
stream.ReadNetworkByte (); // type_parameter_index
69+
stream.ReadNetworkByte (); // bound_index
70+
break;
71+
case TypeAnnotationTargetType.Field:
72+
case TypeAnnotationTargetType.MethodReturn:
73+
case TypeAnnotationTargetType.MethodReceiver:
74+
// empty_target — no bytes
75+
break;
76+
case TypeAnnotationTargetType.MethodFormalParameter:
77+
FormalParameterIndex = stream.ReadNetworkByte ();
78+
break;
79+
case TypeAnnotationTargetType.Throws:
80+
stream.ReadNetworkUInt16 (); // throws_type_index
81+
break;
82+
case TypeAnnotationTargetType.LocalVariable:
83+
case TypeAnnotationTargetType.ResourceVariable: {
84+
var table_length = stream.ReadNetworkUInt16 ();
85+
for (int i = 0; i < table_length; ++i) {
86+
stream.ReadNetworkUInt16 (); // start_pc
87+
stream.ReadNetworkUInt16 (); // length
88+
stream.ReadNetworkUInt16 (); // index
89+
}
90+
break;
91+
}
92+
case TypeAnnotationTargetType.ExceptionParameter:
93+
stream.ReadNetworkUInt16 (); // exception_table_index
94+
break;
95+
case TypeAnnotationTargetType.Instanceof:
96+
case TypeAnnotationTargetType.New:
97+
case TypeAnnotationTargetType.ConstructorReference:
98+
case TypeAnnotationTargetType.MethodReference:
99+
stream.ReadNetworkUInt16 (); // offset
100+
break;
101+
case TypeAnnotationTargetType.Cast:
102+
case TypeAnnotationTargetType.ConstructorInvocationTypeArgument:
103+
case TypeAnnotationTargetType.MethodInvocationTypeArgument:
104+
case TypeAnnotationTargetType.ConstructorReferenceTypeArgument:
105+
case TypeAnnotationTargetType.MethodReferenceTypeArgument:
106+
stream.ReadNetworkUInt16 (); // offset
107+
stream.ReadNetworkByte (); // type_argument_index
108+
break;
109+
default:
110+
throw new NotSupportedException ($"Unknown type_annotation target_type: 0x{(byte)TargetType:X2}");
111+
}
112+
113+
// type_path: u1 path_length, then path_length * { u1 type_path_kind; u1 type_argument_index; }
114+
TypePathLength = stream.ReadNetworkByte ();
115+
for (int i = 0; i < TypePathLength; ++i) {
116+
stream.ReadNetworkByte (); // type_path_kind
117+
stream.ReadNetworkByte (); // type_argument_index
118+
}
119+
120+
// The remaining bytes match the regular `annotation` structure.
121+
Annotation = new Annotation (constantPool, stream);
122+
}
123+
124+
public override string ToString ()
125+
{
126+
return $"TypeAnnotation({TargetType}, paramIndex={FormalParameterIndex}, pathLength={TypePathLength}, {Annotation})";
127+
}
128+
}
129+
}

0 commit comments

Comments
 (0)