use of org.eclipse.jdt.core.dom.Annotation in project flux by eclipse.
the class ASTNodes method getTopMostType.
/**
* Returns the topmost ancestor of <code>node</code> that is a {@link Type} (but not a {@link UnionType}).
* <p>
* <b>Note:</b> The returned node often resolves to a different binding than the given <code>node</code>!
*
* @param node the starting node, can be <code>null</code>
* @return the topmost type or <code>null</code> if the node is not a descendant of a type node
* @see #getNormalizedNode(ASTNode)
*/
public static Type getTopMostType(ASTNode node) {
ASTNode result = null;
while (node instanceof Type && !(node instanceof UnionType) || node instanceof Name || node instanceof Annotation || node instanceof MemberValuePair || node instanceof Expression) {
// Expression could maybe be reduced to expression node types that can appear in an annotation
result = node;
node = node.getParent();
}
if (result instanceof Type)
return (Type) result;
return null;
}
use of org.eclipse.jdt.core.dom.Annotation in project eclipse-pmd by acanda.
the class SuppressWarningsQuickFix method createReplacementSuppressWarningsAnnotation.
private Annotation createReplacementSuppressWarningsAnnotation(final Annotation existingAnnotation, final AST ast) {
final Annotation replacement;
if (existingAnnotation == null || existingAnnotation.isMarkerAnnotation()) {
final SingleMemberAnnotation annotation = createAnnotation(ast, SingleMemberAnnotation.class);
annotation.setValue(createPMDLiteralValue(ast));
replacement = annotation;
} else if (existingAnnotation.isSingleMemberAnnotation()) {
final SingleMemberAnnotation existingSingleMemberAnnotation = (SingleMemberAnnotation) existingAnnotation;
final SingleMemberAnnotation annotation = createAnnotation(ast, SingleMemberAnnotation.class);
annotation.setValue(createArrayInitializer(existingSingleMemberAnnotation.getValue()));
replacement = annotation;
} else if (existingAnnotation.isNormalAnnotation()) {
final NormalAnnotation existingNormalAnnotation = (NormalAnnotation) existingAnnotation;
final NormalAnnotation annotation = createAnnotation(ast, NormalAnnotation.class);
createAnnotationValues(existingNormalAnnotation, annotation);
replacement = annotation;
} else {
replacement = existingAnnotation;
}
return replacement;
}
use of org.eclipse.jdt.core.dom.Annotation in project AutoRefactor by JnRouvignac.
the class ObsoleteRedundantModifiersCleanUp method reorderModifiers.
private void reorderModifiers(final List<IExtendedModifier> reorderedModifiers) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteRedundantModifiersCleanUp_description);
for (int i = 0; i < reorderedModifiers.size(); i++) {
IExtendedModifier extendedModifier = reorderedModifiers.get(i);
if (extendedModifier.isModifier()) {
rewrite.moveToIndex((Modifier) extendedModifier, i, ASTNodes.createMoveTarget(rewrite, (Modifier) extendedModifier), group);
} else {
rewrite.moveToIndex((Annotation) extendedModifier, i, ASTNodes.createMoveTarget(rewrite, (Annotation) extendedModifier), group);
}
}
}
use of org.eclipse.jdt.core.dom.Annotation in project evosuite by EvoSuite.
the class ResolutionMarkerEvoIgnoreForClass method run.
@Override
public void run(IMarker marker) {
IResource res = marker.getResource();
try {
CompilationUnit compunit = CompilationUnitManager.getCompilationUnit(res);
int position = marker.getAttribute(IMarker.CHAR_START, 0) + 1;
if (position == 0) {
int line = marker.getAttribute(IMarker.LINE_NUMBER, 0);
position = compunit.getPosition(line, 0);
}
AST ast = compunit.getAST();
ASTRewrite rewriter = ASTRewrite.create(ast);
Annotation annotation = ast.newNormalAnnotation();
annotation.setTypeName(ast.newName("EvoIgnore"));
ImportDeclaration id = ast.newImportDeclaration();
id.setName(ast.newName("org.evosuite.quickfixes.annotations.EvoIgnore"));
ListRewrite lr = rewriter.getListRewrite(compunit, CompilationUnit.TYPES_PROPERTY);
// lr.insertFirst(annotation, null);
lr.insertAt(annotation, 0, null);
lr.insertAt(id, 0, null);
ITextFileBufferManager bm = FileBuffers.getTextFileBufferManager();
IPath path = compunit.getJavaElement().getPath();
try {
bm.connect(path, null, null);
ITextFileBuffer textFileBuffer = bm.getTextFileBuffer(path, null);
IDocument document = textFileBuffer.getDocument();
TextEdit edits = rewriter.rewriteAST(document, null);
edits.apply(document);
textFileBuffer.commit(null, false);
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedTreeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
bm.disconnect(path, null, null);
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// (4)
}
System.out.println(lr.getRewrittenList() + "\nPosition: " + position + "\nEdits: " + rewriter.toString());
} catch (JavaModelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
marker.delete();
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
use of org.eclipse.jdt.core.dom.Annotation in project evosuite by EvoSuite.
the class ResolutionMarkerEvoIgnoreForMethod method run.
@Override
public void run(IMarker marker) {
// TODO Auto-generated method stub
IResource res = marker.getResource();
try {
ICompilationUnit icomp = CompilationUnitManager.getICompilationUnit(res);
CompilationUnit compunit = CompilationUnitManager.getCompilationUnit(res);
int position = marker.getAttribute(IMarker.CHAR_START, 0) + 1;
if (position == 1) {
int line = marker.getAttribute(IMarker.LINE_NUMBER, 0);
position = compunit.getPosition(line, 0);
}
AST ast = compunit.getAST();
ASTRewrite rewriter = ASTRewrite.create(ast);
IJavaElement element = icomp.getElementAt(position);
IJavaElement method = getMethod(element);
// TypeDeclaration td = (TypeDeclaration) compunit.types().get(0);
TypeDeclaration td = (TypeDeclaration) compunit.types().get(0);
MethodDeclaration md = td.getMethods()[0];
int counter = 1;
while (!md.getName().getFullyQualifiedName().equals(method.getElementName()) && counter < td.getMethods().length) {
md = td.getMethods()[counter];
System.out.println(md.getName().getFullyQualifiedName() + " " + method.getElementName());
counter++;
}
Annotation annotation = ast.newNormalAnnotation();
annotation.setTypeName(ast.newName("EvoIgnore"));
ImportDeclaration id = ast.newImportDeclaration();
id.setName(ast.newName("org.evosuite.quickfixes.annotations.EvoIgnore"));
ListRewrite lr = rewriter.getListRewrite(md.getParent(), TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
ListRewrite lr2 = rewriter.getListRewrite(compunit, CompilationUnit.TYPES_PROPERTY);
// lr.insertFirst(annotation, null);
lr.insertBefore(annotation, md, null);
lr2.insertAt(id, 0, null);
ITextFileBufferManager bm = FileBuffers.getTextFileBufferManager();
IPath path = compunit.getJavaElement().getPath();
try {
bm.connect(path, null, null);
ITextFileBuffer textFileBuffer = bm.getTextFileBuffer(path, null);
IDocument document = textFileBuffer.getDocument();
TextEdit edits = rewriter.rewriteAST(document, null);
edits.apply(document);
textFileBuffer.commit(null, false);
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedTreeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
bm.disconnect(path, null, null);
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// (4)
}
System.out.println(lr.getRewrittenList() + "\nPosition: " + position + "\nEdits: " + rewriter.toString());
} catch (JavaModelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
marker.delete();
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Aggregations