Skip to content

Commit 084a8f4

Browse files
committed
[Java.Interop, generator] Fix Byte naming, support.
A funny thing happened while writing 959e14c: I saw that JniBuiltinMarshalers.tt wasn't doing anything for java.lang.Byte! (Quite an oversight! :-( So I add an entry for "Byte", and it promptly breaks because of a naming inconsistency: for most JNI marshal methods, I've been following .NET convention -- CallInt64Method(), not CallLongMethod(). I failed to do so with respect to "byte", calling it Byte instead of SByte (the Java `byte` type is signed, not unsigned). (Interestingly I properly named JavaSByteArray. It's the JNI Call*Method() names which are off!) Add SByte marshaling to JniBuiltinMarshalers.tt so that values of type java.lang.Byte and 'B' can be properly auto-marshaled. Fixup generator to emit CallSByteMethod() methods instead of CallByteMethod() methods. Fixup all Call*ByteMethod() uses to be Call*SByteMethod() in the public API, promoting consistency (and allowing the new SByte auto-marshaler to actually compile).
1 parent 959e14c commit 084a8f4

File tree

8 files changed

+140
-103
lines changed

8 files changed

+140
-103
lines changed

src/Java.Interop/Java.Interop/JniBuiltinMarshalers.cs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ partial class JavaVM {
1212
new KeyValuePair<Type, JniTypeInfo>(typeof (void), new JniTypeInfo ("V", typeIsKeyword: true)),
1313
new KeyValuePair<Type, JniTypeInfo>(typeof (void), new JniTypeInfo ("java/lang/Void")),
1414

15-
new KeyValuePair<Type, JniTypeInfo>(typeof (sbyte), new JniTypeInfo ("B", true)),
1615
new KeyValuePair<Type, JniTypeInfo>(typeof (Boolean), new JniTypeInfo ("Z", typeIsKeyword: true)),
1716
new KeyValuePair<Type, JniTypeInfo>(typeof (Boolean), new JniTypeInfo ("java/lang/Boolean")),
17+
new KeyValuePair<Type, JniTypeInfo>(typeof (SByte), new JniTypeInfo ("B", typeIsKeyword: true)),
18+
new KeyValuePair<Type, JniTypeInfo>(typeof (SByte), new JniTypeInfo ("java/lang/Byte")),
1819
new KeyValuePair<Type, JniTypeInfo>(typeof (Char), new JniTypeInfo ("C", typeIsKeyword: true)),
1920
new KeyValuePair<Type, JniTypeInfo>(typeof (Char), new JniTypeInfo ("java/lang/Character")),
2021
new KeyValuePair<Type, JniTypeInfo>(typeof (Int16), new JniTypeInfo ("S", typeIsKeyword: true)),
@@ -39,6 +40,11 @@ partial class JavaVM {
3940
GetValueFromJni = JniBoolean.GetValueFromJni,
4041
CreateLocalRef = JniBoolean.CreateLocalRef,
4142
}),
43+
new KeyValuePair<Type, JniMarshalInfo>(typeof (SByte), new JniMarshalInfo {
44+
CreateJValue = JniByte.CreateJValue,
45+
GetValueFromJni = JniByte.GetValueFromJni,
46+
CreateLocalRef = JniByte.CreateLocalRef,
47+
}),
4248
new KeyValuePair<Type, JniMarshalInfo>(typeof (Char), new JniMarshalInfo {
4349
CreateJValue = JniCharacter.CreateJValue,
4450
GetValueFromJni = JniCharacter.GetValueFromJni,
@@ -106,6 +112,40 @@ internal static object GetValueFromJni (JniReferenceSafeHandle self, JniHandleOw
106112
}
107113
}
108114

115+
static class JniByte {
116+
internal const string JniTypeName = "java/lang/Byte";
117+
118+
static JniType _TypeRef;
119+
static JniType TypeRef {
120+
get {return JniType.GetCachedJniType (ref _TypeRef, JniTypeName);}
121+
}
122+
123+
internal static JValue CreateJValue (object value)
124+
{
125+
return new JValue ((SByte) value);
126+
}
127+
128+
static JniInstanceMethodID init;
129+
internal static JniLocalReference CreateLocalRef (object value)
130+
{
131+
Debug.Assert (value is SByte, "Expected value of type `SByte`; was: " + (value == null ? "<null>" : value.GetType ().FullName));
132+
TypeRef.GetCachedConstructor (ref init, "(B)V");
133+
return TypeRef.NewObject (init, new JValue ((SByte) value));
134+
}
135+
136+
static JniInstanceMethodID byteValue;
137+
internal static object GetValueFromJni (JniReferenceSafeHandle self, JniHandleOwnership transfer, Type targetType)
138+
{
139+
Debug.Assert (targetType == null || targetType == typeof (SByte), "Expected targetType==typeof(SByte); was: " + targetType);
140+
TypeRef.GetCachedInstanceMethod (ref byteValue, "byteValue", "()B");
141+
try {
142+
return byteValue.CallVirtualSByteMethod (self);
143+
} finally {
144+
JniEnvironment.Handles.Dispose (self, transfer);
145+
}
146+
}
147+
}
148+
109149
static class JniCharacter {
110150
internal const string JniTypeName = "java/lang/Character";
111151

src/Java.Interop/Java.Interop/JniBuiltinMarshalers.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace Java.Interop {
1111
<#
1212
var types = new[]{
1313
new { Name = "Boolean", Type = "Boolean", JniType = "Z", GetValue = "booleanValue" },
14+
new { Name = "Byte", Type = "SByte", JniType = "B", GetValue = "byteValue" },
1415
new { Name = "Character", Type = "Char", JniType = "C", GetValue = "charValue" },
1516
new { Name = "Short", Type = "Int16", JniType = "S", GetValue = "shortValue" },
1617
new { Name = "Integer", Type = "Int32", JniType = "I", GetValue = "intValue" },
@@ -28,7 +29,6 @@ namespace Java.Interop {
2829
new KeyValuePair<Type, JniTypeInfo>(typeof (void), new JniTypeInfo ("V", typeIsKeyword: true)),
2930
new KeyValuePair<Type, JniTypeInfo>(typeof (void), new JniTypeInfo ("java/lang/Void")),
3031

31-
new KeyValuePair<Type, JniTypeInfo>(typeof (sbyte), new JniTypeInfo ("B", true)),
3232
<#
3333
foreach (var type in types) {
3434
#>

src/Java.Interop/Java.Interop/JniInstanceMethodID.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ public bool CallVirtualBooleanMethod (JniReferenceSafeHandle @this, params JValu
3939
return JniEnvironment.Members.CallBooleanMethod (@this, this, parameters);
4040
}
4141

42-
public sbyte CallVirtualByteMethod (JniReferenceSafeHandle @this)
42+
public sbyte CallVirtualSByteMethod (JniReferenceSafeHandle @this)
4343
{
44-
return JniEnvironment.Members.CallByteMethod (@this, this);
44+
return JniEnvironment.Members.CallSByteMethod (@this, this);
4545
}
4646

47-
public sbyte CallVirtualByteMethod (JniReferenceSafeHandle @this, params JValue[] parameters)
47+
public sbyte CallVirtualSByteMethod (JniReferenceSafeHandle @this, params JValue[] parameters)
4848
{
49-
return JniEnvironment.Members.CallByteMethod (@this, this, parameters);
49+
return JniEnvironment.Members.CallSByteMethod (@this, this, parameters);
5050
}
5151

5252
public char CallVirtualCharMethod (JniReferenceSafeHandle @this)
@@ -139,14 +139,14 @@ public bool CallNonvirtualBooleanMethod (JniReferenceSafeHandle @this, JniRefere
139139
return JniEnvironment.Members.CallNonvirtualBooleanMethod (@this, declaringType, this, parameters);
140140
}
141141

142-
public sbyte CallNonvirtualByteMethod (JniReferenceSafeHandle @this, JniReferenceSafeHandle declaringType)
142+
public sbyte CallNonvirtualSByteMethod (JniReferenceSafeHandle @this, JniReferenceSafeHandle declaringType)
143143
{
144-
return JniEnvironment.Members.CallNonvirtualByteMethod (@this, declaringType, this);
144+
return JniEnvironment.Members.CallNonvirtualSByteMethod (@this, declaringType, this);
145145
}
146146

147-
public sbyte CallNonvirtualByteMethod (JniReferenceSafeHandle @this, JniReferenceSafeHandle declaringType, params JValue[] parameters)
147+
public sbyte CallNonvirtualSByteMethod (JniReferenceSafeHandle @this, JniReferenceSafeHandle declaringType, params JValue[] parameters)
148148
{
149-
return JniEnvironment.Members.CallNonvirtualByteMethod (@this, declaringType, this, parameters);
149+
return JniEnvironment.Members.CallNonvirtualSByteMethod (@this, declaringType, this, parameters);
150150
}
151151

152152
public char CallNonvirtualCharMethod (JniReferenceSafeHandle @this, JniReferenceSafeHandle declaringType)

0 commit comments

Comments
 (0)