model-lang-java 0.7.0

Java enricher: HTTP endpoints served by handler methods (Spring, JAX-RS) and GWT RPC calls
Documentation
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
//! What the enricher reads from one Java file: types, their handler methods
//! (HTTP routes without the class prefix) and the RPC-shaped calls of each
//! method or constructor. Positions are tree-sitter's (0-based line, byte
//! column), the same as `model-treesitter` records in `name_range`, which is
//! how a declaration here finds its entity in the model.

use anyhow::{Context, anyhow};
use model_core::Range;
use tree_sitter::{Node, Parser};

use crate::rpc::{self, RawRpcCall};

/// Position of a declaration's name: `(line, column)`.
pub(crate) type Pos = (u32, u32);

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Verb {
    Get,
    Post,
    Put,
    Delete,
    Patch,
    Any,
}

impl Verb {
    pub(crate) fn as_str(self) -> &'static str {
        match self {
            Verb::Get => "GET",
            Verb::Post => "POST",
            Verb::Put => "PUT",
            Verb::Delete => "DELETE",
            Verb::Patch => "PATCH",
            Verb::Any => "ANY",
        }
    }
}

/// A `{verb, path}` pair. A path that is not a plain literal is `?`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Route {
    pub verb: Verb,
    pub path: String,
}

#[derive(Debug, Clone)]
pub(crate) struct MethodDecl {
    pub name: String,
    pub arity: usize,
    pub constructor: bool,
    pub name_pos: Pos,
    /// Own endpoints, with no class-level prefix (inheritance input).
    pub routes: Vec<Route>,
    pub rpc: Vec<RawRpcCall>,
}

#[derive(Debug, Clone)]
pub(crate) struct TypeDecl {
    /// Fully qualified, nested types joined with `$`: `p.Outer$Inner`.
    pub id: String,
    pub name: String,
    pub interface: bool,
    pub name_pos: Pos,
    /// Simple names of the extended and implemented types.
    pub supertypes: Vec<String>,
    /// Own class-level prefixes, when the type declares a mapping.
    pub prefixes: Option<Vec<String>>,
    pub methods: Vec<MethodDecl>,
}

#[derive(Debug, Clone, Default)]
pub(crate) struct FileFacts {
    pub file: String,
    pub package: Option<String>,
    /// Non-static imports as written: `a.b.C` or `a.b.*`.
    pub imports: Vec<String>,
    pub types: Vec<TypeDecl>,
}

pub(crate) fn range_of(n: Node) -> Range {
    let (s, e) = (n.start_position(), n.end_position());
    Range::new(s.row as u32, s.column as u32, e.row as u32, e.column as u32)
}

fn pos_of(n: Node) -> Pos {
    let p = n.start_position();
    (p.row as u32, p.column as u32)
}

pub(crate) fn text<'a>(n: Node, src: &'a str) -> &'a str {
    src.get(n.start_byte()..n.end_byte()).unwrap_or("")
}

/// Simple name of a type node, without generics, qualifiers or array suffix.
/// None for primitives and other non-class types.
pub(crate) fn type_name(n: Node, src: &str) -> Option<String> {
    match n.kind() {
        "type_identifier" => Some(text(n, src).to_string()),
        "generic_type" => n.named_child(0).and_then(|c| type_name(c, src)),
        "scoped_type_identifier" => {
            let mut cur = n.walk();
            let last = n
                .named_children(&mut cur)
                .filter(|c| c.kind() == "type_identifier")
                .last();
            last.map(|c| text(c, src).to_string())
        }
        "array_type" => n
            .child_by_field_name("element")
            .and_then(|c| type_name(c, src)),
        _ => None,
    }
}

fn annotation_nodes<'t>(n: Node<'t>, src: &str) -> Vec<(String, Node<'t>)> {
    let mut out = Vec::new();
    let mut cur = n.walk();
    for m in n.children(&mut cur).filter(|c| c.kind() == "modifiers") {
        let mut c2 = m.walk();
        for a in m.children(&mut c2) {
            if matches!(a.kind(), "annotation" | "marker_annotation")
                && let Some(name) = a.child_by_field_name("name")
            {
                let t = text(name, src);
                out.push((t.rsplit('.').next().unwrap_or(t).to_string(), a));
            }
        }
    }
    out
}

/// Value expressions of annotation argument `keys` (or the positional one if
/// `positional`); an array initializer is flattened.
fn annotation_values<'t>(a: Node<'t>, src: &str, keys: &[&str], positional: bool) -> Vec<Node<'t>> {
    let Some(args) = a.child_by_field_name("arguments") else {
        return Vec::new();
    };
    let mut found = None;
    let mut cur = args.walk();
    for c in args.named_children(&mut cur) {
        if c.kind() == "element_value_pair" {
            let k = c.child_by_field_name("key").map_or("", |k| text(k, src));
            if keys.contains(&k) {
                found = c.child_by_field_name("value");
            }
        } else if positional && found.is_none() {
            found = Some(c);
        }
    }
    let Some(v) = found else {
        return Vec::new();
    };
    if matches!(
        v.kind(),
        "element_value_array_initializer" | "array_initializer"
    ) {
        let mut c = v.walk();
        v.named_children(&mut c).collect()
    } else {
        vec![v]
    }
}

/// Literal string value, or `?` for anything that is not a plain literal.
fn literal_or_unknown(v: Node, src: &str) -> String {
    let t = text(v, src);
    if v.kind() == "string_literal" && !t.starts_with("\"\"\"") && t.len() >= 2 {
        t[1..t.len() - 1].to_string()
    } else {
        "?".to_string()
    }
}

/// Paths of a mapping annotation; one empty path when none is given.
fn annotation_paths(a: Node, src: &str, positional: bool) -> Vec<String> {
    let paths: Vec<String> = annotation_values(a, src, &["value", "path"], positional)
        .into_iter()
        .map(|v| literal_or_unknown(v, src))
        .collect();
    if paths.is_empty() {
        vec![String::new()]
    } else {
        paths
    }
}

pub(crate) fn join_paths(prefix: &str, path: &str) -> String {
    if prefix == "?" || path == "?" {
        return "?".to_string();
    }
    let parts: Vec<&str> = prefix
        .split('/')
        .chain(path.split('/'))
        .filter(|p| !p.is_empty())
        .collect();
    format!("/{}", parts.join("/"))
}

/// Own class-level prefixes, or `None` when the class declares no mapping.
/// An empty list marks an HTTP client.
fn class_prefixes(class: Node, src: &str) -> Option<Vec<String>> {
    let anns = annotation_nodes(class, src);
    // HTTP clients (Feign, `@HttpExchange`) call endpoints, they do not serve
    // them: no prefix means `method_routes` emits nothing.
    if anns
        .iter()
        .any(|(n, _)| matches!(n.as_str(), "FeignClient" | "HttpExchange"))
    {
        return Some(Vec::new());
    }
    for (name, a) in anns {
        if matches!(name.as_str(), "RequestMapping" | "Path") {
            return Some(annotation_paths(a, src, true));
        }
    }
    None
}

fn verb_of(name: &str) -> Option<Verb> {
    Some(match name {
        "GET" | "Get" => Verb::Get,
        "POST" | "Post" => Verb::Post,
        "PUT" | "Put" => Verb::Put,
        "DELETE" | "Delete" => Verb::Delete,
        "PATCH" | "Patch" => Verb::Patch,
        _ => return None,
    })
}

/// Endpoints served by a method, without class prefix: Spring mappings or
/// JAX-RS `@Path` + verb.
fn method_routes(m: Node, src: &str) -> Vec<Route> {
    let anns = annotation_nodes(m, src);
    let mut routes: Vec<(Verb, Vec<String>)> = Vec::new();
    let jaxrs_path = anns.iter().find(|(n, _)| n == "Path").map(|(_, a)| *a);
    for (name, a) in &anns {
        if let Some(v) = name.strip_suffix("Mapping").and_then(verb_of) {
            routes.push((v, annotation_paths(*a, src, true)));
        } else if name == "RequestMapping" {
            let methods = annotation_values(*a, src, &["method"], false);
            // Absent `method` means any verb; values that are not a known
            // verb (constant, HEAD, OPTIONS) yield no endpoint, not a guess.
            let verbs: Vec<Verb> = if methods.is_empty() {
                vec![Verb::Any]
            } else {
                methods
                    .into_iter()
                    .filter_map(|v| {
                        let t = text(v, src);
                        verb_of(t.rsplit('.').next().unwrap_or(t))
                    })
                    .collect()
            };
            let paths = annotation_paths(*a, src, true);
            for v in verbs {
                routes.push((v, paths.clone()));
            }
        } else if name.chars().all(|c| c.is_ascii_uppercase())
            && let Some(v) = verb_of(name)
        {
            let paths =
                jaxrs_path.map_or_else(|| vec![String::new()], |p| annotation_paths(p, src, true));
            routes.push((v, paths));
        }
    }
    let mut out: Vec<Route> = Vec::new();
    for (verb, paths) in routes {
        for p in &paths {
            let e = Route {
                verb,
                path: join_paths("", p),
            };
            if !out.contains(&e) {
                out.push(e);
            }
        }
    }
    out
}

fn collect_types(n: Node, src: &str, out: &mut Vec<String>) {
    if let Some(name) = type_name(n, src) {
        out.push(name);
        return;
    }
    let mut cur = n.walk();
    for c in n.named_children(&mut cur) {
        collect_types(c, src, out);
    }
}

fn arity(params: Option<Node>) -> usize {
    params.map_or(0, |p| {
        let mut cur = p.walk();
        p.named_children(&mut cur)
            .filter(|c| matches!(c.kind(), "formal_parameter" | "spread_parameter"))
            .count()
    })
}

struct Ctx<'a> {
    src: &'a str,
    package: Option<String>,
    types: Vec<TypeDecl>,
    /// Field receivers of the enclosing types, outermost first.
    fields: Vec<rpc::Env>,
    /// Inside a top-level interface (GWT service/async twins): no RPC calls.
    in_interface: bool,
}

impl Ctx<'_> {
    fn type_decl(&mut self, node: Node, container: Option<&str>) {
        let interface = match node.kind() {
            "class_declaration" | "record_declaration" | "enum_declaration" => false,
            "interface_declaration" | "annotation_type_declaration" => true,
            _ => return,
        };
        let Some(name_node) = node.child_by_field_name("name") else {
            return;
        };
        let name = text(name_node, self.src);
        let id = match (container, &self.package) {
            (Some(c), _) => format!("{c}${name}"),
            (None, Some(p)) => format!("{p}.{name}"),
            (None, None) => name.to_string(),
        };
        let own = class_prefixes(node, self.src);
        let client = own.as_ref().is_some_and(Vec::is_empty);
        let mut supertypes = Vec::new();
        let mut cur = node.walk();
        for c in node.children(&mut cur) {
            if matches!(
                c.kind(),
                "superclass" | "super_interfaces" | "extends_interfaces"
            ) {
                collect_types(c, self.src, &mut supertypes);
            }
        }
        self.types.push(TypeDecl {
            id: id.clone(),
            name: name.to_string(),
            interface,
            name_pos: pos_of(name_node),
            supertypes,
            prefixes: own.filter(|p| !p.is_empty()),
            methods: Vec::new(),
        });
        let index = self.types.len() - 1;

        let saved_iface = self.in_interface;
        if container.is_none() {
            self.in_interface = interface;
        }
        if let Some(body) = node.child_by_field_name("body") {
            self.fields.push(rpc::field_env(body, self.src));
            self.members(body, &id, index, client);
            self.fields.pop();
        }
        self.in_interface = saved_iface;
    }

    fn members(&mut self, body: Node, tid: &str, index: usize, client: bool) {
        let mut cur = body.walk();
        for m in body.named_children(&mut cur) {
            match m.kind() {
                "enum_body_declarations" => self.members(m, tid, index, client),
                "class_declaration"
                | "interface_declaration"
                | "enum_declaration"
                | "record_declaration"
                | "annotation_type_declaration" => self.type_decl(m, Some(tid)),
                "method_declaration" | "constructor_declaration" => {
                    let Some(n) = m.child_by_field_name("name") else {
                        continue;
                    };
                    let constructor = m.kind() == "constructor_declaration";
                    let mut rpc_calls = Vec::new();
                    if !self.in_interface {
                        rpc::collect(m, self.src, &self.fields, &mut rpc_calls);
                    }
                    let routes = if constructor || client {
                        Vec::new()
                    } else {
                        method_routes(m, self.src)
                    };
                    self.types[index].methods.push(MethodDecl {
                        name: text(n, self.src).to_string(),
                        arity: arity(m.child_by_field_name("parameters")),
                        constructor,
                        name_pos: pos_of(n),
                        routes,
                        rpc: rpc_calls,
                    });
                }
                _ => {}
            }
        }
    }
}

/// Reads one Java source. `file` is the path stored in the facts.
pub(crate) fn parse_source(file: &str, src: &str) -> anyhow::Result<FileFacts> {
    let mut parser = Parser::new();
    parser
        .set_language(&tree_sitter_java::LANGUAGE.into())
        .context("loading Java grammar")?;
    let tree = parser
        .parse(src, None)
        .ok_or_else(|| anyhow!("tree-sitter failed to parse {file}"))?;
    let root = tree.root_node();

    let mut package = None;
    let mut imports = Vec::new();
    let mut cur = root.walk();
    for n in root.children(&mut cur) {
        match n.kind() {
            "package_declaration" => {
                let mut c = n.walk();
                package = n
                    .named_children(&mut c)
                    .find(|x| matches!(x.kind(), "scoped_identifier" | "identifier"))
                    .map(|x| text(x, src).to_string());
            }
            "import_declaration" => {
                let mut c = n.walk();
                let is_static = n.children(&mut c).any(|x| x.kind() == "static");
                let mut c = n.walk();
                let has_star = n.children(&mut c).any(|x| x.kind() == "asterisk");
                let mut c = n.walk();
                let path = n
                    .named_children(&mut c)
                    .find(|x| matches!(x.kind(), "scoped_identifier" | "identifier"))
                    .map(|x| text(x, src).to_string());
                if let (false, Some(p)) = (is_static, path) {
                    imports.push(if has_star { format!("{p}.*") } else { p });
                }
            }
            _ => {}
        }
    }

    let mut ctx = Ctx {
        src,
        package: package.clone(),
        types: Vec::new(),
        fields: Vec::new(),
        in_interface: false,
    };
    let mut cur = root.walk();
    for n in root.children(&mut cur) {
        ctx.type_decl(n, None);
    }
    Ok(FileFacts {
        file: file.to_string(),
        package,
        imports,
        types: ctx.types,
    })
}