Java Code Examples for org.eclipse.jdt.core.dom.ASTNode#setStructuralProperty()

The following examples show how to use org.eclipse.jdt.core.dom.ASTNode#setStructuralProperty() . 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: AbstractASTResolution.java    From eclipse-cs with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * 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 2
Source File: BaseQuickFix.java    From vscode-checkstyle with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 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 3
Source File: BaseTranslator.java    From junion with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
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);
		}		
	}