droid_wrap_macros/
lib.rs

1/*
2 * Copyright (c) 2024. The RigelA open source project team and
3 * its contributors reserve all rights.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and limitations under the License.
12 */
13
14mod entry;
15mod java;
16mod utils;
17
18use proc_macro::TokenStream;
19
20/// 安卓平台的入口
21///
22/// 将此宏标记在`fn main()`函数上可以自动实现安卓应用的入口函数。
23///
24/// # 示例
25///
26/// ```
27/// use droid_wrap_macros::android_main;
28///
29/// #[android_main]
30/// fn main() {}
31/// ```
32#[proc_macro_attribute]
33pub fn android_main(_args: TokenStream, input: TokenStream) -> TokenStream {
34    entry::android_main(input.into()).into()
35}
36
37/// 定义java class,将此属性标记在struct上,可以自动实现操作java对象的必要功能。
38///
39/// # Arguments
40///
41/// * `attrs`: 属性输入。
42/// * `input`: struct输入。
43///
44/// returns: TokenStream
45///
46/// # Examples
47///
48/// ```
49/// use droid_wrap_macros::java_class;
50/// #[java_class(name = "java/lang/System")]
51/// struct System;
52/// ```
53#[proc_macro_attribute]
54pub fn java_class(attrs: TokenStream, input: TokenStream) -> TokenStream {
55    java::java_class(attrs.into(), input.into()).into()
56}
57
58/// 实现java类的方法,将此属性标记在fn函数上,可以自动实现调用java方法,可以自动识别静态方法(如果参数中没有“self”)。
59///
60/// # Arguments
61///
62/// * `attrs`: 属性输入。
63/// * `input`: 函数输入。
64///
65/// returns: TokenStream
66///
67/// # Examples
68///
69/// ```
70/// use droid_wrap_macros::java_method;
71/// struct System;
72/// impl System {
73/// #[java_method]
74/// fn current_time_millis() -> i64 {}
75/// }
76/// ```
77#[proc_macro_attribute]
78pub fn java_method(attrs: TokenStream, input: TokenStream) -> TokenStream {
79    java::java_method(attrs.into(), input.into()).into()
80}
81
82/// 实现java类的构造器,将此属性标记在fn函数上,可以自动实现调用java类的构造器。
83///
84/// # Arguments
85///
86/// * `_`: 未使用。
87/// * `input`: 函数输入。
88///
89/// returns: TokenStream
90///
91/// # Examples
92///
93/// ```
94/// use droid_wrap_macros::java_constructor;
95/// struct Integer;
96/// impl Integer {
97/// #[java_constructor]
98/// fn new(value: i32) -> Self {}
99/// }
100/// ```
101#[proc_macro_attribute]
102pub fn java_constructor(_: TokenStream, input: TokenStream) -> TokenStream {
103    java::java_constructor(input.into()).into()
104}
105
106/// 定义java interface,将此属性标记在trait上,可以自动实现提供java对象与rust对象的互操作的功能。
107///
108/// # Arguments
109///
110/// * `attrs`: 属性。
111/// * `input`: 特征输入。
112///
113/// returns: TokenStream
114///
115/// # Examples
116///
117/// ```
118/// use droid_wrap_macros::java_interface;
119/// trait Runnable {
120/// fn run(&self);
121/// }
122/// ```
123#[proc_macro_attribute]
124pub fn java_interface(attrs: TokenStream, input: TokenStream) -> TokenStream {
125    java::java_interface(attrs.into(), input.into()).into()
126}
127
128/// 实现java interface,将此属性标记在impl上,可以自动实现java接口的动态代理,从而实现java层回调rust层。
129/// 其中在接口中定义的每一个方法将自动实现并暴露给java层,但以下划线“_”开头的函数除外。
130///
131/// # Arguments
132///
133/// * `attrs`: 属性。
134/// * `input`: impl输入。
135///
136/// returns: TokenStream
137///
138/// # Examples
139///
140/// ```
141/// use std::fmt::{Debug, Formatter};
142/// use droid_wrap_macros::{java_interface,java_implement};
143/// #[java_interface(name = "java/lang/Runnable")]
144/// trait Runnable {
145/// fn run(&self);
146/// }
147/// struct RunnableImpl;
148/// impl PartialEq for RunnableImpl {
149///     fn eq(&self, other: &Self) -> bool {
150///         todo!()
151///     }
152/// }
153/// impl Debug for RunnableImpl {
154///     fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
155///         todo!()
156///     }
157/// }
158/// #[java_implement]
159/// impl Runnable for RunnableImpl {
160/// fn run(&self) {
161/// println!("Called from java.");
162/// }
163/// }
164/// ```
165#[proc_macro_attribute]
166pub fn java_implement(attrs: TokenStream, input: TokenStream) -> TokenStream {
167    java::java_implement(attrs.into(), input.into()).into()
168}
169
170/// 实现java类的字段,将此属性标记在带有get或set的fn函数上,可以自动实现访问java字段的能力,可以自动识别静态字段(如果参数中没有“self”)。
171///
172/// # Arguments
173///
174/// * `_`: 未使用。
175/// * `input`: 函数输入。
176///
177/// returns: TokenStream
178///
179/// # Examples
180///
181/// ```
182/// use droid_wrap_macros::java_field;
183///
184/// pub struct LayoutParams;
185///
186/// impl LayoutParams {
187///     #[java_field]
188///     pub fn get_width(&self) -> i32 {}
189///
190///     #[java_field]
191///     pub fn set_width(&self, value: i32) {}
192/// }
193/// ```
194#[proc_macro_attribute]
195pub fn java_field(attrs: TokenStream, input: TokenStream) -> TokenStream {
196    java::java_field(attrs.into(), input.into()).into()
197}