::type;
41 |
42 | } // namespace jni
43 |
44 | #endif // JNI_BIND_IMPLEMENTATION_VOID_H_
45 |
--------------------------------------------------------------------------------
/java/com/google/BUILD:
--------------------------------------------------------------------------------
1 | load("@rules_java//java:defs.bzl", "java_library")
2 |
3 | licenses(["notice"])
4 |
5 | java_library(
6 | name = "random_string_java",
7 | srcs = ["RandomString.java"],
8 | )
9 |
10 | cc_library(
11 | name = "java_runtime_environment",
12 | hdrs = ["java_runtime_environment.h"],
13 | deps = ["//:jni_bind"],
14 | )
15 |
16 | cc_binary(
17 | name = "main",
18 | srcs = ["main.cc"],
19 | data = [":random_string_java"],
20 | deps = [
21 | ":java_runtime_environment",
22 | "//:jni_bind",
23 | ],
24 | )
25 |
--------------------------------------------------------------------------------
/java/com/google/README.md:
--------------------------------------------------------------------------------
1 | This binary is meant to demonstrate a simple binary that is entirely written in native.
2 |
3 | This is experimental.
4 |
--------------------------------------------------------------------------------
/java/com/google/RandomString.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 Google LLC
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.google;
18 |
19 | import java.util.Random;
20 |
21 | /** Just a toy model to make sure that we have a large data structure in memory. */
22 | class RandomString {
23 | public RandomString() {}
24 |
25 | public static String generateRandomString(int length) {
26 | String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
27 |
28 | Random random = new Random();
29 | StringBuilder sb = new StringBuilder(length);
30 |
31 | for (int i = 0; i < length; i++) {
32 | int randomIndex = random.nextInt(characters.length());
33 | char randomChar = characters.charAt(randomIndex);
34 | sb.append(randomChar);
35 | }
36 |
37 | return sb.toString();
38 | }
39 |
40 | public String format() {
41 | return generateRandomString(50);
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/javatests/com/jnibind/android/AndroidDefaultManifest.xml:
--------------------------------------------------------------------------------
1 |
16 |
17 |
21 |
24 |
25 |
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/javatests/com/jnibind/android/ClassLoaderHelperClass.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 Google LLC
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.jnibind.android;
18 |
19 | /** A helper class to be loaded remotely. */
20 | class ClassLoaderHelperClass {
21 | private final int intVal;
22 |
23 | public ClassLoaderHelperClass(int intVal) {
24 | this.intVal = intVal;
25 | }
26 |
27 | /** Returns the the value passed into the constructor */
28 | public int getValue() {
29 | return intVal;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/javatests/com/jnibind/android/ClassLoaderHelperSubclass.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 Google LLC
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.jnibind.android;
18 |
19 | /**
20 | * A subclass of {@link ClassLoaderHelperClass} that returns a different value.
21 | *
22 | * This helps test when subclasses are placed in side of native wrappers for their parent
23 | * classes.
24 | */
25 | class ClassLoaderHelperSubclass extends ClassLoaderHelperClass {
26 | public ClassLoaderHelperSubclass(int intVal) {
27 | super(intVal);
28 | }
29 |
30 | /** Returns the inverse of the value passed into the constructor */
31 | @Override
32 | public int getValue() {
33 | return -super.getValue();
34 | }
35 |
36 | /**
37 | * The wrapper currently does not have information about relationships between classes, so this
38 | * method explicitly returns an object as its parent class.
39 | */
40 | public ClassLoaderHelperClass castToParent() {
41 | return this;
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/javatests/com/jnibind/android/FieldTestHelper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 Google LLC
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.jnibind.android;
18 |
19 | import com.google.android.apps.common.proguard.UsedByNative;
20 |
21 | /**
22 | * This object is used to set mock expectations for FieldTest as inner classes do not support
23 | * mocking and the class definition must match the filename.
24 | */
25 | public class FieldTestHelper {
26 | @UsedByNative("field_test_jni.cc")
27 | public int intField;
28 |
29 | @UsedByNative("field_test_jni.cc")
30 | public float floatField;
31 |
32 | @UsedByNative("field_test_jni.cc")
33 | public double doubleField;
34 | }
35 |
--------------------------------------------------------------------------------
/javatests/com/jnibind/android/ObjectTestHelper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 Google LLC
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.jnibind.android;
18 |
19 | /** Simple object for use with RJni object tests. */
20 | public class ObjectTestHelper {
21 | public ObjectTestHelper() {
22 | intVal1 = 0;
23 | intVal2 = 0;
24 | intVal3 = 0;
25 | objectVal = null;
26 | }
27 |
28 | public ObjectTestHelper(Object objectVal) {
29 | this.objectVal = objectVal;
30 | }
31 |
32 | public ObjectTestHelper(int intVal) {
33 | this.intVal1 = intVal;
34 | }
35 |
36 | public ObjectTestHelper(int intVal, int intVal2) {
37 | this.intVal1 = intVal;
38 | this.intVal2 = intVal2;
39 | }
40 |
41 | public ObjectTestHelper(int intVal1, int intVal2, int intVal3) {
42 | this.intVal1 = intVal1;
43 | this.intVal2 = intVal2;
44 | this.intVal3 = intVal3;
45 | }
46 |
47 | public ObjectTestHelper returnNewObjectWithFieldSetToSum(int val1, int val2) {
48 | return new ObjectTestHelper(val1 + val2);
49 | }
50 |
51 | public void foo() {}
52 |
53 | public ObjectTestHelper returnNewObjectWithFieldSetToSum(ObjectTestHelper rhs) {
54 | return new ObjectTestHelper(
55 | this.intVal1 + rhs.intVal1, this.intVal2 + rhs.intVal2, this.intVal3 + rhs.intVal3);
56 | }
57 |
58 | public boolean isEqualTo(ObjectTestHelper rhs) {
59 | return this.intVal1 == rhs.intVal1
60 | && this.intVal2 == rhs.intVal2
61 | && this.intVal3 == rhs.intVal3;
62 | }
63 |
64 | public int intVal1;
65 | public int intVal2;
66 | public int intVal3;
67 | public Object objectVal;
68 | }
69 |
--------------------------------------------------------------------------------
/javatests/com/jnibind/android/StringTestHelper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 Google LLC
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.jnibind.android;
18 |
19 | import com.google.android.apps.common.proguard.UsedByNative;
20 |
21 | /**
22 | * This object is used to set mock expectations for StringTest as inner classes do not support
23 | * mocking and the class definition must match the filename.
24 | */
25 | public class StringTestHelper {
26 | /** Void Methods. */
27 | @UsedByNative("string_test_jni.cc")
28 | public void voidMethodTakesString(String s) {}
29 |
30 | @UsedByNative("string_test_jni.cc")
31 | public void voidMethodTakesTwoStrings(String s1, String s2) {}
32 |
33 | @UsedByNative("string_test_jni.cc")
34 | public void voidMethodTakesFiveStrings(String s1, String s2, String s3, String s4, String s5) {}
35 |
36 | /** String Methods. */
37 | @UsedByNative("string_test_jni.cc")
38 | public String stringMethodTakesString(String s) {
39 | return s;
40 | }
41 |
42 | @UsedByNative("string_test_jni.cc")
43 | public String stringMethodTakesTwoStrings(String s1, String s2) {
44 | return s1 + s2;
45 | }
46 |
47 | @UsedByNative("string_test_jni.cc")
48 | public String stringMethodTakesFiveStrings(
49 | String s1, String s2, String s3, String s4, String s5) {
50 | return s1 + s2 + s3 + s4 + s5;
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/javatests/com/jnibind/android/ThreadTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2023 Google LLC
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.jnibind.android;
18 |
19 | import static org.mockito.Mockito.verify;
20 |
21 | import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner;
22 | import org.junit.AfterClass;
23 | import org.junit.Rule;
24 | import org.junit.Test;
25 | import org.junit.runner.RunWith;
26 | import org.mockito.Mock;
27 | import org.mockito.junit.MockitoJUnit;
28 | import org.mockito.junit.MockitoRule;
29 |
30 | @RunWith(AndroidJUnit4ClassRunner.class)
31 | public class ThreadTest {
32 | private static final String TAG = "ThreadTest";
33 |
34 | @Mock public ObjectTestHelper objectTestHelper;
35 | @Rule public final MockitoRule mockito = MockitoJUnit.rule();
36 |
37 | static {
38 | System.loadLibrary("thread_test_jni");
39 | }
40 |
41 | public ThreadTest() {}
42 |
43 | @AfterClass
44 | public static void doShutDown() {
45 | jniTearDown();
46 | }
47 |
48 | @Test
49 | public void runsThreadedWork() {
50 | runsThreadedWorkOnObject(this);
51 | verify(objectTestHelper).foo();
52 | }
53 |
54 | // Tears down the JvmRef.
55 | static native void jniTearDown();
56 |
57 | // Creates a thread, waits until it finishes some work, uses passed object.
58 | // native void RunsThreadedWorkOnObject(ObjectTestHelper objectTestHelper);
59 | native void runsThreadedWorkOnObject(ThreadTest testFixture);
60 | }
61 |
--------------------------------------------------------------------------------
/javatests/com/jnibind/android/object_test_helper_jni.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 Google LLC
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #ifndef JNI_BIND_JAVATESTS_COM_JNIBIND_TEST_H
18 | #define JNI_BIND_JAVATESTS_COM_JNIBIND_TEST_H
19 |
20 | #include "jni_bind.h"
21 |
22 | // clang-format off
23 | constexpr jni::Class kObjectClass {
24 | "java/lang/Object"
25 | };
26 |
27 | constexpr jni::Class kObjectTestHelperClass {
28 | "com/jnibind/android/ObjectTestHelper",
29 |
30 | jni::Constructor<>{},
31 | jni::Constructor{kObjectClass},
32 | jni::Constructor{},
33 | jni::Constructor{},
34 |
35 | jni::Method{"returnNewObjectWithFieldSetToSum",
36 | jni::Overload{
37 | jni::Return{jni::Class{"com/jnibind/android/ObjectTestHelper"}},
38 | jni::Params{}
39 | },
40 | jni::Overload{
41 | jni::Return{jni::Class{"com/jnibind/android/ObjectTestHelper"}},
42 | jni::Params{jni::Class{"com/jnibind/android/ObjectTestHelper"}}
43 | },
44 | },
45 | ::jni::Method{"foo", ::jni::Return{}, ::jni::Params{}},
46 |
47 | jni::Field{"intVal1", int{}},
48 | jni::Field{"intVal2", int{}},
49 | jni::Field{"intVal3", int{}}
50 | };
51 | // clang-format on
52 |
53 | #endif // JNI_BIND_JAVATESTS_COM_JNIBIND_TEST_H
54 |
--------------------------------------------------------------------------------
/javatests/com/jnibind/test/AndroidDefaultManifest.xml:
--------------------------------------------------------------------------------
1 |
16 |
17 |
21 |
24 |
25 |
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/javatests/com/jnibind/test/ClassLoaderHelperClass.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 Google LLC
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.jnibind.test;
17 |
18 | /** A helper class to be loaded remotely. */
19 | public class ClassLoaderHelperClass {
20 | private final int intVal;
21 |
22 | public ClassLoaderHelperClass() {
23 | this.intVal = 123456;
24 | }
25 |
26 | public ClassLoaderHelperClass(int intVal) {
27 | this.intVal = intVal;
28 | }
29 |
30 | /** Returns the the value passed into the constructor */
31 | public int getValue() {
32 | return intVal;
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/javatests/com/jnibind/test/FieldTestHelper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 Google LLC
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.jnibind.test;
18 |
19 | /**
20 | * This object is used to set mock expectations for FieldTest as inner classes do not support
21 | * mocking and the class definition must match the filename.
22 | */
23 | public class FieldTestHelper {
24 | public int intField = 0;
25 | public float floatField = 0;
26 | public double doubleField = 0;
27 | public String stringField = "";
28 |
29 | FieldTestHelper() {}
30 |
31 | FieldTestHelper(int intField, float floatField, double doubleField) {
32 | this.intField = intField;
33 | this.floatField = floatField;
34 | this.doubleField = doubleField;
35 | }
36 |
37 | boolean isEqualTo(FieldTestHelper rhs) {
38 | return intField == rhs.intField
39 | && floatField == rhs.floatField
40 | && doubleField == rhs.doubleField
41 | && stringField.equals(rhs.stringField);
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/javatests/com/jnibind/test/StringTestHelper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 Google LLC
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.jnibind.test;
18 |
19 | /**
20 | * This object is used to set mock expectations for StringTest as inner classes do not support
21 | * mocking and the class definition must match the filename.
22 | */
23 | public class StringTestHelper {
24 | /** Void Methods. */
25 | public void voidMethodTakesString(String s) {}
26 | public void voidMethodTakesTwoStrings(String s1, String s2) {}
27 | public void voidMethodTakesFiveStrings(String s1, String s2, String s3, String s4, String s5) {}
28 |
29 | /** String Methods. */
30 | public String stringMethodTakesString(String s) {
31 | return s;
32 | }
33 |
34 | public String stringMethodTakesTwoStrings(String s1, String s2) {
35 | return s1 + s2;
36 | }
37 |
38 | public String stringMethodTakesFiveStrings(
39 | String s1, String s2, String s3, String s4, String s5) {
40 | return s1 + s2 + s3 + s4 + s5;
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/javatests/com/jnibind/test/builder_jni.cc:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2023 Google LLC
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #include
18 |
19 | #include "object_test_helper_jni.h"
20 | #include "jni_bind.h"
21 |
22 | #define JNI_MTHD(x, y, ...) \
23 | x Java_com_jnibind_test_BuilderTest_##y(JNIEnv*, jclass, ##__VA_ARGS__)
24 |
25 | using ::jni::Class;
26 | using ::jni::Constructor;
27 | using ::jni::LocalObject;
28 | using ::jni::Method;
29 | using ::jni::Params;
30 | using ::jni::Return;
31 | using ::jni::Self;
32 |
33 | static std::unique_ptr> jvm;
34 |
35 | // clang-format off
36 | constexpr Class kBuilder {
37 | "com/jnibind/test/BuilderTest$Builder",
38 |
39 | Constructor<>{},
40 |
41 | Method{"setOne", Return{Self{}}, Params{}},
42 | Method{"setTwo", Return{Self{}}, Params{}},
43 | Method{"setThree", Return{Self{}}, Params{}},
44 | Method{"build", Return{kObjectTestHelperClass}},
45 | };
46 | // clang-format on
47 |
48 | extern "C" {
49 |
50 | JNI_BIND_EXPORT jint JNI_BIND_CALL JNI_OnLoad(JavaVM* pjvm, void* reserved) {
51 | jvm.reset(new jni::JvmRef(pjvm));
52 |
53 | return JNI_VERSION_1_6;
54 | }
55 |
56 | JNI_MTHD(void, nativeJniTeardown) { jvm = nullptr; }
57 |
58 | JNI_MTHD(jobject, useBuilderToCreateObject) {
59 | return LocalObject{}
60 | .Call<"setOne">(111)
61 | .Call<"setTwo">(222)
62 | .Call<"setThree">(333)
63 | .Call<"build">()
64 | .Release();
65 | }
66 |
67 | } // extern "C"
68 |
--------------------------------------------------------------------------------
/javatests/com/jnibind/test/class_loader_test_jni.cc:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 Google LLC
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #include
18 |
19 | #include "jni_bind.h"
20 |
21 | using ::jni::Class;
22 | using ::jni::ClassLoader;
23 | using ::jni::kDefaultClassLoader;
24 | using ::jni::LocalClassLoader;
25 | using ::jni::SupportedClassSet;
26 |
27 | static constexpr Class kClassLoaderHelperClass{
28 | "com/jnibind/test/ClassLoaderHelperClass"};
29 |
30 | static constexpr ClassLoader kTestClassLoader{
31 | kDefaultClassLoader, SupportedClassSet{kClassLoaderHelperClass}};
32 |
33 | static constexpr jni::Jvm kJvmWithCustomClassLoaderSupport{kTestClassLoader};
34 |
35 | static std::unique_ptr> jvm;
36 |
37 | extern "C" {
38 |
39 | JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* pjvm, void* reserved) {
40 | jvm.reset(new jni::JvmRef(pjvm));
41 |
42 | return JNI_VERSION_1_6;
43 | }
44 |
45 | JNIEXPORT void JNICALL
46 | Java_com_jnibind_test_ClassLoaderTest_jniTearDown(JNIEnv* env, jclass) {
47 | jvm.reset();
48 | }
49 |
50 | JNIEXPORT jobject JNICALL
51 | Java_com_jnibind_test_ClassLoaderTest_jniBuildNewObjectsFromClassLoader(
52 | JNIEnv* env, jclass, jobject jclass_loader_obj) {
53 | LocalClassLoader
54 | class_loader{jclass_loader_obj};
55 |
56 | return class_loader.BuildLocalObject().Release();
57 | }
58 |
59 | } // extern "C"
60 |
--------------------------------------------------------------------------------
/javatests/com/jnibind/test/modulo.h:
--------------------------------------------------------------------------------
1 | #ifndef JNI_BIND_METAPROGRAMMING_MODULO_H_
2 | #define JNI_BIND_METAPROGRAMMING_MODULO_H_
3 |
4 | #include
5 | #include
6 |
7 | namespace jni {
8 |
9 | // Increments `val` by `increment_count`, wrapping to {0} at `max`.
10 | // This can be useful for generalising tests over different size types.
11 | template
12 | T Modulo(std::size_t increment_count, T val = T{0},
13 | T max = std::numeric_limits::max()) {
14 | T ret = val;
15 |
16 | for (std::size_t i = 0; i < increment_count; ++i) {
17 | ret += T{1};
18 | while (ret >= max) {
19 | ret -= max;
20 | }
21 | }
22 |
23 | return ret;
24 | }
25 |
26 | } // namespace jni
27 |
28 | #endif // JNI_BIND_METAPROGRAMMING_MODULO_H_
29 |
--------------------------------------------------------------------------------
/javatests/com/jnibind/test/modulo_test.cc:
--------------------------------------------------------------------------------
1 | #include "javatests/com/jnibind/test/modulo.h"
2 |
3 | #include
4 |
5 | #include
6 |
7 | using ::jni::Modulo;
8 |
9 | namespace {
10 |
11 | TEST(Modulo, SimpleIntTests) {
12 | EXPECT_EQ(Modulo(0), 0);
13 | EXPECT_EQ(Modulo(1), 1);
14 | EXPECT_EQ(Modulo(std::numeric_limits::max() - 1),
15 | std::numeric_limits::max() - 1);
16 | EXPECT_EQ(Modulo(std::numeric_limits::max()), 0);
17 |
18 | EXPECT_EQ(Modulo(0, 1), 1);
19 | EXPECT_EQ(Modulo(0, 5), 5);
20 | EXPECT_EQ(Modulo(1, 1), 2);
21 | EXPECT_EQ(Modulo(1, 5), 6);
22 | }
23 |
24 | } // namespace
25 |
--------------------------------------------------------------------------------
/javatests/com/jnibind/test/object_test_helper_jni.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 Google LLC
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #ifndef JNI_BIND_JAVATESTS_COM_JNIBIND_TEST_H
18 | #define JNI_BIND_JAVATESTS_COM_JNIBIND_TEST_H
19 |
20 | #include "jni_bind.h"
21 |
22 | // clang-format off
23 | constexpr jni::Class kObjectClass {
24 | "java/lang/Object"
25 | };
26 |
27 | constexpr jni::Class kObjectTestHelperClass {
28 | "com/jnibind/test/ObjectTestHelper",
29 |
30 | ::jni::Constructor<>{},
31 | ::jni::Constructor{kObjectClass},
32 | ::jni::Constructor{},
33 | ::jni::Constructor{},
34 |
35 | ::jni::Method{"returnNewObjectWithFieldSetToSum",
36 | ::jni::Overload{
37 | ::jni::Return{jni::Class{"com/jnibind/test/ObjectTestHelper"}},
38 | ::jni::Params{}
39 | },
40 | ::jni::Overload{
41 | ::jni::Return{jni::Class{"com/jnibind/test/ObjectTestHelper"}},
42 | ::jni::Params{jni::Class{"com/jnibind/test/ObjectTestHelper"}}
43 | },
44 | },
45 |
46 | ::jni::Method{"foo", ::jni::Return{}, ::jni::Params{}},
47 | ::jni::Method{"increment", ::jni::Return{}, ::jni::Params{}},
48 | ::jni::Method{"print", ::jni::Return{}, ::jni::Params{}},
49 |
50 | ::jni::Field{"intVal1", int{}},
51 | ::jni::Field{"intVal2", int{}},
52 | ::jni::Field{"intVal3", int{}}
53 | };
54 |
55 | // clang-format on
56 |
57 | #endif // JNI_BIND_JAVATESTS_COM_JNIBIND_TEST_H
58 |
--------------------------------------------------------------------------------
/jni_bind_release_leader.inc:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 Google LLC
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | /*******************************************************************************
18 | * JNI Bind Version __JNI_BIND_VERSION__.
19 | * Beta Public Release.
20 | ********************************************************************************
21 | * This header is the single header version which you can use to quickly test or
22 | * deploy in your own project without using Bazel. It is self contained.
23 | *
24 | * To use this header you must be compiling with clang and with C++17 support.
25 | *
26 | * It must also have jni.h in its build path. I.e. the following must compile:
27 | * #include
28 | *
29 | * See GitHub for sample API usage.
30 | * https://github.com/google/jni-bind
31 | *
32 | * 🚨 Are you enjoying JNI Bind? Please consider adding a ⭐️ on GitHub! 🚨
33 | * JNI Bind is the culmination of a lot of hard work and your support is greatly
34 | * appreciated.
35 | *
36 | * CODE BELOW IS AUTO GENERATED.
37 | *******************************************************************************/
38 |
39 | #ifndef JNI_BIND_RELEASE_
40 | #define JNI_BIND_RELEASE_
41 |
42 | #include
43 |
--------------------------------------------------------------------------------
/jni_bind_release_trailer.inc:
--------------------------------------------------------------------------------
1 |
2 | #endif // JNI_BIND_RELEASE_
3 |
--------------------------------------------------------------------------------
/jni_dep.h:
--------------------------------------------------------------------------------
1 | #ifndef JNI_BIND_JNI_HELPER_JNI_DEP_H_
2 | #define JNI_BIND_JNI_HELPER_JNI_DEP_H_
3 |
4 | // IWYU pragma: begin_exports
5 | #include
6 |
7 | // These declarations help prevent bad clang-tidy warnings.
8 | using jbyte = jbyte;
9 | using jboolean = jboolean;
10 | using jchar = jchar;
11 | using jshort = jshort;
12 | using jint = jint;
13 | using jlong = jlong;
14 | using jfloat = jfloat;
15 | using jdouble = jdouble;
16 | using jsize = jsize;
17 |
18 | using jobject = jobject;
19 | using jclass = jclass;
20 | using jthrowable = jthrowable;
21 | using jstring = jstring;
22 | using jarray = jarray;
23 | using jbooleanArray = jbooleanArray;
24 | using jbyteArray = jbyteArray;
25 | using jcharArray = jcharArray;
26 | using jshortArray = jshortArray;
27 | using jintArray = jintArray;
28 | using jlongArray = jlongArray;
29 | using jfloatArray = jfloatArray;
30 | using jdoubleArray = jdoubleArray;
31 | using jobjectArray = jobjectArray;
32 |
33 | using jweak = jweak;
34 | using jvalue = jvalue;
35 | using jfieldID = jfieldID;
36 | using jmethodID = jmethodID;
37 |
38 | using JavaVM = JavaVM;
39 | using JNIEnv = JNIEnv;
40 |
41 | // IWYU pragma: end_exports
42 |
43 | #endif // JNI_BIND_JNI_HELPER_JNI_DEP_H_
44 |
--------------------------------------------------------------------------------
/metaprogramming/all.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 Google LLC
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #ifndef JNI_BIND_METAPROGRAMMING_ALL_H_
18 | #define JNI_BIND_METAPROGRAMMING_ALL_H_
19 |
20 | #include
21 |
22 | namespace jni::metaprogramming {
23 |
24 | template
25 | struct All {
26 | template
27 | using type =
28 | typename std::conjunction...>::type;
29 | };
30 |
31 | template
32 | using All_t = typename All::template type;
33 |
34 | template
35 | static constexpr bool All_v = All_t::value;
36 |
37 | } // namespace jni::metaprogramming
38 |
39 | #endif // JNI_BIND_METAPROGRAMMING_ALL_H_
40 |
--------------------------------------------------------------------------------
/metaprogramming/all_unique.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 Google LLC
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #ifndef JNI_BIND_METAPROGRAMMING_ALL_UNIQUE_H_
18 | #define JNI_BIND_METAPROGRAMMING_ALL_UNIQUE_H_
19 |
20 | #include
21 |
22 | #include "contains.h"
23 |
24 | namespace jni::metaprogramming {
25 |
26 | template
27 | struct AllUnique {
28 | static constexpr bool value = true;
29 | using type = std::bool_constant;
30 | };
31 |
32 | template
33 | struct AllUnique {
34 | static constexpr bool value =
35 | !Contains_v && AllUnique::value;
36 | using type = std::bool_constant;
37 | };
38 |
39 | template
40 | static constexpr bool AllUnique_v = AllUnique::value;
41 |
42 | // Constexpr value implementation.
43 | constexpr bool AllUniqueValues(...) { return true; }
44 |
45 | template
46 | constexpr bool AllUniqueValues(const T1&& t1, const Ts&&... ts) {
47 | return (!ContainsValue(t1, ts...)) && AllUniqueValues(ts...);
48 | }
49 |
50 | } // namespace jni::metaprogramming
51 |
52 | #endif // JNI_BIND_METAPROGRAMMING_ALL_UNIQUE_H_
53 |
--------------------------------------------------------------------------------
/metaprogramming/any.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 Google LLC
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #ifndef JNI_BIND_METAPROGRAMMING_ANY_H_
18 | #define JNI_BIND_METAPROGRAMMING_ANY_H_
19 |
20 | #include
21 |
22 | #include "tuple_manipulation.h"
23 |
24 | namespace jni::metaprogramming {
25 |
26 | template
27 | struct Any {
28 | template
29 | using type =
30 | typename std::disjunction...>::type;
31 | };
32 |
33 | template
34 | using Any_t = typename Any::template type;
35 |
36 | template
37 | static constexpr bool Any_v = Any_t::value;
38 |
39 | template
40 | using Any_Tup = TupleUnroller_t, Ts>;
41 |
42 | template
43 | static constexpr bool Any_Tup_v = TupleUnroller_t, Ts>::value;
44 |
45 | } // namespace jni::metaprogramming
46 |
47 | #endif // JNI_BIND_METAPROGRAMMING_ANY_H_
48 |
--------------------------------------------------------------------------------
/metaprogramming/any_test.cc:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 Google LLC
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #include "any.h"
18 |
19 | #include
20 |
21 | #include "invoke.h"
22 | #include "same.h"
23 |
24 | using ::jni::metaprogramming::Any;
25 | using ::jni::metaprogramming::Any_t;
26 | using ::jni::metaprogramming::Any_v;
27 | using ::jni::metaprogramming::Invoke_t;
28 | using ::jni::metaprogramming::Same;
29 |
30 | namespace {
31 |
32 | struct A {};
33 | struct B {};
34 | struct C {};
35 | struct D {};
36 | struct E {};
37 |
38 | static_assert(
39 | std::is_same_v>, A, B, C, D>, std::true_type>);
40 | static_assert(
41 | std::is_same_v>, A, B, C, D>, std::true_type>);
42 | static_assert(
43 | std::is_same_v>, A, B, C, D>, std::true_type>);
44 | static_assert(
45 | std::is_same_v>, A, B, C, D>, std::true_type>);
46 | static_assert(
47 | std::is_same_v>, A, B, C, D>, std::false_type>);
48 |
49 | static_assert(std::is_same_v, A, B, C, D>, std::true_type>);
50 | static_assert(std::is_same_v, A, B, C, D>, std::true_type>);
51 | static_assert(std::is_same_v, A, B, C, D>, std::true_type>);
52 | static_assert(std::is_same_v, A, B, C, D>, std::true_type>);
53 | static_assert(std::is_same_v, A, B, C, D>, std::false_type>);
54 |
55 | static_assert(Any_v, A, B, C, D>);
56 | static_assert(Any_v, A, B, C, D>);
57 | static_assert(Any_v, A, B, C, D>);
58 | static_assert(Any_v, A, B, C, D>);
59 | static_assert(!Any_v, A, B, C, D>);
60 |
61 | } // namespace
62 |
--------------------------------------------------------------------------------
/metaprogramming/apply.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 Google LLC
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #ifndef JNI_BIND_METAPROGRAMMING_APPLY_H_
18 | #define JNI_BIND_METAPROGRAMMING_APPLY_H_
19 |
20 | #include "tuple_manipulation.h"
21 |
22 | namespace jni::metaprogramming {
23 |
24 | // Forwards all arguments to a metafunction. Note, Apply is itself a type
25 | // whose |type| (aka its metafunction's invocation).
26 | template class Function>
27 | struct Apply {
28 | template
29 | using type = Function;
30 | };
31 |
32 | template class Function, typename... Ts>
33 | using Apply_t = typename Apply::template type;
34 |
35 | // Forwards all arguments from a tuple to a metafunction.
36 | // i.e. Apply_t == ApplyTup_t>.
37 | template class Function, typename TupleType>
38 | using ApplyTup_t = TupleUnroller_t, TupleType>;
39 |
40 | } // namespace jni::metaprogramming
41 |
42 | #endif // JNI_BIND_METAPROGRAMMING_APPLY_H_
43 |
--------------------------------------------------------------------------------
/metaprogramming/apply_test.cc:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 Google LLC
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #include "apply.h"
18 |
19 | #include
20 | #include
21 |
22 | #include "invoke.h"
23 | #include "same.h"
24 |
25 | using ::jni::metaprogramming::Apply;
26 | using ::jni::metaprogramming::Apply_t;
27 | using ::jni::metaprogramming::Invoke_t;
28 | using ::jni::metaprogramming::InvokePerTupArg_t;
29 | using ::jni::metaprogramming::Same;
30 |
31 | namespace {
32 |
33 | struct A {};
34 | struct B {};
35 | struct C {};
36 |
37 | // Ops can be deferred.
38 | using DeferredOp = Apply;
39 | static_assert(std::is_same_v, Same>);
40 | static_assert(std::is_same_v, Same>);
41 |
42 | // And then later invoked.
43 | static_assert(std::is_same_v, A>, std::true_type>);
44 | static_assert(std::is_same_v, B>, std::false_type>);
45 |
46 | // Arguments to metafunctions can also be deferred.
47 | // This reads as: "Is A the same as A then B, then C".
48 | using SampleTypes = std::tuple;
49 | using Operator = Apply;
50 | static_assert(std::is_same_v<
51 | InvokePerTupArg_t, SampleTypes>,
52 | std::tuple>);
53 |
54 | } // namespace
55 |
--------------------------------------------------------------------------------
/metaprogramming/base.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 Google LLC
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #ifndef JNI_BIND_METAPROGRAMMING_BASE_H_
18 | #define JNI_BIND_METAPROGRAMMING_BASE_H_
19 |
20 | #include
21 |
22 | namespace jni::metaprogramming {
23 |
24 | template
25 | struct Base {
26 | template
27 | using type = typename std::is_base_of>::type;
28 | };
29 |
30 | template
31 | using Base_t = typename Base::template type;
32 |
33 | } // namespace jni::metaprogramming
34 |
35 | #endif // JNI_BIND_METAPROGRAMMING_BASE_H_
36 |
--------------------------------------------------------------------------------
/metaprogramming/base_filter_test.cc:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 Google LLC
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #include "base_filter.h"
18 |
19 | #include
20 | #include
21 |
22 | using ::jni::metaprogramming::BaseFilter_t;
23 |
24 | namespace {
25 |
26 | struct S {};
27 | struct S1 {};
28 | struct S2 {};
29 |
30 | struct D : S {};
31 | struct D2 : D {};
32 | struct D3 : D2 {};
33 |
34 | static_assert(std::is_base_of_v);
35 |
36 | static_assert(std::is_same_v, std::tuple<>>);
37 | static_assert(std::is_same_v