Reputation: 2275
I would like to insert line comments in my code, programmatically. I am visiting a method declaration and I want to insert a line comment (or more) above it, using the AST of the method. Could anyone please give me a code example of how to do that? I have been searching for a long time and no success yet.
Upvotes: 2
Views: 1825
Reputation: 1547
I believe you can insert comments programatically using the Eclipse AST API. It seems that you are using that API already so it would be even better if you stick to the same API. You have a lot of code examples here and one of them is the code for inserting Javadoc. I am pasting it here anyway:
Javadoc jc = ast.newJavadoc();
TagElement tag = ast.newTagElement();
TextElement te = ast.newTextElement();
tag.fragments().add(te);
te.setText("Sample SWT Composite class created using the ASTParser");
jc.tags().add(tag);
tag = ast.newTagElement();
tag.setTagName(TagElement.TAG_AUTHOR);
tag.fragments().add(ast.newSimpleName("Manoel Marques"));
jc.tags().add(tag);
classType.setJavadoc(jc);
I believe this is a sufficient guideline.
Upvotes: 2
Reputation: 6463
Here's how I would do it:
void insertComment(String comment, OutputStream fos) throws IOException {
String commentKeyword = "//";
fos.write(commentKeyword.getBytes());
fos.write(comment.getBytes());
fos.write("\r\n".getBytes());
}
I don't think there is a native way to do this...
Upvotes: 1
Reputation: 11273
The only way I can think of, to insert code programmatically, is to open a Filestream and write to the text file at a specific pointer location.
Upvotes: 0