更改TypeDeclaration时,Eclipse AST不会更改类文件 [英] Eclipse AST not changing class files when changing TypeDeclaration
问题描述
我正在尝试使用Java AST编辑多个Java类。但是我的更改不会显示在Java类文件中。
我想要做什么?我想要一个 IPackageFragment
并访问所有的 ICompilationUnit
。对于每个声明的类,我想将超级类设置为一个特定的类(使用超类的限定名称,,因为它是一个Xtend类)。我也尝试通过文档
类应用编辑。
What specifically do I want to do? I want to take an IPackageFragment
and visit all the ICompilationUnit
s. For every declared class I want to set the super class to a specific class (using the qualified name of the super class, because it is an Xtend class). I also tried applying the edits via the Document
class.
例如: A class main.model.someClass
应该继承自 wrappers.main.model.someClassWrapper
For example: A class main.model.someClass
should inherit from wrappers.main.model.someClassWrapper
我对JDT API比较新,所以我找不到类文件没有改变的原因。我已经检查过这篇文章,但它没有帮助我。我试图保持尽可能接近来自如何训练JDT Dragon 从Stackoverflow获得的其他提示/示例。但是这不行。
I am relatively new to the JDT API, so I cannot find the reason why the class files are not changed. I already checked this post, but it did not help me. I tried to stay as close as possible to the examples from How To Train the JDT Dragon other tips/examples I got from Stackoverflow. But it will not work.
这是我如何做的:
private void editTypesIn(IPackageFragment myPackage) throws JavaModelException {
for (ICompilationUnit unit : myPackage.getCompilationUnits()) {
TypeVisitor visitor = new TypeVisitor(myPackage.getElementName(), unit);
unit.becomeWorkingCopy(new NullProgressMonitor());
CompilationUnit parse = parse(unit);
parse.recordModifications();
parse.accept(visitor);
}
}
private static CompilationUnit parse(ICompilationUnit unit) {
ASTParser parser = ASTParser.newParser(AST.JLS8);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(unit);
parser.setResolveBindings(true);
return (CompilationUnit) parser.createAST(null); // parse
}
这是访客类:
public class TypeVisitor extends ASTVisitor {
private final String currentPackage;
private final ICompilationUnit compilationUnit;
public TypeVisitor(String currentPackage, ICompilationUnit compilationUnit) {
this.currentPackage = currentPackage;
this.compilationUnit = compilationUnit;
}
@Override
public boolean visit(TypeDeclaration node) {
if (!node.isInterface()) { // is class
setSuperClass(node, "wrappers." + currentPackage + "." + node.getName().toString() + "Wrapper");
}
return super.visit(node);
}
public void setSuperClass(TypeDeclaration declaration, String qualifiedName) {
try {
// create ast and rewrite:
AST ast = declaration.getAST();
ASTRewrite astRewrite = ASTRewrite.create(ast);
// set super:
Name name = ast.newName(qualifiedName);
Type type = ast.newSimpleType(name);
declaration.setSuperclassType(type);
// apply changes
TextEdit edits = astRewrite.rewriteAST();
compilationUnit.applyTextEdit(edits, new NullProgressMonitor());
compilationUnit.commitWorkingCopy(true, new NullProgressMonitor());
} catch (JavaModelException exception) {
exception.printStackTrace();
} catch (IllegalArgumentException exception) {
exception.printStackTrace();
} catch (MalformedTreeException exception) {
exception.printStackTrace();
}
}
}
提前感谢任何帮助
推荐答案
我想你的修改将为空,对吗?
I guess your edits will be empty, right?
通过调用 CompilationUnit.recordModifications()
,告诉AST在内部创建一个 ASTRewrite
,将对其进行修改。
By calling CompilationUnit.recordModifications()
you tell the AST to internally create an ASTRewrite
into which modifications will be recorded.
由 ASTRewrite.create(..)
您正在创建第二个 ASTRewrite
其中不知道正在记录的修改。
By ASTRewrite.create(..)
you are creating a second ASTRewrite
which knows nothing of the modifications being recorded.
消费记录修改的唯一API是 CompilationUnit.rewrite(IDocument,Map)
(参见字段 AST.rewriter
)。
The only API for consuming the recorded modifications is CompilationUnit.rewrite(IDocument,Map)
(see references to field AST.rewriter
).
如果您需要使用自己的 ASTRewrite
应用描述性方法,使用例如 ASTRewrite.set(..)
。
If you need to use your own ASTRewrite
apply the "Descriptive approach", using, e.g., ASTRewrite.set(..)
.
这篇关于更改TypeDeclaration时,Eclipse AST不会更改类文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!