Skip to content

add java analysis tutorial #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions tutorial/notebook/java_analysis.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Java 分析示例\n",
"这是一个使用CodeFuse-Query分析java项目的教程。在教程中,你将体验到使用命令行工具对代码仓库进行数据化,然后使用Godel语言来分析这个仓库。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"STEP 0: 克隆要分析的仓库。我们以蚂蚁集团的 SOFABolt 项目为例。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!git clone https://github.com/sofastack/sofa-bolt.git"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"STEP 1: 代码数据化。使用 `sparrow database create` 命令创建一个db文件,需要指定待分析的仓库地址,分析的语言,以及db文件的存储路径。该文件存储着代码仓库的结构化数据,之后的分析就是针对这份数据进行。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!sparrow database create --source-root sofa-bolt --data-language-type java --output ./db/sofa-bolt"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"STEP 2: 使用Godel分析语言分析db文件。在本教程中,可以点击代码左侧的执行按钮,直接运行分析脚本。在命令行中,你可以使用 `sparrow query run` 命令来执行查询脚本,具体可以使用`sparrow query run -h` 来获取详细参数信息。\n",
"在这个例子中,我们使用Godel语言查询了全仓库的类方法。你也可以在where子句中加入过滤条件,获取你想要的结果。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%db db/sofa-bolt\n",
"// script\n",
"use coref::java::{Callable, Class, Interface, JavaDB}\n",
"\n",
"fn db() -> JavaDB {\n",
" return JavaDB::load(\"coref_java_src.db\")\n",
"}\n",
"\n",
"query class_method from\n",
" m in Callable(db()),\n",
" c in Class(db())\n",
"where\n",
" c.id = m.getBelongedClass().id\n",
"select\n",
" c.getQualifiedName() as className,\n",
" m.getName() as methodName,\n",
" m.getSignature() as methodSignature"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Godel kernel",
"language": "rust",
"name": "godel-jupyter"
},
"language_info": {
"file_extension": ".gdl",
"help_links": [
{
"text": "Godel kernel Magics",
"url": "https://sparrow.alipay.com"
}
],
"mimetype": "text/rust",
"name": "rust",
"version": "0.0.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}