如何使用ASTRewrite来替换一个具有PrimitiveType的特定SimpleType? [英] How to use ASTRewrite to replace a particular SimpleType with a PrimitiveType?

查看:259
本文介绍了如何使用ASTRewrite来替换一个具有PrimitiveType的特定SimpleType?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在编译基于Java的语言(Processing)之前对一些代码进行预处理。在这种语言中,所有颜色的实例都需要用int替换。例如,以下是代码片段:

I need to preprocess some code before compiling for a java based language - Processing. In this language, all instances of type color, need to be replaced with int. For ex, here's a code snippet:

color red = 0xffaabbcc;
color[][] primary = new color[10][10];

预处理后,上述代码应如下所示:

After preprocessing, the above code should look like:

int red = 0xffaabbcc;
int[][] primary = new int[10][10];

我在非eclipse环境中工作。我正在使用Eclipse JDT ASTParser来执行此操作。我实现了我的ASTVisitor访问所有SimpleType节点。以下是ASTVisitor实现的代码片段:

I'm working in a non eclipse environment. I'm using Eclipse JDT ASTParser to do this. I've implemented my ASTVisitor which visits all SimpleType nodes. Here's the code snippet from the ASTVisitor implementation:

public boolean visit(SimpleType node) {
    if (node.toString().equals("color")) {
        System.out.println("ST color type detected: "
                + node.getStartPosition());
        // 1
        rewrite.replace(node,
                rewrite.getAST().newPrimitiveType(PrimitiveType.INT), null);
        // 2
        node.setStructuralProperty(SimpleType.NAME_PROPERTY, rewrite
                .getAST().newSimpleName("int")); // 2
    }
    return true;
}

这里重写是ASTRewrite的一个实例。
第1行没有效果(第2行注释掉)。并且第2行导致IllegalArgumentException被抛出,因为newSimpleName()将不接受任何java关键字,如int。

Here rewrite is an instance of ASTRewrite. Line 1 has no effect(with line 2 commented out). And line 2 causes IllegalArgumentException to be thrown because newSimpleName() will not accept any java keywords like int.

查找并替换所有颜色与正则表达式的实例似乎不像正确的方式,因为它可能会导致不必要的变化。但是我可能错了。

Finding and replacing all instances of color with regex doesn't seem like the right way to me since it could cause unnecessary changes. But I may be wrong.

我该如何实现?或者有什么替代的解决方案或方法我可以采取?

How can I achieve this? Or are there any alternate solutions or approaches I can take?

谢谢

更新编辑:
这是执行ASTRewrite的代码段:

Update Here's the snippet which performs ASTRewrite:

    CompilationUnit cu = (CompilationUnit) parser.createAST(null);
    cu.recordModifications();
    rewrite = ASTRewrite.create(cu.getAST());
    cu.accept(new XQASTVisitor());

    TextEdit edits = cu.rewrite(doc, null);
    try {
        edits.apply(doc);
    } catch (MalformedTreeException e) {
        e.printStackTrace();
    } catch (BadLocationException e) {
        e.printStackTrace();
    }

XQAstVisitor是包含上述访问方法的访问者类。还有其他替代,我执行正确执行。只有这一个会导致问题。

XQAstVisitor is the visitor class which contains the above visit method. There are other substitution that I'm performing that execute correctly. Only this one causes problems.

推荐答案

我发现你的错误!
此语句:

I found your errors! This statement:

TextEdit edits = cu.rewrite(doc, null);

不正确。
应该由以下语句替换:

is not right. And should be replaced by the following statement:

TextEdit edits = rewrite.rewriteAST(doc, null);

最后,再次将修改的文档解析为CompilationUnit,将应用更改。
此外,语句:

In the end, parse the modified doc into CompilationUnit again, the changes will be applied. What's more, the statement:

node.setStructuralProperty(SimpleType.NAME_PROPERTY, rewrite.getAST().newSimpleName("int"));

不需要。

这篇关于如何使用ASTRewrite来替换一个具有PrimitiveType的特定SimpleType?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆