forked from Dirbaio/java-spaghetti
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethod_mangling_style.rs
More file actions
277 lines (254 loc) · 9.04 KB
/
method_mangling_style.rs
File metadata and controls
277 lines (254 loc) · 9.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
use cafebabe::descriptors::{FieldType, MethodDescriptor};
use serde_derive::Deserialize;
use super::rust_identifier::{IdentifierManglingError, javaify_identifier, rustify_identifier};
use crate::parser_util::{Id, IdPart};
#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Eq, Hash)]
#[serde(rename_all = "snake_case")]
pub enum MethodManglingStyle {
/// Leave the original method name alone as much as possible.
/// Constructors will still be renamed from "\<init>" to "new".
///
/// # Examples:
///
/// | Java | Rust |
/// | --------- | --------- |
/// | getFoo | getFoo |
/// | \<init\> | new |
Java,
/// Leave the original method name alone as much as possible... with unqualified typenames appended for disambiguation.
/// Constructors will still be renamed from "\<init>" to "new".
///
/// # Examples:
///
/// | Java | Rust |
/// | --------- | ------------- |
/// | getFoo | getFoo_int |
/// | \<init\> | new_Object |
JavaShortSignature,
/// Leave the original method name alone as much as possible... with qualified typenames appended for disambiguation.
/// Constructors will still be renamed from "\<init>" to "new".
///
/// # Examples:
///
/// | Java | Rust |
/// | --------- | --------------------- |
/// | getFoo | getFoo_int |
/// | \<init\> | new_java_lang_Object |
JavaLongSignature,
/// Rename the method to use rust style naming conventions.
///
/// # Examples:
///
/// | Java | Rust |
/// | --------- | --------- |
/// | getFoo | get_foo |
/// | \<init\> | new |
Rustify,
/// Rename the method to use rust style naming conventions, with unqualified typenames appended for disambiguation.
///
/// # Examples:
///
/// | Java | Rust |
/// | --------- | ------------- |
/// | getFoo | get_foo_int |
/// | \<init\> | new_object |
RustifyShortSignature,
/// Rename the method to use rust style naming conventions, with qualified typenames appended for disambiguation.
///
/// # Examples:
///
/// | Java | Rust |
/// | --------- | --------------------- |
/// | getFoo | get_foo_int |
/// | \<init\> | new_java_lang_object |
RustifyLongSignature,
}
#[test]
fn method_mangling_style_mangle_test() {
use std::borrow::Cow;
use cafebabe::descriptors::{ClassName, FieldDescriptor, ReturnDescriptor};
let desc_no_arg_ret_v = MethodDescriptor {
parameters: Vec::new(),
return_type: ReturnDescriptor::Void,
};
let desc_arg_i_ret_v = MethodDescriptor {
parameters: vec![FieldDescriptor {
dimensions: 0,
field_type: FieldType::Integer,
}],
return_type: ReturnDescriptor::Void,
};
let desc_arg_obj_ret_v = MethodDescriptor {
parameters: vec![FieldDescriptor {
dimensions: 0,
field_type: FieldType::Object(ClassName::try_from(Cow::Borrowed("java/lang/Object")).unwrap()),
}],
return_type: ReturnDescriptor::Void,
};
for &(name, sig, java, java_short, java_long, rust, rust_short, rust_long) in &[
(
"getFoo",
&desc_no_arg_ret_v,
"getFoo",
"getFoo",
"getFoo",
"get_foo",
"get_foo",
"get_foo",
),
(
"getFoo",
&desc_arg_i_ret_v,
"getFoo",
"getFoo_int",
"getFoo_int",
"get_foo",
"get_foo_int",
"get_foo_int",
),
(
"getFoo",
&desc_arg_obj_ret_v,
"getFoo",
"getFoo_Object",
"getFoo_java_lang_Object",
"get_foo",
"get_foo_object",
"get_foo_java_lang_object",
),
("<init>", &desc_no_arg_ret_v, "new", "new", "new", "new", "new", "new"),
(
"<init>",
&desc_arg_i_ret_v,
"new",
"new_int",
"new_int",
"new",
"new_int",
"new_int",
),
(
"<init>",
&desc_arg_obj_ret_v,
"new",
"new_Object",
"new_java_lang_Object",
"new",
"new_object",
"new_java_lang_object",
),
// TODO: get1DFoo
// TODO: array types (primitive + non-primitive)
] {
assert_eq!(MethodManglingStyle::Java.mangle(name, sig).unwrap(), java);
assert_eq!(
MethodManglingStyle::JavaShortSignature.mangle(name, sig).unwrap(),
java_short
);
assert_eq!(
MethodManglingStyle::JavaLongSignature.mangle(name, sig).unwrap(),
java_long
);
assert_eq!(MethodManglingStyle::Rustify.mangle(name, sig).unwrap(), rust);
assert_eq!(
MethodManglingStyle::RustifyShortSignature.mangle(name, sig).unwrap(),
rust_short
);
assert_eq!(
MethodManglingStyle::RustifyLongSignature.mangle(name, sig).unwrap(),
rust_long
);
}
}
#[test]
fn mangle_method_name_test() {
use cafebabe::descriptors::{MethodDescriptor, ReturnDescriptor};
let desc = MethodDescriptor {
parameters: Vec::new(),
return_type: ReturnDescriptor::Void,
};
assert_eq!(
MethodManglingStyle::Rustify.mangle("isFooBar", &desc).unwrap(),
"is_foo_bar"
);
assert_eq!(
MethodManglingStyle::Rustify.mangle("XMLHttpRequest", &desc).unwrap(),
"xml_http_request"
);
assert_eq!(
MethodManglingStyle::Rustify.mangle("getFieldID_Input", &desc).unwrap(),
"get_field_id_input"
);
}
impl MethodManglingStyle {
pub fn mangle(&self, name: &str, descriptor: &MethodDescriptor) -> Result<String, IdentifierManglingError> {
let name = match name {
"" => {
return Err(IdentifierManglingError::EmptyString);
}
"<init>" => "new",
"<clinit>" => {
return Err(IdentifierManglingError::NotApplicable("Static type ctor"));
}
name => name,
};
let (rustify, long_sig) = match self {
MethodManglingStyle::Java => return javaify_identifier(name),
MethodManglingStyle::Rustify => return rustify_identifier(name),
MethodManglingStyle::JavaShortSignature => (false, false),
MethodManglingStyle::JavaLongSignature => (false, true),
MethodManglingStyle::RustifyShortSignature => (true, false),
MethodManglingStyle::RustifyLongSignature => (true, true),
};
let mut buffer = name.to_string();
for arg in descriptor.parameters.iter() {
match &arg.field_type {
FieldType::Boolean => buffer.push_str("_boolean"),
FieldType::Byte => buffer.push_str("_byte"),
FieldType::Char => buffer.push_str("_char"),
FieldType::Short => buffer.push_str("_short"),
FieldType::Integer => buffer.push_str("_int"),
FieldType::Long => buffer.push_str("_long"),
FieldType::Float => buffer.push_str("_float"),
FieldType::Double => buffer.push_str("_double"),
FieldType::Object(class_name) => {
let class = Id::from(class_name);
if long_sig {
for component in class.iter() {
buffer.push('_');
match component {
IdPart::Namespace(namespace) => {
buffer.push_str(namespace);
}
IdPart::ContainingClass(cls) => {
buffer.push_str(cls);
}
IdPart::LeafClass(cls) => {
buffer.push_str(cls);
}
}
}
} else {
// short style
if let Some(IdPart::LeafClass(leaf)) = class.iter().last() {
buffer.push('_');
buffer.push_str(leaf);
} else if arg.dimensions == 0 {
// XXX: `if arg.dimensions == 0` is just keeping the behaviour
// before porting to cafebabe, is it a bug?
buffer.push_str("_unknown");
}
}
}
};
for _ in 0..arg.dimensions {
buffer.push_str("_array");
}
}
if rustify {
rustify_identifier(&buffer)
} else {
javaify_identifier(&buffer)
}
}
}