org.eclipse.jdt.core.dom.StructuralPropertyDescriptor Java Examples
The following examples show how to use
org.eclipse.jdt.core.dom.StructuralPropertyDescriptor.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EmptyStatementQuickfix.java From eclipse-cs with GNU Lesser General Public License v2.1 | 6 votes |
/** * {@inheritDoc} */ @Override protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartPosition) { return new ASTVisitor() { @Override public boolean visit(EmptyStatement node) { if (containsPosition(lineInfo, node.getStartPosition())) { // early exit if the statement is mandatory, e.g. only // statement in a for-statement without block StructuralPropertyDescriptor p = node.getLocationInParent(); if (p.isChildProperty() && ((ChildPropertyDescriptor) p).isMandatory()) { return false; } node.delete(); } return false; } }; }
Example #2
Source File: IntroduceFactoryRefactoring.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
/** * Finds and returns the <code>ASTNode</code> for the given source text * selection, if it is an entire constructor call or the class name portion * of a constructor call or constructor declaration, or null otherwise. * @param unit The compilation unit in which the selection was made * @param offset The textual offset of the start of the selection * @param length The length of the selection in characters * @return ClassInstanceCreation or MethodDeclaration */ private ASTNode getTargetNode(ICompilationUnit unit, int offset, int length) { ASTNode node= ASTNodes.getNormalizedNode(NodeFinder.perform(fCU, offset, length)); if (node.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION) return node; if (node.getNodeType() == ASTNode.METHOD_DECLARATION && ((MethodDeclaration)node).isConstructor()) return node; // we have some sub node. Make sure its the right child of the parent StructuralPropertyDescriptor location= node.getLocationInParent(); ASTNode parent= node.getParent(); if (location == ClassInstanceCreation.TYPE_PROPERTY) { return parent; } else if (location == MethodDeclaration.NAME_PROPERTY && ((MethodDeclaration)parent).isConstructor()) { return parent; } return null; }
Example #3
Source File: InlineConstantRefactoring.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
@Override public boolean visit(Name name) { StructuralPropertyDescriptor locationInParent= name.getLocationInParent(); if (locationInParent == ExpressionMethodReference.NAME_PROPERTY || locationInParent == TypeMethodReference.NAME_PROPERTY || locationInParent == SuperMethodReference.NAME_PROPERTY) { return false; } SimpleName leftmost= getLeftmost(name); IBinding leftmostBinding= leftmost.resolveBinding(); if (leftmostBinding instanceof IVariableBinding || leftmostBinding instanceof IMethodBinding || leftmostBinding instanceof ITypeBinding) { if (shouldUnqualify(leftmost)) unqualifyMemberName(leftmost); else qualifyUnqualifiedMemberNameIfNecessary(leftmost); } if (leftmostBinding instanceof ITypeBinding) { String addedImport= fNewLocationCuRewrite.getImportRewrite().addImport((ITypeBinding)leftmostBinding, fNewLocationContext); fNewLocationCuRewrite.getImportRemover().registerAddedImport(addedImport); } return false; }
Example #4
Source File: ASTResolving.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
public static BodyDeclaration findParentBodyDeclaration(ASTNode node, boolean treatModifiersOutside) { StructuralPropertyDescriptor lastLocation= null; while (node != null) { if (node instanceof BodyDeclaration) { BodyDeclaration decl= (BodyDeclaration) node; if (!treatModifiersOutside || lastLocation != decl.getModifiersProperty()) { return decl; } treatModifiersOutside= false; } lastLocation= node.getLocationInParent(); node= node.getParent(); } return (BodyDeclaration) node; }
Example #5
Source File: ASTResolving.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
/** * Finds the ancestor type of <code>node</code> (includes <code>node</code> in the search). * * @param node the node to start the search from, can be <code>null</code> * @param treatModifiersOutside if set, modifiers are not part of their type, but of the type's * parent * @return returns the ancestor type of <code>node</code> (AbstractTypeDeclaration or * AnonymousTypeDeclaration) if any (including <code>node</code>), <code>null</code> * otherwise */ public static ASTNode findParentType(ASTNode node, boolean treatModifiersOutside) { StructuralPropertyDescriptor lastLocation= null; while (node != null) { if (node instanceof AbstractTypeDeclaration) { AbstractTypeDeclaration decl= (AbstractTypeDeclaration) node; if (!treatModifiersOutside || lastLocation != decl.getModifiersProperty()) { return decl; } } else if (node instanceof AnonymousClassDeclaration) { return node; } lastLocation= node.getLocationInParent(); node= node.getParent(); } return null; }
Example #6
Source File: QuickAssistProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private static <T extends ASTNode> T getEnclosingHeader(ASTNode node, Class<T> headerType, StructuralPropertyDescriptor... headerProperties) { if (headerType.isInstance(node)) return headerType.cast(node); while (node != null) { ASTNode parent= node.getParent(); if (headerType.isInstance(parent)) { StructuralPropertyDescriptor locationInParent= node.getLocationInParent(); for (StructuralPropertyDescriptor property : headerProperties) { if (locationInParent == property) return headerType.cast(parent); } return null; } node= parent; } return null; }
Example #7
Source File: Bindings.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
/** * Returns the type binding of the node's type context or null if the node is inside * an annotation, type parameter, super type declaration, or Javadoc of a top level type. * The result of this method is equal to the result of {@link #getBindingOfParentType(ASTNode)} for nodes in the type's body. * * @param node an AST node * @return the type binding of the node's parent type context, or <code>null</code> */ public static ITypeBinding getBindingOfParentTypeContext(ASTNode node) { StructuralPropertyDescriptor lastLocation= null; while (node != null) { if (node instanceof AbstractTypeDeclaration) { AbstractTypeDeclaration decl= (AbstractTypeDeclaration) node; if (lastLocation == decl.getBodyDeclarationsProperty() || lastLocation == decl.getJavadocProperty()) { return decl.resolveBinding(); } else if (decl instanceof EnumDeclaration && lastLocation == EnumDeclaration.ENUM_CONSTANTS_PROPERTY) { return decl.resolveBinding(); } } else if (node instanceof AnonymousClassDeclaration) { return ((AnonymousClassDeclaration) node).resolveBinding(); } lastLocation= node.getLocationInParent(); node= node.getParent(); } return null; }
Example #8
Source File: ASTResolving.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
public static ITypeBinding guessBindingForTypeReference(ASTNode node) { StructuralPropertyDescriptor locationInParent= node.getLocationInParent(); if (locationInParent == QualifiedName.QUALIFIER_PROPERTY) { return null; // can't guess type for X.A } if (locationInParent == SimpleType.NAME_PROPERTY || locationInParent == NameQualifiedType.NAME_PROPERTY) { node= node.getParent(); } ITypeBinding binding= Bindings.normalizeTypeBinding(getPossibleTypeBinding(node)); if (binding != null) { if (binding.isWildcardType()) { return normalizeWildcardType(binding, true, node.getAST()); } } return binding; }
Example #9
Source File: RefactoringAvailabilityTester.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private static ASTNode getInlineableMethodNode(ASTNode node, IJavaElement unit) { if (node == null) return null; switch (node.getNodeType()) { case ASTNode.SIMPLE_NAME: StructuralPropertyDescriptor locationInParent= node.getLocationInParent(); if (locationInParent == MethodDeclaration.NAME_PROPERTY) { return node.getParent(); } else if (locationInParent == MethodInvocation.NAME_PROPERTY || locationInParent == SuperMethodInvocation.NAME_PROPERTY) { return unit instanceof ICompilationUnit ? node.getParent() : null; // don't start on invocations in binary } return null; case ASTNode.EXPRESSION_STATEMENT: node= ((ExpressionStatement)node).getExpression(); } switch (node.getNodeType()) { case ASTNode.METHOD_DECLARATION: return node; case ASTNode.METHOD_INVOCATION: case ASTNode.SUPER_METHOD_INVOCATION: case ASTNode.CONSTRUCTOR_INVOCATION: return unit instanceof ICompilationUnit ? node : null; // don't start on invocations in binary } return null; }
Example #10
Source File: RewriteEventStore.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
public void addEvent(ASTNode parent, StructuralPropertyDescriptor childProperty, RewriteEvent event) { validateHasChildProperty(parent, childProperty); if (event.isListRewrite()) { validateIsListProperty(childProperty); } EventHolder holder= new EventHolder(parent, childProperty, event); List entriesList = (List) this.eventLookup.get(parent); if (entriesList != null) { for (int i= 0; i < entriesList.size(); i++) { EventHolder curr= (EventHolder) entriesList.get(i); if (curr.childProperty == childProperty) { entriesList.set(i, holder); this.lastEvent= null; return; } } } else { entriesList= new ArrayList(3); this.eventLookup.put(parent, entriesList); } entriesList.add(holder); }
Example #11
Source File: RewriteEventStore.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
public RewriteEvent getEvent(ASTNode parent, StructuralPropertyDescriptor property) { validateHasChildProperty(parent, property); if (this.lastEvent != null && this.lastEvent.parent == parent && this.lastEvent.childProperty == property) { return this.lastEvent.event; } List entriesList = (List) this.eventLookup.get(parent); if (entriesList != null) { for (int i= 0; i < entriesList.size(); i++) { EventHolder holder= (EventHolder) entriesList.get(i); if (holder.childProperty == property) { this.lastEvent= holder; return holder.event; } } } return null; }
Example #12
Source File: ASTNodes.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
public static ASTNode findParent(ASTNode node, StructuralPropertyDescriptor[][] pathes) { for (int p= 0; p < pathes.length; p++) { StructuralPropertyDescriptor[] path= pathes[p]; ASTNode current= node; int d= path.length - 1; for (; d >= 0 && current != null; d--) { StructuralPropertyDescriptor descriptor= path[d]; if (!descriptor.equals(current.getLocationInParent())) break; current= current.getParent(); } if (d < 0) return current; } return null; }
Example #13
Source File: NecessaryParenthesesChecker.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private static boolean locationNeedsParentheses(StructuralPropertyDescriptor locationInParent) { if (locationInParent instanceof ChildListPropertyDescriptor && locationInParent != InfixExpression.EXTENDED_OPERANDS_PROPERTY) { // e.g. argument lists of MethodInvocation, ClassInstanceCreation, dimensions of ArrayCreation ... return false; } if (locationInParent == VariableDeclarationFragment.INITIALIZER_PROPERTY || locationInParent == SingleVariableDeclaration.INITIALIZER_PROPERTY || locationInParent == ReturnStatement.EXPRESSION_PROPERTY || locationInParent == EnhancedForStatement.EXPRESSION_PROPERTY || locationInParent == ForStatement.EXPRESSION_PROPERTY || locationInParent == WhileStatement.EXPRESSION_PROPERTY || locationInParent == DoStatement.EXPRESSION_PROPERTY || locationInParent == AssertStatement.EXPRESSION_PROPERTY || locationInParent == AssertStatement.MESSAGE_PROPERTY || locationInParent == IfStatement.EXPRESSION_PROPERTY || locationInParent == SwitchStatement.EXPRESSION_PROPERTY || locationInParent == SwitchCase.EXPRESSION_PROPERTY || locationInParent == ArrayAccess.INDEX_PROPERTY || locationInParent == ThrowStatement.EXPRESSION_PROPERTY || locationInParent == SynchronizedStatement.EXPRESSION_PROPERTY || locationInParent == ParenthesizedExpression.EXPRESSION_PROPERTY) { return false; } return true; }
Example #14
Source File: InternalASTRewrite.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
void postAddChildEvent(ASTNode node, ASTNode child, StructuralPropertyDescriptor property) { if(property.isChildListProperty()) { ListRewriteEvent event = getListEvent(node, property); List list = (List)node.getStructuralProperty(property); int i = list.indexOf(child); int s = list.size(); int index; if(i + 1 < s) { ASTNode nextNode = (ASTNode)list.get(i + 1); index = event.getIndex(nextNode, ListRewriteEvent.NEW); } else { index = -1; } event.insert(child, index); if(child != null) { markAsMoveOrCopyTarget(node, child); } } }
Example #15
Source File: RefactorProcessor.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
private static <T extends ASTNode> T getEnclosingHeader(ASTNode node, Class<T> headerType, StructuralPropertyDescriptor... headerProperties) { if (headerType.isInstance(node)) { return headerType.cast(node); } while (node != null) { ASTNode parent = node.getParent(); if (headerType.isInstance(parent)) { StructuralPropertyDescriptor locationInParent = node.getLocationInParent(); for (StructuralPropertyDescriptor property : headerProperties) { if (locationInParent == property) { return headerType.cast(parent); } } return null; } node = parent; } return null; }
Example #16
Source File: ExtractFieldRefactoring.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
private ASTNode getEnclosingBodyNode() throws JavaModelException { ASTNode node = getSelectedExpression().getAssociatedNode(); // expression must be in a method, lambda or initializer body. // make sure it is not in method or parameter annotation StructuralPropertyDescriptor location = null; while (node != null && !(node instanceof BodyDeclaration)) { location = node.getLocationInParent(); node = node.getParent(); if (node instanceof LambdaExpression) { break; } } if (location == MethodDeclaration.BODY_PROPERTY || location == Initializer.BODY_PROPERTY || (location == LambdaExpression.BODY_PROPERTY && ((LambdaExpression) node).resolveMethodBinding() != null)) { return (ASTNode) node.getStructuralProperty(location); } return null; }
Example #17
Source File: ExtractTempRefactoring.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
private ASTNode getEnclosingBodyNode() throws JavaModelException { ASTNode node = getSelectedExpression().getAssociatedNode(); // expression must be in a method, lambda or initializer body // make sure it is not in method or parameter annotation StructuralPropertyDescriptor location = null; while (node != null && !(node instanceof BodyDeclaration)) { location = node.getLocationInParent(); node = node.getParent(); if (node instanceof LambdaExpression) { break; } } if (location == MethodDeclaration.BODY_PROPERTY || location == Initializer.BODY_PROPERTY || (location == LambdaExpression.BODY_PROPERTY && ((LambdaExpression) node).resolveMethodBinding() != null)) { return (ASTNode) node.getStructuralProperty(location); } return null; }
Example #18
Source File: SemanticHighlightings.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
@Override public boolean consumes(SemanticToken token) { // 1: match types SimpleName name = token.getNode(); ASTNode node = name.getParent(); int nodeType = node.getNodeType(); if (nodeType != ASTNode.SIMPLE_TYPE && nodeType != ASTNode.QUALIFIED_TYPE) { return false; } // 2: match type arguments StructuralPropertyDescriptor locationInParent = node.getLocationInParent(); if (locationInParent == ParameterizedType.TYPE_ARGUMENTS_PROPERTY) { return true; } return false; }
Example #19
Source File: ImplementOccurrencesFinder.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
public String initialize(CompilationUnit root, ASTNode node) { if (!(node instanceof Name)) return SearchMessages.ImplementOccurrencesFinder_invalidTarget; fSelectedNode= ASTNodes.getNormalizedNode(node); if (!(fSelectedNode instanceof Type)) return SearchMessages.ImplementOccurrencesFinder_invalidTarget; StructuralPropertyDescriptor location= fSelectedNode.getLocationInParent(); if (location != TypeDeclaration.SUPERCLASS_TYPE_PROPERTY && location != TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY && location != EnumDeclaration.SUPER_INTERFACE_TYPES_PROPERTY) return SearchMessages.ImplementOccurrencesFinder_invalidTarget; fSelectedType= ((Type)fSelectedNode).resolveBinding(); if (fSelectedType == null) return SearchMessages.ImplementOccurrencesFinder_invalidTarget; fStart= fSelectedNode.getParent(); // type declaration fASTRoot= root; fDescription= Messages.format(SearchMessages.ImplementOccurrencesFinder_occurrence_description, BasicElementLabels.getJavaElementName(fSelectedType.getName())); return null; }
Example #20
Source File: NodeUtils.java From SimFix with GNU General Public License v2.0 | 6 votes |
public static List<ASTNode> getAllSiblingNodes(ASTNode node){ List<ASTNode> siblings = new ArrayList<>(); StructuralPropertyDescriptor structuralPropertyDescriptor = node.getLocationInParent(); if (structuralPropertyDescriptor == null) { return siblings; } else if(structuralPropertyDescriptor.isChildListProperty()){ List list = (List) node.getParent().getStructuralProperty(structuralPropertyDescriptor); for(Object object : list){ if(object instanceof ASTNode){ siblings.add((ASTNode) object); } } } // else if(structuralPropertyDescriptor.isChildProperty()){ // ASTNode child = (ASTNode) node.getParent().getStructuralProperty(structuralPropertyDescriptor); // siblings.add(child); // } return siblings; }
Example #21
Source File: BaseQuickFix.java From vscode-checkstyle with GNU Lesser General Public License v3.0 | 6 votes |
/** * Replaces a node in an AST with another node. If the replacement is successful * the original node is deleted. * * @param node The node to replace. * @param replacement The replacement node. * @return <code>true</code> if the node was successfully replaced. */ protected boolean replace(final ASTNode node, final ASTNode replacement) { final ASTNode parent = node.getParent(); final StructuralPropertyDescriptor descriptor = node.getLocationInParent(); if (descriptor != null) { if (descriptor.isChildProperty()) { parent.setStructuralProperty(descriptor, replacement); node.delete(); return true; } else if (descriptor.isChildListProperty()) { @SuppressWarnings("unchecked") final List<ASTNode> children = (List<ASTNode>) parent.getStructuralProperty(descriptor); children.set(children.indexOf(node), replacement); node.delete(); return true; } } return false; }
Example #22
Source File: EmptyStatementQuickFix.java From vscode-checkstyle with GNU Lesser General Public License v3.0 | 6 votes |
@Override public ASTVisitor getCorrectingASTVisitor(IRegion lineInfo, int markerStartOffset) { return new ASTVisitor() { @Override public boolean visit(EmptyStatement node) { if (containsPosition(lineInfo, node.getStartPosition())) { // early exit if the statement is mandatory, e.g. only // statement in a for-statement without block final StructuralPropertyDescriptor p = node.getLocationInParent(); if (p.isChildProperty() && ((ChildPropertyDescriptor) p).isMandatory()) { return false; } node.delete(); } return false; } }; }
Example #23
Source File: AbstractASTResolution.java From eclipse-cs with GNU Lesser General Public License v2.1 | 6 votes |
/** * Replaces a node in an AST with another node. If the replacement is successful the original node * is deleted. * * @param node * The node to replace. * @param replacement * The replacement node. * @return <code>true</code> if the node was successfully replaced. */ protected boolean replace(final ASTNode node, final ASTNode replacement) { final ASTNode parent = node.getParent(); final StructuralPropertyDescriptor descriptor = node.getLocationInParent(); if (descriptor != null) { if (descriptor.isChildProperty()) { parent.setStructuralProperty(descriptor, replacement); node.delete(); return true; } else if (descriptor.isChildListProperty()) { @SuppressWarnings("unchecked") final List<ASTNode> children = (List<ASTNode>) parent.getStructuralProperty(descriptor); children.set(children.indexOf(node), replacement); node.delete(); return true; } } return false; }
Example #24
Source File: JavaElementHyperlinkDetector.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
/** * Finds the target for break or continue node. * * @param input the editor input * @param region the region * @return the break or continue target location or <code>null</code> if none * @since 3.7 */ public static OccurrenceLocation findBreakOrContinueTarget(ITypeRoot input, IRegion region) { CompilationUnit astRoot= SharedASTProvider.getAST(input, SharedASTProvider.WAIT_NO, null); if (astRoot == null) return null; ASTNode node= NodeFinder.perform(astRoot, region.getOffset(), region.getLength()); ASTNode breakOrContinueNode= null; boolean labelSelected= false; if (node instanceof SimpleName) { SimpleName simpleName= (SimpleName) node; StructuralPropertyDescriptor location= simpleName.getLocationInParent(); if (location == ContinueStatement.LABEL_PROPERTY || location == BreakStatement.LABEL_PROPERTY) { breakOrContinueNode= simpleName.getParent(); labelSelected= true; } } else if (node instanceof ContinueStatement || node instanceof BreakStatement) breakOrContinueNode= node; if (breakOrContinueNode == null) return null; BreakContinueTargetFinder finder= new BreakContinueTargetFinder(); if (finder.initialize(astRoot, breakOrContinueNode) == null) { OccurrenceLocation[] locations= finder.getOccurrences(); if (locations != null) { if (breakOrContinueNode instanceof BreakStatement && !labelSelected) return locations[locations.length - 1]; // points to the end of target statement return locations[0]; // points to the beginning of target statement } } return null; }
Example #25
Source File: StructureSelectionAction.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
static ASTNode[] getSiblingNodes(ASTNode node) { ASTNode parent= node.getParent(); StructuralPropertyDescriptor locationInParent= node.getLocationInParent(); if (locationInParent.isChildListProperty()) { List<? extends ASTNode> siblings= ASTNodes.getChildListProperty(parent, (ChildListPropertyDescriptor) locationInParent); return siblings.toArray(new ASTNode[siblings.size()]); } return null; }
Example #26
Source File: SemanticHighlightings.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
@Override public boolean consumes(SemanticToken token) { SimpleName node= token.getNode(); StructuralPropertyDescriptor location= node.getLocationInParent(); if (location == VariableDeclarationFragment.NAME_PROPERTY || location == SingleVariableDeclaration.NAME_PROPERTY) { ASTNode parent= node.getParent(); if (parent instanceof VariableDeclaration) { parent= parent.getParent(); return parent == null || !(parent instanceof FieldDeclaration); } } return false; }
Example #27
Source File: CopyQualifiedNameAction.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
/** * Checks whether the given name belongs to a {@link ClassInstanceCreation} and if so, returns * its constructor binding. * * @param nameNode the name node * @return the constructor binding or <code>null</code> if not found * @since 3.7 */ private IBinding getConstructorBindingIfAvailable(Name nameNode) { ASTNode type= ASTNodes.getNormalizedNode(nameNode); StructuralPropertyDescriptor loc= type.getLocationInParent(); if (loc == ClassInstanceCreation.TYPE_PROPERTY) { return ((ClassInstanceCreation) type.getParent()).resolveConstructorBinding(); } return null; }
Example #28
Source File: ASTRewrite.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
/** * Replaces the given node in this rewriter. The replacement node * must either be brand new (not part of the original AST) or a placeholder * node (for example, one created by {@link #createCopyTarget(ASTNode)} * or {@link #createStringPlaceholder(String, int)}). The AST itself * is not actually modified in any way; rather, the rewriter just records * a note that this node has been replaced. * * @param node the node being replaced. The node can either be an original node in the AST * or (since 3.4) a new node already inserted or used as replacement in this AST rewriter. * @param replacement the replacement node, or <code>null</code> if no * replacement * @param editGroup the edit group in which to collect the corresponding * text edits, or <code>null</code> if ungrouped * @throws IllegalArgumentException if the node is null, or if the node is not part * of this rewriter's AST, or if the replacement node is not a new node (or * placeholder), or if the described modification is otherwise invalid */ public final void replace(ASTNode node, ASTNode replacement, TextEditGroup editGroup) { if (node == null) { throw new IllegalArgumentException(); } StructuralPropertyDescriptor property; ASTNode parent; if (RewriteEventStore.isNewNode(node)) { // replace a new node, bug 164862 PropertyLocation location= this.eventStore.getPropertyLocation(node, RewriteEventStore.NEW); if (location != null) { property= location.getProperty(); parent= location.getParent(); } else { throw new IllegalArgumentException("Node is not part of the rewriter's AST"); //$NON-NLS-1$ } } else { property= node.getLocationInParent(); parent= node.getParent(); } if (property.isChildListProperty()) { getListRewrite(parent, (ChildListPropertyDescriptor) property).replace(node, replacement, editGroup); } else { set(parent, property, replacement, editGroup); } }
Example #29
Source File: Java50Fix.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private static ASTNode getDeclaringNode(ASTNode selectedNode) { ASTNode declaringNode= null; if (selectedNode instanceof MethodDeclaration) { declaringNode= selectedNode; } else if (selectedNode instanceof SimpleName) { StructuralPropertyDescriptor locationInParent= selectedNode.getLocationInParent(); if (locationInParent == MethodDeclaration.NAME_PROPERTY || locationInParent == TypeDeclaration.NAME_PROPERTY) { declaringNode= selectedNode.getParent(); } else if (locationInParent == VariableDeclarationFragment.NAME_PROPERTY) { declaringNode= selectedNode.getParent().getParent(); } } return declaringNode; }
Example #30
Source File: RewriteEventStore.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
public final CopySourceInfo createRangeCopy(ASTNode parent, StructuralPropertyDescriptor childProperty, ASTNode first, ASTNode last, boolean isMove, ASTNode internalPlaceholder, ASTNode replacingNode, TextEditGroup editGroup) { CopySourceInfo copyInfo= createCopySourceInfo(null, internalPlaceholder, isMove); internalPlaceholder.setProperty(INTERNAL_PLACEHOLDER_PROPERTY, internalPlaceholder); NodeRangeInfo copyRangeInfo= new NodeRangeInfo(parent, childProperty, first, last, copyInfo, replacingNode, editGroup); ListRewriteEvent listEvent= getListEvent(parent, childProperty, true); int indexFirst= listEvent.getIndex(first, ListRewriteEvent.OLD); if (indexFirst == -1) { throw new IllegalArgumentException("Start node is not a original child of the given list"); //$NON-NLS-1$ } int indexLast= listEvent.getIndex(last, ListRewriteEvent.OLD); if (indexLast == -1) { throw new IllegalArgumentException("End node is not a original child of the given list"); //$NON-NLS-1$ } if (indexFirst > indexLast) { throw new IllegalArgumentException("Start node must be before end node"); //$NON-NLS-1$ } if (this.nodeRangeInfos == null) { this.nodeRangeInfos= new HashMap(); } PropertyLocation loc= new PropertyLocation(parent, childProperty); List innerList= (List) this.nodeRangeInfos.get(loc); if (innerList == null) { innerList= new ArrayList(2); this.nodeRangeInfos.put(loc, innerList); } else { assertNoOverlap(listEvent, indexFirst, indexLast, innerList); } innerList.add(copyRangeInfo); return copyInfo; }