use of org.eclipse.jdt.core.dom.Modifier in project eclipse-cs by checkstyle.
the class FinalClassQuickfix method handleGetCorrectingASTVisitor.
/**
* {@inheritDoc}
*/
@Override
protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartOffset) {
return new ASTVisitor() {
@SuppressWarnings("unchecked")
@Override
public boolean visit(TypeDeclaration 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 (!Modifier.isFinal(node.getModifiers())) {
Modifier finalModifier = node.getAST().newModifier(ModifierKeyword.FINAL_KEYWORD);
node.modifiers().add(finalModifier);
// reorder modifiers into their correct order
List<?> reorderedModifiers = ModifierOrderQuickfix.reOrderModifiers(node.modifiers());
node.modifiers().clear();
node.modifiers().addAll(reorderedModifiers);
}
}
return true;
}
};
}
use of org.eclipse.jdt.core.dom.Modifier in project eclipse-cs by checkstyle.
the class FinalLocalVariableQuickfix method handleGetCorrectingASTVisitor.
/**
* {@inheritDoc}
*/
@Override
protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartOffset) {
return new ASTVisitor() {
@SuppressWarnings("unchecked")
@Override
public boolean visit(SingleVariableDeclaration node) {
if (containsPosition(node, markerStartOffset) && !Modifier.isFinal(node.getModifiers())) {
if (!Modifier.isFinal(node.getModifiers())) {
Modifier finalModifier = node.getAST().newModifier(ModifierKeyword.FINAL_KEYWORD);
node.modifiers().add(finalModifier);
}
}
return true;
}
@SuppressWarnings("unchecked")
@Override
public boolean visit(VariableDeclarationStatement node) {
if (containsPosition(node, markerStartOffset) && !Modifier.isFinal(node.getModifiers())) {
if (!Modifier.isFinal(node.getModifiers())) {
Modifier finalModifier = node.getAST().newModifier(ModifierKeyword.FINAL_KEYWORD);
node.modifiers().add(finalModifier);
}
}
return true;
}
};
}
use of org.eclipse.jdt.core.dom.Modifier 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.Modifier in project eclipse.jdt.ls by eclipse.
the class ModifierCorrectionSubProcessor method findVisibilityModifier.
private static Modifier findVisibilityModifier(List<IExtendedModifier> modifiers) {
for (int i = 0; i < modifiers.size(); i++) {
IExtendedModifier curr = modifiers.get(i);
if (curr instanceof Modifier) {
Modifier modifier = (Modifier) curr;
ModifierKeyword keyword = modifier.getKeyword();
if (keyword == ModifierKeyword.PUBLIC_KEYWORD || keyword == ModifierKeyword.PROTECTED_KEYWORD || keyword == ModifierKeyword.PRIVATE_KEYWORD) {
return modifier;
}
}
}
return null;
}
use of org.eclipse.jdt.core.dom.Modifier in project lombok by rzwitserloot.
the class PatchValEclipse method createModifier.
public static Modifier createModifier(AST ast, ModifierKeyword keyword, int start, int end) {
Modifier modifier = null;
try {
modifier = Reflection.modifierConstructor.newInstance(ast);
} catch (InstantiationException e) {
throw Lombok.sneakyThrow(e);
} catch (IllegalAccessException e) {
throw Lombok.sneakyThrow(e);
} catch (InvocationTargetException e) {
throw Lombok.sneakyThrow(e);
}
if (modifier != null) {
modifier.setKeyword(keyword);
modifier.setSourceRange(start, end - start + 1);
}
return modifier;
}
Aggregations