@@ -13,13 +13,13 @@ public static class ManagedApiImporter
13
13
[ Obsolete ( "Use the TypeDefinitionCache overload for better performance." , error : true ) ]
14
14
public static JavaTypeCollection Parse ( AssemblyDefinition assembly , JavaTypeCollection collection ) => throw new NotSupportedException ( ) ;
15
15
16
- public static JavaTypeCollection Parse ( AssemblyDefinition assembly , JavaTypeCollection collection , TypeDefinitionCache resolver )
16
+ public static JavaTypeCollection Parse ( AssemblyDefinition assembly , JavaTypeCollection collection , TypeDefinitionCache resolver , ApiImporterOptions options )
17
17
{
18
18
var types_to_add = new List < JavaTypeModel > ( ) ;
19
19
20
20
foreach ( var md in assembly . Modules )
21
21
foreach ( var td in md . Types ) {
22
- if ( ! ShouldSkipType ( td , resolver ) && ParseType ( td , collection ) is JavaTypeModel type )
22
+ if ( ! ShouldSkipType ( td , resolver , options ) && ParseType ( td , collection , options ) is JavaTypeModel type )
23
23
types_to_add . Add ( type ) ;
24
24
}
25
25
@@ -33,21 +33,21 @@ public static JavaTypeCollection Parse (AssemblyDefinition assembly, JavaTypeCol
33
33
return collection ;
34
34
}
35
35
36
- public static JavaTypeModel ? ParseType ( TypeDefinition type , JavaTypeCollection collection )
36
+ public static JavaTypeModel ? ParseType ( TypeDefinition type , JavaTypeCollection collection , ApiImporterOptions options )
37
37
{
38
38
if ( ! type . IsPublic && ! type . IsNested )
39
39
return null ;
40
40
41
41
if ( ! ShouldImport ( type ) )
42
42
return null ;
43
43
44
- var model = type . IsInterface ? ( JavaTypeModel ? ) ParseInterface ( type , collection ) : ParseClass ( type , collection ) ;
44
+ var model = type . IsInterface ? ( JavaTypeModel ? ) ParseInterface ( type , collection , options ) : ParseClass ( type , collection , options ) ;
45
45
46
46
if ( model is null )
47
47
return null ;
48
48
49
49
foreach ( var nested in type . NestedTypes )
50
- if ( ParseType ( nested , collection ) is JavaTypeModel nested_model )
50
+ if ( ParseType ( nested , collection , options ) is JavaTypeModel nested_model )
51
51
model . NestedTypes . Add ( nested_model ) ;
52
52
53
53
return model ;
@@ -89,19 +89,19 @@ static bool ShouldImport (TypeDefinition td)
89
89
return true ;
90
90
}
91
91
92
- public static JavaClassModel ? ParseClass ( TypeDefinition type , JavaTypeCollection collection )
92
+ public static JavaClassModel ? ParseClass ( TypeDefinition type , JavaTypeCollection collection , ApiImporterOptions options )
93
93
{
94
94
// TODO: type parameters?
95
95
var obs_attr = GetObsoleteAttribute ( type . CustomAttributes ) ;
96
- var reg_attr = GetRegisterAttribute ( type . CustomAttributes ) ;
96
+ var reg_attr = GetRegisterAttribute ( type . CustomAttributes , options ) ;
97
97
98
98
if ( reg_attr is null )
99
99
return null ;
100
100
101
101
var encoded_fullname = ( ( string ) reg_attr . ConstructorArguments [ 0 ] . Value ) . Replace ( '/' , '.' ) ;
102
102
var ( package , nested_name ) = DecodeRegisterJavaFullName ( encoded_fullname ) ;
103
103
104
- var base_jni = GetBaseTypeJni ( type ) ;
104
+ var base_jni = GetBaseTypeJni ( type , options ) ;
105
105
106
106
var model = new JavaClassModel (
107
107
javaPackage : GetOrCreatePackage ( collection , package , type . Namespace ) ,
@@ -118,20 +118,20 @@ static bool ShouldImport (TypeDefinition td)
118
118
annotatedVisibility : string . Empty
119
119
) ; ;
120
120
121
- ParseImplementedInterfaces ( type , model ) ;
121
+ ParseImplementedInterfaces ( type , model , options ) ;
122
122
123
123
foreach ( var method in type . Methods . Where ( m => ! m . IsConstructor ) )
124
- if ( ParseMethod ( method , model ) is JavaMethodModel m )
124
+ if ( ParseMethod ( method , model , options ) is JavaMethodModel m )
125
125
model . Methods . Add ( m ) ;
126
126
127
127
return model ;
128
128
}
129
129
130
- public static JavaInterfaceModel ? ParseInterface ( TypeDefinition type , JavaTypeCollection collection )
130
+ public static JavaInterfaceModel ? ParseInterface ( TypeDefinition type , JavaTypeCollection collection , ApiImporterOptions options )
131
131
{
132
132
// TODO: type paramters?
133
133
var obs_attr = GetObsoleteAttribute ( type . CustomAttributes ) ;
134
- var reg_attr = GetRegisterAttribute ( type . CustomAttributes ) ;
134
+ var reg_attr = GetRegisterAttribute ( type . CustomAttributes , options ) ;
135
135
136
136
if ( reg_attr is null )
137
137
return null ;
@@ -149,22 +149,22 @@ static bool ShouldImport (TypeDefinition td)
149
149
annotatedVisibility : ""
150
150
) ;
151
151
152
- ParseImplementedInterfaces ( type , model ) ;
152
+ ParseImplementedInterfaces ( type , model , options ) ;
153
153
154
154
foreach ( var method in type . Methods )
155
- if ( ParseMethod ( method , model ) is JavaMethodModel m )
155
+ if ( ParseMethod ( method , model , options ) is JavaMethodModel m )
156
156
model . Methods . Add ( m ) ;
157
157
158
158
return model ;
159
159
}
160
160
161
- public static JavaMethodModel ? ParseMethod ( MethodDefinition method , JavaTypeModel declaringType )
161
+ public static JavaMethodModel ? ParseMethod ( MethodDefinition method , JavaTypeModel declaringType , ApiImporterOptions options )
162
162
{
163
163
if ( method . IsPrivate || method . IsAssembly )
164
164
return null ;
165
165
166
166
var obs_attr = GetObsoleteAttribute ( method . CustomAttributes ) ;
167
- var reg_attr = GetRegisterAttribute ( method . CustomAttributes ) ;
167
+ var reg_attr = GetRegisterAttribute ( method . CustomAttributes , options ) ;
168
168
169
169
if ( reg_attr is null )
170
170
return null ;
@@ -225,7 +225,7 @@ static void AddReferenceTypeRecursive (JavaTypeModel type, JavaTypeCollection co
225
225
AddReferenceTypeRecursive ( nested , collection ) ;
226
226
}
227
227
228
- static bool ShouldSkipType ( TypeDefinition type , TypeDefinitionCache cache )
228
+ static bool ShouldSkipType ( TypeDefinition type , TypeDefinitionCache cache , ApiImporterOptions options )
229
229
{
230
230
// We want to use Java's collection types instead of our managed adapter.
231
231
// eg: 'Java.Util.ArrayList' over 'Android.Runtime.JavaList'
@@ -246,28 +246,28 @@ static bool ShouldSkipType (TypeDefinition type, TypeDefinitionCache cache)
246
246
? type . Module . GetType ( type . FullName . Substring ( 0 , type . FullName . IndexOf ( '`' ) ) )
247
247
: null ;
248
248
249
- if ( ShouldSkipGeneric ( type , non_generic_type , cache ) )
249
+ if ( ShouldSkipGeneric ( type , non_generic_type , cache , options ) )
250
250
return true ;
251
251
252
252
return false ;
253
253
}
254
254
255
- static bool ShouldSkipGeneric ( TypeDefinition ? a , TypeDefinition ? b , TypeDefinitionCache cache )
255
+ static bool ShouldSkipGeneric ( TypeDefinition ? a , TypeDefinition ? b , TypeDefinitionCache cache , ApiImporterOptions options )
256
256
{
257
257
if ( a == null || b == null )
258
258
return false ;
259
259
if ( ! a . ImplementsInterface ( "Android.Runtime.IJavaObject" , cache ) || ! b . ImplementsInterface ( "Android.Runtime.IJavaObject" , cache ) )
260
260
return false ;
261
261
262
- return GetRegisteredJavaTypeName ( a ) == GetRegisteredJavaTypeName ( b ) ;
262
+ return GetRegisteredJavaTypeName ( a , options ) == GetRegisteredJavaTypeName ( b , options ) ;
263
263
}
264
264
265
- static string ? TypeReferenceToJavaType ( TypeReference type )
265
+ static string ? TypeReferenceToJavaType ( TypeReference type , ApiImporterOptions options )
266
266
{
267
- var retval = GetRegisteredJavaName ( type ) ;
267
+ var retval = GetRegisteredJavaName ( type , options ) ;
268
268
269
269
if ( retval != null && type is GenericInstanceType generic ) {
270
- var parameters = generic . GenericArguments . Select ( ga => GetRegisteredJavaName ( ga . Resolve ( ) ) ) . ToArray ( ) ;
270
+ var parameters = generic . GenericArguments . Select ( ga => GetRegisteredJavaName ( ga . Resolve ( ) , options ) ) . ToArray ( ) ;
271
271
272
272
if ( parameters . WhereNotNull ( ) . Any ( ) )
273
273
retval += $ "<{ string . Join ( ", " , parameters . WhereNotNull ( ) ) } >";
@@ -276,14 +276,14 @@ static bool ShouldSkipGeneric (TypeDefinition? a, TypeDefinition? b, TypeDefinit
276
276
return retval ;
277
277
}
278
278
279
- static string ? GetRegisteredJavaName ( TypeReference type )
279
+ static string ? GetRegisteredJavaName ( TypeReference type , ApiImporterOptions options )
280
280
{
281
281
var td = type . Resolve ( ) ;
282
282
283
- return GetRegisteredJavaTypeName ( td ) ;
283
+ return GetRegisteredJavaTypeName ( td , options ) ;
284
284
}
285
285
286
- static void ParseImplementedInterfaces ( TypeDefinition type , JavaTypeModel model )
286
+ static void ParseImplementedInterfaces ( TypeDefinition type , JavaTypeModel model , ApiImporterOptions options )
287
287
{
288
288
foreach ( var iface_impl in type . Interfaces ) {
289
289
var iface = iface_impl . InterfaceType ;
@@ -292,7 +292,7 @@ static void ParseImplementedInterfaces (TypeDefinition type, JavaTypeModel model
292
292
if ( iface_def is null || iface_def . IsNotPublic )
293
293
continue ;
294
294
295
- if ( GetRegisterAttribute ( iface_def . CustomAttributes ) is CustomAttribute reg_attr ) {
295
+ if ( GetRegisterAttribute ( iface_def . CustomAttributes , options ) is CustomAttribute reg_attr ) {
296
296
var jni = ( string ) reg_attr . ConstructorArguments [ 0 ] . Value ;
297
297
var name = jni . Replace ( '/' , '.' ) . Replace ( '$' , '.' ) ;
298
298
@@ -301,7 +301,7 @@ static void ParseImplementedInterfaces (TypeDefinition type, JavaTypeModel model
301
301
}
302
302
}
303
303
304
- static string GetBaseTypeJni ( TypeDefinition type )
304
+ static string GetBaseTypeJni ( TypeDefinition type , ApiImporterOptions options )
305
305
{
306
306
// Find a Java base type, ignoring generic types, if nothing else it will be Java.Lang.Object
307
307
TypeDefinition ? base_type = type ;
@@ -319,7 +319,7 @@ static string GetBaseTypeJni (TypeDefinition type)
319
319
if ( base_type . HasGenericParameters || base_type . IsGenericInstance )
320
320
continue ;
321
321
322
- if ( GetRegisterAttribute ( base_type . CustomAttributes ) is CustomAttribute reg_attr )
322
+ if ( GetRegisterAttribute ( base_type . CustomAttributes , options ) is CustomAttribute reg_attr )
323
323
return ( string ) reg_attr . ConstructorArguments [ 0 ] . Value ;
324
324
}
325
325
@@ -329,19 +329,19 @@ static string GetBaseTypeJni (TypeDefinition type)
329
329
static CustomAttribute ? GetObsoleteAttribute ( Collection < CustomAttribute > attributes ) =>
330
330
attributes . FirstOrDefault ( a => a . AttributeType . FullNameCorrected ( ) == "System.ObsoleteAttribute" ) ;
331
331
332
- static CustomAttribute ? GetRegisterAttribute ( Collection < CustomAttribute > attributes ) =>
332
+ static CustomAttribute ? GetRegisterAttribute ( Collection < CustomAttribute > attributes , ApiImporterOptions options ) =>
333
333
attributes . FirstOrDefault ( a => {
334
334
var attrType = a . AttributeType . FullNameCorrected ( ) ;
335
- return attrType == "Android.Runtime.RegisterAttribute" ||
336
- attrType == "Java.Interop.JniTypeSignatureAttribute" ;
335
+
336
+ return options . SupportedTypeMapAttributes . Contains ( attrType ) ;
337
337
} ) ;
338
338
339
- static string ? GetRegisteredJavaTypeName ( TypeDefinition type )
339
+ static string ? GetRegisteredJavaTypeName ( TypeDefinition type , ApiImporterOptions options )
340
340
{
341
341
if ( GetSpecialCase ( type ) is string s )
342
342
return s ;
343
343
344
- if ( GetRegisterAttribute ( type . CustomAttributes ) is CustomAttribute reg_attr )
344
+ if ( GetRegisterAttribute ( type . CustomAttributes , options ) is CustomAttribute reg_attr )
345
345
return ( ( string ) reg_attr . ConstructorArguments [ 0 ] . Value ) . Replace ( '/' , '.' ) ;
346
346
347
347
return null ;
0 commit comments