AST对Java文件的结构解析--标记一下
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.FieldDeclaration;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.PackageDeclaration;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
public class TestAST
{
public static void main(String[] args)
{
String content = "";
try
{
File file = new File("G:\\workspaces_base\\plugins_workspaces\\ToolbarDemo\\src\\com\\sxw\\toolbar\\TableViewer5.java");
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String record = null;
int recCount = 0;
while ((record = br.readLine()) != null)
{
recCount++;
// System.out.println("Line " + recCount + ": " + record);
content = content + record;
}
br.close();
fr.close();
}
catch (IOException e)
{
e.printStackTrace();
}
ASTParser parsert = ASTParser.newParser(AST.JLS3);
parsert.setSource(content.toCharArray());
CompilationUnit result = (CompilationUnit) parsert.createAST(null);
AST ast = result.getAST();
List types = result.types();
TypeDeclaration typeDec = (TypeDeclaration) types.get(0);
List importList = result.imports();
PackageDeclaration packetDec = result.getPackage();
String className = typeDec.getName().toString();
MethodDeclaration methodDec[] = typeDec.getMethods();
FieldDeclaration fieldDec[] = typeDec.getFields();
System.out.println("Field:");
for(FieldDeclaration fieldDesc : fieldDec)
{
System.out.println(((VariableDeclarationFragment)fieldDesc.fragments().get(0)).getName().getFullyQualifiedName());
}
System.out.println("Method:");
for (MethodDeclaration method : methodDec)
{
System.out.println(method.getName());
// System.out.println(method.getBody());
Block block = method.getBody();
// 怎样继续下去得到上面代码1的第7行使用了ClassA 类和它的getClassbMethod 方法
}
}
}
分享到:
相关推荐
java开发常用 jdt-compiler-3.1.1.jar
包括以下包:jdt-3.2.1-r321_v20060823.jar、 jdt-3.3.0-v20070607-1300.jar、jdt-3.4.2_r894.jar、jdt-compiler.jar、org.eclipse.datatools.enablement.jdt.classpath_1.0.1.v201107221501.jar、org.eclipse.jdt....
eclipse-JDT-3.2.2 免费下载 完全版eclipse-JDT-3.2.2 免费下载 完全版eclipse-JDT-3.2.2 免费下载 完全版eclipse-JDT-3.2.2 免费下载 完全版eclipse-JDT-3.2.2 免费下载 完全版eclipse-JDT-3.2.2 免费下载 完全版...
jasper-compiler-jdt-5.5.15.jar
安装vim开发环境所需要的包。由于是国外下载,所以非常慢。
eclipse3.0.1 and NLpack-eclipse-JDT-SDK-3.0.x
Itext中文显示,基于iTextAsian.jar ,另外在ireport中也需要用到这个jar包。以及ireport编译以及字体的相关设置均需用到这些jar包。希望能帮到你。
tomcat 源码 ant 下载时用到的eclipse-JDT-3.3.1.zip
eclipse-jdt-3.6.1-6.13.el6.i686.rpm
在原JDT-6型仪器系统基础上,对陀螺测斜定向系统进行改进,包括地面仪数据采集板、绞车控制器等,使数据通讯仅用RS232或USB即可实现,与之配套的软件GyroSProcess可运行于Windows XP等平台。新系统可提高测斜作业效率,并...
Clicy是Eclipse的插件,可生成Java文件AST的图布局。 首先,使用Eclipse JDT(Java开发工具)生成并解析Abstract语法树,然后将其转换为点格式。 在您的eclipse文件夹中写入了一个包含点格式AST的新文件。 到目前...
学习JDT的AST相关网站和资料收集 从入门到理解到使用 相当不错哦
资源来自pypi官网。 资源全名:jdt-0.0.2.linux-x86_64.tar.gz
org.eclipse.jdt.astview_1.1.7.201103211622.jar
The JDT Enablement component extends the Faceted Project Framework to integrate with Java Development Tools. The component includes the Java facet, modeling of the JVM-based runtimes and tools for ...
基于Eclipse-JDT生成Java源代码的AST并可视化
进度条,实现对动画加载的计算,并计算出时间,同步动画播放
实测可用