Drop fromJava impl and unused std impl
This commit is contained in:
1 parent
2fac1c8e4c
commit
8e08a4881f
7 files changed
+1
-423
No files matched your search
@@ -1 +0,0 @@
|
||||
mod std;
|
||||
@@ -1,212 +0,0 @@
|
||||
mod net;
|
||||
|
||||
use crate::{FromJava, JnixEnv};
|
||||
use jni::signature::ReturnType;
|
||||
use jni::{
|
||||
objects::{AutoLocal, JObject, JString, JValue},
|
||||
signature::Primitive,
|
||||
sys::{jboolean, jint, JNI_FALSE},
|
||||
};
|
||||
|
||||
impl<'env, 'sub_env, T> FromJava<'env, JValue<'sub_env>> for T
|
||||
where
|
||||
'env: 'sub_env,
|
||||
T: FromJava<'env, JObject<'sub_env>>,
|
||||
{
|
||||
const JNI_SIGNATURE: &'static str = T::JNI_SIGNATURE;
|
||||
|
||||
fn from_java(env: &JnixEnv<'env>, source: JValue<'sub_env>) -> Self {
|
||||
match source {
|
||||
JValue::Object(object) => T::from_java(env, object),
|
||||
_ => panic!(
|
||||
"Can't convert non-object Java type. Expected type signature {}",
|
||||
Self::JNI_SIGNATURE
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'env, 'sub_env, 'borrow, T> FromJava<'env, AutoLocal<'sub_env, 'borrow>> for T
|
||||
where
|
||||
'env: 'sub_env,
|
||||
'sub_env: 'borrow,
|
||||
T: for<'inner_borrow> FromJava<'env, JObject<'inner_borrow>>,
|
||||
{
|
||||
const JNI_SIGNATURE: &'static str = T::JNI_SIGNATURE;
|
||||
|
||||
fn from_java(env: &JnixEnv<'env>, source: AutoLocal<'sub_env, 'borrow>) -> Self {
|
||||
T::from_java(env, source.as_obj())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'env> FromJava<'env, jboolean> for bool {
|
||||
const JNI_SIGNATURE: &'static str = "Z";
|
||||
|
||||
fn from_java(_: &JnixEnv<'env>, source: jboolean) -> Self {
|
||||
source != JNI_FALSE
|
||||
}
|
||||
}
|
||||
|
||||
impl<'env, 'sub_env> FromJava<'env, JValue<'sub_env>> for bool
|
||||
where
|
||||
'env: 'sub_env,
|
||||
{
|
||||
const JNI_SIGNATURE: &'static str = "Z";
|
||||
|
||||
fn from_java(env: &JnixEnv<'env>, source: JValue<'sub_env>) -> Self {
|
||||
match source {
|
||||
JValue::Bool(boolean) => bool::from_java(env, boolean),
|
||||
_ => panic!("Can't convert Java type, expected a boolean primitive"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'env> FromJava<'env, jint> for i32 {
|
||||
const JNI_SIGNATURE: &'static str = "I";
|
||||
|
||||
fn from_java(_: &JnixEnv<'env>, source: jint) -> Self {
|
||||
source as i32
|
||||
}
|
||||
}
|
||||
|
||||
impl<'env, 'sub_env> FromJava<'env, JValue<'sub_env>> for i32
|
||||
where
|
||||
'env: 'sub_env,
|
||||
{
|
||||
const JNI_SIGNATURE: &'static str = "I";
|
||||
|
||||
fn from_java(env: &JnixEnv<'env>, source: JValue<'sub_env>) -> Self {
|
||||
match source {
|
||||
JValue::Int(integer) => i32::from_java(env, integer),
|
||||
_ => panic!("Can't convert Java type, expected an integer primitive"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'env, 'sub_env> FromJava<'env, JString<'sub_env>> for String
|
||||
where
|
||||
'env: 'sub_env,
|
||||
{
|
||||
const JNI_SIGNATURE: &'static str = "Ljava/lang/String;";
|
||||
|
||||
fn from_java(env: &JnixEnv<'env>, source: JString<'sub_env>) -> Self {
|
||||
String::from(
|
||||
env.get_string(source)
|
||||
.expect("Failed to convert from Java String"),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'env, 'sub_env> FromJava<'env, JObject<'sub_env>> for String
|
||||
where
|
||||
'env: 'sub_env,
|
||||
{
|
||||
const JNI_SIGNATURE: &'static str = "Ljava/lang/String;";
|
||||
|
||||
fn from_java(env: &JnixEnv<'env>, source: JObject<'sub_env>) -> Self {
|
||||
String::from_java(env, JString::from(source))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'env, 'sub_env, T> FromJava<'env, JObject<'sub_env>> for Option<T>
|
||||
where
|
||||
'env: 'sub_env,
|
||||
T: FromJava<'env, JObject<'sub_env>>,
|
||||
{
|
||||
const JNI_SIGNATURE: &'static str = T::JNI_SIGNATURE;
|
||||
|
||||
fn from_java(env: &JnixEnv<'env>, source: JObject<'sub_env>) -> Self {
|
||||
if source.is_null() {
|
||||
None
|
||||
} else {
|
||||
Some(T::from_java(env, source))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'env, 'sub_env, T> FromJava<'env, JString<'sub_env>> for Option<T>
|
||||
where
|
||||
'env: 'sub_env,
|
||||
T: FromJava<'env, JString<'sub_env>>,
|
||||
{
|
||||
const JNI_SIGNATURE: &'static str = T::JNI_SIGNATURE;
|
||||
|
||||
fn from_java(env: &JnixEnv<'env>, source: JString<'sub_env>) -> Self {
|
||||
if source.is_null() {
|
||||
None
|
||||
} else {
|
||||
Some(T::from_java(env, source))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'env, 'sub_env> FromJava<'env, JObject<'sub_env>> for Option<i32> {
|
||||
const JNI_SIGNATURE: &'static str = "Ljava/lang/Integer;";
|
||||
|
||||
fn from_java(env: &JnixEnv<'env>, source: JObject<'sub_env>) -> Self {
|
||||
if source.is_null() {
|
||||
None
|
||||
} else {
|
||||
let class = env.get_class("java/lang/Integer");
|
||||
let method_id = env
|
||||
.get_method_id(&class, "intValue", "()I")
|
||||
.expect("Failed to get method ID for Integer.intValue()");
|
||||
let return_type = ReturnType::Primitive(Primitive::Int);
|
||||
|
||||
let int_value = env
|
||||
.call_method_unchecked(source, method_id, return_type, &[])
|
||||
.expect("Failed to call Integer.intValue()")
|
||||
.i()
|
||||
.expect("Call to Integer.intValue() did not return an int primitive");
|
||||
|
||||
Some(int_value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'env, 'sub_env, T> FromJava<'env, JObject<'sub_env>> for Vec<T>
|
||||
where
|
||||
'env: 'sub_env,
|
||||
T: FromJava<'env, JObject<'sub_env>>,
|
||||
{
|
||||
const JNI_SIGNATURE: &'static str = "Ljava/util/ArrayList;";
|
||||
|
||||
fn from_java(env: &JnixEnv<'env>, source: JObject<'sub_env>) -> Self {
|
||||
let class = env.get_class("java/util/ArrayList");
|
||||
let size_method_id = env
|
||||
.get_method_id(&class, "size", "()I")
|
||||
.expect("Failed to get method ID for ArrayList.size()");
|
||||
let size_return_type = ReturnType::Primitive(Primitive::Int);
|
||||
|
||||
let item_count = env
|
||||
.call_method_unchecked(source, size_method_id, size_return_type, &[])
|
||||
.expect("Failed to call ArrayList.size()")
|
||||
.i()
|
||||
.expect("Call to ArrayList.size() did not return an int primitive");
|
||||
|
||||
let mut target = Vec::with_capacity(item_count as usize);
|
||||
|
||||
let get_method_id = env
|
||||
.get_method_id(&class, "get", "(I)Ljava/lang/Object;")
|
||||
.expect("Failed to get method ID for ArrayList.get()");
|
||||
let get_return_type = ReturnType::Object;
|
||||
|
||||
for index in 0..item_count {
|
||||
let object = env
|
||||
.call_method_unchecked(
|
||||
source,
|
||||
get_method_id,
|
||||
get_return_type.clone(),
|
||||
&[JValue::from(index).to_jni()],
|
||||
)
|
||||
.expect("Failed to call ArrayList.get()")
|
||||
.l()
|
||||
.expect("Call to ArrayList.get() did not return an object");
|
||||
let item = T::from_java(env, object);
|
||||
|
||||
target.push(item);
|
||||
}
|
||||
|
||||
target
|
||||
}
|
||||
}
|
||||
@@ -1,89 +0,0 @@
|
||||
use crate::{FromJava, JnixEnv};
|
||||
use jni::objects::JObject;
|
||||
use jni::signature::ReturnType;
|
||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
|
||||
|
||||
fn read_inet_address_octets<'env, 'o>(env: &JnixEnv<'env>, source: JObject<'o>) -> JObject<'env>
|
||||
where
|
||||
'o: 'env,
|
||||
{
|
||||
let class = env.get_class("java/net/InetAddress");
|
||||
let method_id = env
|
||||
.get_method_id(&class, "getAddress", "()[B")
|
||||
.expect("Failed to get method ID for InetAddress.getAddress()");
|
||||
let return_type = ReturnType::Array;
|
||||
|
||||
env.call_method_unchecked(source, method_id, return_type, &[])
|
||||
.expect("Failed to call InetAddress.getAddress()")
|
||||
.l()
|
||||
.expect("Call to InetAddress.getAddress() did not return an object")
|
||||
}
|
||||
|
||||
fn address_from_octets<'env, 'o, A, B>(env: &JnixEnv<'env>, octets: JObject<'o>, mut buffer: B) -> A
|
||||
where
|
||||
'o: 'env,
|
||||
A: From<B>,
|
||||
B: AsMut<[u8]>,
|
||||
{
|
||||
let mut signed_octets = [0i8; 16];
|
||||
let buffer_octets = buffer.as_mut();
|
||||
let buffer_size = buffer_octets.len();
|
||||
|
||||
env.get_byte_array_region(octets.into_raw(), 0, &mut signed_octets[..buffer_size])
|
||||
.expect("Failed to read octets returned by InetAddress.getAddress()");
|
||||
|
||||
for index in 0..buffer_size {
|
||||
buffer_octets[index] = signed_octets[index] as u8;
|
||||
}
|
||||
|
||||
A::from(buffer)
|
||||
}
|
||||
|
||||
impl<'env, 'sub_env> FromJava<'env, JObject<'sub_env>> for Ipv4Addr
|
||||
where
|
||||
'env: 'sub_env,
|
||||
{
|
||||
const JNI_SIGNATURE: &'static str = "Ljava/net/Inet4Address;";
|
||||
|
||||
fn from_java(env: &JnixEnv<'env>, source: JObject<'sub_env>) -> Self {
|
||||
let octets = read_inet_address_octets(env, source);
|
||||
|
||||
address_from_octets(env, octets, [0u8; 4])
|
||||
}
|
||||
}
|
||||
|
||||
impl<'env, 'sub_env> FromJava<'env, JObject<'sub_env>> for Ipv6Addr
|
||||
where
|
||||
'env: 'sub_env,
|
||||
{
|
||||
const JNI_SIGNATURE: &'static str = "Ljava/net/Inet6Address;";
|
||||
|
||||
fn from_java(env: &JnixEnv<'env>, source: JObject<'sub_env>) -> Self {
|
||||
let octets = read_inet_address_octets(env, source);
|
||||
|
||||
address_from_octets(env, octets, [0u8; 16])
|
||||
}
|
||||
}
|
||||
|
||||
impl<'env, 'sub_env> FromJava<'env, JObject<'sub_env>> for IpAddr
|
||||
where
|
||||
'env: 'sub_env,
|
||||
{
|
||||
const JNI_SIGNATURE: &'static str = "Ljava/net/InetAddress;";
|
||||
|
||||
fn from_java(env: &JnixEnv<'env>, source: JObject<'sub_env>) -> Self {
|
||||
let octets = read_inet_address_octets(env, source);
|
||||
let octet_count = env
|
||||
.get_array_length(octets.into_raw())
|
||||
.expect("Failed to get length of byte array returned by InetAddress.getAddress()");
|
||||
|
||||
match octet_count {
|
||||
4 => address_from_octets(env, octets, [0u8; 4]),
|
||||
16 => address_from_octets(env, octets, [0u8; 16]),
|
||||
count => panic!(
|
||||
"Invalid number of octets returned by InetAddress.getAddress(): {}",
|
||||
count
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
mod implementations;
|
||||
|
||||
use crate::JnixEnv;
|
||||
|
||||
/// Conversion from a Java type into its Rust equivalent.
|
||||
///
|
||||
/// A type that implements this trait has an equivalent Java representation.
|
||||
pub trait FromJava<'env, JavaType> {
|
||||
/// The [JNI signature] of the Java type.
|
||||
///
|
||||
/// [JNI signature]: https://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/types.html#wp16432
|
||||
const JNI_SIGNATURE: &'static str;
|
||||
|
||||
/// Performs the conversion.
|
||||
fn from_java(env: &JnixEnv<'env>, source: JavaType) -> Self;
|
||||
}
|
||||
@@ -1,5 +1,3 @@
|
||||
mod net;
|
||||
|
||||
use crate::{IntoJava, JnixEnv};
|
||||
use jni::{
|
||||
objects::{AutoLocal, JList, JObject, JValue},
|
||||
|
||||
@@ -1,101 +0,0 @@
|
||||
use crate::{IntoJava, JnixEnv};
|
||||
use jni::signature::ReturnType;
|
||||
use jni::{
|
||||
objects::{AutoLocal, JObject, JValue},
|
||||
sys::jint,
|
||||
};
|
||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
|
||||
|
||||
fn ipvx_addr_into_java<'borrow, 'env: 'borrow>(
|
||||
original_octets: &[u8],
|
||||
env: &'borrow JnixEnv<'env>,
|
||||
) -> AutoLocal<'env, 'borrow> {
|
||||
let class = env.get_class("java/net/InetAddress");
|
||||
let constructor = env
|
||||
.get_static_method_id(&class, "getByAddress", "([B)Ljava/net/InetAddress;")
|
||||
.expect("Failed to get InetAddress.getByAddress method ID");
|
||||
|
||||
let octets_array = env
|
||||
.new_byte_array(original_octets.len() as i32)
|
||||
.expect("Failed to create byte array to store IP address");
|
||||
|
||||
let octet_data: Vec<i8> = original_octets
|
||||
.into_iter()
|
||||
.map(|octet| *octet as i8)
|
||||
.collect();
|
||||
|
||||
env.set_byte_array_region(octets_array, 0, &octet_data)
|
||||
.expect("Failed to copy IP address octets to byte array");
|
||||
|
||||
let octets = env.auto_local(unsafe { JObject::from_raw(octets_array) });
|
||||
let result = env
|
||||
.call_static_method_unchecked(
|
||||
&class,
|
||||
constructor,
|
||||
ReturnType::Object,
|
||||
&[JValue::from(&octets).to_jni()],
|
||||
)
|
||||
.expect("Failed to create InetAddress Java object");
|
||||
|
||||
match result {
|
||||
JValue::Object(object) => env.auto_local(object),
|
||||
value => {
|
||||
panic!(
|
||||
"InetAddress.getByAddress returned an invalid value: {:?}",
|
||||
value
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'borrow, 'env: 'borrow> IntoJava<'borrow, 'env> for Ipv4Addr {
|
||||
const JNI_SIGNATURE: &'static str = "Ljava/net/InetAddress;";
|
||||
|
||||
type JavaType = AutoLocal<'env, 'borrow>;
|
||||
|
||||
fn into_java(self, env: &'borrow JnixEnv<'env>) -> Self::JavaType {
|
||||
ipvx_addr_into_java(self.octets().as_ref(), env)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'borrow, 'env: 'borrow> IntoJava<'borrow, 'env> for Ipv6Addr {
|
||||
const JNI_SIGNATURE: &'static str = "Ljava/net/InetAddress;";
|
||||
|
||||
type JavaType = AutoLocal<'env, 'borrow>;
|
||||
|
||||
fn into_java(self, env: &'borrow JnixEnv<'env>) -> Self::JavaType {
|
||||
ipvx_addr_into_java(self.octets().as_ref(), env)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'borrow, 'env: 'borrow> IntoJava<'borrow, 'env> for IpAddr {
|
||||
const JNI_SIGNATURE: &'static str = "Ljava/net/InetAddress;";
|
||||
|
||||
type JavaType = AutoLocal<'env, 'borrow>;
|
||||
|
||||
fn into_java(self, env: &'borrow JnixEnv<'env>) -> Self::JavaType {
|
||||
match self {
|
||||
IpAddr::V4(address) => address.into_java(env),
|
||||
IpAddr::V6(address) => address.into_java(env),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'borrow, 'env: 'borrow> IntoJava<'borrow, 'env> for SocketAddr {
|
||||
const JNI_SIGNATURE: &'static str = "Ljava/net/InetSocketAddress;";
|
||||
|
||||
type JavaType = AutoLocal<'env, 'borrow>;
|
||||
|
||||
fn into_java(self, env: &'borrow JnixEnv<'env>) -> Self::JavaType {
|
||||
let ip_address = self.ip().into_java(env);
|
||||
let port = self.port() as jint;
|
||||
let parameters = [JValue::Object(ip_address.as_obj()), JValue::Int(port)];
|
||||
|
||||
let class = env.get_class("java/net/InetSocketAddress");
|
||||
let object = env
|
||||
.new_object(&class, "(Ljava/net/InetAddress;I)V", ¶meters)
|
||||
.expect("Failed to convert SocketAddr Rust type into InetSocketAddress Java object");
|
||||
|
||||
env.auto_local(object)
|
||||
}
|
||||
}
|
||||
@@ -86,10 +86,9 @@
|
||||
pub extern crate jni;
|
||||
|
||||
mod as_jvalue;
|
||||
mod from_java;
|
||||
mod into_java;
|
||||
mod jnix_env;
|
||||
|
||||
pub use self::{as_jvalue::AsJValue, from_java::FromJava, into_java::IntoJava, jnix_env::JnixEnv};
|
||||
pub use self::{as_jvalue::AsJValue, into_java::IntoJava, jnix_env::JnixEnv};
|
||||
#[cfg(feature = "derive")]
|
||||
pub use jnix_macros::{FromJava, IntoJava};
|
||||
Reference in new issue
Block a user