Example usage for org.eclipse.jdt.core.dom SimpleName getLocationInParent

List of usage examples for org.eclipse.jdt.core.dom SimpleName getLocationInParent

Introduction

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

Prototype

public final StructuralPropertyDescriptor getLocationInParent() 

Source Link

Document

Returns the location of this node within its parent, or null if this is a root node.

Usage

From source file:com.google.dart.java2dart.SyntaxTranslator.java

License:Open Source License

@Override
public boolean visit(org.eclipse.jdt.core.dom.SimpleName node) {
    IBinding binding = node.resolveBinding();
    SimpleIdentifier result = identifier(node.getIdentifier());
    putReference(binding, result);//from  w  w w. j  a v a  2 s. c  o m
    // may be statically imported field, generate PropertyAccess
    {
        org.eclipse.jdt.core.dom.StructuralPropertyDescriptor locationInParent = node.getLocationInParent();
        if (binding instanceof IVariableBinding) {
            org.eclipse.jdt.core.dom.IVariableBinding variableBinding = (org.eclipse.jdt.core.dom.IVariableBinding) binding;
            org.eclipse.jdt.core.dom.ASTNode parent = node.getParent();
            if (locationInParent == org.eclipse.jdt.core.dom.EnumConstantDeclaration.ARGUMENTS_PROPERTY
                    || locationInParent == org.eclipse.jdt.core.dom.ClassInstanceCreation.ARGUMENTS_PROPERTY
                    || locationInParent == org.eclipse.jdt.core.dom.MethodInvocation.ARGUMENTS_PROPERTY
                    || locationInParent == org.eclipse.jdt.core.dom.ConstructorInvocation.ARGUMENTS_PROPERTY
                    || locationInParent == org.eclipse.jdt.core.dom.SuperConstructorInvocation.ARGUMENTS_PROPERTY
                    || locationInParent == org.eclipse.jdt.core.dom.Assignment.RIGHT_HAND_SIDE_PROPERTY
                    || locationInParent == org.eclipse.jdt.core.dom.SwitchCase.EXPRESSION_PROPERTY
                    || parent instanceof org.eclipse.jdt.core.dom.InfixExpression
                    || parent instanceof org.eclipse.jdt.core.dom.ConditionalExpression
                    || parent instanceof org.eclipse.jdt.core.dom.ReturnStatement) {
                ITypeBinding declaringBinding = variableBinding.getDeclaringClass();
                ITypeBinding enclosingBinding = getEnclosingTypeBinding(node);
                if (declaringBinding != null && enclosingBinding != declaringBinding
                        && org.eclipse.jdt.core.dom.Modifier.isStatic(variableBinding.getModifiers())) {
                    SimpleIdentifier target = identifier(declaringBinding.getName());
                    putReference(declaringBinding, target);
                    return done(propertyAccess(target, result));
                }
            }
        }
    }
    // may be statically imported method, generate PrefixedIdentifier
    {
        org.eclipse.jdt.core.dom.StructuralPropertyDescriptor locationInParent = node.getLocationInParent();
        if (binding instanceof IMethodBinding) {
            IMethodBinding methodBinding = (IMethodBinding) binding;
            if (locationInParent == org.eclipse.jdt.core.dom.MethodInvocation.NAME_PROPERTY
                    && ((org.eclipse.jdt.core.dom.MethodInvocation) node.getParent()).getExpression() == null) {
                ITypeBinding declaringBinding = methodBinding.getDeclaringClass();
                ITypeBinding enclosingBinding = getEnclosingTypeBinding(node);
                if (declaringBinding != null && enclosingBinding != declaringBinding
                        && org.eclipse.jdt.core.dom.Modifier.isStatic(methodBinding.getModifiers())) {
                    SimpleIdentifier prefix = identifier(declaringBinding.getName());
                    putReference(declaringBinding, prefix);
                    return done(identifier(prefix, result));
                }
            }
        }
    }
    // done
    return done(result);
}

From source file:org.autorefactor.refactoring.rules.VariableDefinitionsUsesVisitor.java

License:Open Source License

private void addDefinitionOrUse(SimpleName node, ChildPropertyDescriptor definitionPropertyDescriptor) {
    if (node.getLocationInParent() == definitionPropertyDescriptor) {
        definitions.add(node);//  w w  w .  j a  v a2s.com
    } else {
        uses.add(node);
    }
}

From source file:org.eclipse.recommenders.jdt.templates.SnippetCodeBuilder.java

License:Open Source License

private boolean isUnqualified(SimpleName name) {
    return !QualifiedName.NAME_PROPERTY.equals(name.getLocationInParent());
}

From source file:org.eclipse.recommenders.jdt.templates.SnippetCodeBuilder.java

License:Open Source License

private boolean isUnqualifiedMethodInvocation(SimpleName name) {
    if (!MethodInvocation.NAME_PROPERTY.equals(name.getLocationInParent())) {
        return false;
    }//from  w  w  w.j a v a 2 s.com
    MethodInvocation methodInvocation = (MethodInvocation) name.getParent();
    if (methodInvocation.getExpression() != null) {
        return false;
    }
    return true;
}

From source file:org.eclipse.recommenders.jdt.templates.SnippetCodeBuilder.java

License:Open Source License

private boolean isDeclaration(SimpleName name) {
    if (VariableDeclarationFragment.NAME_PROPERTY.equals(name.getLocationInParent())) {
        return true;
    } else if (SingleVariableDeclaration.NAME_PROPERTY.equals(name.getLocationInParent())) {
        return true;
    } else {//from   w w  w. ja v  a 2s .  c  o m
        return false;
    }
}

From source file:org.eclipse.scout.sdk.saml.importer.internal.jdt.imports.ScopeAnalyzer.java

License:Open Source License

public IBinding[] getDeclarationsInScope(SimpleName selector, int flags) {
    try {/*from   ww w.j  a v  a2  s  . com*/
        // special case for switch on enum
        if (selector.getLocationInParent() == SwitchCase.EXPRESSION_PROPERTY) {
            ITypeBinding binding = ((SwitchStatement) selector.getParent().getParent()).getExpression()
                    .resolveTypeBinding();
            if (binding != null && binding.isEnum()) {
                return getEnumContants(binding);
            }
        }

        ITypeBinding parentTypeBinding = OrganizeImportsHelper.getBindingOfParentType(selector);
        if (parentTypeBinding != null) {
            ITypeBinding binding = getQualifier(selector);
            DefaultBindingRequestor requestor = new DefaultBindingRequestor(parentTypeBinding, flags);
            if (binding == null) {
                addLocalDeclarations(selector, flags, requestor);
                addTypeDeclarations(parentTypeBinding, flags, requestor);
            } else {
                addInherited(binding, flags, requestor);
            }

            List<IBinding> result = requestor.getResult();
            return result.toArray(new IBinding[result.size()]);
        }
        return NO_BINDING;
    } finally {
        clearLists();
    }
}

From source file:org.eclipse.scout.sdk.saml.importer.internal.jdt.imports.ScopeAnalyzer.java

License:Open Source License

public boolean isDeclaredInScope(IBinding declaration, SimpleName selector, int flags) {
    try {//  w w w . j a  v  a2s  .  c o m
        // special case for switch on enum
        if (selector.getLocationInParent() == SwitchCase.EXPRESSION_PROPERTY) {
            ITypeBinding binding = ((SwitchStatement) selector.getParent().getParent()).getExpression()
                    .resolveTypeBinding();
            if (binding != null && binding.isEnum()) {
                return hasEnumContants(declaration, binding.getTypeDeclaration());
            }
        }

        ITypeBinding parentTypeBinding = OrganizeImportsHelper.getBindingOfParentTypeContext(selector);
        if (parentTypeBinding != null) {
            ITypeBinding binding = getQualifier(selector);
            SearchRequestor requestor = new SearchRequestor(declaration, parentTypeBinding, flags);
            if (binding == null) {
                addLocalDeclarations(selector, flags, requestor);
                if (requestor.found())
                    return requestor.isVisible();
                addTypeDeclarations(parentTypeBinding, flags, requestor);
                if (requestor.found())
                    return requestor.isVisible();
            } else {
                addInherited(binding, flags, requestor);
                if (requestor.found())
                    return requestor.isVisible();
            }
        }
        return false;
    } finally {
        clearLists();
    }
}

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

License:Open Source License

/**
 * Examples:/*  ww  w. j a  v  a  2s.com*/
 * 
 * <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();
}