java抽象语法树_【JAVA-JDT-AST】Java抽象语法树的构建、遍历及转成dot格式(附Github源码)...

Background:

最近为了重现tree-based clone detection的论文:L. Jiang, G. Misherghi, Z. Su, and S. Glondu. Deckard: Scalable and accurate tree-based detection of code clones. In Proceedings of ICSE, 2007.

需要对Java class中每个method构建AST,然后将AST转化成dot格式,最后转换成vector(这一步作者已经在Github实现(https://github.com/skyhover/Deckard):执行vdbgen即可)。

通过判断vector之间的相似性来判断代码之间的相似性。

这个过程是tree-based clone detection的核心思想。

首先找一个class文件当例子:

Input : test.java

publicclass test {

int i = 1;

public void testNonEscaped() {

startServer(NonEscapedURIResource.class);

WebResource r = Client.create().resource(getUri().userInfo("x.y").path("x%20y").build());

assertEquals("CONTENT", r.get(String.class));

}

}

Output: test.java_testNonEscaped.dot

digraph "DirectedGraph" {

graph [label = "testNonEscaped", labelloc=t, concentrate = true];

"13329486" [ type=31 line=4 ]

"327177752" [ type=83 line=4 ]

"1458540918" [ type=39 line=4 ]

"1164371389" [ type=42 line=4 ]

"517210187" [ type=8 line=4 ]

"267760927" [ type=21 line=5 ]

"633070006" [ type=32 line=5 ]

"1459794865" [ type=42 line=5 ]

"1776957250" [ type=57 line=5 ]

"1268066861" [ type=43 line=5 ]

"827966648" [ type=42 line=5 ]

"1938056729" [ type=60 line=7 ]

"1273765644" [ type=43 line=7 ]

"701141022" [ type=42 line=7 ]

"1447689627" [ type=59 line=7 ]

"112061925" [ type=42 line=7 ]

"764577347" [ type=32 line=7 ]

"1344645519" [ type=32 line=7 ]

"1234776885" [ type=42 line=7 ]

"540159270" [ type=42 line=7 ]

"422250493" [ type=42 line=7 ]

"1690287238" [ type=32 line=7 ]

"1690254271" [ type=32 line=7 ]

"1440047379" [ type=32 line=7 ]

"343965883" [ type=32 line=7 ]

"230835489" [ type=42 line=7 ]

"280884709" [ type=42 line=7 ]

"1847509784" [ type=45 line=7 ]

"2114650936" [ type=42 line=7 ]

"1635756693" [ type=45 line=7 ]

"504527234" [ type=42 line=7 ]

"101478235" [ type=21 line=8 ]

"540585569" [ type=32 line=8 ]

"1007653873" [ type=42 line=8 ]

"836514715" [ type=45 line=8 ]

"1414521932" [ type=32 line=8 ]

"828441346" [ type=42 line=8 ]

"1899073220" [ type=42 line=8 ]

"555826066" [ type=57 line=8 ]

"174573182" [ type=43 line=8 ]

"858242339" [ type=42 line=8 ]

"13329486" -> "327177752"

"13329486" -> "1458540918"

"13329486" -> "1164371389"

"13329486" -> "517210187"

"517210187" -> "267760927"

"267760927" -> "633070006"

"633070006" -> "1459794865"

"633070006" -> "1776957250"

"1776957250" -> "1268066861"

"1268066861" -> "827966648"

"517210187" -> "1938056729"

"1938056729" -> "1273765644"

"1273765644" -> "701141022"

"1938056729" -> "1447689627"

"1447689627" -> "112061925"

"1447689627" -> "764577347"

"764577347" -> "1344645519"

"1344645519" -> "1234776885"

"1344645519" -> "540159270"

"764577347" -> "422250493"

"764577347" -> "1690287238"

"1690287238" -> "1690254271"

"1690254271" -> "1440047379"

"1440047379" -> "343965883"

"343965883" -> "230835489"

"1440047379" -> "280884709"

"1440047379" -> "1847509784"

"1690254271" -> "2114650936"

"1690254271" -> "1635756693"

"1690287238" -> "504527234"

"517210187" -> "101478235"

"101478235" -> "540585569"

"540585569" -> "1007653873"

"540585569" -> "836514715"

"540585569" -> "1414521932"

"1414521932" -> "828441346"

"1414521932" -> "1899073220"

"1414521932" -> "555826066"

"555826066" -> "174573182"

"174573182" -> "858242339"

}

0f39b19639b5ce0398bc16894440f3fc.png

主要步骤:

1.将Java代码转成AST;

2.重写ASTVisitor中的visit方法根据自己的需要去遍历AST;

3.AST转.dot格式。

主要的类:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AST(Abstract Syntax Tree抽象语法树)是将代码解析为树状结构的一种数据结构,用于表示程序代码的语法结构。在Java中,可以使用第三方库解析动态字符串并生成语法树。 在Java中,常用的AST解析工具是Eclipse JDTJava Development Tools)。JDT是Eclipse平台上的Java开发工具,它提供了解析Java代码并生成AST的能力。 使用JDT解析动态字符串并生成语法树的一般步骤如下: 1. 导入JDT相关的库,例如org.eclipse.jdt.core.dom。 2. 创建一个ASTParser对象,该对象将用于解析字符串并生成AST。 3. 调用setSource方法,将要解析的字符串作为参数传入。 4. 调用createAST方法,该方法将返回一个CompilationUnit对象,表示生成的语法树。 5. 使用CompilationUnit对象进行进一步的操作,例如遍历、分析、修改等。 以下是一个简单示例: ```java import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.ASTParser; import org.eclipse.jdt.core.dom.CompilationUnit; public class ASTParserExample { public static void main(String[] args) { String code = "public class HelloWorld { public static void main(String[] args) { System.out.println(\"Hello, World!\"); } }"; ASTParser parser = ASTParser.newParser(AST.JLS11); parser.setSource(code.toCharArray()); CompilationUnit cu = (CompilationUnit) parser.createAST(null); // 进一步操作语法树,例如遍历、分析、修改等 // 输出语法树 System.out.println(cu.toString()); } } ``` 这段代码会将给定的动态字符串解析为一个表示整个Java程序语法结构的语法树,并输出语法树的字符串表示。 需要注意的是,AST解析是一个复杂的过程,涉及到语法分析和语义分析等步骤。随着程序代码的复杂度增加,可能需要进一步了解AST的使用和相关API,以便更好地处理和分析生成的语法树
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值