Skip to content

Java

JavaParser

Bases: ParserBase

Java specific parser. Uses a generic grammar for multiple versions of java. It uses tree_sitter.

Source code in src/parser/languages/java.py
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
class JavaParser(ParserBase, lang=Extension.java.name):
    """
    Java specific parser. Uses a generic grammar for multiple versions of java. It uses tree_sitter.
    """

    def __init__(self, library_path: Path | str):
        super().__init__(library_path)
        self.language: Language = Language(tsjava.language())
        self.parser: Parser = Parser(self.language)
        self.identifiers_pattern: str = """
                                        ((identifier) @identifier)
                                        ((type_identifier) @type)
                                        """
        self.identifiers_query = self.language.query(self.identifiers_pattern)

        self.keywords = set()

        self.package_pattern = """
                               (package_declaration ((
                                                    scoped_identifier scope: (
                                                        scoped_identifier scope: (identifier) name: (identifier)
                                                    ) 
                                                    name: (identifier)
                                                    )) @package
                               )
                             """

        self.package_query = self.language.query(self.package_pattern)

    def get_package(self, file: Path, code: bytes, tree: Tree) -> str:
        package = self.package_query.captures(tree.root_node)
        if package:
            return self.get_node_text(code, package)[0]

        return '.'