Commit f0e101f6 authored by Marco Konersmann's avatar Marco Konersmann

addAnnotation now also handles IMethods

parent 2fec5c4c
......@@ -44,6 +44,7 @@ import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.FieldDeclaration;
import org.eclipse.jdt.core.dom.MarkerAnnotation;
import org.eclipse.jdt.core.dom.MemberValuePair;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.Name;
import org.eclipse.jdt.core.dom.NormalAnnotation;
import org.eclipse.jdt.core.dom.SimpleName;
......@@ -135,9 +136,9 @@ public class ASTUtils {
return null;
}
public synchronized static void addAnnotationToField(final IField field, final Annotation originalAnnotation,
public synchronized static void addAnnotationToMember(final IMember member, final Annotation originalAnnotation,
final Annotation replacementAnnotation) {
CompilationUnit astRoot = parse(field.getCompilationUnit(), null);
CompilationUnit astRoot = parse(member.getCompilationUnit(), null);
final ASTRewrite rewriter = createRewriter(astRoot);
if (originalAnnotation != null) {
rewriter.replace(originalAnnotation, replacementAnnotation, null);
......@@ -147,7 +148,15 @@ public class ASTUtils {
@Override
public boolean visit(FieldDeclaration node) {
if (((VariableDeclarationFragment) node.fragments().get(0)).getName().getIdentifier()
.equals(field.getElementName()))
.equals(member.getElementName()))
rewriter.getListRewrite(node, node.getModifiersProperty()).insertAt(replacementAnnotation, 0,
null);
return false;
}
@Override
public boolean visit(MethodDeclaration node) {
if (node.getName().getIdentifier().equals(member.getElementName()))
rewriter.getListRewrite(node, node.getModifiersProperty()).insertAt(replacementAnnotation, 0,
null);
return false;
......@@ -156,7 +165,7 @@ public class ASTUtils {
}
// Do rewrite
final ICompilationUnit cu = field.getCompilationUnit();
final ICompilationUnit cu = member.getCompilationUnit();
try {
final IDocument document = new Document(cu.getSource());
......@@ -232,9 +241,11 @@ public class ASTUtils {
final Name newName = astRoot.getAST().newName(annotationName);
replacementAnnotation.setTypeName(newName);
if (annotatable instanceof IField) {
if (annotatable instanceof IMember) {
Annotation o = originalAnnotation.size() > 0 ? originalAnnotation.get(0) : null;
addAnnotationToField((IField) annotatable, o, replacementAnnotation);
addAnnotationToMember((IMember) annotatable, o, replacementAnnotation);
} else {
throw new IllegalArgumentException("annotatable must be an IMember");
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment