Java中Parser的用法

前言 以下翻译自官网的书 入门-Start JavaParser Class 用途:把Java源码转换成 JavaParser定义的Statement对象 Eg

前言

以下翻译自官网的书

入门-Start

JavaParser Class

用途:把Java源码转换成 JavaParser定义的Statement对象

Eg:

Statement expression = JavaParser.parseStatement("int a=0;");

CompilationUnit Class

用途:是一个完整的类文件的表示
在AST中,你可以把这个类看成是AST的根节点

Visitor Classes

用途:用于找到某个类型的节点

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方法能够将FILE_PATH所定义的类文件编译成CompilationUnit(可以理解为AST树的根节点,存有被编译的源代码的一切信息),接下来我们就可以通过遍历CompilationUnit来获得我们想要得到的信息啦!

Comments

一个节点(node)只能有一个comment
可以在他的comment域中访问

comment的类型

  • LineComment
  • BlockComment
  • JavadocComment

Orphan Comment:
不能归为AST树中的某一个节点的Comment

Pretty Printing and Lexical Preservation

Pretty Printing: 打印出格式化的代码
Lexical Preservation: 原来的代码什么样,现在的代码就什么样

Javaparser-Solving Symbols and References

JavaParser开发者教程

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. 是类内定义的,还是类外定义的…),因此JavaSymbolSolver有这样几种类型去确定类来自哪里,

简单来说下面这些类就是 JavaSymbolSolver查找类的方式

TypeSolver的类型

类型

功能

JarTypeSolver

在.jar文件中寻找类,我们需要传入一个.jar文件的位置

JavaParserTypeSolve

在souce file中寻找文件,我们只需传入根目录即可

ReflectionTypeSolver

一些类作为语言的一部分被定义,比如 java.lang.Object

MemoryTypeSolver

简单地返回我们记录的文件,多用于测试

CombinedTypeSolver

将几个不同的solver合并成一个

其他:

类(com.github.javaparser.symbolsolver)

功能

JavaSymbolSolver

创建后插入CompilationUnit可以创建符号解析
使用方法:ParserConfiguration.setSymbolResolver(SymbolResolver)

Ex1: 获取变量(引用)的类型

public class GetTypeOfReference {
private static final String FILE_PATH = "src/main/java/org/javaparser/exampl

4 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

这个例子应用如下的场景:
解析一个变量的类型是来自哪里

懒得截代码了,大家自己找官网的pdf看吧

Ex4 Resolving method calls

可以知道一个method调用的对应的函数签名(signature)

Ex5 Using the Combined Type Solver

使用结合的type solver

Ex6 Using the MemoryTypeSolver JavaSymbolSolver

JavaSymbolSolver

  • combinedsolver

到此这篇关于Java Parser使用指南的文章就介绍到这了,更多相关Java Parser使用内容请搜索好代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持好代码网!

标签: Java Parser