-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathInternalExtensions.cs
2646 lines (2404 loc) · 113 KB
/
InternalExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2024 MASES s.r.l.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Refer to LICENSE for more information.
*/
using System.IO;
using System;
using System.IO.Compression;
using System.Linq;
using System.Collections.Generic;
using Java.Lang;
using Java.Lang.Reflect;
using System.Text;
using MASES.JNetReflector.Templates;
using System.Runtime.CompilerServices;
namespace MASES.JNetReflector
{
static class JNetReflectorExtensions
{
static string _CurrentJavadocBaseUrl;
static int _CurrentJavadocVersion;
static Java.Lang.ClassLoader _loader;
static Java.Lang.ClassLoader SystemClassLoader
{
get
{
if (_loader == null) _loader = Java.Lang.ClassLoader.SystemClassLoader;
return _loader;
}
}
#region General info
public static void SetJavaDocInfo(string currentJavadocBaseUrl, int currentJavadocVersion)
{
_CurrentJavadocBaseUrl = currentJavadocBaseUrl;
_CurrentJavadocVersion = currentJavadocVersion;
}
#endregion
#region string extension
public static bool IsJVMNestedClass(this string entry)
{
if (entry.Contains(SpecialNames.NestedClassSeparator)) return true;
return false;
}
public static bool IsReservedName(this string entry)
{
var testName = entry.Contains(SpecialNames.BeginGenericDeclaration) ? entry.Substring(0, entry.IndexOf(SpecialNames.BeginGenericDeclaration)) : entry;
if (SpecialNames.ReservedLanguageNames.Any((n) => testName.Equals(n))) return true;
if (SpecialNames.ReservedJNetNames.Any((n) => testName.Equals(n))) return true;
if (SpecialNames.NumberStartNames.Any((n) => testName.StartsWith(n))) return true;
return false;
}
public static bool CollapseWithClassOrNestedClass(this string entry, int nestingLevel, IEnumerable<Class> classDefinitions)
{
foreach (var classDefinition in classDefinitions)
{
bool collpase = false;
if (classDefinition.IsJVMNestedClass())
{
if (classDefinition.JVMNestingLevels() == nestingLevel) // same level of requester
{
collpase = entry == classDefinition.JVMNestedClassName(nestingLevel, null, false);
}
else if (classDefinition.JVMNestingLevels() > nestingLevel) // more levels then requester
{
collpase = entry == classDefinition.JVMNestedClassName(nestingLevel + 1, null, false);
}
}
else
{
collpase = entry == classDefinition.JVMSimpleClassName();
}
if (collpase) return true;
}
return false;
}
public static bool CollapseWithOtherMethods(this string entry, Method methodToCheck, IEnumerable<Method> methodToBeReflected, IEnumerable<Class> classDefinitions, bool camel)
{
foreach (var method in methodToBeReflected)
{
if (methodToCheck.GenericString == method.GenericString) continue; // bypass this method
var testName = method.MethodName(classDefinitions, false, camel);
testName = testName.Contains(SpecialNames.BeginGenericDeclaration) ? testName.Substring(0, testName.IndexOf(SpecialNames.BeginGenericDeclaration)) : testName;
if (entry == testName)
{
return true;
}
}
return false;
}
public static Class JVMClass(this string entry, bool throwOnError = false)
{
try
{
return Class.ForName(entry, true, SystemClassLoader);
}
catch
{
if (throwOnError) throw;
return null;
}
}
static bool IsJVMListenerClassAvoidJavaFile(this string typeName)
{
if (JNetReflectorCore.ClassesToAvoidJavaListener != null && JNetReflectorCore.ClassesToAvoidJavaListener.Any((o) => typeName == o)) return true;
return false;
}
static bool IsJVMListenerClass(this string typeName)
{
if (typeName.StartsWith(SpecialNames.JavaUtilFunctions)) return true;
if (typeName.EndsWith(SpecialNames.JavaLangListener)) return true;
if (typeName.EndsWith(SpecialNames.JavaLangAdapter)) return true;
if (JNetReflectorCore.ClassesToBeListener != null && JNetReflectorCore.ClassesToBeListener.Any((o) => typeName == o)) return true;
return false;
}
public static string JVMNestedClassName(this string entry)
{
return entry.Substring(entry.LastIndexOf(SpecialNames.NestedClassSeparator) + 1);
}
public static string JVMSimpleClassName(this string entry)
{
var cName = entry.Remove(0, entry.LastIndexOf(SpecialNames.NamespaceSeparator) + 1);
cName = cName.Contains(SpecialNames.NestedClassSeparator) ? cName.Substring(0, cName.LastIndexOf(SpecialNames.NestedClassSeparator)) : cName;
return cName;
}
public static string Namespace(string fullName, bool camel)
{
if (fullName.EndsWith(SpecialNames.ClassExtension))
{
fullName = fullName.Remove(fullName.IndexOf(SpecialNames.ClassExtension));
}
var package = fullName.Contains(SpecialNames.NamespaceSeparator) ? fullName.Substring(0, fullName.LastIndexOf(SpecialNames.NamespaceSeparator)) : fullName;
if (JNetReflectorCore.NamespacesInConflict != null)
{
foreach (var nsc in JNetReflectorCore.NamespacesInConflict)
{
if (package.StartsWith(nsc))
{
package = package.Replace(nsc, nsc + SpecialNames.NamespaceSuffix);
break;
}
}
}
var splitted = package.Split(SpecialNames.NamespaceSeparator);
var ns = string.Join(SpecialNames.NamespaceSeparator.ToString(), splitted.Select((o) => camel ? Camel(o) : o));
return ns;
}
public static string Camel(this string str)
{
if (str.Length == 0 || str.Length == 1) return str;
else return char.ToUpper(str[0]) + str.Substring(1);
}
static string ToFullQualifiedClassName(string canonicalName, bool camel)
{
if (canonicalName.Contains(SpecialNames.NamespaceSeparator))
{
string className = canonicalName.Substring(canonicalName.LastIndexOf(SpecialNames.NamespaceSeparator) + 1);
className = Namespace(canonicalName, camel) + SpecialNames.NamespaceSeparator + className.Replace(SpecialNames.NestedClassSeparator, SpecialNames.NamespaceSeparator);
return className;
}
else
{
return canonicalName.Replace(SpecialNames.NestedClassSeparator, SpecialNames.NamespaceSeparator);
}
}
static string ToFullQualifiedInterfaceName(string canonicalName, bool camel)
{
string nsStr = string.Empty;
string className = string.Empty;
if (canonicalName.Contains(SpecialNames.BeginGenericDeclaration))
{
var generic = canonicalName.Substring(canonicalName.IndexOf(SpecialNames.BeginGenericDeclaration));
var baseClass = canonicalName.Substring(0, canonicalName.IndexOf(generic));
if (baseClass.Contains(SpecialNames.NamespaceSeparator))
{
className = baseClass.Substring(baseClass.LastIndexOf(SpecialNames.NamespaceSeparator) + 1);
nsStr = baseClass.Substring(0, baseClass.LastIndexOf(SpecialNames.NamespaceSeparator));
}
else
{
className = baseClass;
}
className += generic;
}
else
{
if (canonicalName.Contains(SpecialNames.NamespaceSeparator))
{
className = canonicalName.Substring(canonicalName.LastIndexOf(SpecialNames.NamespaceSeparator) + 1);
nsStr = canonicalName.Substring(0, canonicalName.LastIndexOf(SpecialNames.NamespaceSeparator));
}
else
{
className = canonicalName;
}
}
return string.IsNullOrWhiteSpace(nsStr) ? "I" + className : nsStr + SpecialNames.NamespaceSeparator + "I" + className;
}
static string ConvertClassesInConflict(this string fName)
{
string nName = string.Empty;
string cName = string.Empty;
if (fName.Contains(SpecialNames.NamespaceSeparator))
{
var index = fName.LastIndexOf(SpecialNames.NamespaceSeparator);
nName = fName.Substring(0, index);
cName = fName.Substring(index + 1);
}
else
{
cName = fName;
}
if (JNetReflectorCore.ClassesInConflict != null)
{
foreach (var cic in JNetReflectorCore.ClassesInConflict)
{
if (cName == cic)
{
cName += "Class";
break;
}
}
}
return string.IsNullOrEmpty(nName) ? cName : nName + SpecialNames.NamespaceSeparator + cName;
}
public static string AddTabLevel(this string origin, int level)
{
if (string.IsNullOrEmpty(origin)) return origin;
string tabber = string.Empty;
for (int i = 0; i < level; i++)
{
tabber += " ";
}
StringBuilder sb = new StringBuilder();
var pieces = origin.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
for (int i = 0; i < pieces.Length; i++)
{
if (i > 0 && i != pieces.Length - 1 && pieces[i - 1].Length != 0 && pieces[i].Length == 0) sb.Append(Environment.NewLine);
else if (i == pieces.Length - 1 && pieces[i].Length != 0) sb.Append(tabber + pieces[i]);
else if (i == pieces.Length - 1 && pieces[i].Length == 0) sb.Append(pieces[i]);
else sb.AppendLine(tabber + pieces[i]);
}
return sb.ToString();
}
public static string ConvertToJavadoc(this string result)
{
if (result.EndsWith(SpecialNames.JavaLangAnyType)) result = result.Substring(0, result.IndexOf(SpecialNames.JavaLangAnyType));
if (result.EndsWith(", new()")) result = result.Substring(0, result.IndexOf(", new()"));
if (result.Contains(SpecialNames.ArrayTypeTrailer)) result = result.Substring(0, result.IndexOf(SpecialNames.ArrayTypeTrailer));
return result.Replace(SpecialNames.BeginGenericDeclaration, "{").Replace(SpecialNames.EndGenericDeclaration, "}");
}
public static string RemoveThrowsAndCleanupSignature(this string methodSignature)
{
if (methodSignature.Contains(SpecialNames.JavaLangThrows))
{
methodSignature = methodSignature.Substring(0, methodSignature.IndexOf(SpecialNames.JavaLangThrows));
return methodSignature.TrimEnd();
}
else if (methodSignature.EndsWith(';'))
{
return methodSignature.Substring(0, methodSignature.Length - 1);
}
return methodSignature;
}
public static string AddClassNameToSignature(this string methodSignature, string className)
{
int index = methodSignature.IndexOf('(');
if (index == -1) return methodSignature; // not a method
int index2 = methodSignature.Substring(0, index).LastIndexOf(' ');
methodSignature = methodSignature.Insert(index2 + 1, className + ".");
return methodSignature;
}
public static string SignatureFromGenericString(this IReadOnlyDictionary<string, string> methodSignatures, string genString)
{
var filteredGenString = genString.RemoveThrowsAndCleanupSignature();
string signature = null;
methodSignatures.TryGetValue(filteredGenString, out signature);
return signature;
}
#endregion
#region ZipArchiveEntry extension
public static bool IsSpecialFolder(this ZipArchiveEntry entry)
{
var name = entry.FullName.ToLowerInvariant();
if (name.Contains(FileNameAndDirectory.METAINF.ToLowerInvariant())
|| (JNetReflectorCore.NamespacesToAvoid != null && JNetReflectorCore.NamespacesToAvoid.Any((n) => entry.Namespace(false).StartsWith(n))))
{
return true;
}
return false;
}
public static bool IsSpecialClass(this ZipArchiveEntry entry)
{
if (entry.Name.EndsWith(SpecialNames.NestedClassSeparator.ToString()) // special class defined from Scala conversion
|| (entry.IsJVMNestedClass()
&& SpecialNames.SpecialNumberedNames.Any((o) => entry.JVMNestedClassName().StartsWith(o)))
) return true;
return false;
}
public static bool IsFolder(this ZipArchiveEntry entry)
{
if (entry.Length == 0) return true;
return false;
}
public static bool IsJVMNestedClass(this ZipArchiveEntry entry)
{
if (entry.Length != 0
&& entry.Name.EndsWith(FileNameAndDirectory.JavaClassExtension)
&& entry.Name.Contains(SpecialNames.NestedClassSeparator)) return true;
return false;
}
public static bool IsJVMClass(this ZipArchiveEntry entry)
{
if (entry.Length != 0
&& entry.Name.EndsWith(FileNameAndDirectory.JavaClassExtension)
&& !entry.Name.Contains(SpecialNames.NestedClassSeparator)) return true;
return false;
}
public static string Namespace(this ZipArchiveEntry entry, bool camel)
{
return Namespace(entry.FullName.Replace(SpecialNames.JNISeparator, SpecialNames.NamespaceSeparator), camel);
}
public static string JVMClassName(this ZipArchiveEntry entry)
{
var cName = Path.GetFileNameWithoutExtension(entry.Name);
return cName.Contains(SpecialNames.NestedClassSeparator) ? cName.Substring(0, cName.LastIndexOf(SpecialNames.NestedClassSeparator)) : cName;
}
public static string JVMNestedClassName(this ZipArchiveEntry entry)
{
var cName = Path.GetFileNameWithoutExtension(entry.Name);
return cName.Substring(cName.LastIndexOf(SpecialNames.NestedClassSeparator) + 1);
}
public static string JVMFullQualifiedClassName(this ZipArchiveEntry entry)
{
var cName = entry.FullName;
cName = cName.Contains(SpecialNames.ClassExtension) ? cName.Substring(0, cName.LastIndexOf(SpecialNames.ClassExtension)) : cName;
return cName.Replace(SpecialNames.JNISeparator, SpecialNames.NamespaceSeparator);
}
public static Class JVMClass(this ZipArchiveEntry entry)
{
try
{
var cName = entry.JVMFullQualifiedClassName();
return Class.ForName(cName, true, SystemClassLoader);
}
catch
{
return null;
}
}
#endregion
#region TypeVariable[] extension
public static void GetGenerics(this Java.Lang.Reflect.TypeVariable[] entries, IList<string> genArguments, IList<KeyValuePair<string, string>> genClauses, string prefix, bool reportNative, bool usedInGenerics, bool camel, out bool mustBeAvoided)
{
mustBeAvoided = false;
foreach (var entry in entries)
{
List<string> genArgumentsLocal = new List<string>();
List<KeyValuePair<string, string>> genClauseLocal = new List<KeyValuePair<string, string>>();
entry.GetGenerics(genArgumentsLocal, genClauseLocal, prefix, reportNative, usedInGenerics, camel, out var localMustBeAvoided);
mustBeAvoided |= localMustBeAvoided;
foreach (var item in genArgumentsLocal)
{
if (genArguments != null && !genArguments.Contains(item))
{
genArguments?.Add(item);
}
}
foreach (var item in genClauseLocal)
{
if (genClauses != null)
{
bool hasKey = false;
foreach (var genClause in genClauses)
{
if (genClause.Key == item.Key) { hasKey = true; break; }
}
if (!hasKey) genClauses?.Add(item);
}
}
}
}
static string ApplyGenerics(this TypeVariable[] entries, IList<KeyValuePair<string, string>> genClause, string prefix, string name, bool usedInGenerics, bool camel)
{
List<string> genArguments = new List<string>();
entries.GetGenerics(genArguments, genClause, prefix, true, usedInGenerics, camel, out bool _);
var parameters = genArguments.ConvertGenerics();
if (!string.IsNullOrEmpty(parameters))
{
return $"{name}<{parameters}>";
}
return name;
}
public static string WhereClauses(this TypeVariable[] entry, bool usedInGenerics, bool camel)
{
StringBuilder sbWhere = new StringBuilder();
foreach (var typeParameter in entry)
{
StringBuilder sbBounds = new StringBuilder();
foreach (var bound in typeParameter.Bounds)
{
if (!IsJVMNativeType(bound.TypeName))
{
string result = bound.GetBound(usedInGenerics, camel);
sbBounds.AppendFormat("{0}, ", result);
}
}
var bounds = sbBounds.ToString();
if (!string.IsNullOrEmpty(bounds))
{
bounds = bounds.Substring(0, bounds.LastIndexOf(", "));
sbWhere.AppendFormat(" where {0}: {1}", typeParameter.Name, bounds);
}
}
var parameters = sbWhere.ToString();
return parameters;
}
public static string Namespace(this TypeVariable entry, bool camel)
{
var typeName = entry.Name;
typeName = typeName.Contains(SpecialNames.BeginGenericDeclaration) ? typeName.Substring(0, typeName.IndexOf(SpecialNames.BeginGenericDeclaration)) : typeName;
return Namespace(typeName, camel);
}
public static bool IsNamespaceToAvoid(this TypeVariable entry)
{
if (JNetReflectorCore.NamespacesToAvoid != null && JNetReflectorCore.NamespacesToAvoid.Any((n) => entry.Namespace(false).StartsWith(n))) return true;
return false;
}
public static bool IsClassToAvoid(this TypeVariable entry)
{
var typeName = entry.Name;
if (typeName.EndsWith(SpecialNames.ArrayTypeTrailer)) typeName = typeName.Remove(typeName.LastIndexOf(SpecialNames.ArrayTypeTrailer));
typeName = typeName.Contains(SpecialNames.BeginGenericDeclaration) ? typeName.Substring(0, typeName.IndexOf(SpecialNames.BeginGenericDeclaration)) : typeName;
if (JNetReflectorCore.ClassesToAvoid != null && JNetReflectorCore.ClassesToAvoid.Any((n) => typeName == n)) return true;
return false;
}
#endregion
#region Type extension
public static bool TypeNameMustBeAvoided(this string typeName)
{
bool toBeAvoided = false;
if (typeName.Contains(SpecialNames.BeginGenericDeclaration))
{
var clsName = typeName.Substring(0, typeName.IndexOf(SpecialNames.BeginGenericDeclaration));
clsName = clsName.Contains(SpecialNames.ArrayTypeTrailer) ? clsName.Substring(0, clsName.IndexOf(SpecialNames.ArrayTypeTrailer)) : clsName;
toBeAvoided = clsName.ClassTypeNameMustBeAvoided();
if (!toBeAvoided)
{
var genTypes = typeName.Substring(typeName.IndexOf(SpecialNames.BeginGenericDeclaration) + 1);
genTypes = genTypes.Substring(0, genTypes.LastIndexOf(SpecialNames.EndGenericDeclaration));
var types = genTypes.Split(',', ' ');
foreach (var type in types)
{
if (string.IsNullOrEmpty(type) || type == SpecialNames.JavaLangAnyType || type == "extends" || type == "super") continue;
toBeAvoided |= TypeNameMustBeAvoided(type.Trim());
}
}
}
else
{
var clsName = typeName.Contains(SpecialNames.ArrayTypeTrailer) ? typeName.Substring(0, typeName.IndexOf(SpecialNames.ArrayTypeTrailer)) : typeName;
toBeAvoided = clsName.ClassTypeNameMustBeAvoided();
}
return toBeAvoided;
}
public static int JVMNestingLevels(this Java.Lang.Reflect.Type type)
{
var result = type.TypeName.Split(SpecialNames.NestedClassSeparator);
return result.Length - 1;
}
public static string JVMInterfaceName(this Java.Lang.Reflect.Type type, IList<KeyValuePair<string, string>> genClause, bool usedInGenerics, bool camel, out bool mustBeAvoided)
{
var tName = type.GetGenerics(null, genClause, string.Empty, true, usedInGenerics, camel, out mustBeAvoided);
string genName = null;
string nsName = null;
string cName = tName;
if (tName.Contains(SpecialNames.BeginGenericDeclaration))
{
genName = tName.Substring(tName.IndexOf(SpecialNames.BeginGenericDeclaration));
cName = tName.Substring(0, tName.IndexOf(SpecialNames.BeginGenericDeclaration));
}
if (cName.Contains(SpecialNames.NamespaceSeparator))
{
nsName = cName.Substring(0, cName.LastIndexOf(SpecialNames.NamespaceSeparator));
cName = cName.Substring(cName.LastIndexOf(SpecialNames.NamespaceSeparator) + 1);
}
cName = "I" + cName;
cName = genName != null ? cName + genName : cName;
return nsName != null ? nsName + SpecialNames.NamespaceSeparator + cName : cName;
}
public static bool IsJVMListenerClass(this Java.Lang.Reflect.Type type)
{
return type.TypeName.IsJVMListenerClass();
}
public static string Type(this Java.Lang.Reflect.Type type, Class clazz, IList<string> genArguments, IList<KeyValuePair<string, string>> genClauses, string prefix, bool usedInGenerics, bool camel)
{
var retString = type.GetGenerics(genArguments, genClauses, prefix, true, usedInGenerics, camel, out bool _);
if (clazz.IsJVMGenericClass() && genClauses != null)
{
List<string> classArguments = new List<string>();
List<KeyValuePair<string, string>> classClauses = new List<KeyValuePair<string, string>>();
clazz.GetGenerics(classArguments, classClauses, string.Empty, usedInGenerics, JNetReflectorCore.UseCamel);
int classNeedsConstraints = 0;
foreach (var classClause in classClauses)
{
if (genClauses != null)
{
foreach (var genClause in genClauses)
{
if (classClause.Key == genClause.Key && !string.IsNullOrWhiteSpace(classClause.Value))
{
classNeedsConstraints++;
}
}
}
else if (!string.IsNullOrWhiteSpace(classClause.Value))
{
classNeedsConstraints++;
}
}
if (classNeedsConstraints > 0) // the return class have some constraint
{
int genClausesConstraint = 0;
// check if genClauses has something in it near to the one expected from the class
foreach (var genClause in genClauses)
{
foreach (var classClause in classClauses)
{
if (classClause.Value != null && genClause.Value != null && classClause.Value == genClause.Value)
{
genClausesConstraint++; // found matched constraint
}
}
}
if (classNeedsConstraints != genClausesConstraint) // constraints does not match
{
genArguments.Clear();
genClauses.Clear();
return retString.Contains(SpecialNames.BeginGenericDeclaration) ? retString.Substring(0, retString.IndexOf(SpecialNames.BeginGenericDeclaration)) : retString;
}
}
}
return retString;
}
static bool IsGenerics(this Java.Lang.Reflect.Type entry)
{
if (entry.IsInstanceOf<TypeVariable>())
{
return true;
}
else if (entry.IsInstanceOf<ParameterizedType>())
{
return true;
}
else if (entry.IsInstanceOf<GenericArrayType>())
{
return true;
}
else if (entry.IsInstanceOf<WildcardType>())
{
return true;
}
return false;
}
static string ApplyGenerics(this Java.Lang.Reflect.Type entry, IList<string> genArguments, IList<KeyValuePair<string, string>> genClause, string prefix, bool reportNative, bool usedInGenerics, bool camel)
{
var retClass = entry.GetGenerics(genArguments, genClause, prefix, reportNative, usedInGenerics, camel, out bool _);
return retClass;
}
static string ApplyGenerics(this Java.Lang.Reflect.Type[] entries, string prefix, bool reportNative, bool usedInGenerics, bool camel)
{
List<string> genArguments = new List<string>();
List<KeyValuePair<string, string>> genClause = new List<KeyValuePair<string, string>>();
entries.GetGenerics(genArguments, genClause, prefix, reportNative, usedInGenerics, camel, out bool _);
var parameters = genArguments.ConvertGenerics();
if (!string.IsNullOrEmpty(parameters))
{
return $"<{parameters}>";
}
return string.Empty;
}
public static string ApplyGenerics(this IEnumerable<string> entry)
{
var parameters = entry.ConvertGenerics();
if (!string.IsNullOrEmpty(parameters))
{
return $"<{parameters}>";
}
return string.Empty;
}
static string ConvertGenerics(this IEnumerable<string> entry)
{
StringBuilder sb = new StringBuilder();
if (entry != null)
{
foreach (var item in entry)
{
sb.AppendFormat("{0}, ", item);
}
}
var parameters = sb.ToString();
if (!string.IsNullOrEmpty(parameters))
{
parameters = parameters.Substring(0, parameters.LastIndexOf(", "));
return parameters;
}
return string.Empty;
}
public static string ConvertClauses(this IEnumerable<KeyValuePair<string, string>> entries, bool isGeneric)
{
if (JNetReflectorCore.AvoidCSharpGenericClauseDefinition || !isGeneric) return string.Empty;
StringBuilder sbWhere = new StringBuilder();
foreach (var clause in entries)
{
if (!string.IsNullOrEmpty(clause.Value)
&& clause.Key != clause.Value) // this avoids circular clauses
{
sbWhere.AppendFormat(" where {0}: {1}", clause.Key, clause.Value);
}
}
var parameters = sbWhere.ToString();
return parameters;
}
static void GetGenerics(this Java.Lang.Reflect.Type[] entries, IList<string> genArguments, IList<KeyValuePair<string, string>> genClause, string prefix, bool reportNative, bool usedInGenerics, bool camel, out bool mustBeAvoided)
{
mustBeAvoided = false;
foreach (var entry in entries)
{
entry.GetGenerics(genArguments, genClause, prefix, reportNative, usedInGenerics, camel, out var localMustBeAvoided);
mustBeAvoided |= localMustBeAvoided;
}
}
static string GetBound(this Java.Lang.Reflect.Type bound, bool usedInGenerics, bool camel)
{
var bClass = bound.TypeName.JVMClass();
string result;
if (bClass != null && bClass.IsInterface())
{
result = bClass.JVMInterfaceName(new List<KeyValuePair<string, string>>(), usedInGenerics, true) + ", new()"; // the new constraint means the type shall be a class implementing the interface
}
else
{
result = ToNetType(bound.TypeName, false, camel);
}
return result;
}
static string GetGenerics(this Java.Lang.Reflect.Type entry, IList<string> genArguments, IList<KeyValuePair<string, string>> genClause, string prefix, bool reportNative, bool usedInGenerics, bool camel, out bool mustBeAvoided)
{
if (entry.IsInstanceOf<TypeVariable>())
{
return entry.CastTo<TypeVariable>().GetGenerics(genArguments, genClause, prefix, reportNative, usedInGenerics, camel, out mustBeAvoided);
}
else if (entry.IsInstanceOf<ParameterizedType>())
{
return entry.CastTo<ParameterizedType>().GetGenerics(genArguments, genClause, prefix, reportNative, usedInGenerics, camel, out mustBeAvoided);
}
else if (entry.IsInstanceOf<GenericArrayType>())
{
return entry.CastTo<GenericArrayType>().GetGenerics(genArguments, genClause, prefix, reportNative, usedInGenerics, camel, out mustBeAvoided);
}
else if (entry.IsInstanceOf<WildcardType>())
{
return entry.CastTo<WildcardType>().GetGenerics(genArguments, genClause, prefix, reportNative, usedInGenerics, camel, out mustBeAvoided);
}
mustBeAvoided = entry.TypeName.TypeNameMustBeAvoided();
string retVal = string.Empty;
if (reportNative)
{
if (IsVoid(entry.TypeName))
{
return SpecialNames.JavaLangVoid;
}
else
{
retVal = ToNetType(entry.TypeName, false, camel);
}
}
return retVal;
}
static string GetGenerics(this GenericArrayType entry, IList<string> genArguments, IList<KeyValuePair<string, string>> genClauses, string prefix, bool reportNative, bool usedInGenerics, bool camel, out bool mustBeAvoided)
{
List<string> genArgumentsLocal = new List<string>();
List<KeyValuePair<string, string>> genClauseLocal = new List<KeyValuePair<string, string>>();
var result = entry.GenericComponentType.GetGenerics(genArgumentsLocal, genClauseLocal, prefix, reportNative, usedInGenerics, camel, out mustBeAvoided);
foreach (var item in genArgumentsLocal)
{
if (genArguments != null && !genArguments.Contains(item))
{
genArguments?.Add(item);
}
}
foreach (var item in genClauseLocal)
{
if (genClauses != null)
{
bool hasKey = false;
foreach (var genClause in genClauses)
{
if (genClause.Key == item.Key) { hasKey = true; break; }
}
if (!hasKey) genClauses?.Add(item);
}
}
return result.EndsWith(SpecialNames.ArrayTypeTrailer) ? result : result + SpecialNames.ArrayTypeTrailer;
}
static string GetGenerics(this ParameterizedType entry, IList<string> genArguments, IList<KeyValuePair<string, string>> genClause, string prefix, bool reportNative, bool usedInGenerics, bool camel, out bool mustBeAvoided)
{
List<string> types = new List<string>();
List<string> genArgumentsLocal = new List<string>();
List<KeyValuePair<string, string>> genClauseLocal = new List<KeyValuePair<string, string>>();
bool constraintMismatch = false;
var cName = entry.TypeName;
cName = cName.Contains(SpecialNames.BeginGenericDeclaration) ? cName.Substring(0, cName.IndexOf(SpecialNames.BeginGenericDeclaration)) : cName;
var cEntry = cName.JVMClass();
mustBeAvoided = cEntry.MustBeAvoided();
for (int i = 0; i < entry.ActualTypeArguments.Length; i++)
{
var actualType = entry.ActualTypeArguments[i];
var actualTypeName = actualType.TypeName;
var expectedType = cEntry.TypeParameters[i];
var resType = actualType.GetGenerics(genArgumentsLocal, genClauseLocal, prefix, reportNative, usedInGenerics, camel, out var localMustBeAvoided);
mustBeAvoided |= localMustBeAvoided;
foreach (var bound in expectedType.Bounds)
{
string result = bound.GetBound(usedInGenerics, camel);
if (actualTypeName == SpecialNames.JavaLangAnyType && !(result == SpecialNames.NetObject || result == (SpecialNames.NetObject + SpecialNames.ArrayTypeTrailer)
|| result.Contains(SpecialNames.JavaLangAnyType))) // type used in Java to define any-type
{
constraintMismatch = true;
}
}
types.Add(resType);
}
var type = entry.ToNetType(camel);
if (constraintMismatch || entry.IsClassToAvoidInGenerics())
{
return type;
}
foreach (var item in genArgumentsLocal)
{
genArguments?.Add(item);
}
foreach (var item in genClauseLocal)
{
genClause?.Add(item);
}
return type.StartsWith(SpecialNames.JavaLangClass) ? type : entry.ToNetType(camel) + types?.ApplyGenerics();
}
static string GetGenerics(this TypeVariable entry, IList<string> genArguments, IList<KeyValuePair<string, string>> genClause, string prefix, bool reportNative, bool usedInGenerics, bool camel, out bool mustBeAvoided)
{
mustBeAvoided = false;
genArguments?.Add(entry.Name);
List<string> bounds = null;
/*** this piece of code crashes the JVM
if (entry.Bounds.Length != 0)
{
bounds = new List<string>();
foreach (var bound in entry.Bounds)
{
var result = bound.GetGenerics(null, null, null, true, camel);
if (!(result == "object" || result == "object[]")) bounds.Add(result);
}
}
so try to limit it to the not generic value
*****/
if (entry.Bounds.Length != 0)
{
bounds = new List<string>();
foreach (var bound in entry.Bounds)
{
string result = bound.GetBound(usedInGenerics, camel);
if (!(result == SpecialNames.NetObject || result == (SpecialNames.NetObject + SpecialNames.ArrayTypeTrailer)
|| result.Contains(SpecialNames.JavaLangAnyType))) // type used in Java to define any-type
{
bounds.Add(result);
}
}
}
genClause?.Add(new KeyValuePair<string, string>(entry.Name, bounds?.ConvertGenerics()));
return entry.Name;
}
static string GetGenerics(this WildcardType entry, IList<string> genArguments, IList<KeyValuePair<string, string>> genClause, string prefix, bool reportNative, bool usedInGenerics, bool camel, out bool mustBeAvoided)
{
mustBeAvoided = false;
string retVal = string.Empty;
if (entry.LowerBounds.Length == 0)
{
for (int i = 0; i < entry.UpperBounds.Length; i++)
{
List<string> innerGenArguments = new List<string>();
List<KeyValuePair<string, string>> innerGenClauses = new List<KeyValuePair<string, string>>();
var upper = GetGenerics(entry.UpperBounds[i], innerGenArguments, innerGenClauses, prefix, reportNative, usedInGenerics, camel, out var localMustBeAvoided);
mustBeAvoided |= localMustBeAvoided;
if (IsNetNativeType(upper))
{
retVal = upper;
}
else
{
upper = upper.EndsWith(SpecialNames.JavaLangAnyType) ? upper.Substring(0, upper.LastIndexOf(SpecialNames.JavaLangAnyType)) : upper;
var upperConverted = upper.Replace(SpecialNames.NamespaceSeparator, '_')
.Replace(", ", "_")
.Replace(",", "_")
.Replace(SpecialNames.BeginGenericDeclaration, "_")
.Replace(SpecialNames.EndGenericDeclaration, "_")
.Replace(SpecialNames.JavaLangAnyType, "_");
if (prefix == null)
{
retVal = upperConverted;
}
else
{
retVal = $"{prefix}Extends{upperConverted}";
}
genArguments?.Add(retVal);
genClause?.Add(new KeyValuePair<string, string>(retVal, IsNetNativeType(upper) ? null : upper));
if (entry.UpperBounds[i].IsInstanceOf<TypeVariable>())
{
if (genArguments != null && !genArguments.Contains(upper))
{
genArguments?.Add(upper);
}
if (genClause != null)
{
bool hasKey = false;
foreach (var item in genClause)
{
if (item.Key == upper) { hasKey = true; break; }
}
if (!hasKey) genClause?.Add(new KeyValuePair<string, string>(upper, null));
}
}
if (!JNetReflectorCore.AvoidCSharpGenericClauseDefinition)
{
foreach (var item in innerGenArguments)
{
if (item != upper && genArguments != null && !genArguments.Contains(item))
{
genArguments?.Add(item);
}
}
foreach (var item in innerGenClauses)
{
if (item.Key != upper && genClause != null && !genClause.ContainsClause(item))
{
genClause?.Add(item);
}
}
}
}
}
}
else if (entry.LowerBounds.Length != 0 && entry.LowerBounds.Length == entry.UpperBounds.Length)
{
for (int i = 0; i < entry.LowerBounds.Length; i++)
{
List<string> innerGenArguments = new List<string>();
List<KeyValuePair<string, string>> innerGenClauses = new List<KeyValuePair<string, string>>();
var upper = GetGenerics(entry.UpperBounds[i], null, null, prefix, reportNative, usedInGenerics, camel, out var upperMustBeAvoided);
mustBeAvoided |= upperMustBeAvoided;
upper = upper.EndsWith(SpecialNames.JavaLangAnyType) ? upper.Substring(0, upper.LastIndexOf(SpecialNames.JavaLangAnyType)) : upper;
var lower = GetGenerics(entry.LowerBounds[i], innerGenArguments, innerGenClauses, prefix, reportNative, usedInGenerics, camel, out var lowerMustBeAvoided);
mustBeAvoided |= lowerMustBeAvoided;
lower = lower.EndsWith(SpecialNames.JavaLangAnyType) ? lower.Substring(0, lower.LastIndexOf(SpecialNames.JavaLangAnyType)) : lower;
if (IsNetNativeType(lower))
{
retVal = lower;
}
else
{
var upperConverted = upper.Replace(SpecialNames.NamespaceSeparator, '_')
.Replace(", ", "_")
.Replace(",", "_")
.Replace(SpecialNames.BeginGenericDeclaration, "_")
.Replace(SpecialNames.EndGenericDeclaration, "_")
.Replace(SpecialNames.JavaLangAnyType, "_");
var lowerConverted = lower.Replace(SpecialNames.NamespaceSeparator, '_')
.Replace(", ", "_")
.Replace(",", "_")
.Replace(SpecialNames.BeginGenericDeclaration, "_")
.Replace(SpecialNames.EndGenericDeclaration, "_")
.Replace(SpecialNames.JavaLangAnyType, "_");
if (prefix == null)
{
retVal = lowerConverted;
}
else
{
retVal = $"{prefix}{upperConverted}Super{lowerConverted}";
}
genArguments?.Add(retVal);
genClause?.Add(new KeyValuePair<string, string>(retVal, IsNetNativeType(lower) ? null : lower));
if (entry.LowerBounds[i].IsInstanceOf<TypeVariable>())
{
if (genArguments != null && !genArguments.Contains(lower))
{
genArguments?.Add(lower);
}
if (genClause != null)
{
bool hasKey = false;
foreach (var item in genClause)
{
if (item.Key == lower) { hasKey = true; break; }
}
if (!hasKey) genClause?.Add(new KeyValuePair<string, string>(lower, null));
}
}
if (!JNetReflectorCore.AvoidCSharpGenericClauseDefinition)
{
foreach (var item in innerGenArguments)