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

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

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom QualifiedName 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:net.atos.optimus.common.tools.ltk.ImportsGenerationVisitor.java

License:Open Source License

/**
 * Checks Qualified Type, represented by its use in parent node, its name,
 * and its type binding./*from   w  ww .jav  a  2 s  .co m*/
 * 
 * @param node
 *            : Parent Node
 * @param qualifiedName
 *            : Qualified Name
 * @param typeBinding
 *            : Type Binding
 */
@SuppressWarnings("unchecked")
private void checkQualifiedType(ASTNode node, QualifiedName qualifiedName, ITypeBinding typeBinding) {

    // At first we extract package name & type for Type, by :
    // - Splitting the String if Type Binding has been deduced (recovered)
    // - Taking it from Binding otherwise
    String fullyQualifiedName = qualifiedName.getFullyQualifiedName();
    String typeName = null;
    String packageName = null;

    if (typeBinding == null || typeBinding.isRecovered()) {
        typeName = qualifiedName.getFullyQualifiedName()
                .substring(qualifiedName.getFullyQualifiedName().lastIndexOf(".") + 1);
        packageName = qualifiedName.getFullyQualifiedName().substring(0,
                qualifiedName.getFullyQualifiedName().lastIndexOf("."));
    } else {
        typeBinding = typeBinding.getErasure();
        typeName = typeBinding.getName();
        IPackageBinding packageBinding = typeBinding.getPackage();
        if (packageBinding == null)
            return;
        packageName = packageBinding.getName();
    }

    // Checks if name should be trimmed (if class with same name but
    // different package has already been registered), and trims it if
    // needed
    if (shouldTrimName(packageName, typeName)) {
        StructuralPropertyDescriptor locationInParent = qualifiedName.getLocationInParent();
        if (locationInParent == null)
            return;
        if (locationInParent.isChildListProperty()) {
            ChildListPropertyDescriptor clpd = (ChildListPropertyDescriptor) locationInParent;
            List<ASTNode> astNodes = (List<ASTNode>) node.getStructuralProperty(clpd);
            astNodes.remove(qualifiedName);
            astNodes.add(node.getAST().newName(typeName));
        } else {
            node.setStructuralProperty(locationInParent, node.getAST().newName(typeName));
        }
        hasModifications = true;
    }

    // Checks if import should be added (e.g. package is not java.lang) and
    // does it if needed
    if (shouldAddImport(node, typeName, packageName, fullyQualifiedName)) {
        this.tryAddImport(node.getAST(), fullyQualifiedName);
        if (!typesToPackageBinding.containsKey(typeName))
            typesToPackageBinding.put(typeName, packageName);
    } else if (this.currentPackageName.equals(packageName))
        if (!typesToPackageBinding.containsKey(typeName))
            typesToPackageBinding.put(typeName, packageName);
}

From source file:org.eclipse.wb.core.model.JavaInfo.java

License:Open Source License

/**
 * @return the {@link ASTNode} that should be used to display given related {@link ASTNode}.
 *//*from w w w .  java  2s .  com*/
public static ASTNode getRelatedNodeForSource(ASTNode node) {
    ASTNode sourceNode = node;
    // part of invocation
    if (node.getLocationInParent() == MethodInvocation.EXPRESSION_PROPERTY) {
        sourceNode = node.getParent();
    }
    if (node.getLocationInParent() == MethodInvocation.ARGUMENTS_PROPERTY) {
        sourceNode = node.getParent();
    }
    if (node.getLocationInParent() == SuperConstructorInvocation.ARGUMENTS_PROPERTY) {
        sourceNode = node.getParent();
    }
    if (node.getLocationInParent() == SuperMethodInvocation.ARGUMENTS_PROPERTY) {
        sourceNode = node.getParent();
    }
    if (node.getLocationInParent() == ClassInstanceCreation.ARGUMENTS_PROPERTY) {
        sourceNode = node.getParent();
    }
    // javaInfo.foo = something
    if (node.getLocationInParent() == QualifiedName.QUALIFIER_PROPERTY) {
        QualifiedName qualifiedName = (QualifiedName) node.getParent();
        sourceNode = qualifiedName;
        if (qualifiedName.getLocationInParent() == Assignment.LEFT_HAND_SIDE_PROPERTY) {
            sourceNode = qualifiedName.getParent();
        }
    }
    // done
    return sourceNode;
}

From source file:org.eclipse.wb.internal.core.model.util.MorphingSupport.java

License:Open Source License

@Override
protected void morph_properties(T newComponent) throws Exception {
    ComponentDescription newComponentDescription = newComponent.getDescription();
    // move related nodes
    for (ASTNode node : m_component.getRelatedNodes()) {
        // check if method invocation can exist in new component
        if (node.getLocationInParent() == MethodInvocation.EXPRESSION_PROPERTY) {
            MethodInvocation invocation = (MethodInvocation) node.getParent();
            String signature = AstNodeUtils.getMethodSignature(invocation);
            if (newComponentDescription.getMethod(signature) == null) {
                m_editor.removeEnclosingStatement(invocation);
                continue;
            }//from  w  w w  . j  a  va  2s .  c o m
        }
        // check if assignment can exist in new component
        if (node.getLocationInParent() == QualifiedName.QUALIFIER_PROPERTY) {
            QualifiedName fieldAccess = (QualifiedName) node.getParent();
            if (fieldAccess.getLocationInParent() == Assignment.LEFT_HAND_SIDE_PROPERTY) {
                String fieldName = fieldAccess.getName().getIdentifier();
                if (ReflectionUtils.getFieldByName(newComponentDescription.getComponentClass(),
                        fieldName) == null) {
                    m_editor.removeEnclosingStatement(node);
                    continue;
                }
            }
        }
        // OK, we can add this related node
        newComponent.addRelatedNode(node);
    }
}

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

License:Open Source License

/**
 * @param node/*from w  w  w .ja va 2  s  .  co  m*/
 *          the {@link ASTNode} that represents should be qualifier of field.
 * 
 * @return the {@link Expression} ({@link QualifiedName} or {@link FieldAccess}) used as left side
 *         of {@link Assignment} to some field. May return <code>null</code>, if given node is not
 *         left part of {@link Assignment}.
 */
public static Expression getFieldAssignment(ASTNode node) {
    // FieldAccess = value
    if (node.getLocationInParent() == FieldAccess.EXPRESSION_PROPERTY) {
        FieldAccess fieldAccess = (FieldAccess) node.getParent();
        if (fieldAccess.getLocationInParent() == Assignment.LEFT_HAND_SIDE_PROPERTY) {
            return fieldAccess;
        }
    }
    // QualifiedName = value
    if (node.getLocationInParent() == QualifiedName.QUALIFIER_PROPERTY) {
        QualifiedName qualifiedName = (QualifiedName) node.getParent();
        if (qualifiedName.getLocationInParent() == Assignment.LEFT_HAND_SIDE_PROPERTY) {
            return qualifiedName;
        }
    }
    // not an Assignment part
    return null;
}