Rename Gradle targets Android (#2226)

This commit is contained in:
Bart Louwers
2024-04-02 23:15:50 +02:00
committed by GitHub
parent 37170ed61c
commit 9f8ae7a98b
1081 changed files with 132 additions and 370 deletions

View File

@@ -0,0 +1,50 @@
#include "json_array.hpp"
#include "json_element.hpp"
namespace mbgl {
namespace android {
namespace gson {
jni::Local<jni::Object<JsonArray>> JsonArray::New(jni::JNIEnv& env, const std::vector<mbgl::Value>& values) {
static auto& javaClass = jni::Class<JsonArray>::Singleton(env);
static auto constructor = javaClass.GetConstructor(env);
static auto addMethod = javaClass.GetMethod<void(jni::Object<JsonElement>)>(env, "add");
auto jsonArray = javaClass.New(env, constructor);
for (const auto& v : values) {
jsonArray.Call(env, addMethod, JsonElement::New(env, v));
}
return jsonArray;
}
std::vector<mbgl::Value> JsonArray::convert(jni::JNIEnv& env, const jni::Object<JsonArray>& jsonArray) {
std::vector<mbgl::Value> values;
if (jsonArray) {
static auto& javaClass = jni::Class<JsonArray>::Singleton(env);
static auto getMethod = javaClass.GetMethod<jni::Object<JsonElement>(jni::jint)>(env, "get");
static auto sizeMethod = javaClass.GetMethod<jni::jint()>(env, "size");
int size = jsonArray.Call(env, sizeMethod);
values.reserve(uint(size));
for (int i = 0; i < size; i++) {
auto entry = jsonArray.Call(env, getMethod, i);
if (entry) {
values.push_back(JsonElement::convert(env, entry));
}
}
}
return values;
}
void JsonArray::registerNative(jni::JNIEnv& env) {
jni::Class<JsonArray>::Singleton(env);
}
} // namespace gson
} // namespace android
} // namespace mbgl

View File

@@ -0,0 +1,24 @@
#pragma once
#include "json_element.hpp"
#include <jni/jni.hpp>
namespace mbgl {
namespace android {
namespace gson {
class JsonArray {
public:
using SuperTag = JsonElement;
static constexpr auto Name() { return "com/google/gson/JsonArray"; };
static jni::Local<jni::Object<JsonArray>> New(jni::JNIEnv&, const std::vector<mbgl::Value>&);
static std::vector<mbgl::Value> convert(JNIEnv&, const jni::Object<JsonArray>&);
static void registerNative(jni::JNIEnv&);
};
} // namespace gson
} // namespace android
} // namespace mbgl

View File

@@ -0,0 +1,77 @@
#include "json_element.hpp"
#include "json_array.hpp"
#include "json_object.hpp"
#include "json_primitive.hpp"
namespace mbgl {
namespace android {
namespace gson {
jni::Local<jni::Object<JsonElement>> JsonElement::New(jni::JNIEnv& env, const mbgl::Value& value) {
static auto& primitive = jni::Class<JsonPrimitive>::Singleton(env);
static auto stringConstructor = primitive.GetConstructor<jni::String>(env);
static auto numberConstructor = primitive.GetConstructor<jni::Number>(env);
static auto booleanConstructor = primitive.GetConstructor<jni::Boolean>(env);
return value.match(
[&](const mbgl::NullValue&) { return jni::Local<jni::Object<JsonElement>>(); },
[&](const std::string& value) {
return primitive.New(env, stringConstructor, jni::Make<jni::String>(env, value));
},
[&](const double value) { return primitive.New(env, numberConstructor, jni::Box(env, value)); },
[&](const int64_t value) { return primitive.New(env, numberConstructor, jni::Box(env, value)); },
[&](const uint64_t value) {
return primitive.New(env, numberConstructor, jni::Box(env, int64_t(value))); // TODO: should use BigInteger
},
[&](const bool value) {
return primitive.New(env, booleanConstructor, jni::Box(env, value ? jni::jni_true : jni::jni_false));
},
[&](const std::vector<mbgl::Value>& values) { return JsonArray::New(env, values); },
[&](const mbgl::PropertyMap& values) { return JsonObject::New(env, values); });
}
mbgl::Value JsonElement::convert(jni::JNIEnv& env, const jni::Object<JsonElement>& jsonElement) {
if (!jsonElement) {
return mbgl::NullValue();
}
static auto& elementClass = jni::Class<JsonElement>::Singleton(env);
static auto isJsonObject = elementClass.GetMethod<jni::jboolean()>(env, "isJsonObject");
static auto isJsonArray = elementClass.GetMethod<jni::jboolean()>(env, "isJsonArray");
static auto isJsonPrimitive = elementClass.GetMethod<jni::jboolean()>(env, "isJsonPrimitive");
static auto& primitiveClass = jni::Class<JsonPrimitive>::Singleton(env);
static auto isBoolean = primitiveClass.GetMethod<jni::jboolean()>(env, "isBoolean");
static auto isString = primitiveClass.GetMethod<jni::jboolean()>(env, "isString");
static auto isNumber = primitiveClass.GetMethod<jni::jboolean()>(env, "isNumber");
static auto getAsBoolean = primitiveClass.GetMethod<jni::jboolean()>(env, "getAsBoolean");
static auto getAsString = primitiveClass.GetMethod<jni::String()>(env, "getAsString");
static auto getAsDouble = primitiveClass.GetMethod<jni::jdouble()>(env, "getAsDouble");
if (jsonElement.Call(env, isJsonPrimitive)) {
auto primitive = jni::Cast(env, primitiveClass, jsonElement);
if (primitive.Call(env, isBoolean)) {
return bool(primitive.Call(env, getAsBoolean));
} else if (primitive.Call(env, isNumber)) {
return primitive.Call(env, getAsDouble);
} else if (primitive.Call(env, isString)) {
return jni::Make<std::string>(env, primitive.Call(env, getAsString));
} else {
return mbgl::NullValue();
}
} else if (jsonElement.Call(env, isJsonObject)) {
return JsonObject::convert(env, jni::Cast(env, jni::Class<JsonObject>::Singleton(env), jsonElement));
} else if (jsonElement.Call(env, isJsonArray)) {
return JsonArray::convert(env, jni::Cast(env, jni::Class<JsonArray>::Singleton(env), jsonElement));
} else {
return mbgl::NullValue();
}
}
void JsonElement::registerNative(jni::JNIEnv& env) {
jni::Class<JsonElement>::Singleton(env);
}
} // namespace gson
} // namespace android
} // namespace mbgl

View File

@@ -0,0 +1,24 @@
#pragma once
#include <mbgl/util/feature.hpp>
#include <jni/jni.hpp>
namespace mbgl {
namespace android {
namespace gson {
class JsonElement {
public:
using SuperTag = jni::ObjectTag;
static constexpr auto Name() { return "com/google/gson/JsonElement"; };
static jni::Local<jni::Object<JsonElement>> New(jni::JNIEnv&, const mbgl::Value&);
static mbgl::Value convert(JNIEnv&, const jni::Object<JsonElement>&);
static void registerNative(jni::JNIEnv&);
};
} // namespace gson
} // namespace android
} // namespace mbgl

View File

@@ -0,0 +1,62 @@
#include "json_object.hpp"
#include "json_element.hpp"
#include "../java/util.hpp"
namespace mbgl {
namespace android {
namespace gson {
jni::Local<jni::Object<JsonObject>> JsonObject::New(jni::JNIEnv& env, const mbgl::PropertyMap& values) {
static auto& javaClass = jni::Class<JsonObject>::Singleton(env);
static auto constructor = javaClass.GetConstructor(env);
static auto addMethod = javaClass.GetMethod<void(jni::String, jni::Object<JsonElement>)>(env, "add");
auto jsonObject = javaClass.New(env, constructor);
for (auto& item : values) {
jsonObject.Call(env, addMethod, jni::Make<jni::String>(env, item.first), JsonElement::New(env, item.second));
}
return jsonObject;
}
template <typename F> // void (jni::String, jni::Object<gson::JsonElement>)
static void iterateEntrySet(jni::JNIEnv& env, const jni::Object<JsonObject>& jsonObject, F callback) {
// Get Set<Map.Entry<String, JsonElement>>
static auto& javaClass = jni::Class<JsonObject>::Singleton(env);
static auto method = javaClass.GetMethod<jni::Object<java::util::Set>()>(env, "entrySet");
auto entryArray = java::util::Set::toArray<java::util::Map::Entry>(env, jsonObject.Call(env, method));
size_t size = entryArray.Length(env);
for (size_t i = 0; i < size; i++) {
auto entry = entryArray.Get(env, i);
if (entry) {
callback(java::util::Map::Entry::getKey<jni::StringTag>(env, entry),
java::util::Map::Entry::getValue<gson::JsonElement>(env, entry));
}
}
}
mbgl::PropertyMap JsonObject::convert(jni::JNIEnv& env, const jni::Object<JsonObject>& jsonObject) {
mbgl::PropertyMap map;
if (jsonObject) {
iterateEntrySet(
env, jsonObject, [&map, &env](const jni::String& jId, const jni::Object<gson::JsonElement>& jsonElement) {
map[jni::Make<std::string>(env, jId)] = JsonElement::convert(env, jsonElement);
});
}
return map;
}
void JsonObject::registerNative(jni::JNIEnv& env) {
jni::Class<JsonObject>::Singleton(env);
}
} // namespace gson
} // namespace android
} // namespace mbgl

View File

@@ -0,0 +1,24 @@
#pragma once
#include "json_element.hpp"
#include <jni/jni.hpp>
namespace mbgl {
namespace android {
namespace gson {
class JsonObject {
public:
using SuperTag = JsonElement;
static constexpr auto Name() { return "com/google/gson/JsonObject"; };
static jni::Local<jni::Object<JsonObject>> New(jni::JNIEnv&, const mbgl::PropertyMap&);
static mbgl::PropertyMap convert(JNIEnv&, const jni::Object<JsonObject>&);
static void registerNative(jni::JNIEnv&);
};
} // namespace gson
} // namespace android
} // namespace mbgl

View File

@@ -0,0 +1,13 @@
#include "json_primitive.hpp"
namespace mbgl {
namespace android {
namespace gson {
void JsonPrimitive::registerNative(jni::JNIEnv &env) {
jni::Class<JsonPrimitive>::Singleton(env);
}
} // namespace gson
} // namespace android
} // namespace mbgl

View File

@@ -0,0 +1,21 @@
#pragma once
#include "json_element.hpp"
#include <jni/jni.hpp>
namespace mbgl {
namespace android {
namespace gson {
class JsonPrimitive {
public:
using SuperTag = JsonElement;
static constexpr auto Name() { return "com/google/gson/JsonPrimitive"; };
static void registerNative(jni::JNIEnv&);
};
} // namespace gson
} // namespace android
} // namespace mbgl