如何使用 JDT 将方法源添加到现有 java 文件?
我正在编写一个简单的 eclipse 插件,它是一个代码生成器。用户可以选择一个现有的方法,然后用JDT在相应的测试文件中生成一个新的测试方法。
假设测试文件已经存在,其内容为:
public class UserTest extends TestCase {
public void setUp(){}
public void tearDown(){}
public void testCtor(){}
}
现在我已经生成了一些测试代码:
/** complex javadoc */
public void testSetName() {
....
// complex logic
}
我想要做的是将其附加到现有的 UserTest
中。我必须编码:
String sourceContent = FileUtils.readFileToString("UserTest.java", "UTF-8");
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(content.toCharArray());
CompilationUnit testUnit = (CompilationUnit) parser.createAST(null);
String newTestCode = "public void testSetName() { .... }";
// get the first type
final List<TypeDeclaration> list = new ArrayList<TypeDeclaration>();
testUnit .accept(new ASTVisitor() {
@Override
public boolean visit(TypeDeclaration node) {
list.add(node);
return false;
}
});
TypeDeclaration type = list.get(0);
type.insertMethod(newTestCode); // ! no this method: insertMethod
但是没有这样的方法insertMethod
。
我现在知道两个选择:
- 不要使用
jdt
,只是将新代码插入到测试文件中,在最后一个}
之前 - 使用
testUnit.getAST().newMethodDeclaration ()
创建一个方法,然后更新它。
但我不喜欢这两个选项,我希望有像 insertMethod
这样的东西,它可以让我向测试编译单元附加一些文本,或者将测试代码转换为 MethodDeclaration,然后附加到测试编译单元。
更新
我看到nonty的回答,发现jdt中有两个CompilationUnit
。一个是 org.eclipse.jdt.internal.core.CompilationUnit,另一个是 org.eclipse.jdt.core.dom.CompilationUnit。我用的是第二个,没有人用过第一个。
我需要补充一下我的问题:一开始我想创建一个eclipse插件,但后来我发现很难通过swt创建一个复杂的UI,所以我决定创建一个Web应用程序来生成代码。我从 eclipse 复制了这些 jdt jar,因此我可以使用 org.eclipse.jdt.core.dom.CompilationUnit。
有没有办法在 eclipse 之外使用 org.eclipse.jdt.internal.core.CompilationUnit ?
I'm writing a simple eclipse plugin, which is a code generator. User can choose an existing method, then generate a new test method in corresponding test file, with JDT.
Assume the test files is already existed, and it's content is:
public class UserTest extends TestCase {
public void setUp(){}
public void tearDown(){}
public void testCtor(){}
}
Now I have generate some test code:
/** complex javadoc */
public void testSetName() {
....
// complex logic
}
What I want to do is to append it to the existing UserTest
. I have to code:
String sourceContent = FileUtils.readFileToString("UserTest.java", "UTF-8");
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(content.toCharArray());
CompilationUnit testUnit = (CompilationUnit) parser.createAST(null);
String newTestCode = "public void testSetName() { .... }";
// get the first type
final List<TypeDeclaration> list = new ArrayList<TypeDeclaration>();
testUnit .accept(new ASTVisitor() {
@Override
public boolean visit(TypeDeclaration node) {
list.add(node);
return false;
}
});
TypeDeclaration type = list.get(0);
type.insertMethod(newTestCode); // ! no this method: insertMethod
But there is no such a method insertMethod
.
I know two options now:
- Do not use
jdt
, just to insert the new code to the test file, before last}
- use
testUnit.getAST().newMethodDeclaration()
to create a method, then update it.
But I don't like these two options, I hope there is something like insertMethod
, which can let me append some text to the test compilation unit, or convert the test code to a MethodDeclaration, then append to the test compilation unit.
UPDATE
I see nonty's answer, and found there are two CompilationUnit
in jdt. One is org.eclipse.jdt.internal.core.CompilationUnit
, another is org.eclipse.jdt.core.dom.CompilationUnit
. I used the second one, and nonty used the first one.
I need to supplement my question: At first I want to create a eclipse-plugin, but later I found it hard to create a complex UI by swt, so I decided to create a web app to generate the code. I copied those jdt jars from eclipse, so I can just use org.eclipse.jdt.core.dom.CompilationUnit
.
Is there a way to use org.eclipse.jdt.internal.core.CompilationUnit
outside eclipse?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有什么问题
您甚至可以指定在哪里添加新方法以及是否“覆盖”任何现有方法。
What is wrong with
You can even specify where to add the new method and whether to "overwrite" any exiting method.
JavaCore 有一个方法可以从给定的 IFile 创建 CompilationUnit。
JavaCore has a method to create a CompilationUnit from a given IFile.