• 介绍

JDT(Java Development Tools)是Eclipse中有关Java开发的一个组件,该组件中封装了大多数Java开发中的功能,也可以说它是Eclipse支持Java开发的核心组件。 本文将会介绍到:AST(Abstract Syntax Tree)、IProject、IJavaProject、IType等内容。 想了解更多Eclipse中的组件,可以在本站中搜索“Eclipse框架下的组件”。 下面先对几个重要的接口进行介绍。

  • AST

JDT组件使用AST将Java中的对象(接口、类、方法、属性等)建立了一系列的模型。通过AST可以把Java对象当做一个树,用访问节点的方式来修改。

  • IJavaElement

[caption id="attachment_4314" align="aligncenter" width="217"] ijavaelement_tree[/caption] 从上图中可以看到org.eclipse.jdt.core.IJavaElement接口是所有Java对象模型中的父,而且JDT中包含了所有的Java对象。 IType代表一个类 IMethod代表一个方法 IAnnotation代表一个注解 下面是一个可以创建一个Java类型的例子: [codesyntax lang="java"]

/*** 创建一个类(class)或者接口(interface)* @param project 工程对象* @param type 应当是class、interface或者enum* @param packageName 包名* @param typeName 应当是Java源文件名称但不包含后缀,例如:Test.java就应该是Test* @param superCls 父类* @param superInters 接口列表* @return*/public static ICompilationUnit createCompilationUnit(IJavaProject project, String type,String packageName, String typeName, IProgressMonitor monitor, String superCls, String ...superInters) {IPackageFragmentRoot packageFragmentRoot = null;ICompilationUnit unit = null;try {//先查找当前工程中是否已经有了给类型IType targetType = project.findType(packageName, typeName, monitor);if(targetType != null){unit = targetType.getCompilationUnit();}} catch (JavaModelException e) {Logger.error(e.getMessage(), e);}try {if(unit == null){try {//查找可以用于创建Java类型的包IPackageFragmentRoot[] roots = project.getPackageFragmentRoots();for(IPackageFragmentRoot root : roots){if(root.isReadOnly() || root.isArchive() || root.isExternal()){continue;}packageFragmentRoot = root;break;}} catch (JavaModelException e) {Logger.error(e.getMessage(), e);}IPackageFragment packageFragment = packageFragmentRoot.getPackageFragment(packageName);//创建一个内容为空的Java类型unit = packageFragment.createCompilationUnit(String.format("%s.java", typeName), "", false, monitor);}unit.becomeWorkingCopy(monitor); //先把当前编译对象变为副本IBuffer buffer = unit.getBuffer();String simpleTypeStub = constructSimpleTypeStub(type, typeName, superCls, superInters);String lineDelimiter = System.getProperty("line.separator", "\n");String cuContent = constructCUContent(unit, simpleTypeStub, lineDelimiter, monitor);buffer.setContents(cuContent);return unit;} catch (JavaModelException e) {Logger.error(String.format("创建Java过程失败,详细信息[%s]。", e.getMessage()), e);} catch (CoreException e) {Logger.error(e.getMessage(), e);}return unit;}public static String constructSimpleTypeStub(String typeName) {return constructSimpleTypeStub("class", typeName, null);}public static String constructSimpleTypeStub(String type, String typeName, String superCls, String ...superInters) {StringBuffer buf = new StringBuffer("public ");buf.append(type);buf.append(" ");buf.append(typeName);if(StringUtil.isNotEmpty(superCls)) {buf.append(" extends ");buf.append(superCls);}if(superInters != null && superInters.length > 0) {buf.append(" implements");for(String superInter : superInters) {buf.append(" ");buf.append(superInter);}}buf.append("{\n}");return buf.toString();}/*** 构建Java类型的具体文件内容* @param cu* @param typeContent* @param lineDelimiter* @return* @throws CoreException*/public static String constructCUContent(ICompilationUnit cu, String typeContent, String lineDelimiter,IProgressMonitor monitor) throws CoreException {String fileComment = "";// getFileComment(cu, lineDelimiter);String typeComment = "";// getTypeComment(cu, lineDelimiter);IPackageFragment pack = (IPackageFragment) cu.getParent();String content = CodeGeneration.getCompilationUnitContent(cu, fileComment, typeComment, typeContent, lineDelimiter);if (content != null) {ASTParser parser = ASTParser.newParser(AST.JLS8);parser.setProject(cu.getJavaProject());parser.setSource(content.toCharArray());CompilationUnit unit = (CompilationUnit) parser.createAST(monitor);if (((pack.isDefaultPackage()) || (unit.getPackage() != null)) && (!unit.types().isEmpty())) {return content;}}StringBuffer buf = new StringBuffer();if (!pack.isDefaultPackage()) {buf.append("package ").append(pack.getElementName()).append(';');}buf.append(lineDelimiter).append(lineDelimiter);if (typeComment != null) {buf.append(typeComment).append(lineDelimiter);}buf.append(typeContent);return buf.toString();}

[/codesyntax]

  • IPackageFragmentRoot

IPackageFragment createPackageFragment(String name, boolean force, IProgressMonitor monitor)throws JavaModelException; 该方法可以创建一个包(package),创建完之后需要调用接口IPackageFragment的保存(save)方法。

  • IParent

[caption id="attachment_4316" align="aligncenter" width="300"] iparent_tree[/caption] 从上图中可以看到org.eclipse.jdt.core.IJavaProject并不是接口org.eclipse.core.resources.IProject的子接口,这一点一定要注意——IJavaProject和IProject是包含关系。

  • IContainer

[caption id="attachment_4317" align="aligncenter" width="300"] icontainer_tree[/caption] 从上图中可以看到,在Eclipse中文件夹(IFolder)和工程(IProject)以及工作空间(IWorkspaceRoot)是同等级的,都是属于容器(容器也是一种资源)。

  • IResource

[caption id="attachment_4315" align="aligncenter" width="300"] iresource_tree[/caption] org.eclipse.core.resources.IResource接口是Eclipse中所有资源(文件、文件夹、工程、workspace等)的父。

  • IProject

project.refreshLocal(IResource.DEPTH_INFINITE, monitor); 该方法可以刷新当前工程 未完待续。。。

转载于:https://my.oschina.net/surenpi/blog/816608

Eclipse插件开发JDT组件介绍相关推荐

  1. eclipse插件开发资料 参考代码

    首页 zhangxinzhou 的BLOG 写留言 邀请进圈子 发消息 加友情链接 进家园 加好友 2011年度IT博客大赛 十大杰出IT博客评选中 博客统计信息 用户名:zhangxinzhou 文 ...

  2. 【插件开发】—— 1 Eclipse插件开发导盲

    在真正接触eclipse插件开发一个月后,对插件的开发过程以及技术要求,也有了一定的了解.遥想之前像无头苍蝇一样乱撞乱学,真心觉得浪费了不少时间.这里就总结一下学习的主要过程以及需要的资料. Ecli ...

  3. Eclipse 插件开发遇到问题心得总结

    Eclipse 插件开发遇到问题心得总结 Posted on 2011-07-17 00:51 季枫 阅读(3997) 评论(0) 编辑 收藏 1.Eclipse 中插件开发多语言的实现 为了使用 . ...

  4. android的应用组件,跟我学android-Android应用基本组件介绍(五)

    Activity activity 是最基本的模块,我们成为活动,一个activity通常就是一个单独的屏幕,每一个活动都被实现为一个独立的类,且都继承活动的基类.在activity的实现类里显示用户 ...

  5. 客快物流大数据项目(六):Docker与虚拟机的形象比喻及组件介绍

    目录 Docker与虚拟机的形象比喻及组件介绍 一.Docker与虚拟机的形象比喻

  6. 2021年大数据Hadoop(二十六):YARN三大组件介绍

    全网最详细的Hadoop文章系列,强烈建议收藏加关注! 后面更新文章都会列出历史文章目录,帮助大家回顾知识重点. 目录 本系列历史文章 前言 Yarn三大组件介绍 ResourceManager No ...

  7. 【Netty】Netty组件介绍

    Netty组件介绍 Netty有 Bootstrap/ServerBootstrap,Channel,EventLoop,ChannelFuture, ChannelHandler,ChannelPi ...

  8. c# BackgroundWorker组件介绍(属性、方法、事件)

    c# BackgroundWorker组件介绍(属性.方法.事件) 2008-07-03 16:28 在VS2005中添加了BackgroundWorker组件,该组件在多线程编程方面使用起来非常方便 ...

  9. java hbase创建_hadoop组件介绍及python 连接Hbase

    Ambari Apache Ambari是一种基于Web的工具,支持Hadoop集群的供应.管理和监控.是Apache顶级开源项目之一,由Hortonworks公司开源. Ø 官方网站地址: http ...

最新文章

  1. Spring定时器--时间设置规则
  2. leetcode算法题--连续差相同的数字
  3. linux-java
  4. 第一课 前言 学PHP就是为了做网站
  5. iOS底层探索(二) - 写给小白看的Clang编译过程原理
  6. padodb time.inc.php,怎样实现PHP中ADODB事务处理
  7. Django之ORM字段和参数
  8. PHP基础题带详细答案,PHP基础语法试题(一)答案版.docx
  9. lambda stream 循环_jdk8-lambda-stream的使用
  10. “宽带中国、智慧岳阳”项目启动
  11. [规划酱@国土空间] ArcGIS工具| 三调转换之新用地用海分类
  12. 攒机笔记二十二:台式组装机(2022.9)
  13. 整洁面向对象分层架构 (Clean Object-Oriented and Layered Architecture)
  14. 个人计算机中的防病毒软件无法,为什么无法在计算机上安装360防病毒软件?
  15. 微信小程序创建节点选择器获取宽高wx.createSelectorQuery
  16. dat文件导入cad画图步骤_准确又高效的读入CAD几何——详解Fluent Meshing导入文件的几种方法...
  17. 中缀向后缀转换表达式
  18. 前方高能!githubjava开源项目
  19. xtrabackup备份恢复MySQL数据库
  20. Autofac实现依赖注入

热门文章

  1. 无线路由器无法在计算机上设置,无线路由器设置管理地址无法打开解决方法
  2. 【python爬虫学习】cookie模拟登陆
  3. 看了 web.dev 的 631 篇博客,我总结了这些内容
  4. MongoDB+Node.js+express简单实现数据的提交与回显
  5. 什么是EDM营销?EDM营销和一般营销方式的区别分析
  6. 从《我不是潘金莲》谈程序员的核心竞争力
  7. 潘金莲写给比尔盖茨的情书
  8. 一位博士在华为的 22 年
  9. scratch成语接龙 电子学会图形化编程scratch等级考试四级真题和答案解析2021-9
  10. 怎么用u盘重装系统?石大师u盘重装win10系统步骤