Example usage for org.eclipse.jdt.internal.compiler.ast AbstractVariableDeclaration FIELD

List of usage examples for org.eclipse.jdt.internal.compiler.ast AbstractVariableDeclaration FIELD

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.ast AbstractVariableDeclaration FIELD.

Prototype

int FIELD

To view the source code for org.eclipse.jdt.internal.compiler.ast AbstractVariableDeclaration FIELD.

Click Source Link

Usage

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

License:Open Source License

public int match(FieldDeclaration node, MatchingNodeSet nodeSet) {
    int referencesLevel = IMPOSSIBLE_MATCH;
    if (this.pattern.findReferences)
        // must be a write only access with an initializer
        if (this.pattern.writeAccess && !this.pattern.readAccess && node.initialization != null)
            if (matchesName(this.pattern.name, node.name))
                referencesLevel = this.pattern.mustResolve ? POSSIBLE_MATCH : ACCURATE_MATCH;

    int declarationsLevel = IMPOSSIBLE_MATCH;
    if (this.pattern.findDeclarations) {
        switch (node.getKind()) {
        case AbstractVariableDeclaration.FIELD:
        case AbstractVariableDeclaration.ENUM_CONSTANT:
            if (matchesName(this.pattern.name, node.name))
                if (matchesTypeReference(((FieldPattern) this.pattern).typeSimpleName, node.type))
                    declarationsLevel = this.pattern.mustResolve ? POSSIBLE_MATCH : ACCURATE_MATCH;
            break;
        }//  w w w  .  j a  v  a 2 s .c  o m
    }
    return nodeSet.addMatch(node, referencesLevel >= declarationsLevel ? referencesLevel : declarationsLevel); // use the stronger match
}

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

License:Open Source License

/**
 * Creates an IField from the given field declaration and type.
 *///from  w w w .java  2  s.  co m
protected IJavaElement createHandle(FieldDeclaration fieldDeclaration, TypeDeclaration typeDeclaration,
        IJavaElement parent) {
    if (!(parent instanceof IType))
        return parent;
    IType type = (IType) parent;

    switch (fieldDeclaration.getKind()) {
    case AbstractVariableDeclaration.FIELD:
    case AbstractVariableDeclaration.ENUM_CONSTANT:
        return ((IType) parent).getField(new String(fieldDeclaration.name));
    }
    if (type.isBinary()) {
        // do not return initializer for binary types
        // see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=98378
        return type;
    }
    // find occurrence count of the given initializer in its type declaration
    int occurrenceCount = 0;
    FieldDeclaration[] fields = typeDeclaration.fields;
    int length = fields == null ? 0 : fields.length;
    for (int i = 0; i < length; i++) {
        if (fields[i].getKind() == AbstractVariableDeclaration.INITIALIZER) {
            occurrenceCount++;
            if (fields[i].equals(fieldDeclaration))
                break;
        }
    }
    return ((IType) parent).getInitializer(occurrenceCount);
}

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.
 *//* w  w  w.j  av a 2s  .co  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.ajdt.core.parserbridge.AJSourceElementNotifier.java

License:Open Source License

protected void notifySourceElementRequestor(FieldDeclaration fieldDeclaration, TypeDeclaration declaringType) {

    // range check
    boolean isInRange = initialPosition <= fieldDeclaration.declarationSourceStart
            && eofPosition >= fieldDeclaration.declarationSourceEnd;

    switch (fieldDeclaration.getKind()) {
    case AbstractVariableDeclaration.ENUM_CONSTANT:
        if (this.reportReferenceInfo) {
            // accept constructor reference for enum constant
            if (fieldDeclaration.initialization instanceof AllocationExpression) {
                AllocationExpression alloc = (AllocationExpression) fieldDeclaration.initialization;
                requestor.acceptConstructorReference(declaringType.name,
                        alloc.arguments == null ? 0 : alloc.arguments.length, alloc.sourceStart);
            }/*  www. jav  a  2s  .  c  o m*/
        }
        // fall through next case
    case AbstractVariableDeclaration.FIELD:
        int fieldEndPosition = this.sourceEnds.get(fieldDeclaration);
        if (fieldEndPosition == -1) {
            // use the declaration source end by default
            fieldEndPosition = fieldDeclaration.declarationSourceEnd;
        }
        if (isInRange) {
            int currentModifiers = fieldDeclaration.modifiers;

            // remember deprecation so as to not lose it below
            boolean deprecated = (currentModifiers & ClassFileConstants.AccDeprecated) != 0
                    || hasDeprecatedAnnotation(fieldDeclaration.annotations);

            char[] typeName = null;
            if (fieldDeclaration.type == null) {
                // enum constant
                typeName = declaringType.name;
                currentModifiers |= ClassFileConstants.AccEnum;
            } else {
                // regular field
                typeName = CharOperation.concatWith(fieldDeclaration.type.getParameterizedTypeName(), '.');
            }
            ISourceElementRequestor.FieldInfo fieldInfo = new ISourceElementRequestor.FieldInfo();
            fieldInfo.declarationStart = fieldDeclaration.declarationSourceStart;
            fieldInfo.name = fieldDeclaration.name;
            fieldInfo.modifiers = deprecated
                    ? (currentModifiers & ExtraCompilerModifiers.AccJustFlag) | ClassFileConstants.AccDeprecated
                    : currentModifiers & ExtraCompilerModifiers.AccJustFlag;
            fieldInfo.type = typeName;
            fieldInfo.nameSourceStart = fieldDeclaration.sourceStart;
            fieldInfo.nameSourceEnd = fieldDeclaration.sourceEnd;
            fieldInfo.categories = (char[][]) this.nodesToCategories.get(fieldDeclaration);
            fieldInfo.annotations = fieldDeclaration.annotations;
            fieldInfo.node = fieldDeclaration;
            requestor.enterField(fieldInfo);
        }
        this.visitIfNeeded(fieldDeclaration, declaringType);
        if (isInRange) {
            requestor.exitField(
                    // filter out initializations that are not a constant (simple check)
                    (fieldDeclaration.initialization == null
                            || fieldDeclaration.initialization instanceof ArrayInitializer
                            || fieldDeclaration.initialization instanceof AllocationExpression
                            || fieldDeclaration.initialization instanceof ArrayAllocationExpression
                            || fieldDeclaration.initialization instanceof Assignment
                            || fieldDeclaration.initialization instanceof ClassLiteralAccess
                            || fieldDeclaration.initialization instanceof MessageSend
                            || fieldDeclaration.initialization instanceof ArrayReference
                            || fieldDeclaration.initialization instanceof ThisReference) ? -1
                                    : fieldDeclaration.initialization.sourceStart,
                    fieldEndPosition, fieldDeclaration.declarationSourceEnd);
        }
        break;
    case AbstractVariableDeclaration.INITIALIZER:
        if (isInRange) {
            requestor.enterInitializer(fieldDeclaration.declarationSourceStart, fieldDeclaration.modifiers);
        }
        this.visitIfNeeded((Initializer) fieldDeclaration);
        if (isInRange) {
            requestor.exitInitializer(fieldDeclaration.declarationSourceEnd);
        }
        break;
    }
}

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 w w  .j ava2s. 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;
}