{"id":20601762,"url":"https://github.com/c2nes/javalang","last_synced_at":"2025-05-15T03:07:43.331Z","repository":{"id":4857698,"uuid":"6012320","full_name":"c2nes/javalang","owner":"c2nes","description":"Pure Python Java parser and tools","archived":false,"fork":false,"pushed_at":"2023-09-17T20:29:07.000Z","size":134,"stargazers_count":772,"open_issues_count":92,"forks_count":162,"subscribers_count":18,"default_branch":"master","last_synced_at":"2025-05-11T17:37:40.711Z","etag":null,"topics":["java","library","parser","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"jclouds/jclouds-labs-google","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/c2nes.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2012-09-29T19:21:02.000Z","updated_at":"2025-05-08T18:54:11.000Z","dependencies_parsed_at":"2024-06-18T12:39:47.425Z","dependency_job_id":"439da1b3-e014-4fb3-b76e-0a06805ddcab","html_url":"https://github.com/c2nes/javalang","commit_stats":{"total_commits":144,"total_committers":16,"mean_commits":9.0,"dds":0.5833333333333333,"last_synced_commit":"566963547575e93d305871d9cb26ce47ff1a036e"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/c2nes%2Fjavalang","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/c2nes%2Fjavalang/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/c2nes%2Fjavalang/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/c2nes%2Fjavalang/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/c2nes","download_url":"https://codeload.github.com/c2nes/javalang/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254264769,"owners_count":22041794,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["java","library","parser","python"],"created_at":"2024-11-16T09:11:56.120Z","updated_at":"2025-05-15T03:07:38.320Z","avatar_url":"https://github.com/c2nes.png","language":"Python","readme":"\n========\njavalang\n========\n\n.. image:: https://travis-ci.org/c2nes/javalang.svg?branch=master\n  :target: https://travis-ci.org/c2nes/javalang\n\n.. image:: https://badge.fury.io/py/javalang.svg\n    :target: https://badge.fury.io/py/javalang\n\njavalang is a pure Python library for working with Java source\ncode. javalang provides a lexer and parser targeting Java 8. The\nimplementation is based on the Java language spec available at\nhttp://docs.oracle.com/javase/specs/jls/se8/html/.\n\nThe following gives a very brief introduction to using javalang.\n\n---------------\nGetting Started\n---------------\n\n.. code-block:: python\n\n    \u003e\u003e\u003e import javalang\n    \u003e\u003e\u003e tree = javalang.parse.parse(\"package javalang.brewtab.com; class Test {}\")\n\nThis will return a ``CompilationUnit`` instance. This object is the root of a\ntree which may be traversed to extract different information about the\ncompilation unit,\n\n.. code-block:: python\n\n    \u003e\u003e\u003e tree.package.name\n    u'javalang.brewtab.com'\n    \u003e\u003e\u003e tree.types[0]\n    ClassDeclaration\n    \u003e\u003e\u003e tree.types[0].name\n    u'Test'\n\nThe string passed to ``javalang.parse.parse()`` must represent a complete unit\nwhich simply means it should represent a complete, valid Java source file. Other\nmethods in the ``javalang.parse`` module allow for some smaller code snippets to\nbe parsed without providing an entire compilation unit.\n\nWorking with the syntax tree\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\n``CompilationUnit`` is a subclass of ``javalang.ast.Node``, as are its\ndescendants in the tree. The ``javalang.tree`` module defines the different\ntypes of ``Node`` subclasses, each of which represent the different syntaxual\nelements you will find in Java code. For more detail on what node types are\navailable, see the ``javalang/tree.py`` source file until the documentation is\ncomplete.\n\n``Node`` instances support iteration,\n\n.. code-block:: python\n\n    \u003e\u003e\u003e for path, node in tree:\n    ...     print path, node\n    ... \n    () CompilationUnit\n    (CompilationUnit,) PackageDeclaration\n    (CompilationUnit, [ClassDeclaration]) ClassDeclaration\n\nThis iteration can also be filtered by type,\n\n.. code-block:: python\n\n    \u003e\u003e\u003e for path, node in tree.filter(javalang.tree.ClassDeclaration):\n    ...     print path, node\n    ... \n    (CompilationUnit, [ClassDeclaration]) ClassDeclaration\n\n---------------\nComponent Usage\n---------------\n\nInternally, the ``javalang.parse.parse`` method is a simple method which creates\na token stream for the input, initializes a new ``javalang.parser.Parser``\ninstance with the given token stream, and then invokes the parser's ``parse()``\nmethod, returning the resulting ``CompilationUnit``. These components may be\nalso be used individually.\n\nTokenizer\n^^^^^^^^^\n\nThe tokenizer/lexer may be invoked directly be calling ``javalang.tokenizer.tokenize``,\n\n.. code-block:: python\n\n    \u003e\u003e\u003e javalang.tokenizer.tokenize('System.out.println(\"Hello \" + \"world\");')\n    \u003cgenerator object tokenize at 0x1ce5190\u003e\n\nThis returns a generator which provides a stream of ``JavaToken`` objects. Each\ntoken carries position (line, column) and value information,\n\n.. code-block:: python\n\n    \u003e\u003e\u003e tokens = list(javalang.tokenizer.tokenize('System.out.println(\"Hello \" + \"world\");'))\n    \u003e\u003e\u003e tokens[6].value\n    u'\"Hello \"'\n    \u003e\u003e\u003e tokens[6].position\n    (1, 19)\n\nThe tokens are not directly instances of ``JavaToken``, but are instead\ninstances of subclasses which identify their general type,\n\n.. code-block:: python\n\n    \u003e\u003e\u003e type(tokens[6])\n    \u003cclass 'javalang.tokenizer.String'\u003e\n    \u003e\u003e\u003e type(tokens[7])\n    \u003cclass 'javalang.tokenizer.Operator'\u003e\n\n\n**NOTE:** The shift operators ``\u003e\u003e`` and ``\u003e\u003e\u003e`` are represented by multiple\n``\u003e`` tokens. This is because multiple ``\u003e`` may appear in a row when closing\nnested generic parameter/arguments lists. This abiguity is instead resolved by\nthe parser.\n\nParser\n^^^^^^\n\nTo parse snippets of code, a parser may be used directly,\n\n.. code-block:: python\n\n    \u003e\u003e\u003e tokens = javalang.tokenizer.tokenize('System.out.println(\"Hello \" + \"world\");')\n    \u003e\u003e\u003e parser = javalang.parser.Parser(tokens)\n    \u003e\u003e\u003e parser.parse_expression()\n    MethodInvocation\n\nThe parse methods are designed for incremental parsing so they will not restart\nat the beginning of the token stream. Attempting to call a parse method more\nthan once will result in a ``JavaSyntaxError`` exception.\n\nInvoking the incorrect parse method will also result in a ``JavaSyntaxError``\nexception,\n\n.. code-block:: python\n\n    \u003e\u003e\u003e tokens = javalang.tokenizer.tokenize('System.out.println(\"Hello \" + \"world\");')\n    \u003e\u003e\u003e parser = javalang.parser.Parser(tokens)\n    \u003e\u003e\u003e parser.parse_type_declaration()\n    Traceback (most recent call last):\n      File \"\u003cstdin\u003e\", line 1, in \u003cmodule\u003e\n      File \"javalang/parser.py\", line 336, in parse_type_declaration\n        return self.parse_class_or_interface_declaration()\n      File \"javalang/parser.py\", line 353, in parse_class_or_interface_declaration\n        self.illegal(\"Expected type declaration\")\n      File \"javalang/parser.py\", line 122, in illegal\n        raise JavaSyntaxError(description, at)\n    javalang.parser.JavaSyntaxError\n\nThe ``javalang.parse`` module also provides convenience methods for parsing more\ncommon types of code snippets.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fc2nes%2Fjavalang","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fc2nes%2Fjavalang","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fc2nes%2Fjavalang/lists"}