use of org.eclipse.jdt.core.dom.ASTVisitor in project eclipse-cs by checkstyle.
the class RedundantModifierQuickfix method handleGetCorrectingASTVisitor.
/**
* {@inheritDoc}
*/
@Override
protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartOffset) {
return new ASTVisitor() {
@SuppressWarnings("unchecked")
@Override
public boolean visit(MethodDeclaration node) {
if (containsPosition(node, markerStartOffset)) {
List<ModifierKeyword> redundantKeyWords = Collections.emptyList();
if (node.getParent() instanceof TypeDeclaration) {
TypeDeclaration type = (TypeDeclaration) node.getParent();
if (type.isInterface()) {
redundantKeyWords = Arrays.asList(new ModifierKeyword[] { ModifierKeyword.PUBLIC_KEYWORD, ModifierKeyword.ABSTRACT_KEYWORD, ModifierKeyword.FINAL_KEYWORD });
} else if (Modifier.isFinal(type.getModifiers())) {
redundantKeyWords = Arrays.asList(new ModifierKeyword[] { ModifierKeyword.FINAL_KEYWORD });
}
}
deleteRedundantModifiers(node.modifiers(), redundantKeyWords);
}
return true;
}
@SuppressWarnings("unchecked")
@Override
public boolean visit(FieldDeclaration node) {
// recalculate start position because optional javadoc is mixed
// into the original start position
int pos = node.getStartPosition() + (node.getJavadoc() != null ? node.getJavadoc().getLength() + JAVADOC_COMMENT_LENGTH : 0);
if (containsPosition(lineInfo, pos)) {
List<ModifierKeyword> redundantKeyWords = Collections.emptyList();
if (node.getParent() instanceof TypeDeclaration) {
TypeDeclaration type = (TypeDeclaration) node.getParent();
if (type.isInterface()) {
redundantKeyWords = Arrays.asList(new ModifierKeyword[] { ModifierKeyword.PUBLIC_KEYWORD, ModifierKeyword.ABSTRACT_KEYWORD, ModifierKeyword.FINAL_KEYWORD, ModifierKeyword.STATIC_KEYWORD });
}
} else if (node.getParent() instanceof AnnotationTypeDeclaration) {
redundantKeyWords = Arrays.asList(new ModifierKeyword[] { ModifierKeyword.PUBLIC_KEYWORD, ModifierKeyword.ABSTRACT_KEYWORD, ModifierKeyword.FINAL_KEYWORD, ModifierKeyword.STATIC_KEYWORD });
}
deleteRedundantModifiers(node.modifiers(), redundantKeyWords);
}
return true;
}
@SuppressWarnings("unchecked")
@Override
public boolean visit(AnnotationTypeMemberDeclaration node) {
// recalculate start position because optional javadoc is mixed
// into the original start position
int pos = node.getStartPosition() + (node.getJavadoc() != null ? node.getJavadoc().getLength() + JAVADOC_COMMENT_LENGTH : 0);
if (containsPosition(lineInfo, pos)) {
if (node.getParent() instanceof AnnotationTypeDeclaration) {
List<ModifierKeyword> redundantKeyWords = Arrays.asList(new ModifierKeyword[] { ModifierKeyword.PUBLIC_KEYWORD, ModifierKeyword.ABSTRACT_KEYWORD, ModifierKeyword.FINAL_KEYWORD, ModifierKeyword.STATIC_KEYWORD });
deleteRedundantModifiers(node.modifiers(), redundantKeyWords);
}
}
return true;
}
private void deleteRedundantModifiers(List<ASTNode> modifiers, List<ModifierKeyword> redundantModifierKeywords) {
Iterator<ASTNode> it = modifiers.iterator();
while (it.hasNext()) {
ASTNode node = it.next();
if (node instanceof Modifier) {
Modifier modifier = (Modifier) node;
if (redundantModifierKeywords.contains(modifier.getKeyword())) {
it.remove();
}
}
}
}
};
}
use of org.eclipse.jdt.core.dom.ASTVisitor in project eclipse-cs by checkstyle.
the class DefaultComesLastQuickfix method handleGetCorrectingASTVisitor.
/**
* {@inheritDoc}
*/
@Override
protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartOffset) {
return new ASTVisitor() {
@SuppressWarnings("unchecked")
@Override
public boolean visit(SwitchCase node) {
if (containsPosition(lineInfo, node.getStartPosition())) {
if (node.isDefault() && !isLastSwitchCase(node)) {
SwitchStatement switchStatement = (SwitchStatement) node.getParent();
List<ASTNode> defaultCaseStatements = new ArrayList<>();
defaultCaseStatements.add(node);
// collect all statements belonging to the default case
int defaultStatementIndex = switchStatement.statements().indexOf(node);
for (int i = defaultStatementIndex + 1; i < switchStatement.statements().size(); i++) {
ASTNode tmpNode = (ASTNode) switchStatement.statements().get(i);
if (!(tmpNode instanceof SwitchCase)) {
defaultCaseStatements.add(tmpNode);
} else {
break;
}
}
// move the statements to the end of the statement list
switchStatement.statements().removeAll(defaultCaseStatements);
switchStatement.statements().addAll(defaultCaseStatements);
}
}
return true;
}
private boolean isLastSwitchCase(SwitchCase switchCase) {
SwitchStatement switchStatement = (SwitchStatement) switchCase.getParent();
// collect all statements belonging to the default case
int defaultStatementIndex = switchStatement.statements().indexOf(switchCase);
for (int i = defaultStatementIndex + 1; i < switchStatement.statements().size(); i++) {
ASTNode tmpNode = (ASTNode) switchStatement.statements().get(i);
if (tmpNode instanceof SwitchCase) {
return false;
}
}
return true;
}
};
}
use of org.eclipse.jdt.core.dom.ASTVisitor in project eclipse-cs by checkstyle.
the class MissingSwitchDefaultQuickfix method handleGetCorrectingASTVisitor.
/**
* {@inheritDoc}
*/
@Override
protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartOffset) {
return new ASTVisitor() {
@SuppressWarnings("unchecked")
@Override
public boolean visit(SwitchStatement node) {
if (containsPosition(lineInfo, node.getStartPosition())) {
SwitchCase defNode = node.getAST().newSwitchCase();
defNode.setExpression(null);
node.statements().add(defNode);
node.statements().add(node.getAST().newBreakStatement());
}
// also visit children
return true;
}
};
}
use of org.eclipse.jdt.core.dom.ASTVisitor in project bndtools by bndtools.
the class BaselineErrorHandler method generateAddedMethodMarker.
/*
* List<MarkerData> generateAddedTypeMarker(IJavaProject javaProject, String name, Delta ifAdded) { // TODO
* Auto-generated method stub return null; }
*/
List<MarkerData> generateAddedMethodMarker(IJavaProject javaProject, String className, final String methodName, final Delta requiresDelta) throws JavaModelException {
final List<MarkerData> markers = new LinkedList<MarkerData>();
final CompilationUnit ast = createAST(javaProject, className);
if (ast != null) {
ast.accept(new ASTVisitor() {
@Override
public boolean visit(MethodDeclaration methodDecl) {
String signature = ASTUtil.buildMethodSignature(methodDecl);
if (signature.equals(methodName)) {
// Create the marker attribs here
Map<String, Object> attribs = new HashMap<String, Object>();
attribs.put(IMarker.CHAR_START, methodDecl.getStartPosition());
attribs.put(IMarker.CHAR_END, methodDecl.getStartPosition() + methodDecl.getLength());
String message = String.format("This method was added, which requires a %s change to the package.", requiresDelta);
attribs.put(IMarker.MESSAGE, message);
markers.add(new MarkerData(ast.getJavaElement().getResource(), attribs, false));
}
return false;
}
});
}
return markers;
}
use of org.eclipse.jdt.core.dom.ASTVisitor in project bndtools by bndtools.
the class AbstractBuildErrorDetailsHandler method createFieldMarkerData.
/**
* Create a marker on a Java Method
*
* @param javaProject
* @param className - the fully qualified class name (e.g java.lang.String)
* @param methodName
* @param methodSignature - signatures are in "internal form" e.g. (Ljava.lang.Integer;[Ljava/lang/String;Z)V
* @param markerAttributes - attributes that should be included in the marker, typically a message. The start and
* end points for the marker are added by this method.
* @param hasResolutions - true if the marker will have resolutions
* @return Marker Data that can be used to create an {@link IMarker}, or null if no location can be found
* @throws JavaModelException
*/
public static final MarkerData createFieldMarkerData(IJavaProject javaProject, final String className, final String fieldName, final Map<String, Object> markerAttributes, boolean hasResolutions) throws JavaModelException {
final CompilationUnit ast = createAST(javaProject, className);
if (ast == null)
return null;
ast.accept(new ASTVisitor() {
@Override
public boolean visit(FieldDeclaration fieldDecl) {
if (matches(ast, fieldDecl, fieldName)) {
// Create the marker attribs here
markerAttributes.put(IMarker.CHAR_START, fieldDecl.getStartPosition());
markerAttributes.put(IMarker.CHAR_END, fieldDecl.getStartPosition() + fieldDecl.getLength());
}
return false;
}
private boolean matches(@SuppressWarnings("unused") CompilationUnit ast, FieldDeclaration fieldDecl, String fieldName) {
@SuppressWarnings("unchecked") List<VariableDeclarationFragment> list = (List<VariableDeclarationFragment>) fieldDecl.getStructuralProperty(FieldDeclaration.FRAGMENTS_PROPERTY);
for (VariableDeclarationFragment vdf : list) {
if (fieldName.equals(vdf.getName().toString())) {
return true;
}
}
return false;
}
});
if (!markerAttributes.containsKey(IMarker.CHAR_START))
return null;
return new MarkerData(ast.getJavaElement().getResource(), markerAttributes, hasResolutions);
}
Aggregations