Example usage for org.eclipse.jdt.core.dom ClassInstanceCreation NAME_PROPERTY

List of usage examples for org.eclipse.jdt.core.dom ClassInstanceCreation NAME_PROPERTY

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom ClassInstanceCreation NAME_PROPERTY.

Prototype

ChildPropertyDescriptor NAME_PROPERTY

To view the source code for org.eclipse.jdt.core.dom ClassInstanceCreation NAME_PROPERTY.

Click Source Link

Document

The "name" structural property of this node type (child type: Name ) (JLS2 API only).

Usage

From source file:org.eclipse.wb.internal.core.utils.ast.AstEditor.java

License:Open Source License

/**
 * Examples://from   w w  w  .j ava 2 s.  co m
 * 
 * <pre>
  * SWT.NONE = org.eclipse.swt.SWT.NONE
  * new JButton() = new javax.swing.JButton()
  * </pre>
 * 
 * @param theNode
 *          the {@link ASTNode} to get the source.
 * @param transformer
 *          the {@link Function} that can participate in node to source transformation by
 *          providing alternative source for some nodes. Can be <code>null</code>, in not
 *          additional transformation required. If transformer returns not <code>null</code>, we
 *          use it instead of its original source; if <code>null</code> - we continue with
 *          original source.
 * 
 * @return the source of {@link ASTNode} in "external form", i.e. with fully qualified types.
 */
@SuppressWarnings("restriction")
public String getExternalSource(final ASTNode theNode, final Function<ASTNode, String> transformer) {
    final StringBuffer buffer = new StringBuffer(getSource(theNode));
    // remember positions for all nodes
    final Map<ASTNode, Integer> nodePositions = Maps.newHashMap();
    theNode.accept(new ASTVisitor() {
        @Override
        public void postVisit(ASTNode _node) {
            nodePositions.put(_node, _node.getStartPosition());
        }
    });
    // replace "name" with "qualified name"
    theNode.accept(new org.eclipse.jdt.internal.corext.dom.GenericVisitor() {
        @Override
        protected boolean visitNode(ASTNode node) {
            if (transformer != null) {
                String source = transformer.apply(node);
                if (source != null) {
                    replace(node, source);
                    return false;
                }
            }
            return true;
        }

        @Override
        public void endVisit(SimpleName name) {
            if (!AstNodeUtils.isVariable(name)) {
                StructuralPropertyDescriptor location = name.getLocationInParent();
                if (location == SimpleType.NAME_PROPERTY || location == QualifiedName.QUALIFIER_PROPERTY
                        || location == ClassInstanceCreation.NAME_PROPERTY
                        || location == MethodInvocation.EXPRESSION_PROPERTY) {
                    String fullyQualifiedName = AstNodeUtils.getFullyQualifiedName(name, false);
                    replace(name, fullyQualifiedName);
                }
            }
        }

        /**
         * Replace given ASTNode with different source, with updating positions for other nodes.
         */
        private void replace(ASTNode node, String newSource) {
            int nodePosition = nodePositions.get(node);
            // update source
            {
                int sourceStart = nodePosition - theNode.getStartPosition();
                int sourceEnd = sourceStart + node.getLength();
                buffer.replace(sourceStart, sourceEnd, newSource);
            }
            // update positions for nodes
            int lengthDelta = newSource.length() - node.getLength();
            for (Map.Entry<ASTNode, Integer> entry : nodePositions.entrySet()) {
                Integer position = entry.getValue();
                if (position > nodePosition) {
                    entry.setValue(position + lengthDelta);
                }
            }
        }
    });
    // OK, we have updated source
    return buffer.toString();
}