https://github.com/igapyon/simple-eclipseastjava
Simple Eclipse AST Parser sample for Java.
https://github.com/igapyon/simple-eclipseastjava
Last synced: about 1 month ago
JSON representation
Simple Eclipse AST Parser sample for Java.
- Host: GitHub
- URL: https://github.com/igapyon/simple-eclipseastjava
- Owner: igapyon
- License: epl-1.0
- Created: 2016-10-23T08:58:23.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2018-04-18T13:02:58.000Z (about 8 years ago)
- Last Synced: 2025-02-24T11:18:51.080Z (over 1 year ago)
- Language: Java
- Size: 7.78 MB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[target](http://www.igapyon.jp/igapyon/diary/2018/ig180417.html)
2018-04-17 diary: Eclipse AST を利用した シンプルな Java ソースコード解析
=====================================================================================================
[](http://www.igapyon.jp/igapyon/diary/memo/memoigapyon.html) [いがぴょん](http://www.igapyon.jp/igapyon/diary/memo/memoigapyon.html)の日記に関連のあるコンテンツ。
## Eclipse AST を利用した シンプルな Java ソースコード解析
Eclipse AST を利用した Java ソースコード解析の最新な状況をメモしておきます。
まず、ここで調べた結果は以下の github リポジトリに反映済みです。
* [https://github.com/igapy...ipseAstJava](https://github.com/igapyon/simple-EclipseAstJava)
### Eclipse AST に必要な jar ライブラリを利用
Eclipse AST を利用するためには、Eclipse AST に関連する jar ライブラリ一式を揃える必要があります。これは Maven リポジトリを利用すると大変簡単に実現することができます。
Maven の依存関係に、以下のように `org.eclipse.jdt.core` を加えます。これだけで、芋づる式に必要な jar ライブラリを利用することが可能になります。
```xml
org.eclipse.jdt
org.eclipse.jdt.core
3.13.102
```
### シンプルな Eclipse AST コード
Eclipse AST に必要なライブラリ一式が組み込めたら、Eclipse AST を利用したパースコードが記述および実行できます。重要な箇所を以下にピックアップします。
```java
import org.apache.commons.io.FileUtils;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;
final ASTParser parser = ASTParser.newParser(AST.JLS10);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(buf.toString().toCharArray());
final CompilationUnit node = (CompilationUnit) parser.createAST(null);
final SimpleVisitor visitor = new SimpleVisitor();
node.accept(visitor);
```
完全なソースコードは [https://github.com/igapy...ipseAstJava](https://github.com/igapyon/simple-EclipseAstJava) にて参照することができます。
### 関連する情報
* [2016-10-22 diary: Eclipse AST を利用した Java ソースコード解析](http://www.igapyon.jp/igapyon/diary/2016/ig161022.html) : 旧バージョン
* [2016-10-23 diary: Eclipse AST を利用した PHP ソースコード解析](http://www.igapyon.jp/igapyon/diary/2016/ig161023.html)