Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » How to use ASTRewrite to replace a particular SimpleType with a PrimitiveType?(I need to preprocess some code for a java based language - Processing. In this language, all instances of type color, need to be replaced with int. )
icon5.gif  How to use ASTRewrite to replace a particular SimpleType with a PrimitiveType? [message #897378] Mon, 23 July 2012 20:33
Manindra Moharana is currently offline Manindra Moharana
Messages: 5
Registered: April 2012
Junior Member
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];

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;
}

Here rewrite is an instance of ASTRewrite. Line 1 has no effect after applying modifications(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 using regex search 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?

Thanks
Previous Topic:Ant build file copied to workspace dir on import. Relative paths no longer work.
Next Topic:Help me to install eclipse IDE
Goto Forum:
  


Current Time: Wed Oct 22 08:20:56 GMT 2014

Powered by FUDForum. Page generated in 0.04466 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software