下面列出了org.eclipse.jdt.core.dom.ASTNode#setStructuralProperty ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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;
}
/**
* 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;
}
public void replace(ASTNode old, ASTNode neww) {
// if(!copyStack.isEmpty()) {
// if(copyStack.get(copyStack.size()-1) == old) {
// copyStack.set(copyStack.size()-1, neww);
// Log.err("COPY STACK REPLACE");
// Object oldProp = old.getProperty(TYPEBIND_PROP);
// if(oldProp != null && neww.getProperty(TYPEBIND_PROP) == null) {
// Log.err(" copy old prop");
// neww.setProperty(TYPEBIND_PROP, oldProp);
// }
// Object oldProp2 = old.getProperty(TYPEBIND_METHOD_TMP);
// if(oldProp2 != null && neww.getProperty(TYPEBIND_METHOD_TMP) == null) {
// Log.err(" copy old prop2");
// neww.setProperty(TYPEBIND_METHOD_TMP, oldProp2);
// }
// return;
// }
// }
old.setProperty(IS_REPLACE, neww);
ASTNode parent = old.getParent();
StructuralPropertyDescriptor desc = old.getLocationInParent();
if(desc instanceof ChildListPropertyDescriptor) {
ChildListPropertyDescriptor ch = (ChildListPropertyDescriptor)desc;
List<ASTNode> list = (List)parent.getStructuralProperty(ch);
int index = list.indexOf(old);
list.set(index, neww);
}
else {
if(parent instanceof QualifiedName &&
QualifiedName.QUALIFIER_PROPERTY.getId().equals(desc.getId()) &&
!(neww instanceof SimpleName)) {
if(!(neww instanceof Expression))throw new IllegalArgumentException();
//QualifiedName has to be changed to FieldAccess
// throw new IllegalArgumentException("qual name expression");
FieldAccess fa = ast.newFieldAccess();
fa.setExpression((Expression)neww);
fa.setName((SimpleName)copySubtreeIfHasParent(((QualifiedName)parent).getName()));
// for(Map.Entry ee : (Set<Map.Entry>)parent.properties().entrySet()) {
// Log.err("ee " + ee.getKey());
// }
if(parent.getProperty(TYPEBIND_PROP) == null)
fa.setProperty(TYPEBIND_PROP, parent.getProperty(TYPEBIND_PROP));
replace(parent, fa);
return;
}
parent.setStructuralProperty(desc, neww);
}
}