forked from dotnet/java-interop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenericTypeParameter.cs
137 lines (111 loc) · 4.04 KB
/
GenericTypeParameter.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
using System;
using System.Collections.Generic;
namespace MonoDroid.Generation {
public class GenericTypeParameter : ISymbol {
string type;
string java_type;
string jni_type;
GenericParameterDefinition parm;
public GenericTypeParameter (GenericParameterDefinition parm)
{
if (parm == null)
throw new ArgumentNullException ("parm");
this.parm = parm;
#if false // FIXME: we want to enable generic type constraints to get valid JNI output. So far we "remove" any method that involves generic parameter constraints.
type = parm.Constraints != null ? parm.Constraints [0].FullName : "Java.Lang.Object";
java_type = parm != null && parm.Constraints != null && parm.Constraints [0] != null ? parm.Constraints [0].JavaName : "java.lang.Object";
#else
type = "Java.Lang.Object";
java_type = parm != null && parm.Constraints != null && parm.Constraints [0] != null ? parm.Constraints [0].JavaName : "java.lang.Object";
#endif
// some unknown types could result in "null" constraints items. So far we treat this as JLO.
jni_type = parm.Constraints != null && parm.Constraints [0] != null ? parm.Constraints [0].JniName : "Ljava/lang/Object;";
}
public GenericParameterDefinition Definition {
get { return parm; }
}
public string DefaultValue {
get { return "IntPtr.Zero"; }
}
public string FullName {
get { return type; }
}
public string JavaName {
get { return java_type; }
}
public string JniName {
get { return jni_type; }
}
public string NativeType {
get { return "IntPtr"; }
}
public bool IsEnum {
get { return false; }
}
public bool IsArray {
get { return false; }
}
public string ElementType {
get { return null; }
}
public string GetObjectHandleProperty (string variable)
{
return null;
}
public string FromNative (CodeGenerationOptions opt, string varname, bool owned)
{
return String.Format ("({0}) global::Java.Lang.Object.GetObject<{3}> ({1}, {2})", type, varname, owned ? "JniHandleOwnership.TransferLocalRef" : "JniHandleOwnership.DoNotTransfer", opt.GetOutputName (FullName));
}
public string GetGenericType (Dictionary<string, string> mappings)
{
var mapped = mappings != null && mappings.ContainsKey (parm.Name) ? mappings [parm.Name] : null;
return mapped ?? parm.Name;
}
public string ToNative (CodeGenerationOptions opt, string varname, Dictionary<string, string> mappings = null)
{
var mapped = mappings != null && mappings.ContainsKey (parm.Name) ? mappings [parm.Name] : null;
string targetType = opt.GetOutputName (mappings == null ? parm.Name : mapped);
if (targetType == "string")
return string.Format ("new global::Java.Lang.String ({0})", varname);
return varname;
}
public bool Validate (CodeGenerationOptions opt, GenericParameterDefinitionList type_params)
{
return true;
}
public string[] PreCallback (CodeGenerationOptions opt, string var_name, bool owned)
{
return new string[]{
string.Format ("{0} {1} = global::Java.Lang.Object.GetObject<{0}> ({2}, {3});",
opt.GetOutputName (FullName),
var_name,
opt.GetSafeIdentifier (SymbolTable.GetNativeName (var_name)),
owned ? "JniHandleOwnership.TransferLocalRef" : "JniHandleOwnership.DoNotTransfer"),
};
}
public string[] PostCallback (CodeGenerationOptions opt, string var_name)
{
return new string[]{
};
}
public string[] PreCall (CodeGenerationOptions opt, string var_name)
{
return new string[] {
string.Format ("IntPtr {0} = JNIEnv.ToLocalJniHandle ({1});",
opt.GetSafeIdentifier (SymbolTable.GetNativeName (var_name)), opt.GetSafeIdentifier (var_name)),
};
}
public string Call (CodeGenerationOptions opt, string var_name)
{
return opt.GetSafeIdentifier (SymbolTable.GetNativeName (var_name));
}
public string[] PostCall (CodeGenerationOptions opt, string var_name)
{
return new string[]{
string.Format ("JNIEnv.DeleteLocalRef ({0});",
opt.GetSafeIdentifier (SymbolTable.GetNativeName (var_name))),
};
}
public bool NeedsPrep { get { return true; } }
}
}