use of org.eclipse.jdt.core.dom.IExtendedModifier in project AutoRefactor by JnRouvignac.
the class ObsoleteRedundantModifiersCleanUp method removeProtectedModifier.
private boolean removeProtectedModifier(final BodyDeclaration visited) {
List<IExtendedModifier> modifiers = visited.modifiers();
for (Modifier modifier : getModifiersOnly(modifiers)) {
if (modifier.isProtected()) {
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteRedundantModifiersCleanUp_description);
ASTRewrite rewrite = cuRewrite.getASTRewrite();
rewrite.remove(modifier, group);
return false;
}
}
return true;
}
use of org.eclipse.jdt.core.dom.IExtendedModifier in project AutoRefactor by JnRouvignac.
the class StaticConstantRatherThanInstanceConstantCleanUp method visit.
@Override
public boolean visit(final FieldDeclaration visited) {
if (visited.getType().isPrimitiveType() || ASTNodes.hasType(visited.getType().resolveBinding(), Byte.class.getCanonicalName(), Character.class.getCanonicalName(), Short.class.getCanonicalName(), Integer.class.getCanonicalName(), Long.class.getCanonicalName(), Boolean.class.getCanonicalName(), Float.class.getCanonicalName(), Double.class.getCanonicalName(), String.class.getCanonicalName())) {
Modifier finalModifier = null;
Collection<IExtendedModifier> modifiers = visited.modifiers();
for (Modifier modifier : getModifiersOnly(modifiers)) {
if (modifier.isStatic()) {
return true;
}
if (modifier.isFinal()) {
finalModifier = modifier;
}
}
if (finalModifier != null && visited.fragments() != null && visited.fragments().size() == 1) {
Expression initializer = ((VariableDeclarationFragment) visited.fragments().get(0)).getInitializer();
if (ASTNodes.isHardCoded(initializer)) {
addStaticModifier(finalModifier);
return false;
}
}
}
return true;
}
use of org.eclipse.jdt.core.dom.IExtendedModifier in project che by eclipse.
the class ModifierRewrite method internalSetModifiers.
/**
* Sets the given modifiers and removes all other modifiers that match the consideredFlags mask.
* Does not touch other flags and leaves annotations in place.
*
* @param modifiers the modifiers to set
* @param consideredFlags mask of modifiers to consider
* @param editGroup the edit group in which to collect the corresponding text edits, or
* <code>null</code> if ungrouped
* @return a tracked position that contains the changed modifiers
*/
private PositionInformation internalSetModifiers(int modifiers, int consideredFlags, TextEditGroup editGroup) {
int newModifiers = modifiers & consideredFlags;
ITrackedNodePosition trackedFallback = null;
List<ITrackedNodePosition> trackedNodes = new ArrayList<ITrackedNodePosition>();
// remove modifiers
List<IExtendedModifier> originalList = fModifierRewrite.getOriginalList();
for (int i = 0; i < originalList.size(); i++) {
ASTNode curr = (ASTNode) originalList.get(i);
if (curr instanceof Modifier) {
int flag = ((Modifier) curr).getKeyword().toFlagValue();
if ((consideredFlags & flag) != 0) {
if ((newModifiers & flag) == 0) {
fModifierRewrite.remove(curr, editGroup);
if (trackedFallback == null)
trackedFallback = fModifierRewrite.getASTRewrite().track(curr);
}
newModifiers &= ~flag;
}
}
}
// find last annotation
IExtendedModifier lastAnnotation = null;
List<IExtendedModifier> extendedList = fModifierRewrite.getRewrittenList();
for (int i = 0; i < extendedList.size(); i++) {
IExtendedModifier curr = extendedList.get(i);
if (curr.isAnnotation())
lastAnnotation = curr;
}
// add modifiers
List<Modifier> newNodes = ASTNodeFactory.newModifiers(fAst, newModifiers);
for (int i = 0; i < newNodes.size(); i++) {
Modifier curr = newNodes.get(i);
if ((curr.getKeyword().toFlagValue() & VISIBILITY_MODIFIERS) != 0) {
if (lastAnnotation != null)
fModifierRewrite.insertAfter(curr, (ASTNode) lastAnnotation, editGroup);
else
fModifierRewrite.insertFirst(curr, editGroup);
} else {
fModifierRewrite.insertLast(curr, editGroup);
}
trackedNodes.add(fModifierRewrite.getASTRewrite().track(curr));
}
if (trackedNodes.isEmpty()) {
if (trackedFallback == null) {
// out of tricks...
trackedFallback = fModifierRewrite.getASTRewrite().track(fModifierRewrite.getParent());
}
return new LinkedProposalPositionGroup.StartPositionInformation(trackedFallback);
} else {
return new LinkedProposalPositionGroup.TrackedNodesPosition(trackedNodes);
}
}
use of org.eclipse.jdt.core.dom.IExtendedModifier in project che by eclipse.
the class StubUtility2 method getImplementationModifiers.
private static List<IExtendedModifier> getImplementationModifiers(AST ast, IMethodBinding method, boolean inInterface, ImportRewrite importRewrite, ImportRewriteContext context) throws JavaModelException {
IJavaProject javaProject = importRewrite.getCompilationUnit().getJavaProject();
int modifiers = method.getModifiers() & ~Modifier.ABSTRACT & ~Modifier.NATIVE & ~Modifier.PRIVATE;
if (inInterface) {
modifiers = modifiers & ~Modifier.PROTECTED;
if (!method.getDeclaringClass().isInterface()) {
modifiers = modifiers | Modifier.PUBLIC;
}
} else {
modifiers = modifiers & ~Modifier.DEFAULT;
}
IAnnotationBinding[] annotations = method.getAnnotations();
if (modifiers != Modifier.NONE && annotations.length > 0) {
// need an AST of the source method to preserve order of modifiers
IMethod iMethod = (IMethod) method.getJavaElement();
if (iMethod != null && JavaElementUtil.isSourceAvailable(iMethod)) {
CheASTParser parser = CheASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
parser.setSource(iMethod.getTypeRoot());
parser.setIgnoreMethodBodies(true);
CompilationUnit otherCU = (CompilationUnit) parser.createAST(null);
ASTNode otherMethod = NodeFinder.perform(otherCU, iMethod.getSourceRange());
if (otherMethod instanceof MethodDeclaration) {
MethodDeclaration otherMD = (MethodDeclaration) otherMethod;
ArrayList<IExtendedModifier> result = new ArrayList<IExtendedModifier>();
List<IExtendedModifier> otherModifiers = otherMD.modifiers();
for (IExtendedModifier otherModifier : otherModifiers) {
if (otherModifier instanceof Modifier) {
int otherFlag = ((Modifier) otherModifier).getKeyword().toFlagValue();
if ((otherFlag & modifiers) != 0) {
modifiers = ~otherFlag & modifiers;
result.addAll(ast.newModifiers(otherFlag));
}
} else {
Annotation otherAnnotation = (Annotation) otherModifier;
String n = otherAnnotation.getTypeName().getFullyQualifiedName();
for (IAnnotationBinding annotation : annotations) {
ITypeBinding otherAnnotationType = annotation.getAnnotationType();
String qn = otherAnnotationType.getQualifiedName();
if (qn.endsWith(n) && (qn.length() == n.length() || qn.charAt(qn.length() - n.length() - 1) == '.')) {
if (StubUtility2.isCopyOnInheritAnnotation(otherAnnotationType, javaProject))
result.add(importRewrite.addAnnotation(annotation, ast, context));
break;
}
}
}
}
result.addAll(ASTNodeFactory.newModifiers(ast, modifiers));
return result;
}
}
}
ArrayList<IExtendedModifier> result = new ArrayList<IExtendedModifier>();
for (IAnnotationBinding annotation : annotations) {
if (StubUtility2.isCopyOnInheritAnnotation(annotation.getAnnotationType(), javaProject))
result.add(importRewrite.addAnnotation(annotation, ast, context));
}
result.addAll(ASTNodeFactory.newModifiers(ast, modifiers));
return result;
}
use of org.eclipse.jdt.core.dom.IExtendedModifier in project che by eclipse.
the class SurroundWithTryCatchRefactoring method createTryCatchStatement.
private void createTryCatchStatement(org.eclipse.jdt.core.IBuffer buffer, String lineDelimiter) throws CoreException {
List<Statement> result = new ArrayList<Statement>(1);
TryStatement tryStatement = getAST().newTryStatement();
ITypeBinding[] exceptions = fAnalyzer.getExceptions();
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(fAnalyzer.getEnclosingBodyDeclaration(), fImportRewrite);
if (!fIsMultiCatch) {
for (int i = 0; i < exceptions.length; i++) {
ITypeBinding exception = exceptions[i];
CatchClause catchClause = getAST().newCatchClause();
tryStatement.catchClauses().add(catchClause);
SingleVariableDeclaration decl = getAST().newSingleVariableDeclaration();
String varName = StubUtility.getExceptionVariableName(fCUnit.getJavaProject());
String name = fScope.createName(varName, false);
decl.setName(getAST().newSimpleName(name));
Type type = fImportRewrite.addImport(exception, getAST(), context);
decl.setType(type);
catchClause.setException(decl);
Statement st = getCatchBody(ASTNodes.getQualifiedTypeName(type), name, lineDelimiter);
if (st != null) {
catchClause.getBody().statements().add(st);
}
fLinkedProposalModel.getPositionGroup(GROUP_EXC_TYPE + i, true).addPosition(fRewriter.track(decl.getType()), i == 0);
fLinkedProposalModel.getPositionGroup(GROUP_EXC_NAME + i, true).addPosition(fRewriter.track(decl.getName()), false);
}
} else {
List<ITypeBinding> filteredExceptions = filterSubtypeExceptions(exceptions);
CatchClause catchClause = getAST().newCatchClause();
SingleVariableDeclaration decl = getAST().newSingleVariableDeclaration();
String varName = StubUtility.getExceptionVariableName(fCUnit.getJavaProject());
String name = fScope.createName(varName, false);
decl.setName(getAST().newSimpleName(name));
UnionType unionType = getAST().newUnionType();
List<Type> types = unionType.types();
int i = 0;
for (ITypeBinding exception : filteredExceptions) {
Type type = fImportRewrite.addImport(exception, getAST(), context);
types.add(type);
fLinkedProposalModel.getPositionGroup(GROUP_EXC_TYPE + i, true).addPosition(fRewriter.track(type), i == 0);
i++;
}
decl.setType(unionType);
catchClause.setException(decl);
fLinkedProposalModel.getPositionGroup(GROUP_EXC_NAME + 0, true).addPosition(fRewriter.track(decl.getName()), false);
//$NON-NLS-1$
Statement st = getCatchBody("Exception", name, lineDelimiter);
if (st != null) {
catchClause.getBody().statements().add(st);
}
tryStatement.catchClauses().add(catchClause);
}
List<ASTNode> variableDeclarations = getSpecialVariableDeclarationStatements();
ListRewrite statements = fRewriter.getListRewrite(tryStatement.getBody(), Block.STATEMENTS_PROPERTY);
boolean selectedNodeRemoved = false;
ASTNode expressionStatement = null;
for (int i = 0; i < fSelectedNodes.length; i++) {
ASTNode node = fSelectedNodes[i];
if (node instanceof VariableDeclarationStatement && variableDeclarations.contains(node)) {
AST ast = getAST();
VariableDeclarationStatement statement = (VariableDeclarationStatement) node;
// Create a copy and remove the initializer
VariableDeclarationStatement copy = (VariableDeclarationStatement) ASTNode.copySubtree(ast, statement);
List<IExtendedModifier> modifiers = copy.modifiers();
for (Iterator<IExtendedModifier> iter = modifiers.iterator(); iter.hasNext(); ) {
IExtendedModifier modifier = iter.next();
if (modifier.isModifier() && Modifier.isFinal(((Modifier) modifier).getKeyword().toFlagValue())) {
iter.remove();
}
}
List<VariableDeclarationFragment> fragments = copy.fragments();
for (Iterator<VariableDeclarationFragment> iter = fragments.iterator(); iter.hasNext(); ) {
VariableDeclarationFragment fragment = iter.next();
fragment.setInitializer(null);
}
CompilationUnit root = (CompilationUnit) statement.getRoot();
int extendedStart = root.getExtendedStartPosition(statement);
// we have a leading comment and the comment is covered by the selection
if (extendedStart != statement.getStartPosition() && extendedStart >= fSelection.getOffset()) {
String commentToken = buffer.getText(extendedStart, statement.getStartPosition() - extendedStart);
commentToken = Strings.trimTrailingTabsAndSpaces(commentToken);
Type type = statement.getType();
String typeName = buffer.getText(type.getStartPosition(), type.getLength());
copy.setType((Type) fRewriter.createStringPlaceholder(commentToken + typeName, type.getNodeType()));
}
result.add(copy);
// convert the fragments into expression statements
fragments = statement.fragments();
if (!fragments.isEmpty()) {
List<ExpressionStatement> newExpressionStatements = new ArrayList<ExpressionStatement>();
for (Iterator<VariableDeclarationFragment> iter = fragments.iterator(); iter.hasNext(); ) {
VariableDeclarationFragment fragment = iter.next();
Expression initializer = fragment.getInitializer();
if (initializer != null) {
Assignment assignment = ast.newAssignment();
assignment.setLeftHandSide((Expression) fRewriter.createCopyTarget(fragment.getName()));
assignment.setRightHandSide((Expression) fRewriter.createCopyTarget(initializer));
newExpressionStatements.add(ast.newExpressionStatement(assignment));
}
}
if (!newExpressionStatements.isEmpty()) {
if (fSelectedNodes.length == 1) {
expressionStatement = fRewriter.createGroupNode(newExpressionStatements.toArray(new ASTNode[newExpressionStatements.size()]));
} else {
fRewriter.replace(statement, fRewriter.createGroupNode(newExpressionStatements.toArray(new ASTNode[newExpressionStatements.size()])), null);
}
} else {
fRewriter.remove(statement, null);
selectedNodeRemoved = true;
}
} else {
fRewriter.remove(statement, null);
selectedNodeRemoved = true;
}
}
}
result.add(tryStatement);
ASTNode replacementNode;
if (result.size() == 1) {
replacementNode = result.get(0);
} else {
replacementNode = fRewriter.createGroupNode(result.toArray(new ASTNode[result.size()]));
}
if (fSelectedNodes.length == 1) {
if (expressionStatement != null) {
statements.insertLast(expressionStatement, null);
} else {
if (!selectedNodeRemoved)
statements.insertLast(fRewriter.createMoveTarget(fSelectedNodes[0]), null);
}
fRewriter.replace(fSelectedNodes[0], replacementNode, null);
} else {
ListRewrite source = fRewriter.getListRewrite(fSelectedNodes[0].getParent(), (ChildListPropertyDescriptor) fSelectedNodes[0].getLocationInParent());
ASTNode toMove = source.createMoveTarget(fSelectedNodes[0], fSelectedNodes[fSelectedNodes.length - 1], replacementNode, null);
statements.insertLast(toMove, null);
}
}
Aggregations