Skip to content

Instantly share code, notes, and snippets.

@pvlasov
Last active November 16, 2016 15:57
Show Gist options
  • Save pvlasov/0808562671cbe5c9e167ecaa290639b7 to your computer and use it in GitHub Desktop.
Save pvlasov/0808562671cbe5c9e167ecaa290639b7 to your computer and use it in GitHub Desktop.
Shows how to rewrite Java source code
ICompilationUnit compilationUnit = null; // TODO - initialize
ICoreRunnable rewriteRunnable = new ICoreRunnable() {
@Override
public void run(IProgressMonitor monitor) throws CoreException {
// TODO - create sub-monitors for different steps.
ICompilationUnit workingCopy = compilationUnit.getWorkingCopy(monitor);
try {
Document document = new Document(workingCopy.getSource());
// Get the compilation unit for traversing AST
ASTParser parser = ASTParser.newParser(AST.JLS8);
parser.setSource(workingCopy);
parser.setResolveBindings(true);
CompilationUnit compilationUnit = (CompilationUnit) parser.createAST(null);
ASTRewrite rewrite = ASTRewrite.create(compilationUnit.getAST());
// Record modification - to be later written with ASTRewrite
compilationUnit.recordModifications();
// Example of annotating and deprecating a method or a field
IMember iMember = null; // TODO - initialize
String source = iMember.getCompilationUnit().getSource();
String iMemberSource = iMember.getSource();
int memberIndex = source.indexOf(iMemberSource);
NodeFinder nodeFinder = new NodeFinder(compilationUnit.getRoot(), memberIndex, iMemberSource.length());
ASTNode coveredNode = nodeFinder.getCoveredNode();
if (coveredNode instanceof MethodDeclaration && iMember instanceof IMethod) {
String cName = ((MethodDeclaration) coveredNode).getName().getIdentifier();
String iName = iMember.getElementName();
if (cName.equals(iName)) {
ListRewrite listRewrite = rewrite.getListRewrite(coveredNode, MethodDeclaration.MODIFIERS2_PROPERTY);
// Create the annotation
MarkerAnnotation annotation = compilationUnit.getAST().newMarkerAnnotation();
annotation.setTypeName(coveredNode.getAST().newName("Deprecated"));
listRewrite.insertAt(annotation, 0, null);
ASTNode comment = rewrite.createStringPlaceholder("/*\n * Some comment\n */"+System.lineSeparator(), ASTNode.BLOCK_COMMENT);
listRewrite.insertAt(comment, 0, null);
}
} else if (coveredNode instanceof FieldDeclaration && iMember instanceof IField) {
String[] cName = {null};
((FieldDeclaration) coveredNode).accept(new ASTVisitor() {
public boolean visit(VariableDeclarationFragment vdf) {
if (cName[0] == null) {
cName[0] = vdf.getName().getIdentifier();
}
return super.visit(vdf);
}
});
String iName = iMember.getElementName();
if (iName.equals(cName[0])) {
ListRewrite listRewrite = rewrite.getListRewrite(coveredNode, FieldDeclaration.MODIFIERS2_PROPERTY);
// Create the annotation
MarkerAnnotation annotation = compilationUnit.getAST().newMarkerAnnotation();
annotation.setTypeName(coveredNode.getAST().newName("Deprecated"));
listRewrite.insertAt(annotation, 0, null);
ASTNode comment = rewrite.createStringPlaceholder("/*\n * Some comment\n */"+System.lineSeparator(), ASTNode.BLOCK_COMMENT);
listRewrite.insertAt(comment, 0, null);
}
}
TextEdit edits = rewrite.rewriteAST(document, workingCopy.getJavaProject().getOptions(true));
edits.apply(document);
String newSource = document.get();
workingCopy.getBuffer().setContents(newSource);
workingCopy.commitWorkingCopy(false, monitor);
} catch (CoreException e) {
throw e;
} catch (Exception e) {
throw new CoreException(new Status(IStatus.ERROR, "plug-in id here", "Error message here", e));
} finally {
workingCopy.discardWorkingCopy();
}
}
};
ResourcesPlugin.getWorkspace().run(
rewriteRunnable,
ResourcesPlugin.getWorkspace().getRoot(),
IWorkspace.AVOID_UPDATE,
monitor);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment