org.eclipse.jdt.core.dom.StructuralPropertyDescriptor#isChildListProperty ( )源码实例Demo

下面列出了org.eclipse.jdt.core.dom.StructuralPropertyDescriptor#isChildListProperty ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: eclipse-cs   文件: AbstractASTResolution.java
/**
 * 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;
}
 
源代码2 项目: vscode-checkstyle   文件: BaseQuickFix.java
/**
 * 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;
}
 
源代码3 项目: SimFix   文件: NodeUtils.java
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;
 	}
 
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);
		}
	}
}
 
源代码5 项目: eclipse.jdt.ls   文件: ExtractMethodRefactoring.java
private boolean matchesLocationInEnclosingBodyDecl(BodyDeclaration originalEnclosingBodyDeclaration, BodyDeclaration duplicateEnclosingBodyDeclaration, VariableDeclaration originalReturnNode, VariableDeclaration duplicateReturnNode) {
	boolean matches = true;
	ASTNode original = originalReturnNode;
	ASTNode dupliacte = duplicateReturnNode;

	// walk up the parent chains to check if the location of the return nodes in their respective parent chains is same
	do {
		ASTNode originalParent = original.getParent();
		ASTNode duplicateParent = dupliacte.getParent();
		StructuralPropertyDescriptor originalLoc = original.getLocationInParent();
		StructuralPropertyDescriptor duplicateLoc = dupliacte.getLocationInParent();

		if (originalParent != null && duplicateParent != null && originalLoc.getNodeClass().equals(duplicateLoc.getNodeClass()) && originalLoc.getId().equals(duplicateLoc.getId())) {
			if (originalLoc.isChildListProperty() && duplicateLoc.isChildListProperty()) {
				int indexOfOriginal = ((List<?>) originalParent.getStructuralProperty(originalLoc)).indexOf(original);
				int indexOfDuplicate = ((List<?>) duplicateParent.getStructuralProperty(duplicateLoc)).indexOf(dupliacte);
				if (indexOfOriginal != indexOfDuplicate) {
					matches = false;
					break;
				}
			}
		} else {
			matches = false;
			break;
		}

		original = originalParent;
		dupliacte = duplicateParent;

		if ((originalEnclosingBodyDeclaration.equals(original) && !duplicateEnclosingBodyDeclaration.equals(dupliacte)) || (!originalEnclosingBodyDeclaration.equals(original) && duplicateEnclosingBodyDeclaration.equals(dupliacte))) {
			matches = false;
			break;
		}
	} while (!originalEnclosingBodyDeclaration.equals(original) && !duplicateEnclosingBodyDeclaration.equals(dupliacte));

	return matches;
}
 
private ChildListPropertyDescriptor getProperty() {
	List<StructuralPropertyDescriptor> list= fCallerNode.structuralPropertiesForType();
	for (int i= 0; i < list.size(); i++) {
		StructuralPropertyDescriptor curr= list.get(i);
		if (curr.isChildListProperty() && "arguments".equals(curr.getId())) { //$NON-NLS-1$
			return (ChildListPropertyDescriptor) curr;
		}
	}
	return null;

}
 
源代码7 项目: gwt-eclipse-plugin   文件: EquivalentNodeFinder.java
@SuppressWarnings("unchecked")
private int getIndex(ASTNode node) {
  StructuralPropertyDescriptor locationInParent = node.getLocationInParent();
  if (locationInParent != null && locationInParent.isChildListProperty()) {
    List<ASTNode> parentsChildren =
        (List<ASTNode>) node.getParent().getStructuralProperty(
        locationInParent);
    if (parentsChildren != null) {
      return parentsChildren.indexOf(node);
    }
  }

  // The node is not contained within a list-based property on the parent
  return NOT_FROM_LIST;
}
 
/**
 * Returns the list that contains the given ASTNode. If the node
 * isn't part of any list, <code>null</code> is returned.
 *
 * @param node the node in question
 * @return the list that contains the node or <code>null</code>
 */
public static List<? extends ASTNode> getContainingList(ASTNode node) {
	StructuralPropertyDescriptor locationInParent= node.getLocationInParent();
	if (locationInParent != null && locationInParent.isChildListProperty()) {
		return getChildListProperty(node.getParent(), (ChildListPropertyDescriptor) locationInParent);
	}
	return null;
}
 
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;
}
 
private ChildListPropertyDescriptor getProperty() {
	List<StructuralPropertyDescriptor> list= fCallerNode.structuralPropertiesForType();
	for (int i= 0; i < list.size(); i++) {
		StructuralPropertyDescriptor curr= list.get(i);
		if (curr.isChildListProperty() && "arguments".equals(curr.getId())) { //$NON-NLS-1$
			return (ChildListPropertyDescriptor) curr;
		}
	}
	return null;

}
 
/**
 * Removes the given node from its parent in this rewriter. The AST itself
    * is not actually modified in any way; rather, the rewriter just records
    * a note that this node should not be there.
 *
 * @param node the node being removed. 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 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 described modification is invalid
 * (such as removing a required node)
 */
public final void remove(ASTNode node, TextEditGroup editGroup) {
	if (node == null) {
		throw new IllegalArgumentException();
	}

	StructuralPropertyDescriptor property;
	ASTNode parent;
	if (RewriteEventStore.isNewNode(node)) { // remove 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).remove(node, editGroup);
	} else {
		set(parent, property, null, editGroup);
	}
}
 
/**
 * 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);
	}
}
 
void preAddChildEvent(ASTNode node, ASTNode child,	StructuralPropertyDescriptor property) {
	if(property.isChildProperty()) {
		NodeRewriteEvent event = getNodeEvent(node, property);
		event.setNewValue(child);
		if(child != null) {
			markAsMoveOrCopyTarget(node, child);
		}
	} else if(property.isChildListProperty()) {
		// force event creation
		getListEvent(node, property);
	}
}
 
private void validateIsListProperty(StructuralPropertyDescriptor property) {
	if (!property.isChildListProperty()) {
		String message= property.getId() + " is not a list property"; //$NON-NLS-1$
		throw new IllegalArgumentException(message);
	}
}
 
private void validateIsNodeProperty(StructuralPropertyDescriptor property) {
	if (property.isChildListProperty()) {
		String message= property.getId() + " is not a node property"; //$NON-NLS-1$
		throw new IllegalArgumentException(message);
	}
}
 
private void validateIsListProperty(StructuralPropertyDescriptor property) {
	if (!property.isChildListProperty()) {
		String message= property.getId() + " is not a list property"; //$NON-NLS-1$
		throw new IllegalArgumentException(message);
	}
}
 
/**
 * Returns the value of the given property as managed by this rewriter. If the property
 * has been removed, <code>null</code> is returned. If it has been replaced, the replacing value
 * is returned. If the property has not been changed yet, the original value is returned.
 * <p>
 * For child list properties use {@link ListRewrite#getRewrittenList()} to get access to the
 * rewritten nodes in a list. </p>
 *
 * @param node the node
 * @param property the node's property
 * @return the value of the given property as managed by this rewriter
 *
 * @since 3.2
 */
public Object get(ASTNode node, StructuralPropertyDescriptor property) {
	if (node == null || property == null) {
		throw new IllegalArgumentException();
	}
	if (property.isChildListProperty()) {
		throw new IllegalArgumentException("Use the list rewriter to access nodes in a list"); //$NON-NLS-1$
	}
	return this.eventStore.getNewValue(node, property);
}