Example usage for org.eclipse.jdt.core IMember getType

List of usage examples for org.eclipse.jdt.core IMember getType

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IMember getType.

Prototype

IType getType(String name, int occurrenceCount);

Source Link

Document

Returns the local or anonymous type declared in this source member with the given simple name and/or with the specified position relative to the order they are defined in the source.

Usage

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

License:Open Source License

/**
 * Visit the given type declaration and report the nodes that match exactly the
 * search pattern (i.e. the ones in the matching nodes set)
 *//*  w  ww  . jav  a  2s  . co m*/
protected void reportMatching(TypeDeclaration type, IJavaElement parent, int accuracy, MatchingNodeSet nodeSet,
        int occurrenceCount) throws CoreException {
    // create type handle
    IJavaElement enclosingElement = parent;
    if (enclosingElement == null) {
        enclosingElement = createTypeHandle(new String(type.name));
    } else if (enclosingElement instanceof IType) {
        enclosingElement = ((IType) parent).getType(new String(type.name));
    } else if (enclosingElement instanceof IMember) {
        IMember member = (IMember) parent;
        if (member.isBinary()) {
            enclosingElement = ((IClassFile) this.currentPossibleMatch.openable).getType();
        } else {
            enclosingElement = member.getType(new String(type.name), occurrenceCount);
        }
    }
    if (enclosingElement == null)
        return;
    boolean enclosesElement = encloses(enclosingElement);

    // report the type declaration
    if (accuracy > -1 && enclosesElement) {
        int offset = type.sourceStart;
        SearchMatch match = this.patternLocator.newDeclarationMatch(type, enclosingElement, type.binding,
                accuracy, type.sourceEnd - offset + 1, this);
        report(match);
    }

    boolean matchedClassContainer = (this.matchContainer & PatternLocator.CLASS_CONTAINER) != 0;

    // report the type parameters
    if (type.typeParameters != null) {
        reportMatching(type.typeParameters, enclosingElement, parent, type.binding, nodeSet);
    }

    // report annotations
    if (type.annotations != null) {
        reportMatching(type.annotations, enclosingElement, null, type.binding, nodeSet, matchedClassContainer,
                enclosesElement);
    }

    // report references in javadoc
    if (type.javadoc != null) {
        ASTNode[] nodes = nodeSet.matchingNodes(type.declarationSourceStart, type.sourceStart);
        if (nodes != null) {
            if (!matchedClassContainer) {
                for (int i = 0, l = nodes.length; i < l; i++)
                    nodeSet.matchingNodes.removeKey(nodes[i]);
            } else {
                for (int i = 0, l = nodes.length; i < l; i++) {
                    ASTNode node = nodes[i];
                    Integer level = (Integer) nodeSet.matchingNodes.removeKey(node);
                    if (enclosesElement) {
                        this.patternLocator.matchReportReference(node, enclosingElement, null, null,
                                type.binding, level.intValue(), this);
                    }
                }
            }
        }
    }

    // super types
    if ((type.bits & ASTNode.IsAnonymousType) != 0) {
        TypeReference superType = type.allocation.type;
        if (superType != null) {
            Integer level = (Integer) nodeSet.matchingNodes.removeKey(superType);
            if (level != null && matchedClassContainer)
                this.patternLocator.matchReportReference(superType, enclosingElement, null, null, type.binding,
                        level.intValue(), this);
        }
    } else {
        TypeReference superClass = type.superclass;
        if (superClass != null) {
            reportMatchingSuper(superClass, enclosingElement, type.binding, nodeSet, matchedClassContainer);
        }
        TypeReference[] superInterfaces = type.superInterfaces;
        if (superInterfaces != null) {
            for (int i = 0, l = superInterfaces.length; i < l; i++) {
                reportMatchingSuper(superInterfaces[i], enclosingElement, type.binding, nodeSet,
                        matchedClassContainer);
            }
        }
    }

    // filter out element not in hierarchy scope
    boolean typeInHierarchy = type.binding == null || typeInHierarchy(type.binding);
    matchedClassContainer = matchedClassContainer && typeInHierarchy;

    // Visit fields
    FieldDeclaration[] fields = type.fields;
    if (fields != null) {
        if (nodeSet.matchingNodes.elementSize == 0)
            return; // end as all matching nodes were reported
        FieldDeclaration[] otherFields = null;
        int first = -1;
        int length = fields.length;
        for (int i = 0; i < length; i++) {
            FieldDeclaration field = fields[i];
            boolean last = field.endPart2Position == 0 || field.declarationEnd == field.endPart2Position;
            // Store first index of multiple field declaration
            if (!last) {
                if (first == -1) {
                    first = i;
                }
            }
            if (first >= 0) {
                // Store all multiple fields but first one for other elements
                if (i > first) {
                    if (otherFields == null) {
                        otherFields = new FieldDeclaration[length - i];
                    }
                    otherFields[i - 1 - first] = field;
                }
                // On last field, report match with all other elements
                if (last) {
                    for (int j = first; j <= i; j++) {
                        Integer level = (Integer) nodeSet.matchingNodes.removeKey(fields[j]);
                        int value = (level != null && matchedClassContainer) ? level.intValue() : -1;
                        reportMatching(fields[j], otherFields, type, enclosingElement, value, typeInHierarchy,
                                nodeSet);
                    }
                    first = -1;
                    otherFields = null;
                }
            } else {
                // Single field, report normally
                Integer level = (Integer) nodeSet.matchingNodes.removeKey(field);
                int value = (level != null && matchedClassContainer) ? level.intValue() : -1;
                reportMatching(field, null, type, enclosingElement, value, typeInHierarchy, nodeSet);
            }
        }
    }

    // Visit methods
    AbstractMethodDeclaration[] methods = type.methods;
    if (methods != null) {
        if (nodeSet.matchingNodes.elementSize == 0)
            return; // end as all matching nodes were reported
        for (int i = 0, l = methods.length; i < l; i++) {
            AbstractMethodDeclaration method = methods[i];
            Integer level = (Integer) nodeSet.matchingNodes.removeKey(method);
            int value = (level != null && matchedClassContainer) ? level.intValue() : -1;
            reportMatching(method, type, enclosingElement, value, typeInHierarchy, nodeSet);
        }
    }

    // Visit types
    TypeDeclaration[] memberTypes = type.memberTypes;
    if (memberTypes != null) {
        for (int i = 0, l = memberTypes.length; i < l; i++) {
            if (nodeSet.matchingNodes.elementSize == 0)
                return; // end as all matching nodes were reported
            TypeDeclaration memberType = memberTypes[i];
            Integer level = (Integer) nodeSet.matchingNodes.removeKey(memberType);
            int value = (level != null && matchedClassContainer) ? level.intValue() : -1;
            reportMatching(memberType, enclosingElement, value, nodeSet, 1);
        }
    }
}

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

License:Open Source License

/**
 * Create handle by adding child to parent obtained by recursing into parent scopes.
 *///from w  w w  .  j a va 2  s.  c  o m
private IJavaElement createElement(Scope scope, int elementPosition, ICompilationUnit unit,
        HashSet existingElements, HashMap knownScopes) {
    IJavaElement newElement = (IJavaElement) knownScopes.get(scope);
    if (newElement != null)
        return newElement;

    switch (scope.kind) {
    case Scope.COMPILATION_UNIT_SCOPE:
        newElement = unit;
        break;
    case Scope.CLASS_SCOPE:
        IJavaElement parentElement = createElement(scope.parent, elementPosition, unit, existingElements,
                knownScopes);
        switch (parentElement.getElementType()) {
        case IJavaElement.COMPILATION_UNIT:
            newElement = ((ICompilationUnit) parentElement)
                    .getType(new String(scope.enclosingSourceType().sourceName));
            break;
        case IJavaElement.TYPE:
            newElement = ((IType) parentElement).getType(new String(scope.enclosingSourceType().sourceName));
            break;
        case IJavaElement.FIELD:
        case IJavaElement.INITIALIZER:
        case IJavaElement.METHOD:
            IMember member = (IMember) parentElement;
            if (member.isBinary()) {
                return null;
            } else {
                newElement = member.getType(new String(scope.enclosingSourceType().sourceName), 1);
                // increment occurrence count if collision is detected
                if (newElement != null) {
                    while (!existingElements.add(newElement))
                        ((SourceRefElement) newElement).occurrenceCount++;
                }
            }
            break;
        }
        if (newElement != null) {
            knownScopes.put(scope, newElement);
        }
        break;
    case Scope.METHOD_SCOPE:
        IType parentType = (IType) createElement(scope.parent, elementPosition, unit, existingElements,
                knownScopes);
        MethodScope methodScope = (MethodScope) scope;
        if (methodScope.isInsideInitializer()) {
            // inside field or initializer, must find proper one
            TypeDeclaration type = methodScope.referenceType();
            int occurenceCount = 1;
            int length = type.fields == null ? 0 : type.fields.length;
            for (int i = 0; i < length; i++) {
                FieldDeclaration field = type.fields[i];
                if (field.declarationSourceStart <= elementPosition
                        && elementPosition <= field.declarationSourceEnd) {
                    switch (field.getKind()) {
                    case AbstractVariableDeclaration.FIELD:
                    case AbstractVariableDeclaration.ENUM_CONSTANT:
                        newElement = parentType.getField(new String(field.name));
                        break;
                    case AbstractVariableDeclaration.INITIALIZER:
                        newElement = parentType.getInitializer(occurenceCount);
                        break;
                    }
                    break;
                } else if (field.getKind() == AbstractVariableDeclaration.INITIALIZER) {
                    occurenceCount++;
                }
            }
        } else {
            // method element
            AbstractMethodDeclaration method = methodScope.referenceMethod();
            newElement = parentType.getMethod(new String(method.selector),
                    Util.typeParameterSignatures(method));
            if (newElement != null) {
                knownScopes.put(scope, newElement);
            }
        }
        break;
    case Scope.BLOCK_SCOPE:
        // standard block, no element per se
        newElement = createElement(scope.parent, elementPosition, unit, existingElements, knownScopes);
        break;
    }
    return newElement;
}

From source file:org.eclipse.che.jdt.internal.core.search.matching.MatchLocator.java

License:Open Source License

/**
* Visit the given type declaration and report the nodes that match exactly the
* search pattern (i.e. the ones in the matching nodes set)
*//*  w  w w  . j av a2 s .c o  m*/
protected void reportMatching(TypeDeclaration type, IJavaElement parent, int accuracy, MatchingNodeSet nodeSet,
        int occurrenceCount) throws CoreException {
    // create type handle
    IJavaElement enclosingElement = parent;
    if (enclosingElement == null) {
        enclosingElement = createTypeHandle(new String(type.name));
    } else if (enclosingElement instanceof IType) {
        enclosingElement = ((IType) parent).getType(new String(type.name));
    } else if (enclosingElement instanceof IMember) {
        IMember member = (IMember) parent;
        if (member.isBinary()) {
            enclosingElement = ((IClassFile) this.currentPossibleMatch.openable).getType();
        } else {
            enclosingElement = member.getType(new String(type.name), occurrenceCount);
        }
    }
    if (enclosingElement == null)
        return;
    boolean enclosesElement = encloses(enclosingElement);

    // report the type declaration
    if (accuracy > -1 && enclosesElement) {
        int offset = type.sourceStart;
        SearchMatch match = this.patternLocator.newDeclarationMatch(type, enclosingElement, type.binding,
                accuracy, type.sourceEnd - offset + 1, this);
        report(match);
    }

    boolean matchedClassContainer = (this.matchContainer & PatternLocator.CLASS_CONTAINER) != 0;

    // report the type parameters
    if (type.typeParameters != null) {
        reportMatching(type.typeParameters, enclosingElement, parent, type.binding, nodeSet);
    }

    // report annotations
    if (type.annotations != null) {
        reportMatching(type.annotations, enclosingElement, null, type.binding, nodeSet, matchedClassContainer,
                enclosesElement);
    }

    // report references in javadoc
    if (type.javadoc != null) {
        ASTNode[] nodes = nodeSet.matchingNodes(type.declarationSourceStart, type.sourceStart);
        if (nodes != null) {
            if (!matchedClassContainer) {
                for (int i = 0, l = nodes.length; i < l; i++)
                    nodeSet.matchingNodes.removeKey(nodes[i]);
            } else {
                for (int i = 0, l = nodes.length; i < l; i++) {
                    ASTNode node = nodes[i];
                    Integer level = (Integer) nodeSet.matchingNodes.removeKey(node);
                    if (enclosesElement) {
                        this.patternLocator.matchReportReference(node, enclosingElement, null, null,
                                type.binding, level.intValue(), this);
                    }
                }
            }
        }
    }

    // super types
    if ((type.bits & ASTNode.IsAnonymousType) != 0) {
        TypeReference superType = type.allocation.type;
        if (superType != null) {
            Integer level = (Integer) nodeSet.matchingNodes.removeKey(superType);
            if (level != null && matchedClassContainer)
                this.patternLocator.matchReportReference(superType, enclosingElement, null, null, type.binding,
                        level.intValue(), this);
        }
    } else {
        TypeReference superClass = type.superclass;
        if (superClass != null) {
            reportMatchingSuper(superClass, enclosingElement, type.binding, nodeSet, matchedClassContainer);
            for (int i = 0, length = superClass.annotations == null ? 0
                    : superClass.annotations.length; i < length; i++) {
                Annotation[] annotations = superClass.annotations[i];
                if (annotations == null)
                    continue;
                reportMatching(annotations, enclosingElement, null, type.binding, nodeSet,
                        matchedClassContainer, enclosesElement);
            }
        }
        TypeReference[] superInterfaces = type.superInterfaces;
        if (superInterfaces != null) {
            for (int i = 0, l = superInterfaces.length; i < l; i++) {
                reportMatchingSuper(superInterfaces[i], enclosingElement, type.binding, nodeSet,
                        matchedClassContainer);
                TypeReference typeReference = type.superInterfaces[i];
                Annotation[][] annotations = typeReference != null ? typeReference.annotations : null;
                if (annotations != null) {
                    for (int j = 0, length = annotations.length; j < length; j++) {
                        if (annotations[j] == null)
                            continue;
                        reportMatching(annotations[j], enclosingElement, null, type.binding, nodeSet,
                                matchedClassContainer, enclosesElement);
                    }
                }
            }
        }
    }

    // filter out element not in hierarchy scope
    boolean typeInHierarchy = type.binding == null || typeInHierarchy(type.binding);
    matchedClassContainer = matchedClassContainer && typeInHierarchy;

    // Visit fields
    FieldDeclaration[] fields = type.fields;
    if (fields != null) {
        if (nodeSet.matchingNodes.elementSize == 0)
            return; // end as all matching nodes were reported
        FieldDeclaration[] otherFields = null;
        int first = -1;
        int length = fields.length;
        for (int i = 0; i < length; i++) {
            FieldDeclaration field = fields[i];
            boolean last = field.endPart2Position == 0 || field.declarationEnd == field.endPart2Position;
            // Store first index of multiple field declaration
            if (!last) {
                if (first == -1) {
                    first = i;
                }
            }
            if (first >= 0) {
                // Store all multiple fields but first one for other elements
                if (i > first) {
                    if (otherFields == null) {
                        otherFields = new FieldDeclaration[length - i];
                    }
                    otherFields[i - 1 - first] = field;
                }
                // On last field, report match with all other elements
                if (last) {
                    for (int j = first; j <= i; j++) {
                        Integer level = (Integer) nodeSet.matchingNodes.removeKey(fields[j]);
                        int value = (level != null && matchedClassContainer) ? level.intValue() : -1;
                        reportMatching(fields[j], otherFields, type, enclosingElement, value, typeInHierarchy,
                                nodeSet);
                    }
                    first = -1;
                    otherFields = null;
                }
            } else {
                // Single field, report normally
                Integer level = (Integer) nodeSet.matchingNodes.removeKey(field);
                int value = (level != null && matchedClassContainer) ? level.intValue() : -1;
                reportMatching(field, null, type, enclosingElement, value, typeInHierarchy, nodeSet);
            }
        }
    }

    // Visit methods
    AbstractMethodDeclaration[] methods = type.methods;
    if (methods != null) {
        if (nodeSet.matchingNodes.elementSize == 0)
            return; // end as all matching nodes were reported
        for (int i = 0, l = methods.length; i < l; i++) {
            AbstractMethodDeclaration method = methods[i];
            Integer level = (Integer) nodeSet.matchingNodes.removeKey(method);
            int value = (level != null && matchedClassContainer) ? level.intValue() : -1;
            reportMatching(method, type, enclosingElement, value, typeInHierarchy, nodeSet);
        }
    }

    // Visit types
    TypeDeclaration[] memberTypes = type.memberTypes;
    if (memberTypes != null) {
        for (int i = 0, l = memberTypes.length; i < l; i++) {
            if (nodeSet.matchingNodes.elementSize == 0)
                return; // end as all matching nodes were reported
            TypeDeclaration memberType = memberTypes[i];
            Integer level = (Integer) nodeSet.matchingNodes.removeKey(memberType);
            int value = (level != null && matchedClassContainer) ? level.intValue() : -1;
            reportMatching(memberType, enclosingElement, value, nodeSet, 1);
        }
    }
}

From source file:org.eclipse.che.jdt.internal.core.util.HandleFactory.java

License:Open Source License

/**
 * Create handle by adding child to parent obtained by recursing into parent scopes.
 *//*from   w ww  .j  av a 2s  .c  o  m*/
public IJavaElement createElement(Scope scope, int elementPosition, ICompilationUnit unit,
        HashSet existingElements, HashMap knownScopes) {
    IJavaElement newElement = (IJavaElement) knownScopes.get(scope);
    if (newElement != null)
        return newElement;

    switch (scope.kind) {
    case Scope.COMPILATION_UNIT_SCOPE:
        newElement = unit;
        break;
    case Scope.CLASS_SCOPE:
        IJavaElement parentElement = createElement(scope.parent, elementPosition, unit, existingElements,
                knownScopes);
        switch (parentElement.getElementType()) {
        case IJavaElement.COMPILATION_UNIT:
            newElement = ((ICompilationUnit) parentElement)
                    .getType(new String(scope.enclosingSourceType().sourceName));
            break;
        case IJavaElement.TYPE:
            newElement = ((IType) parentElement).getType(new String(scope.enclosingSourceType().sourceName));
            break;
        case IJavaElement.FIELD:
        case IJavaElement.INITIALIZER:
        case IJavaElement.METHOD:
            IMember member = (IMember) parentElement;
            if (member.isBinary()) {
                return null;
            } else {
                newElement = member.getType(new String(scope.enclosingSourceType().sourceName), 1);
                // increment occurrence count if collision is detected
                if (newElement != null) {
                    while (!existingElements.add(newElement))
                        ((SourceRefElement) newElement).occurrenceCount++;
                }
            }
            break;
        }
        if (newElement != null) {
            knownScopes.put(scope, newElement);
        }
        break;
    case Scope.METHOD_SCOPE:
        if (scope.isLambdaScope()) {
            parentElement = createElement(scope.parent, elementPosition, unit, existingElements, knownScopes);
            LambdaExpression expression = (LambdaExpression) scope.originalReferenceContext();
            if (expression.resolvedType != null && expression.resolvedType.isValidBinding()
                    && !(expression.descriptor instanceof ProblemMethodBinding)) { // chain in lambda element only if resolved properly.
                //newElement = new org.eclipse.jdt.internal.core.SourceLambdaExpression((JavaElement) parentElement, expression)
                // .getMethod();

                newElement = LambdaFactory.createLambdaExpression((JavaElement) parentElement, expression)
                        .getMethod();
                knownScopes.put(scope, newElement);
                return newElement;
            }
            return parentElement;
        }
        IType parentType = (IType) createElement(scope.parent, elementPosition, unit, existingElements,
                knownScopes);
        MethodScope methodScope = (MethodScope) scope;
        if (methodScope.isInsideInitializer()) {
            // inside field or initializer, must find proper one
            TypeDeclaration type = methodScope.referenceType();
            int occurenceCount = 1;
            int length = type.fields == null ? 0 : type.fields.length;
            for (int i = 0; i < length; i++) {
                FieldDeclaration field = type.fields[i];
                if (field.declarationSourceStart <= elementPosition
                        && elementPosition <= field.declarationSourceEnd) {
                    switch (field.getKind()) {
                    case AbstractVariableDeclaration.FIELD:
                    case AbstractVariableDeclaration.ENUM_CONSTANT:
                        newElement = parentType.getField(new String(field.name));
                        break;
                    case AbstractVariableDeclaration.INITIALIZER:
                        newElement = parentType.getInitializer(occurenceCount);
                        break;
                    }
                    break;
                } else if (field.getKind() == AbstractVariableDeclaration.INITIALIZER) {
                    occurenceCount++;
                }
            }
        } else {
            // method element
            AbstractMethodDeclaration method = methodScope.referenceMethod();
            newElement = parentType.getMethod(new String(method.selector),
                    Util.typeParameterSignatures(method));
            if (newElement != null) {
                knownScopes.put(scope, newElement);
            }
        }
        break;
    case Scope.BLOCK_SCOPE:
        // standard block, no element per se
        newElement = createElement(scope.parent, elementPosition, unit, existingElements, knownScopes);
        break;
    }
    return newElement;
}