Example usage for org.eclipse.jdt.core IJavaElement getParent

List of usage examples for org.eclipse.jdt.core IJavaElement getParent

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaElement getParent.

Prototype

IJavaElement getParent();

Source Link

Document

Returns the element directly containing this element, or null if this element has no parent.

Usage

From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.TypeReferenceLocator.java

License:Open Source License

protected IJavaElement findElement(IJavaElement element, int accuracy) {
    // need exact match to be able to open on type ref
    if (accuracy != SearchMatch.A_ACCURATE)
        return null;

    // element that references the type must be included in the enclosing element
    DeclarationOfReferencedTypesPattern declPattern = (DeclarationOfReferencedTypesPattern) this.pattern;
    while (element != null && !declPattern.enclosingElement.equals(element))
        element = element.getParent();
    return element;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.TypeReferenceLocator.java

License:Open Source License

void matchReportReference(Expression expr, int lastIndex, TypeBinding refBinding, MatchLocator locator)
        throws CoreException {

    // Look if there's a need to special report for parameterized type
    if (refBinding.isParameterizedType() || refBinding.isRawType()) {

        // Try to refine accuracy
        ParameterizedTypeBinding parameterizedBinding = (ParameterizedTypeBinding) refBinding;
        updateMatch(parameterizedBinding, this.pattern.getTypeArguments(), this.pattern.hasTypeParameters(), 0,
                locator);/* w w  w . java  2 s  .  c o  m*/

        // See whether it is necessary to report or not
        if (this.match.getRule() == 0)
            return; // impossible match
        boolean report = (this.isErasureMatch && this.match.isErasure())
                || (this.isEquivalentMatch && this.match.isEquivalent()) || this.match.isExact();
        if (!report)
            return;

        // Make a special report for parameterized types if necessary
        if (refBinding.isParameterizedType() && this.pattern.hasTypeArguments()) {
            TypeReference typeRef = null;
            TypeReference[] typeArguments = null;
            if (expr instanceof ParameterizedQualifiedTypeReference) {
                typeRef = (ParameterizedQualifiedTypeReference) expr;
                typeArguments = ((ParameterizedQualifiedTypeReference) expr).typeArguments[lastIndex];
            } else if (expr instanceof ParameterizedSingleTypeReference) {
                typeRef = (ParameterizedSingleTypeReference) expr;
                typeArguments = ((ParameterizedSingleTypeReference) expr).typeArguments;
            }
            if (typeRef != null) {
                locator.reportAccurateParameterizedTypeReference(this.match, typeRef, lastIndex, typeArguments);
                return;
            }
        }
    } else if (this.pattern.hasTypeArguments()) { // binding has no type params, compatible erasure if pattern does
        this.match.setRule(SearchPattern.R_ERASURE_MATCH);
    }

    // Report match
    if (expr instanceof ArrayTypeReference) {
        locator.reportAccurateTypeReference(this.match, expr, this.pattern.simpleName);
        return;
    }
    if (refBinding.isLocalType()) {
        // see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=82673
        LocalTypeBinding local = (LocalTypeBinding) refBinding.erasure();
        IJavaElement focus = this.pattern.focus;
        if (focus != null && local.enclosingMethod != null
                && focus.getParent().getElementType() == IJavaElement.METHOD) {
            IMethod method = (IMethod) focus.getParent();
            if (!CharOperation.equals(local.enclosingMethod.selector, method.getElementName().toCharArray())) {
                return;
            }
        }
    }
    if (this.pattern.simpleName == null) {
        this.match.setOffset(expr.sourceStart);
        this.match.setLength(expr.sourceEnd - expr.sourceStart + 1);
    }
    locator.report(this.match);
}

From source file:com.codenvy.ide.ext.java.server.internal.core.SourceMapper.java

License:Open Source License

public int getFlags(IJavaElement element) {
    switch (element.getElementType()) {
    case IJavaElement.LOCAL_VARIABLE:
        LocalVariableElementKey key = new LocalVariableElementKey(element.getParent(),
                element.getElementName());
        if (this.finalParameters != null && this.finalParameters.contains(key)) {
            return Flags.AccFinal;
        }//  w  ww.  jav  a 2s.  c  om
    }
    return 0;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.SourceMapper.java

License:Open Source License

/**
 * Returns the SourceRange for the name of the given element, or
 * {-1, -1} if no source range is known for the name of the element.
 *//*from  w w  w  . j a  va 2  s .  co  m*/
public SourceRange getNameRange(IJavaElement element) {
    switch (element.getElementType()) {
    case IJavaElement.METHOD:
        if (((IMember) element).isBinary()) {
            IJavaElement[] el = getUnqualifiedMethodHandle((IMethod) element, false);
            if (el[1] != null && this.sourceRanges.get(el[0]) == null) {
                element = getUnqualifiedMethodHandle((IMethod) element, true)[0];
            } else {
                element = el[0];
            }
        }
        break;
    case IJavaElement.TYPE_PARAMETER:
        IJavaElement parent = element.getParent();
        if (parent.getElementType() == IJavaElement.METHOD) {
            IMethod method = (IMethod) parent;
            if (method.isBinary()) {
                IJavaElement[] el = getUnqualifiedMethodHandle(method, false);
                if (el[1] != null && this.sourceRanges.get(el[0]) == null) {
                    method = (IMethod) getUnqualifiedMethodHandle(method, true)[0];
                } else {
                    method = (IMethod) el[0];
                }
                element = method.getTypeParameter(element.getElementName());
            }
        }
        break;
    case IJavaElement.LOCAL_VARIABLE:
        LocalVariableElementKey key = new LocalVariableElementKey(element.getParent(),
                element.getElementName());
        SourceRange[] ranges = (SourceRange[]) this.parametersRanges.get(key);
        if (ranges == null) {
            return UNKNOWN_RANGE;
        } else {
            return ranges[1];
        }
    }
    SourceRange[] ranges = (SourceRange[]) this.sourceRanges.get(element);
    if (ranges == null) {
        return UNKNOWN_RANGE;
    } else {
        return ranges[1];
    }
}

From source file:com.codenvy.ide.ext.java.server.internal.core.SourceMapper.java

License:Open Source License

/**
 * Returns the <code>SourceRange</code> for the given element, or
 * {-1, -1} if no source range is known for the element.
 *//*w  w  w  .j  a va 2 s . com*/
public SourceRange getSourceRange(IJavaElement element) {
    switch (element.getElementType()) {
    case IJavaElement.METHOD:
        if (((IMember) element).isBinary()) {
            IJavaElement[] el = getUnqualifiedMethodHandle((IMethod) element, false);
            if (el[1] != null && this.sourceRanges.get(el[0]) == null) {
                element = getUnqualifiedMethodHandle((IMethod) element, true)[0];
            } else {
                element = el[0];
            }
        }
        break;
    case IJavaElement.TYPE_PARAMETER:
        IJavaElement parent = element.getParent();
        if (parent.getElementType() == IJavaElement.METHOD) {
            IMethod method = (IMethod) parent;
            if (method.isBinary()) {
                IJavaElement[] el = getUnqualifiedMethodHandle(method, false);
                if (el[1] != null && this.sourceRanges.get(el[0]) == null) {
                    method = (IMethod) getUnqualifiedMethodHandle(method, true)[0];
                } else {
                    method = (IMethod) el[0];
                }
                element = method.getTypeParameter(element.getElementName());
            }
        }
        break;
    case IJavaElement.LOCAL_VARIABLE:
        LocalVariableElementKey key = new LocalVariableElementKey(element.getParent(),
                element.getElementName());
        SourceRange[] ranges = (SourceRange[]) this.parametersRanges.get(key);
        if (ranges == null) {
            return UNKNOWN_RANGE;
        } else {
            return ranges[0];
        }
    }
    SourceRange[] ranges = (SourceRange[]) this.sourceRanges.get(element);
    if (ranges == null) {
        return UNKNOWN_RANGE;
    } else {
        return ranges[0];
    }
}

From source file:com.codenvy.ide.ext.java.server.internal.core.SourceMapper.java

License:Open Source License

/**
 * Returns the type with the given <code>typeName</code>.  Returns inner classes
 * as well.//w  w w  . java2s  .com
 */
protected IType getType(String typeName) {
    if (typeName.length() == 0) {
        IJavaElement classFile = this.binaryType.getParent();
        String classFileName = classFile.getElementName();
        StringBuffer newClassFileName = new StringBuffer();
        int lastDollar = classFileName.lastIndexOf('$');
        for (int i = 0; i <= lastDollar; i++)
            newClassFileName.append(classFileName.charAt(i));
        newClassFileName.append(Integer.toString(this.anonymousCounter));
        PackageFragment pkg = (PackageFragment) classFile.getParent();
        return new BinaryType(new ClassFile(pkg, manager, newClassFileName.toString()), manager, typeName);
    } else if (this.binaryType.getElementName().equals(typeName))
        return this.binaryType;
    else
        return this.binaryType.getType(typeName);
}

From source file:com.codenvy.ide.ext.java.server.internal.core.SourceRefElement.java

License:Open Source License

/**
 * Return the first instance of IOpenable in the hierarchy of this
 * type (going up the hierarchy from this type);
 *///w  w  w.  ja v a2  s .  com
public IOpenable getOpenableParent() {
    IJavaElement current = getParent();
    while (current != null) {
        if (current instanceof IOpenable) {
            return (IOpenable) current;
        }
        current = current.getParent();
    }
    return null;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.SourceType.java

License:Open Source License

/**
 * @see org.eclipse.jdt.core.IMember/*  w  w  w  . j  a  v  a2s  .c  o m*/
 */
public IType getDeclaringType() {
    IJavaElement parentElement = getParent();
    while (parentElement != null) {
        if (parentElement.getElementType() == IJavaElement.TYPE) {
            return (IType) parentElement;
        } else if (parentElement instanceof IMember) {
            parentElement = parentElement.getParent();
        } else {
            return null;
        }
    }
    return null;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.SourceType.java

License:Open Source License

/**
 * @see org.eclipse.jdt.core.IType/*from  w  w  w .  j a  v a2 s .  c o m*/
 */
public IPackageFragment getPackageFragment() {
    IJavaElement parentElement = this.parent;
    while (parentElement != null) {
        if (parentElement.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
            return (IPackageFragment) parentElement;
        } else {
            parentElement = parentElement.getParent();
        }
    }
    Assert.isTrue(false); // should not happen
    return null;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.util.Util.java

License:Open Source License

public static final boolean isExcluded(IJavaElement element) {
    int elementType = element.getElementType();
    switch (elementType) {
    case IJavaElement.JAVA_MODEL:
    case IJavaElement.JAVA_PROJECT:
    case IJavaElement.PACKAGE_FRAGMENT_ROOT:
        return false;

    case IJavaElement.PACKAGE_FRAGMENT:
        PackageFragmentRoot root = (PackageFragmentRoot) element
                .getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
        File resource = ((PackageFragment) element).resource();
        return resource != null
                && isExcluded(resource, root.fullInclusionPatternChars(), root.fullExclusionPatternChars());

    case IJavaElement.COMPILATION_UNIT:
        root = (PackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
        resource = ((CompilationUnit) element).resource();
        if (resource == null)
            return false;
        if (isExcluded(resource, root.fullInclusionPatternChars(), root.fullExclusionPatternChars()))
            return true;
        return isExcluded(element.getParent());

    default://from  w  w w . j  av a2 s . c  o m
        IJavaElement cu = element.getAncestor(IJavaElement.COMPILATION_UNIT);
        return cu != null && isExcluded(cu);
    }
}