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