use of org.eclipse.jdt.core.dom.ASTNode in project che by eclipse.
the class JavadocTagsSubProcessor method insertTag.
public static void insertTag(ListRewrite rewriter, TagElement newElement, Set<String> sameKindLeadingNames, TextEditGroup groupDescription) {
List<? extends ASTNode> tags = rewriter.getRewrittenList();
String insertedTagName = newElement.getTagName();
ASTNode after = null;
int tagRanking = getTagRanking(insertedTagName);
for (int i = tags.size() - 1; i >= 0; i--) {
TagElement curr = (TagElement) tags.get(i);
String tagName = curr.getTagName();
if (tagName == null || tagRanking > getTagRanking(tagName)) {
after = curr;
break;
}
if (sameKindLeadingNames != null && isSameTag(insertedTagName, tagName)) {
String arg = getArgument(curr);
if (arg != null && sameKindLeadingNames.contains(arg)) {
after = curr;
break;
}
}
}
if (after != null) {
rewriter.insertAfter(newElement, after, groupDescription);
} else {
rewriter.insertFirst(newElement, groupDescription);
}
}
use of org.eclipse.jdt.core.dom.ASTNode in project che by eclipse.
the class RenameLinkedModeRefactoringSession method getModel.
public LinkedModeModel getModel() {
CompilationUnit root = SharedASTProvider.getAST(compilationUnit, SharedASTProvider.WAIT_YES, null);
LinkedPositionGroupImpl group = new LinkedPositionGroupImpl();
ASTNode selectedNode = NodeFinder.perform(root, offset, 0);
if (!(selectedNode instanceof SimpleName)) {
return null;
}
SimpleName nameNode = (SimpleName) selectedNode;
fOriginalName = nameNode.getIdentifier();
final int pos = nameNode.getStartPosition();
ASTNode[] sameNodes = LinkedNodeFinder.findByNode(root, nameNode);
//TODO: copied from LinkedNamesAssistProposal#apply(..):
// sort for iteration order, starting with the node @ offset
Arrays.sort(sameNodes, new Comparator<ASTNode>() {
public int compare(ASTNode o1, ASTNode o2) {
return rank(o1) - rank(o2);
}
/**
* Returns the absolute rank of an <code>ASTNode</code>. Nodes
* preceding <code>pos</code> are ranked last.
*
* @param node the node to compute the rank for
* @return the rank of the node with respect to the invocation offset
*/
private int rank(ASTNode node) {
int relativeRank = node.getStartPosition() + node.getLength() - pos;
if (relativeRank < 0)
return Integer.MAX_VALUE + relativeRank;
else
return relativeRank;
}
});
for (int i = 0; i < sameNodes.length; i++) {
ASTNode elem = sameNodes[i];
RegionImpl position = new RegionImpl();
position.setOffset(elem.getStartPosition());
position.setLength(elem.getLength());
group.addPositions(position);
}
LinkedModeModelImpl model = new LinkedModeModelImpl();
model.addGroups(group);
return model;
}
use of org.eclipse.jdt.core.dom.ASTNode in project che by eclipse.
the class AssociativeInfixExpressionFragment method replace.
public void replace(ASTRewrite rewrite, ASTNode replacement, TextEditGroup textEditGroup) {
ASTNode groupNode = getGroupRoot();
List<Expression> allOperands = findGroupMembersInOrderFor(getGroupRoot());
if (allOperands.size() == fOperands.size()) {
if (replacement instanceof Name && groupNode.getParent() instanceof ParenthesizedExpression) {
// replace including the parenthesized expression around it
rewrite.replace(groupNode.getParent(), replacement, textEditGroup);
} else {
rewrite.replace(groupNode, replacement, textEditGroup);
}
return;
}
rewrite.replace(fOperands.get(0), replacement, textEditGroup);
int first = allOperands.indexOf(fOperands.get(0));
int after = first + fOperands.size();
for (int i = first + 1; i < after; i++) {
rewrite.remove(allOperands.get(i), textEditGroup);
}
}
use of org.eclipse.jdt.core.dom.ASTNode in project che by eclipse.
the class ConvertIterableLoopOperation method convert.
@Override
protected Statement convert(CompilationUnitRewrite cuRewrite, final TextEditGroup group, final LinkedProposalModel positionGroups) throws CoreException {
final AST ast = cuRewrite.getAST();
final ASTRewrite astRewrite = cuRewrite.getASTRewrite();
final ImportRewrite importRewrite = cuRewrite.getImportRewrite();
final ImportRemover remover = cuRewrite.getImportRemover();
fEnhancedForLoop = ast.newEnhancedForStatement();
String[] names = getVariableNameProposals();
String name;
if (fElementVariable != null) {
name = fElementVariable.getName();
} else {
name = names[0];
}
final LinkedProposalPositionGroup pg = positionGroups.getPositionGroup(name, true);
if (fElementVariable != null)
pg.addProposal(name, null, 10);
for (int i = 0; i < names.length; i++) {
pg.addProposal(names[i], null, 10);
}
final Statement body = getForStatement().getBody();
if (body != null) {
final ListRewrite list;
if (body instanceof Block) {
list = astRewrite.getListRewrite(body, Block.STATEMENTS_PROPERTY);
for (final Iterator<Expression> iterator = fOccurrences.iterator(); iterator.hasNext(); ) {
final Statement parent = (Statement) ASTNodes.getParent(iterator.next(), Statement.class);
if (parent != null && list.getRewrittenList().contains(parent)) {
list.remove(parent, null);
remover.registerRemovedNode(parent);
}
}
} else {
list = null;
}
final String text = name;
body.accept(new ASTVisitor() {
private boolean replace(final Expression expression) {
final SimpleName node = ast.newSimpleName(text);
astRewrite.replace(expression, node, group);
remover.registerRemovedNode(expression);
pg.addPosition(astRewrite.track(node), false);
return false;
}
@Override
public final boolean visit(final MethodInvocation node) {
final IMethodBinding binding = node.resolveMethodBinding();
if (binding != null && (binding.getName().equals("next") || binding.getName().equals("nextElement"))) {
//$NON-NLS-1$ //$NON-NLS-2$
final Expression expression = node.getExpression();
if (expression instanceof Name) {
final IBinding result = ((Name) expression).resolveBinding();
if (result != null && result.equals(fIteratorVariable))
return replace(node);
} else if (expression instanceof FieldAccess) {
final IBinding result = ((FieldAccess) expression).resolveFieldBinding();
if (result != null && result.equals(fIteratorVariable))
return replace(node);
}
}
return super.visit(node);
}
@Override
public final boolean visit(final SimpleName node) {
if (fElementVariable != null) {
final IBinding binding = node.resolveBinding();
if (binding != null && binding.equals(fElementVariable)) {
final Statement parent = (Statement) ASTNodes.getParent(node, Statement.class);
if (parent != null && (list == null || list.getRewrittenList().contains(parent)))
pg.addPosition(astRewrite.track(node), false);
}
}
return false;
}
});
fEnhancedForLoop.setBody(getBody(cuRewrite, group, positionGroups));
}
final SingleVariableDeclaration declaration = ast.newSingleVariableDeclaration();
final SimpleName simple = ast.newSimpleName(name);
pg.addPosition(astRewrite.track(simple), true);
declaration.setName(simple);
final ITypeBinding elementType = getElementType(fIteratorVariable.getType());
declaration.setType(importType(elementType, getForStatement(), importRewrite, getRoot()));
if (fMakeFinal) {
ModifierRewrite.create(astRewrite, declaration).setModifiers(Modifier.FINAL, 0, group);
}
remover.registerAddedImport(elementType.getQualifiedName());
fEnhancedForLoop.setParameter(declaration);
fEnhancedForLoop.setExpression(getExpression(astRewrite));
for (Iterator<Expression> iterator = getForStatement().initializers().iterator(); iterator.hasNext(); ) {
ASTNode node = iterator.next();
if (node instanceof VariableDeclarationExpression) {
VariableDeclarationExpression variableDeclarationExpression = (VariableDeclarationExpression) node;
remover.registerRemovedNode(variableDeclarationExpression.getType());
} else {
remover.registerRemovedNode(node);
}
}
for (Iterator<Expression> iterator = getForStatement().updaters().iterator(); iterator.hasNext(); ) {
ASTNode node = iterator.next();
remover.registerRemovedNode(node);
}
return fEnhancedForLoop;
}
use of org.eclipse.jdt.core.dom.ASTNode in project che by eclipse.
the class Java50Fix method getRawReference.
private static SimpleType getRawReference(SimpleName name, CompilationUnit compilationUnit) {
SimpleName[] names = LinkedNodeFinder.findByNode(compilationUnit, name);
for (int j = 0; j < names.length; j++) {
if (names[j].getParent() instanceof VariableDeclarationFragment) {
VariableDeclarationFragment fragment = (VariableDeclarationFragment) names[j].getParent();
if (fragment.getParent() instanceof VariableDeclarationStatement) {
VariableDeclarationStatement statement = (VariableDeclarationStatement) fragment.getParent();
ASTNode result = (ASTNode) statement.getStructuralProperty(VariableDeclarationStatement.TYPE_PROPERTY);
if (isRawTypeReference(result))
return (SimpleType) result;
} else if (fragment.getParent() instanceof FieldDeclaration) {
FieldDeclaration declaration = (FieldDeclaration) fragment.getParent();
ASTNode result = (ASTNode) declaration.getStructuralProperty(FieldDeclaration.TYPE_PROPERTY);
if (isRawTypeReference(result))
return (SimpleType) result;
}
} else if (names[j].getParent() instanceof SingleVariableDeclaration) {
SingleVariableDeclaration declaration = (SingleVariableDeclaration) names[j].getParent();
ASTNode result = (ASTNode) declaration.getStructuralProperty(SingleVariableDeclaration.TYPE_PROPERTY);
if (isRawTypeReference(result))
return (SimpleType) result;
} else if (names[j].getParent() instanceof MethodDeclaration) {
MethodDeclaration methodDecl = (MethodDeclaration) names[j].getParent();
ASTNode result = (ASTNode) methodDecl.getStructuralProperty(MethodDeclaration.RETURN_TYPE2_PROPERTY);
if (isRawTypeReference(result))
return (SimpleType) result;
}
}
return null;
}
Aggregations