Java中Parser的用法
前言
以下翻譯自官網(wǎng)的書
入門-Start
JavaParser Class
用途:把Java源碼轉(zhuǎn)換成 JavaParser定義的Statement對象
Eg:
Statement expression = JavaParser.parseStatement("int a=0;");CompilationUnit Class
用途:是一個完整的類文件的表示
在AST中,你可以把這個類看成是AST的根節(jié)點
Visitor Classes
用途:用于找到某個類型的節(jié)點
A Simple Visitor
package com.github.javaparser;
import com.github.javaparser.*;
import com.github.javaparser.ast.*;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.EnumDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.symbolsolver.JavaSymbolSolver;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import com.github.javaparser.utils.SourceRoot;
import java.io.File;
public class VoidVisitorStarter {
private static final String FILE_PATH = "ReversePolishNotation.java";
public static void main(String[] args) throws Exception {
CompilationUnit cu = JavaParser.parse(new File(FILE_PATH));
}
}上面的parse方法能夠?qū)ILE_PATH所定義的類文件編譯成CompilationUnit(可以理解為AST樹的根節(jié)點,存有被編譯的源代碼的一切信息),接下來我們就可以通過遍歷CompilationUnit來獲得我們想要得到的信息啦!
Comments
一個節(jié)點(node)只能有一個comment
可以在他的comment域中訪問
comment的類型
- LineComment
- BlockComment
- JavadocComment
Orphan Comment:
不能歸為AST樹中的某一個節(jié)點的Comment
Pretty Printing and Lexical Preservation
Pretty Printing: 打印出格式化的代碼
Lexical Preservation: 原來的代碼什么樣,現(xiàn)在的代碼就什么樣
Javaparser-Solving Symbols and References
Symbol的定義:
Java code中的所有name都可以稱作為symbol
TypeSolver的作用:
確定尋找類的位置
原文:What the hell is a Type Solver It is the object which knows where to look for classes. When processing source code you will typically have references to code that is not yet compiled, but it is just present in other source files. You could also use classes contained in JARs or classes from the Java standard libraries. You have just to tell to your TypeSolver where to look for classes and it will figure it out.
由于在解析symbol的時候,我們需要知道這個symbol來自哪里(i.e. 是類內(nèi)定義的,還是類外定義的…),因此JavaSymbolSolver有這樣幾種類型去確定類來自哪里,
簡單來說下面這些類就是 JavaSymbolSolver查找類的方式
TypeSolver的類型:
類型
功能
JarTypeSolver
在.jar文件中尋找類,我們需要傳入一個.jar文件的位置
JavaParserTypeSolve
在souce file中尋找文件,我們只需傳入根目錄即可
ReflectionTypeSolver
一些類作為語言的一部分被定義,比如 java.lang.Object
MemoryTypeSolver
簡單地返回我們記錄的文件,多用于測試
CombinedTypeSolver
將幾個不同的solver合并成一個
其他:
類(com.github.javaparser.symbolsolver)
功能
JavaSymbolSolver
創(chuàng)建后插入CompilationUnit可以創(chuàng)建符號解析
使用方法:ParserConfiguration.setSymbolResolver(SymbolResolver)
Ex1: 獲取變量(引用)的類型
public class GetTypeOfReference {
private static final String FILE_PATH = "src/main/java/org/javaparser/exampl4 es/chapter5/Bar.java";
public static void main(String[] args) throws FileNotFoundException {
TypeSolver typeSolver = new CombinedTypeSolver();
JavaSymbolSolver symbolSolver = new JavaSymbolSolver(typeSolver);
JavaParser.getStaticConfiguration().setSymbolResolver(symbolSolver);
CompilationUnit cu = JavaParser.parse(new File(FILE_PATH));
cu.findAll(AssignExpr.class).forEach(ae -> {
ResolvedType resolvedType = ae.calculateResolvedType();
System.out.println(ae.toString() + " is a: " + resolvedType);
});
}
}Ex2: 使用absolute name直接解析類型
package com.github.awesomelemon;
import com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration;
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
public class UsingTypeSolver {
private static void showReferenceTypeDeclaration(
ResolvedReferenceTypeDeclaration resolvedReferenceTypeDeclaration) {
System.out.println(String.format("== %s ==", resolvedReferenceTypeDeclaration.getQualifiedName()));
System.out.println(" fields:");
resolvedReferenceTypeDeclaration.getAllFields()
.forEach(f -> System.out.println(String.format(" %s %s", f.getType(), f.getName())));
System.out.println(" methods:");
resolvedReferenceTypeDeclaration.getAllMethods()
.forEach(m -> System.out.println(String.format(" %s", m.getQualifiedSignature())));
System.out.println();
}
public static void main(String[] args) {
TypeSolver typeSolver = new ReflectionTypeSolver();
showReferenceTypeDeclaration(typeSolver.solveType("java.lang.Object"));
showReferenceTypeDeclaration(typeSolver.solveType("java.lang.String"));
showReferenceTypeDeclaration(typeSolver.solveType("java.util.List"));
}
}作用是返回以下三個包里面的 symbol是作為field還是method
- java.lang.Object
- java.lang.String
- java.util.List
Ex3 Resolving a Type in a context
這個例子應(yīng)用如下的場景:
解析一個變量的類型是來自哪里
懶得截代碼了,大家自己找官網(wǎng)的pdf看吧
Ex4 Resolving method calls
可以知道一個method調(diào)用的對應(yīng)的函數(shù)簽名(signature)
Ex5 Using the Combined Type Solver
使用結(jié)合的type solver
Ex6 Using the MemoryTypeSolver JavaSymbolSolver
JavaSymbolSolver
- combinedsolver
到此這篇關(guān)于Java Parser使用指南的文章就介紹到這了,更多相關(guān)Java Parser使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中final關(guān)鍵字和final的四種用法實例
final關(guān)鍵字代表最終的、不可改變的,下面這篇文章主要給大家介紹了關(guān)于Java中final關(guān)鍵字和final的四種用法實例,文中通過圖文以及實例代碼介紹的非常詳細,需要的朋友可以參考下2023-02-02
Spring Security單項目權(quán)限設(shè)計過程解析
這篇文章主要介紹了Spring Security單項目權(quán)限設(shè)計過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11
java線程并發(fā)cyclicbarrier類使用示例
CyclicBarrier類似于CountDownLatch也是個計數(shù)器,不同的是CyclicBarrier數(shù)的是調(diào)用了CyclicBarrier.await()進入等待的線程數(shù),當線程數(shù)達到了CyclicBarrier初始時規(guī)定的數(shù)目時,所有進入等待狀態(tài)的線程被喚醒并繼續(xù),下面使用示例學(xué)習(xí)他的使用方法2014-01-01
Spring Boot結(jié)合ECharts案例演示示例
本文主要主要介紹了Spring Boot結(jié)合ECharts案例演示示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
maven倉庫中心mirrors配置多個下載中心(執(zhí)行最快的鏡像)
這篇文章主要介紹了maven倉庫中心mirrors配置多個下載中心(執(zhí)行最快的鏡像),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07

