use of org.eclipse.jdt.core.dom.Modifier in project AutoRefactor by JnRouvignac.
the class ObsoleteLocalVariableRatherThanFieldCleanUp method replaceFieldByLocalVariable.
private void replaceFieldByLocalVariable(final FieldDeclaration field, final VariableDeclarationFragment fragment, final SimpleName reassignment) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteLocalVariableRatherThanFieldCleanUp_description);
boolean isFieldKept = field.fragments().size() != 1;
Assignment reassignmentAssignment = (Assignment) reassignment.getParent();
VariableDeclarationFragment newFragment = ast.newVariableDeclarationFragment(ASTNodes.createMoveTarget(rewrite, reassignment), ASTNodes.createMoveTarget(rewrite, reassignmentAssignment.getRightHandSide()));
List<Dimension> extraDimensions = fragment.extraDimensions();
List<Dimension> newExtraDimensions = newFragment.extraDimensions();
newExtraDimensions.addAll(ASTNodes.createMoveTarget(rewrite, extraDimensions));
VariableDeclarationStatement newDeclareStatement = ast.newVariableDeclarationStatement(isFieldKept ? ASTNodes.createMoveTarget(rewrite, field.getType()) : ast.createCopyTarget(field.getType()), newFragment);
List<IExtendedModifier> modifiers = field.modifiers();
List<IExtendedModifier> newModifiers = newDeclareStatement.modifiers();
for (IExtendedModifier iExtendedModifier : modifiers) {
Modifier modifier = (Modifier) iExtendedModifier;
if (!modifier.isPrivate() && !modifier.isStatic()) {
newModifiers.add(isFieldKept ? ASTNodes.createMoveTarget(rewrite, modifier) : ast.createCopyTarget(modifier));
}
}
ASTNodes.replaceButKeepComment(rewrite, ASTNodes.getTypedAncestor(reassignmentAssignment, Statement.class), newDeclareStatement, group);
if (isFieldKept) {
rewrite.remove(fragment, group);
ASTNodes.replaceButKeepComment(rewrite, field.getType(), ast.createCopyTarget(field.getType()), group);
} else {
rewrite.remove(field, group);
}
}
use of org.eclipse.jdt.core.dom.Modifier in project AutoRefactor by JnRouvignac.
the class DeclarationOutsideLoopRatherThanInsideCleanUp method moveDeclaration.
private void moveDeclaration(final Statement statement, final VariableDeclarationStatement varToMove) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.DeclarationOutsideLoopRatherThanInsideCleanUp_description);
VariableDeclarationFragment fragment = (VariableDeclarationFragment) varToMove.fragments().get(0);
if (fragment.getInitializer() != null) {
Type copyOfType = ast.createCopyTarget(varToMove.getType());
SimpleName name = fragment.getName();
VariableDeclarationFragment newFragment = ast.newVariableDeclarationFragment(ast.createCopyTarget(name));
List<Dimension> extraDimensions = fragment.extraDimensions();
List<Dimension> newExtraDimensions = newFragment.extraDimensions();
newExtraDimensions.addAll(ASTNodes.createMoveTarget(rewrite, extraDimensions));
VariableDeclarationStatement newDeclareStatement = ast.newVariableDeclarationStatement(copyOfType, newFragment);
List<IExtendedModifier> modifiers = varToMove.modifiers();
List<IExtendedModifier> newModifiers = newDeclareStatement.modifiers();
for (IExtendedModifier iExtendedModifier : modifiers) {
Modifier modifier = (Modifier) iExtendedModifier;
if (!modifier.isPrivate() && !modifier.isStatic()) {
newModifiers.add(ASTNodes.createMoveTarget(rewrite, modifier));
}
}
rewrite.insertBefore(newDeclareStatement, statement, group);
ASTNodes.replaceButKeepComment(rewrite, varToMove, ast.newExpressionStatement(ast.newAssignment(ast.createCopyTarget(name), Assignment.Operator.ASSIGN, ASTNodes.createMoveTarget(rewrite, fragment.getInitializer()))), group);
} else {
rewrite.insertBefore(ASTNodes.createMoveTarget(rewrite, varToMove), statement, group);
rewrite.remove(varToMove, group);
}
}
use of org.eclipse.jdt.core.dom.Modifier in project AutoRefactor by JnRouvignac.
the class ImplicitDefaultConstructorRatherThanWrittenOneCleanUp method visit.
@Override
public boolean visit(final TypeDeclaration visited) {
if (!visited.isInterface()) {
MethodDeclaration uniqueConstructor = null;
boolean isPublicClass = false;
boolean isProtectedClass = false;
boolean isPackageClass = true;
boolean isPrivateClass = false;
for (IExtendedModifier extendedModifier : (List<IExtendedModifier>) visited.modifiers()) {
if (extendedModifier.isModifier()) {
Modifier modifier = (Modifier) extendedModifier;
if (modifier.isPublic()) {
isPublicClass = true;
isPackageClass = false;
break;
}
if (modifier.isProtected()) {
isProtectedClass = true;
isPackageClass = false;
break;
}
if (modifier.isPrivate()) {
isPrivateClass = true;
isPackageClass = false;
break;
}
}
}
for (MethodDeclaration methodDeclaration : visited.getMethods()) {
if (methodDeclaration.isConstructor()) {
if (uniqueConstructor != null) {
// Too much constructors
return true;
}
uniqueConstructor = methodDeclaration;
}
}
if (uniqueConstructor != null && (!isCheckedExceptionThrown(uniqueConstructor) || visited.getSuperclassType() == null || ASTNodes.hasType(visited.getSuperclassType().resolveBinding(), Object.class.getCanonicalName())) && Utils.isEmpty(uniqueConstructor.parameters()) && isDefaultStatements(uniqueConstructor)) {
if (uniqueConstructor.modifiers() != null && uniqueConstructor.modifiers().size() == 1) {
IExtendedModifier extendedModifier = (IExtendedModifier) uniqueConstructor.modifiers().get(0);
if (extendedModifier.isModifier()) {
Modifier modifier = (Modifier) extendedModifier;
if (modifier.isPublic() && isPublicClass || modifier.isProtected() && isProtectedClass || modifier.isPrivate() && isPrivateClass) {
TextEditGroup group = new TextEditGroup(MultiFixMessages.ImplicitDefaultConstructorRatherThanWrittenOneCleanUp_description);
ASTRewrite rewrite = cuRewrite.getASTRewrite();
rewrite.remove(uniqueConstructor, group);
return false;
}
}
} else if (Utils.isEmpty(uniqueConstructor.modifiers()) && isPackageClass) {
TextEditGroup group = new TextEditGroup(MultiFixMessages.ImplicitDefaultConstructorRatherThanWrittenOneCleanUp_description);
ASTRewrite rewrite = cuRewrite.getASTRewrite();
rewrite.remove(uniqueConstructor, group);
return false;
}
}
}
return true;
}
use of org.eclipse.jdt.core.dom.Modifier in project AutoRefactor by JnRouvignac.
the class ObsoleteStaticInnerClassThanNonStaticCleanUp method makeStatic.
private void makeStatic(final TypeDeclaration node) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteStaticInnerClassThanNonStaticCleanUp_description);
List<IExtendedModifier> modifiers = node.modifiers();
Modifier static0 = ast.static0();
if (modifiers.isEmpty()) {
rewrite.insertBefore(static0, node, group);
} else {
IExtendedModifier lastModifier = modifiers.get(modifiers.size() - 1);
if (!lastModifier.isModifier()) {
rewrite.insertAfter(static0, (Annotation) lastModifier, group);
} else if (((Modifier) lastModifier).isFinal()) {
rewrite.insertBefore(static0, (Modifier) lastModifier, group);
} else {
rewrite.insertAfter(static0, (Modifier) lastModifier, group);
}
}
}
use of org.eclipse.jdt.core.dom.Modifier in project AutoRefactor by JnRouvignac.
the class ObsoleteRedundantModifiersCleanUp method removePrivateModifier.
private boolean removePrivateModifier(final BodyDeclaration visited) {
List<IExtendedModifier> modifiers = visited.modifiers();
for (Modifier modifier : getModifiersOnly(modifiers)) {
if (modifier.isPrivate()) {
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteRedundantModifiersCleanUp_description);
ASTRewrite rewrite = cuRewrite.getASTRewrite();
rewrite.remove(modifier, group);
return false;
}
}
return true;
}
Aggregations