大致说下这个功能的思路:
1:用字符串构建java文件
2:在内存中构建ijavaelement对象,并对这个对象进行重组(用到了jdt的OrganizeImportsOperation)
3:IFile写入文件
String path = "";//文件绝对路径
IJavaProject project = null;
IPath genSourceFolderPath = new Path(filePath);
for (IProject p : ResourcesPlugin.getWorkspace().getRoot().getProjects()) {
if (p.isOpen()) {
// 查找工程的实际路径是否在源代码生成路径上,如果是,则就是该工程
if (p.getLocation().isPrefixOf(genSourceFolderPath)) {
project = JavaCore.create(p);
}
}
}
if (project == null) {
throw new AresCompilerException("无法生成源码文件:" + path);
}
IPath javaFilePath = new Path(path).makeRelativeTo(project.getProject().getLocation());
IFile javaFile = project.getProject().getFile(javaFilePath);
IJavaElement element = JavaCore.create(javaFile);
String lineDelimiter = StubUtility.getLineDelimiterUsed(null);
formattedSource = CodeFormatterUtil.format(CodeFormatter.K_COMPILATION_UNIT, source.toString(), 0,
lineDelimiter, (IJavaProject) null);
ASTParser parser = ASTParser.newParser(AST.JLS3);
try {
element = ((ICompilationUnit) element).getWorkingCopy(null);
((ICompilationUnit) element).getBuffer().setContents(formattedSource);
} catch (JavaModelException e) {
e.printStackTrace();
}
parser.setSource((ICompilationUnit)element);
final CompilationUnit node = (CompilationUnit) parser.createAST(new NullProgressMonitor());
OrganizeImportsOperation operation = new OrganizeImportsOperation((ICompilationUnit)element, node, true, false, false, new IChooseImportQuery() {
public TypeNameMatch[] chooseImports(TypeNameMatch[][] openChoices, ISourceRange[] ranges) {
return new TypeNameMatch[0];
}
});
InputStream inputStream = null;
try {
TextEdit textEdit = operation.createTextEdit(null);
JavaModelUtil.applyEdit((ICompilationUnit) element, textEdit, false, new NullProgressMonitor());
formattedSource = ((ICompilationUnit) element).getBuffer().getContents();
inputStream = new ByteArrayInputStream(formattedSource.getBytes(javaFile.getCharset()));
if (!javaFile.exists()) {
ResourcesUtil.safelyCreateFile(javaFile, inputStream, false, new NullProgressMonitor());
}else {
javaFile.setContents(inputStream, IFile.FORCE, new NullProgressMonitor());
}
} catch (Exception e) {
e.printStackTrace();
} finally{
IOUtils.closeQuietly(inputStream);
}
分享到:
相关推荐
eclipse.jdt.core, jdt/核心项目库( eclipse jdt core ) JDT内核这是 Eclipse 开发工具的Java的核心部分。 它包含对编译和使用Java代码的非用户界面支持,包括:可以独立运行或者作为 Eclipse IDE的一部分的增量或者...
eclipse JDT的使用,非常使用的学习资源!!!
eclipse jdt, download from git.
Eclipse JDT AST使用方法,word格式的。Eclipse JDT是eclipse中开发Java程序的一套工具,包括editor,语法高亮等等功能,而其中的AST更是十分优秀的解析Java源代码的工具
NULL 博文链接:https://zhenghuazhi.iteye.com/blog/2090825
Class not found: org.eclipse.jdt.core.JDTCompilerAdapter。 解决方法: 拷贝下面这些jar包到ant_home/lib org.eclipse.jdt.compiler.tool_1.0.100.v_972_R35x.jar org.eclipse.jdt.core_3.5.2.v_981_R35x.jar org...
tomcat 源码 ant 下载时用到的eclipse-JDT-3.3.1.zip
org.eclipse.jdt.* 的目录和文件 全部拷贝到 flexbuilder 的plugin (若为myEclipse则是将myEclipse下eclipse\plugins) flashbuilder也一样 ----下面是在FlexBuilder中配置VSS ------- 1. 首先绑定项目到...
NULL 博文链接:https://wu1g119.iteye.com/blog/2287940
org.eclipse.jdt.core_3.8.3.v20130121-145325.jar (如果你没有,但是装了ecplise,可在 \eclipse\plugins中找到) [尤其是org.eclipse.jdt 这个jar包,不好找,网上下的几个不行。。。最后灵机一动,去ecplise...
the guide of Eclipse JDT, from IBM; 随手填上了积分要求,不知该怎样取消,sigh
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 免费下载 完全版...
1.下载Eclipse plugin 补丁文件 org.eclipse.jdt.core_3.6.1.v_A68_R36x.jar: http://meiyoudao.download.csdn.net/ 2.将解压缩后的jar包 放到Eclipse的目录中的Plugin下面, 3.Android开发时代码提示卡死的问题...
包括以下包: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.core 修改后的Eclipse JDT核心以解析存储库中的Java源代码
jdt2.0教程,版本低了一点,单身可以研究jdt的发展过程.
eclipse 自动生成的get set方法 自动加上文本注释,并且注释内容包含字段中我们加的文档注释
解决Eclipse中使用drool时报Caused by: java.lang.RuntimeException: The Eclipse JDT Core jar is not in the classpath的问题。 详细错误: org.drools.RuntimeDroolsException: Unable to load dialect 'org....
解决Eclipse中使用drool时报Caused by: java.lang.RuntimeException: The Eclipse JDT Core jar is not in the classpath的问题。