栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Eclipse AST

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Eclipse AST

我不会将整个源代码发布到这个问题上,因为它很长,但是我会让人们开始。

您将需要的所有文档都在这里:http :
//publib.boulder.ibm.com/infocenter/iadthelp/v6r0/index.jsp?topic=/org.eclipse.jdt.doc.isv/reference/api/org
/eclipse/jdt/core/dom/package-
summary.html


document document = new document("import java.util.List;nnclass Xn{nntpublic void deleteme()nt{nt}nn}n");ASTParser parser = ASTParser.newParser(AST.JLS3);parser.setSource(document.get().toCharArray());CompilationUnit cu = (CompilationUnit)parser.createAST(null);cu.recordModifications();

这将根据您传入的源代码为您创建一个编译单元。

现在,这是一个简单的函数,它按照您所传递的内容打印出类定义内的所有方法:

List<AbstractTypeDeclaration> types = cu.types();for(AbstractTypeDeclaration type : types) {    if(type.getNodeType() == ASTNode.TYPE_DECLARATION) {        // Class def found        List<BodyDeclaration> bodies = type.bodyDeclarations();        for(BodyDeclaration body : bodies) { if(body.getNodeType() == ASTNode.METHOD_DECLARATION) {     MethodDeclaration method = (MethodDeclaration)body;     System.out.println("method declaration: ");     System.out.println("name: " + method.getName().getFullyQualifiedName());     System.out.println("modifiers: " + method.getModifiers());     System.out.println("return type: " + method.getReturnType2().toString()); }        }    }}

这应该使您入门。

确实需要一些时间来适应这一点(以我为例)。但这确实有效,并且是我可以尝试的最佳方法。

祝好运 ;)

ExtremeCoder


编辑:

在我忘记之前,这些是我用来完成此工作的导入文件(我花了很多时间来整理这些文件):

org.eclipse.jdt.core_xxxx.jarorg.eclipse.core.resources_xxxx.jarorg.eclipse.core.jobs_xxxx.jarorg.eclipse.core.runtime_xxxx.jarorg.eclipse.core.contenttype_xxxx.jarorg.eclipse.equinox.common_xxxx.jarorg.eclipse.equinox.preferences_xxxx.jarorg.eclipse.osgi_xxxx.jarorg.eclipse.text_xxxx.jar

其中xxxx表示版本号。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/407316.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号