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

Commit 82a518b

Browse files
committed
[generator] Fix issue where generic types couldn't be found in SymbolTable (#543)
1 parent d61b329 commit 82a518b

2 files changed

Lines changed: 81 additions & 2 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using MonoDroid.Generation;
3+
using NUnit.Framework;
4+
5+
namespace generatortests
6+
{
7+
[TestFixture]
8+
public class SymbolTableTests
9+
{
10+
[Test]
11+
public void FindGenericTypes ()
12+
{
13+
var table = new SymbolTable ();
14+
15+
var list = new InterfaceGen (new GenBaseSupport {
16+
Name = "System.Collections.Generic.IList`1",
17+
FullName = "System.Collections.Generic.IList`1",
18+
JavaSimpleName = "System.Collections.Generic.IList`1"
19+
});
20+
21+
table.AddType (list);
22+
23+
var dict = new InterfaceGen (new GenBaseSupport {
24+
Name = "System.Collections.Generic.IDictionary`2",
25+
FullName = "System.Collections.Generic.IDictionary`2",
26+
JavaSimpleName = "System.Collections.Generic.IDictionary`2"
27+
});
28+
29+
table.AddType (dict);
30+
31+
Assert.NotNull (table.Lookup ("System.Collections.Generic.IList<Java.Util.Locale.LanguageRange>"));
32+
Assert.NotNull (table.Lookup ("System.Collections.Generic.IList<List<Java.Util.Locale.LanguageRange>>"));
33+
34+
Assert.NotNull (table.Lookup ("System.Collections.Generic.IDictionary<string, Java.Util.Locale.LanguageRange>"));
35+
Assert.NotNull (table.Lookup ("System.Collections.Generic.IDictionary<string, List<Java.Util.Locale.LanguageRange>>"));
36+
}
37+
}
38+
}

‎tools/generator/Java.Interop.Tools.Generator.ObjectModel/Symbols/SymbolTable.cs‎

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,15 @@ public ISymbol Lookup (string java_type)
260260
if (all_symbols_cache == null)
261261
all_symbols_cache = new ConcurrentDictionary<string, ISymbol> (symbols.Values.SelectMany (v => v).GroupBy (s => s.FullName).ToDictionary (s => s.Key, s => s.FirstOrDefault ()));
262262

263-
all_symbols_cache.TryGetValue (key, out sym);
263+
if (!all_symbols_cache.TryGetValue (key, out sym)) {
264+
// We may be looking for a type like:
265+
// - System.Collections.Generic.IList<Java.Util.Locale.LanguageRange>
266+
// Our key is "System.Collections.Generic.IList", but it's stored in
267+
// the symbol table with the arity so we need to look for
268+
// "System.Collections.Generic.IList`1" to find a match
269+
key = AddArity (key, type_params);
270+
all_symbols_cache.TryGetValue (key, out sym);
271+
}
264272
}
265273
}
266274
ISymbol result;
@@ -290,7 +298,40 @@ public ISymbol Lookup (string java_type)
290298

291299
return result;
292300
}
293-
301+
302+
private string AddArity (string key, string typeParams)
303+
{
304+
if (string.IsNullOrWhiteSpace (typeParams) || !typeParams.StartsWith ("<") || !typeParams.EndsWith (">"))
305+
return key;
306+
307+
var nested_count = 0;
308+
var arity = 1;
309+
310+
// Remove the outer <>
311+
typeParams = typeParams.Substring (1, typeParams.Length - 2);
312+
313+
foreach (var c in typeParams) {
314+
if (nested_count > 0) {
315+
if (c == '>') {
316+
nested_count--;
317+
continue;
318+
}
319+
320+
if (c == '<') {
321+
nested_count++;
322+
continue;
323+
}
324+
325+
continue;
326+
}
327+
328+
if (c == ',')
329+
arity++;
330+
}
331+
332+
return $"{key}`{arity}";
333+
}
334+
294335
public void Dump ()
295336
{
296337
foreach (var p in symbols) {

0 commit comments

Comments
 (0)