From 690a91874dcaa5b704270055ab693a68354c0525 Mon Sep 17 00:00:00 2001 From: Jamieson Pryor Date: Fri, 13 Sep 2024 08:25:00 -0700 Subject: [PATCH] Add initial (incomplete) definition for `java/lang/Exception` and `java/lang/Throwable`. Full syntax support for `env->ThrowNew` does not exist yet. Inherited methods/fields are not yet supported so these definitions are incomplete. PiperOrigin-RevId: 674307011 --- BUILD | 2 ++ class_defs/BUILD | 32 +++++++++++++++++++++++ class_defs/README.md | 8 ++++++ class_defs/java_lang_classes.h | 7 ----- class_defs/java_lang_exception.h | 45 ++++++++++++++++++++++++++++++++ class_defs/java_lang_throwable.h | 37 ++++++++++++++++++++++++++ jni_bind.h | 2 ++ 7 files changed, 126 insertions(+), 7 deletions(-) create mode 100644 class_defs/README.md create mode 100644 class_defs/java_lang_exception.h create mode 100644 class_defs/java_lang_throwable.h diff --git a/BUILD b/BUILD index a0dafade..daced343 100644 --- a/BUILD +++ b/BUILD @@ -28,6 +28,8 @@ cc_library( deps = [ ":jni_dep", "//class_defs:java_lang_classes", + "//class_defs:java_lang_exception", + "//class_defs:java_lang_throwable", "//class_defs:java_util_array_list", "//class_defs:java_util_classes", "//implementation:array", diff --git a/class_defs/BUILD b/class_defs/BUILD index 0b9c67db..200e8bc5 100644 --- a/class_defs/BUILD +++ b/class_defs/BUILD @@ -12,6 +12,38 @@ filegroup( exports_files(["LICENSE"]) +cc_library( + name = "java_lang_exception", + hdrs = ["java_lang_exception.h"], + deps = [ + ":java_lang_throwable", + "//:jni_dep", + "//implementation:class", + "//implementation:constructor", + "//implementation:method", + "//implementation:params", + "//implementation:return", + "//implementation:self", + "//implementation:static", + ], +) + +cc_library( + name = "java_lang_throwable", + hdrs = ["java_lang_throwable.h"], + deps = [ + "//:jni_dep", + "//implementation:array", + "//implementation:class", + "//implementation:constructor", + "//implementation:method", + "//implementation:params", + "//implementation:return", + "//implementation:self", + "//implementation:static", + ], +) + cc_library( name = "java_lang_classes", hdrs = ["java_lang_classes.h"], diff --git a/class_defs/README.md b/class_defs/README.md new file mode 100644 index 00000000..46cd044f --- /dev/null +++ b/class_defs/README.md @@ -0,0 +1,8 @@ +** WARNING ** + +Class definitions in this directory may have their path change in the future, you may be better suited creating copies until full scrapes of APIs are provided in the future. + +These are used to bootstrap JNI Bind, but may change in the future, and may also be superseded by separate more complete definitions. + +Some definitions in this directory exist solely for phase 1 compilation and as such may be incomplete. + diff --git a/class_defs/java_lang_classes.h b/class_defs/java_lang_classes.h index 66891733..f12c8fac 100644 --- a/class_defs/java_lang_classes.h +++ b/class_defs/java_lang_classes.h @@ -58,13 +58,6 @@ static constexpr Class kJavaLangString{ Method{"toString", Return{jstring{}}, Params<>{}}, }; -static constexpr Class kJavaLangException{ - "java/lang/Exception", - Method{"getMessage", Return{jstring{}}, Params<>{}}, - Method{"printStackTrace", Return{}, Params<>{}}, - Method{"toString", Return{jstring{}}, Params<>{}}, -}; - // clang-format on } // namespace jni diff --git a/class_defs/java_lang_exception.h b/class_defs/java_lang_exception.h new file mode 100644 index 00000000..d58c7e09 --- /dev/null +++ b/class_defs/java_lang_exception.h @@ -0,0 +1,45 @@ +/* + * Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef JNI_BIND_CLASS_DEFS_JAVA_LANG_EXCEPTION_H_ +#define JNI_BIND_CLASS_DEFS_JAVA_LANG_EXCEPTION_H_ + +#include "class_defs/java_lang_throwable.h" +#include "implementation/class.h" +#include "implementation/constructor.h" +#include "implementation/method.h" +#include "implementation/params.h" +#include "implementation/return.h" +#include "jni_dep.h" + +namespace jni { + +static constexpr Class kJavaLangException{ + "java/lang/Exception", + Constructor{}, + Constructor{}, + Constructor{jstring{}, kJavaLangThrowable, jboolean{}, jboolean{}}, + Constructor{kJavaLangThrowable}, + + // Inherited. + Method{"getMessage", Return{jstring{}}, Params<>{}}, + Method{"printStackTrace", Return{}, Params<>{}}, + Method{"toString", Return{jstring{}}, Params<>{}}, +}; + +} // namespace jni + +#endif // JNI_BIND_CLASS_DEFS_JAVA_LANG_EXCEPTION_H_ diff --git a/class_defs/java_lang_throwable.h b/class_defs/java_lang_throwable.h new file mode 100644 index 00000000..f6e7baa8 --- /dev/null +++ b/class_defs/java_lang_throwable.h @@ -0,0 +1,37 @@ +/* + * Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef JNI_BIND_CLASS_DEFS_JAVA_LANG_THROWABLE_H_ +#define JNI_BIND_CLASS_DEFS_JAVA_LANG_THROWABLE_H_ + +#include "implementation/class.h" +#include "implementation/constructor.h" +#include "implementation/self.h" +#include "jni_dep.h" + +namespace jni { + +static constexpr Class kJavaLangThrowable{ + "java/lang/Throwable", + Constructor{}, + Constructor{}, + Constructor{jstring{}, Self{}}, + Constructor{jstring{}, Self{}, jboolean{}, jboolean{}}, + Constructor{Self{}}}; + +} // namespace jni + +#endif // JNI_BIND_CLASS_DEFS_JAVA_LANG_THROWABLE_H_ diff --git a/jni_bind.h b/jni_bind.h index b323a61c..6c4c79c3 100644 --- a/jni_bind.h +++ b/jni_bind.h @@ -81,6 +81,8 @@ static constexpr Configuration kConfig{ // Convenience headers for system libraries. #include "class_defs/java_lang_classes.h" +#include "class_defs/java_lang_exception.h" +#include "class_defs/java_lang_throwable.h" #include "class_defs/java_util_array_list.h" #include "class_defs/java_util_classes.h"