use of org.eclipse.jdt.core.dom.Modifier in project AutoRefactor by JnRouvignac.
the class RemoveUselessModifiersRefactoring method removeFinalModifier.
private boolean removeFinalModifier(List<IExtendedModifier> modifiers) {
boolean result = VISIT_SUBTREE;
for (Modifier m : getModifiersOnly(modifiers)) {
if (m.isFinal()) {
ctx.getRefactorings().remove(m);
result = DO_NOT_VISIT_SUBTREE;
}
}
return result;
}
use of org.eclipse.jdt.core.dom.Modifier in project AutoRefactor by JnRouvignac.
the class RemoveUselessModifiersRefactoring method removePublicStaticFinalModifiers.
private boolean removePublicStaticFinalModifiers(FieldDeclaration node) {
// remove modifiers implied by the context
boolean result = VISIT_SUBTREE;
for (Modifier m : getModifiersOnly(modifiers(node))) {
if (m.isPublic() || m.isStatic() || m.isFinal()) {
ctx.getRefactorings().remove(m);
result = DO_NOT_VISIT_SUBTREE;
}
}
return result;
}
use of org.eclipse.jdt.core.dom.Modifier in project jbosstools-hibernate by jbosstools.
the class ProcessEntityInfo method visit.
@SuppressWarnings("unchecked")
public boolean visit(TypeDeclaration node) {
ITypeBinding typeBinding = node.resolveBinding();
String nodeName = typeBinding == null ? null : typeBinding.getBinaryName();
if (nodeName == null) {
return false;
}
entityInfo = entityInfos.getEntityInfo(nodeName);
if (entityInfo == null) {
return false;
}
if (entityInfo.isAddMappedSuperclassFlag()) {
MarkerAnnotation matd = rewriter.getAST().newMarkerAnnotation();
matd.setTypeName(rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_MAPPEDSUPERCLASS));
ListRewrite lrw = rewriter.getListRewrite(node, TypeDeclaration.MODIFIERS2_PROPERTY);
lrw.insertFirst(matd, null);
}
if (entityInfo.isAddEntityFlag()) {
MarkerAnnotation matd = rewriter.getAST().newMarkerAnnotation();
matd.setTypeName(rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_ENTITY));
ListRewrite lrw = rewriter.getListRewrite(node, TypeDeclaration.MODIFIERS2_PROPERTY);
lrw.insertFirst(matd, null);
}
/**
* /
* if (!entityInfo.isImplicitConstructorFlag() && !entityInfo.isDefaultConstructorFlag() &&
* entityInfo.isAddSerializableInterfaceFlag()) {
* // add serializable interface
* SimpleName sn = null;
* //if (!entityInfo.isAddSerializableInterfaceImportFlag()) {
* sn = rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_SERIALIZABLE);
* //}
* //else {
* // sn = rewriter.getAST().newSimpleName(JPAConst.IMPORT_SERIALIZABLE);
* //}
* SimpleType st = rewriter.getAST().newSimpleType(sn);
* ListRewrite lrw = rewriter.getListRewrite(node, TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY);
* lrw.insertFirst(st, null);
* // add "private static final long serialVersionUID = 1L;"
* // ...
* }
* /*
*/
if (!entityInfo.isImplicitConstructorFlag() && !entityInfo.isDefaultConstructorFlag() && entityInfo.isAddSerializableInterfaceFlag()) {
MethodDeclaration md = rewriter.getAST().newMethodDeclaration();
md.setConstructor(true);
Modifier modifier = rewriter.getAST().newModifier(Modifier.ModifierKeyword.PROTECTED_KEYWORD);
md.modifiers().add(modifier);
Block body = rewriter.getAST().newBlock();
md.setBody(body);
SimpleName sn = rewriter.getAST().newSimpleName(entityInfo.getName());
md.setName(sn);
ListRewrite lrw = rewriter.getListRewrite(node, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
List<?> list = lrw.getOriginalList();
MethodDeclaration insertBeforeNode = null;
Iterator<?> it = list.iterator();
while (it.hasNext()) {
Object obj = it.next();
if (obj instanceof MethodDeclaration) {
insertBeforeNode = (MethodDeclaration) obj;
break;
}
}
if (insertBeforeNode == null) {
lrw.insertLast(md, null);
} else {
lrw.insertBefore(md, insertBeforeNode, null);
}
}
if (enableOptLock && entityInfo.isAddVersionFlag() && !entityInfo.hasVersionAnnotation()) {
// add property "version", add getter/setter getVersion/setVersion,
// add annotation for the property or for the getter
//
// $NON-NLS-1$
final String version = "version";
// $NON-NLS-1$
final String versionType = "Integer";
//
VariableDeclarationFragment vdFragment = rewriter.getAST().newVariableDeclarationFragment();
SimpleName variableName = rewriter.getAST().newSimpleName(version);
vdFragment.setName(variableName);
FieldDeclaration fieldVersion = rewriter.getAST().newFieldDeclaration(vdFragment);
Modifier modifier = rewriter.getAST().newModifier(Modifier.ModifierKeyword.PROTECTED_KEYWORD);
fieldVersion.modifiers().add(modifier);
Name typeName = rewriter.getAST().newName(versionType);
SimpleType type = rewriter.getAST().newSimpleType(typeName);
fieldVersion.setType(type);
//
MethodDeclaration mdGetter = rewriter.getAST().newMethodDeclaration();
modifier = rewriter.getAST().newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
mdGetter.modifiers().add(modifier);
Block body = rewriter.getAST().newBlock();
ReturnStatement returnVersion = rewriter.getAST().newReturnStatement();
variableName = rewriter.getAST().newSimpleName(version);
returnVersion.setExpression(variableName);
body.statements().add(returnVersion);
mdGetter.setBody(body);
// $NON-NLS-1$
SimpleName sn = rewriter.getAST().newSimpleName("getVersion");
mdGetter.setName(sn);
typeName = rewriter.getAST().newName(versionType);
type = rewriter.getAST().newSimpleType(typeName);
mdGetter.setReturnType2(type);
//
MethodDeclaration mdSetter = rewriter.getAST().newMethodDeclaration();
modifier = rewriter.getAST().newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
mdSetter.modifiers().add(modifier);
body = rewriter.getAST().newBlock();
Assignment assignment = rewriter.getAST().newAssignment();
FieldAccess fieldAccess = rewriter.getAST().newFieldAccess();
ThisExpression thisExpression = rewriter.getAST().newThisExpression();
fieldAccess.setExpression(thisExpression);
variableName = rewriter.getAST().newSimpleName(version);
fieldAccess.setName(variableName);
assignment.setLeftHandSide(fieldAccess);
variableName = rewriter.getAST().newSimpleName(version);
assignment.setRightHandSide(variableName);
ExpressionStatement expressionStatement = rewriter.getAST().newExpressionStatement(assignment);
body.statements().add(expressionStatement);
mdSetter.setBody(body);
// $NON-NLS-1$
sn = rewriter.getAST().newSimpleName("setVersion");
mdSetter.setName(sn);
SingleVariableDeclaration svd = rewriter.getAST().newSingleVariableDeclaration();
variableName = rewriter.getAST().newSimpleName(version);
svd.setName(variableName);
typeName = rewriter.getAST().newName(versionType);
type = rewriter.getAST().newSimpleType(typeName);
svd.setType(type);
mdSetter.parameters().add(svd);
//
ListRewrite lrw = rewriter.getListRewrite(node, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
if (entityInfo.getVersionFieldGetter() != FieldGetterType.FIELD && entityInfo.getVersionFieldGetter() != FieldGetterType.FIELD_GETTER) {
lrw.insertLast(fieldVersion, null);
}
if (entityInfo.getVersionFieldGetter() != FieldGetterType.GETTER && entityInfo.getVersionFieldGetter() != FieldGetterType.FIELD_GETTER) {
lrw.insertLast(mdGetter, null);
lrw.insertLast(mdSetter, null);
}
if (annotationStyle == AnnotStyle.FIELDS) {
MarkerAnnotation matd = rewriter.getAST().newMarkerAnnotation();
matd.setTypeName(rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_VERSION));
lrw = rewriter.getListRewrite(fieldVersion, FieldDeclaration.MODIFIERS2_PROPERTY);
lrw.insertFirst(matd, null);
} else if (annotationStyle == AnnotStyle.GETTERS) {
MarkerAnnotation matd = rewriter.getAST().newMarkerAnnotation();
matd.setTypeName(rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_VERSION));
lrw = rewriter.getListRewrite(mdGetter, MethodDeclaration.MODIFIERS2_PROPERTY);
lrw.insertFirst(matd, null);
}
}
return true;
}
use of org.eclipse.jdt.core.dom.Modifier in project eclipse-cs by checkstyle.
the class DesignForExtensionQuickfix method handleGetCorrectingASTVisitor.
/**
* {@inheritDoc}
*/
@Override
protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartOffset) {
return new ASTVisitor() {
@SuppressWarnings("unchecked")
@Override
public boolean visit(MethodDeclaration 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<ASTNode> 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 FinalParametersQuickfix 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;
}
};
}
Aggregations