Skip to content

Commit c1e9c0a

Browse files
committed
[class-parse] Update parameter names backwards
Context: 8ccb837 Context: dotnet/android-libraries#413 Context: https://discord.com/channels/732297728826277939/732297837953679412/902301741159182346 Context: https://repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-stdlib/1.5.31/kotlin-stdlib-1.5.31.jar Context: https://discord.com/channels/732297728826277939/732297837953679412/902554256035426315 dotnet/android-libraries#413 ran into an issue: D:\a\1\s\generated\org.jetbrains.kotlin.kotlin-stdlib\obj\Release\net6.0-android\generated\src\Kotlin.Coroutines.AbstractCoroutineContextElement.cs(100,8): error CS1002: ; expected The offending line: var this = Java.Lang.Object.GetObject<Java.Lang.Object> (native_this, JniHandleOwnership.DoNotTransfer); (Assigning to `this` makes for a very weird error message.) This was eventually tracked down to commit 8ccb837; @jpobst wrote: > previously it produced: > > <parameter name="initial" type="R" jni-type="TR;" /> > <parameter name="operation" type="kotlin.jvm.functions.Function2&lt;? super R, ? super kotlin.coroutines.CoroutineContext.Element, ? extends R&gt;" /> > > now it produces: > > <parameter name="this" type="R" jni-type="TR;" /> > <parameter name="initial" type="kotlin.jvm.functions.Function2&lt;? super R, ? super kotlin.coroutines.CoroutineContext.Element, ? extends R&gt;" /> The (a?) "source" of the problem is that Kotlin is "weird": it emits a Java method with signature: /* partial */ class AbstractCoroutineContextElement { public Object fold(Object initial, Function2 operation); } However, the local variables table declares *three* local variables: 1. `this` of type `kotlin.coroutines.CoroutineContext.Element` 2. `initial` of type `java.lang.Object` 3. `operation` of type `Function2` This is an instance method, so normally we would skip the first local variable, as "normally" the first local variable of an instance method has the same type as the declaring type. The "weirdness" with Kotlin is that the first local parameter type is *not* the same as the declaring type, it's of a "random" implemented interface type! % mono class-parse.exe --dump kotlin/coroutines/AbstractCoroutineContextElement.class … ThisClass: Utf8("kotlin/coroutines/AbstractCoroutineContextElement") … 3: fold (Ljava/lang/Object;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; Public Code(13, Unknown[LineNumberTable](6), LocalVariableTableAttribute( LocalVariableTableEntry(Name='this', Descriptor='Lkotlin/coroutines/CoroutineContext$Element;', StartPC=0, Index=0), LocalVariableTableEntry(Name='initial', Descriptor='Ljava/lang/Object;', StartPC=0, Index=1), LocalVariableTableEntry(Name='operation', Descriptor='Lkotlin/jvm/functions/Function2;', StartPC=0, Index=2))) Signature(<R:Ljava/lang/Object;>(TR;Lkotlin/jvm/functions/Function2<-TR;-Lkotlin/coroutines/CoroutineContext$Element;+TR;>;)TR;) RuntimeInvisibleParameterAnnotationsAttribute(Parameter0(), Parameter1(Annotation('Lorg/jetbrains/annotations/NotNull;', {}))) … Here, we "expect" the `this` local variable to be of type `kotlin.coroutines.AbstractCoroutineContextElement`, but it is instead of type `kotlin.coroutines.CoroutineContext.Element`. This "type mismatch" means that our logic to skip the first local variable doesn't actually skip the first local variable. But wait, Kotlin can throw differently weird stuff at us, too. See e.g. [inline and reified type parameters][0], which can result in local parameter names such as `$i$f$get`. To better address these scenarios, relax and rework the logic in `MethodInfo.UpdateParametersFromLocalVariables()`: instead of requiring that we know the "start" offset between the local variable names and the parameters (previous world order), instead look for a "run" of local variables which have the same types, in the same order, as the descriptor parameters. If there are extra local variables, *ignore them*. The search for the "run" needs to start at the *end* of the local variables table, so that it doesn't inadvertently match the `this` parameter, e.g. as seen with a "copy" constructor `DeclaringType(DeclaringType)`. This allows to "cleanly" handle `fold()`: when checking for a matching "run" of `{ Object, Function2 }`, we'll skip the `this` parameter and nicely align on the `initial` parameter. This does mean that if a method has no descriptor parameters, it'll match *everything*, we arguably have less validation occurring, but @jonpryor isn't convinced we were gaining anything from that. Update `Xamarin.Android.Tools.Bytecode-Tests.targets` so that there are more `.java` files built *without* `java -parameters`, so that the "parameter name inference" logic is actually tested. (When `javac -parameters` is used, the `MethodParametersAttribute` blob is emitted, which contains proper parameter names.) [0]: https://medium.com/swlh/inline-and-reified-type-parameters-in-kotlin-c7585490e103
1 parent 79744f6 commit c1e9c0a

File tree

6 files changed

+139
-36
lines changed

6 files changed

+139
-36
lines changed

src/Xamarin.Android.Tools.Bytecode/Methods.cs

+40-32
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public ParameterInfo[] GetParameters ()
9393
UpdateParametersFromMethodParametersAttribute (parameters);
9494
return parameters;
9595
}
96+
9697
static IEnumerable<string> ExtractTypesFromSignature (string signature)
9798
{
9899
if (signature == null || signature.Length < "()V".Length)
@@ -106,6 +107,7 @@ static IEnumerable<string> ExtractTypesFromSignature (string signature)
106107
yield return Signature.ExtractType (signature, ref index);
107108
}
108109
}
110+
109111
List<ParameterInfo> GetParametersFromDescriptor (out int endParams)
110112
{
111113
var signature = Descriptor;
@@ -165,44 +167,50 @@ void UpdateParametersFromLocalVariables (ParameterInfo[] parameters)
165167
return;
166168

167169
var names = locals.LocalVariables.Where (p => p.StartPC == 0).ToList ();
168-
int namesStart = 0;
169-
if (!AccessFlags.HasFlag (MethodAccessFlags.Static) &&
170-
names.Count > namesStart &&
171-
names [namesStart].Descriptor == DeclaringType.FullJniName) {
172-
namesStart++; // skip `this` parameter
173-
}
174-
if (!DeclaringType.IsStatic &&
175-
IsConstructor &&
176-
names.Count > namesStart &&
177-
DeclaringType.InnerClass != null && DeclaringType.InnerClass.OuterClassName != null &&
178-
names [namesStart].Descriptor == "L" + DeclaringType.InnerClass.OuterClassName + ";") {
179-
namesStart++; // "outer `this`", for non-static inner classes
180-
}
181-
if (!DeclaringType.IsStatic &&
182-
IsConstructor &&
183-
names.Count > namesStart &&
184-
DeclaringType.TryGetEnclosingMethodInfo (out var declaringClass, out var _, out var _) &&
185-
names [namesStart].Descriptor == "L" + declaringClass + ";") {
186-
namesStart++; // "outer `this`", for non-static inner classes
170+
171+
int parametersCount = GetDeclaredParametersCount (parameters);
172+
173+
if (!FindParameterRun (parameters, parametersCount, names, out int namesStart)) {
174+
CheckDescriptorVariablesToLocalVariables (parameters, parameters.Length, names, 0);
175+
return;
187176
}
188177

189-
// For JvmOverloadsConstructor.<init>.(LJvmOverloadsConstructor;IILjava/lang/String;)V
190-
if (namesStart > 0 &&
191-
names.Count > namesStart &&
192-
parameters.Length > 0 &&
193-
names [namesStart].Descriptor != parameters [0].Type.BinaryName &&
194-
names [namesStart-1].Descriptor == parameters [0].Type.BinaryName) {
195-
namesStart--;
178+
for (int i = 0; i < parametersCount; ++i) {
179+
if ((i + namesStart) > names.Count)
180+
break;
181+
parameters [i].Name = names [namesStart+i].Name;
182+
CheckLocalVariableTypeToDescriptorType (i, parameters, names, namesStart);
196183
}
197184

198-
int parametersCount = GetDeclaredParametersCount (parameters);
199185
CheckDescriptorVariablesToLocalVariables (parameters, parametersCount, names, namesStart);
186+
}
200187

201-
int max = Math.Min (parametersCount, names.Count - namesStart);
202-
for (int i = 0; i < max; ++i) {
203-
parameters [i].Name = names [namesStart+i].Name;
204-
CheckLocalVariableTypeToDescriptorType (i, parameters, names, namesStart);
188+
bool FindParameterRun (ParameterInfo[] parameters, int parametersCount, List<LocalVariableTableEntry> names, out int namesStart)
189+
{
190+
namesStart = 0;
191+
192+
if (parametersCount == 0) {
193+
return true;
205194
}
195+
196+
for (int ni = (names.Count - parametersCount); ni >= 0; --ni) {
197+
if ((names.Count - ni) < parametersCount) {
198+
return false;
199+
}
200+
bool valid = true;
201+
for (int i = 0; i < parametersCount; ++i) {
202+
if (parameters [i].Type.BinaryName != names [ni+i].Descriptor) {
203+
valid = false;
204+
break;
205+
}
206+
}
207+
if (valid) {
208+
namesStart = ni;
209+
return true;
210+
}
211+
}
212+
213+
return false;
206214
}
207215

208216
LocalVariableTableAttribute GetLocalVariables ()
@@ -277,7 +285,7 @@ void CheckDescriptorVariablesToLocalVariables (ParameterInfo[] parameters, int p
277285
{
278286
if (AccessFlags.HasFlag (MethodAccessFlags.Synthetic))
279287
return;
280-
if ((names.Count - namesStart) == parametersCount)
288+
if ((names.Count - namesStart) >= parametersCount)
281289
return;
282290
if (IsEnumCtor)
283291
return;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
3+
using Xamarin.Android.Tools.Bytecode;
4+
5+
using NUnit.Framework;
6+
7+
namespace Xamarin.Android.Tools.BytecodeTests {
8+
9+
[TestFixture]
10+
public class JavaTypeNoParametersTests : ClassFileFixture {
11+
12+
const string JavaType = "JavaTypeNoParameters";
13+
14+
[Test]
15+
public void ClassFile_WithNonGenericGlobalType_class ()
16+
{
17+
var c = LoadClassFile (JavaType + ".class");
18+
new ExpectedTypeDeclaration {
19+
MajorVersion = 0x34,
20+
MinorVersion = 0,
21+
ConstantPoolCount = 18,
22+
AccessFlags = ClassAccessFlags.Public | ClassAccessFlags.Super,
23+
FullName = "com/xamarin/JavaTypeNoParameters",
24+
Superclass = new TypeInfo ("java/lang/Object", "Ljava/lang/Object;"),
25+
Methods = {
26+
new ExpectedMethodDeclaration {
27+
Name = "<init>",
28+
AccessFlags = MethodAccessFlags.Public,
29+
ReturnDescriptor = "V",
30+
Parameters = {
31+
new ParameterInfo ("copy", "Lcom/xamarin/JavaTypeNoParameters;", "Lcom/xamarin/JavaTypeNoParameters;"),
32+
},
33+
},
34+
}
35+
}.Assert (c);
36+
}
37+
38+
[Test]
39+
public void XmlDeclaration_WithNonGenericGlobalType_class ()
40+
{
41+
AssertXmlDeclaration (JavaType + ".class", JavaType + ".xml");
42+
}
43+
}
44+
}
45+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<api
2+
api-source="class-parse">
3+
<package
4+
name="com.xamarin"
5+
jni-name="com/xamarin">
6+
<class
7+
abstract="false"
8+
deprecated="not deprecated"
9+
jni-extends="Ljava/lang/Object;"
10+
extends="java.lang.Object"
11+
extends-generic-aware="java.lang.Object"
12+
final="false"
13+
name="JavaTypeNoParameters"
14+
jni-signature="Lcom/xamarin/JavaTypeNoParameters;"
15+
source-file-name="JavaTypeNoParameters.java"
16+
static="false"
17+
visibility="public">
18+
<constructor
19+
deprecated="not deprecated"
20+
final="false"
21+
name="JavaTypeNoParameters"
22+
static="false"
23+
visibility="public"
24+
bridge="false"
25+
synthetic="false"
26+
jni-signature="(Lcom/xamarin/JavaTypeNoParameters;)V">
27+
<parameter
28+
name="copy"
29+
type="com.xamarin.JavaTypeNoParameters"
30+
jni-type="Lcom/xamarin/JavaTypeNoParameters;" />
31+
</constructor>
32+
</class>
33+
</package>
34+
</api>

tests/Xamarin.Android.Tools.Bytecode-Tests/Xamarin.Android.Tools.Bytecode-Tests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
<EmbeddedResource Include="$(IntermediateOutputPath)classes\com\xamarin\JavaType%24RNC%24RPNC.class" />
3939
<EmbeddedResource Include="$(IntermediateOutputPath)classes\com\xamarin\JavaType%24RNC.class" />
4040
<EmbeddedResource Include="$(IntermediateOutputPath)classes\com\xamarin\JavaType.class" />
41+
<EmbeddedResource Include="$(IntermediateOutputPath)classes\com\xamarin\JavaTypeNoParameters.class" />
4142
<EmbeddedResource Include="$(IntermediateOutputPath)classes\com\xamarin\NestedInterface%24DnsSdTxtRecordListener.class" />
4243
<EmbeddedResource Include="$(IntermediateOutputPath)classes\com\xamarin\NestedInterface.class" />
4344
<EmbeddedResource Include="$(IntermediateOutputPath)classes\com\xamarin\ParameterAbstractClass.class" />

tests/Xamarin.Android.Tools.Bytecode-Tests/Xamarin.Android.Tools.Bytecode-Tests.targets

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
<Project>
22

33
<ItemGroup>
4-
<TestJar Include="java\**\*.java" Exclude="java\java\util\Collection.java,java\android\annotation\NonNull.java" />
5-
<TestJarNoParameters Include="java\java\util\Collection.java" />
4+
<TestJarNoParameters Include="java/java/util/Collection.java" />
5+
<TestJarNoParameters Include="java/**/*NoParameters.java" />
6+
<TestJar Include="java\**\*.java" Exclude="@(TestJarNoParameters);java\android\annotation\NonNull.java" />
67
<TestKotlinJar Include="kotlin\**\*.kt" />
78
</ItemGroup>
89

10+
<ItemGroup>
11+
<_BuildClassOutputs Include="@(TestJar->'$(IntermediateOutputPath)classes\%(RecursiveDir)%(Filename).class')" />
12+
<_BuildClassOutputs Include="@(TestJarNoParameters->'$(IntermediateOutputPath)classes\%(RecursiveDir)%(Filename).class')" />
13+
</ItemGroup>
14+
915
<Target Name="BuildClasses"
1016
BeforeTargets="BeforeBuild"
11-
Inputs="@(TestJar)"
12-
Outputs="@(TestJar->'$(IntermediateOutputPath)classes\%(RecursiveDir)%(Filename).class')">
17+
Inputs="@(TestJar);@(TestJarNoParameters)"
18+
Outputs="@(_BuildClassOutputs)">
1319
<MakeDir Directories="$(IntermediateOutputPath)classes" />
1420
<Exec Command="&quot;$(JavaCPath)&quot; -parameters $(_JavacSourceOptions) -g -d &quot;$(IntermediateOutputPath)classes&quot; java/android/annotation/NonNull.java @(TestJar->'%(Identity)', ' ')" />
1521
<Exec Command="&quot;$(JavaCPath)&quot; $(_JavacSourceOptions) -g -d &quot;$(IntermediateOutputPath)classes&quot; @(TestJarNoParameters->'%(Identity)', ' ')" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.xamarin;
2+
3+
public class JavaTypeNoParameters {
4+
/**
5+
* JNI sig: (Lcom/xamarin/JavaTypeNoParameters;)V
6+
*/
7+
public JavaTypeNoParameters (JavaTypeNoParameters copy) {
8+
}
9+
}

0 commit comments

Comments
 (0)