Example usage for org.eclipse.jdt.internal.compiler.lookup Scope BLOCK_SCOPE

List of usage examples for org.eclipse.jdt.internal.compiler.lookup Scope BLOCK_SCOPE

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.lookup Scope BLOCK_SCOPE.

Prototype

int BLOCK_SCOPE

To view the source code for org.eclipse.jdt.internal.compiler.lookup Scope BLOCK_SCOPE.

Click Source Link

Usage

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  ww .j  ava  2s.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.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 ava 2s  .  c  om*/
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;
}

From source file:org.eclipse.jdt.internal.compiler.lookup.Scope.java

License:Open Source License

/**
 * Returns the modifiers of the innermost enclosing declaration.
 * @return modifiers/*from www  . ja v  a 2s .c  om*/
 */
public int getDeclarationModifiers() {
    switch (this.kind) {
    case Scope.BLOCK_SCOPE:
    case Scope.METHOD_SCOPE:
        MethodScope methodScope = methodScope();
        if (!methodScope.isInsideInitializer()) {
            // check method modifiers to see if deprecated
            MethodBinding context = ((AbstractMethodDeclaration) methodScope.referenceContext).binding;
            if (context != null)
                return context.modifiers;
        } else {
            SourceTypeBinding type = ((BlockScope) this).referenceType().binding;

            // inside field declaration ? check field modifier to see if deprecated
            if (methodScope.initializedField != null)
                return methodScope.initializedField.modifiers;
            if (type != null)
                return type.modifiers;
        }
        break;
    case Scope.CLASS_SCOPE:
        ReferenceBinding context = ((ClassScope) this).referenceType().binding;
        if (context != null)
            return context.modifiers;
        break;
    }
    return -1;
}

From source file:org.eclipse.jdt.internal.compiler.lookup.Scope.java

License:Open Source License

/**
 * Returns true if the scope or one of its parent is associated to a given caseStatement, denoting
 * being part of a given switch case statement.
 *///from  ww  w.  j  ava  2  s .c  o m
public boolean isInsideCase(CaseStatement caseStatement) {
    Scope scope = this;
    do {
        switch (scope.kind) {
        case Scope.BLOCK_SCOPE:
            if (((BlockScope) scope).enclosingCase == caseStatement) {
                return true;
            }
        }
        scope = scope.parent;
    } while (scope != null);
    return false;
}

From source file:org.eclipse.jdt.internal.compiler.lookup.Scope.java

License:Open Source License

public boolean isInsideDeprecatedCode() {
    switch (this.kind) {
    case Scope.BLOCK_SCOPE:
    case Scope.METHOD_SCOPE:
        MethodScope methodScope = methodScope();
        if (!methodScope.isInsideInitializer()) {
            // check method modifiers to see if deprecated
            MethodBinding context = ((AbstractMethodDeclaration) methodScope.referenceContext).binding;
            if (context != null && context.isViewedAsDeprecated())
                return true;
        } else if (methodScope.initializedField != null
                && methodScope.initializedField.isViewedAsDeprecated()) {
            // inside field declaration ? check field modifier to see if deprecated
            return true;
        }//from   w ww  .ja v  a  2  s .c  o m
        SourceTypeBinding declaringType = ((BlockScope) this).referenceType().binding;
        if (declaringType != null) {
            declaringType.initializeDeprecatedAnnotationTagBits(); // may not have been resolved until then
            if (declaringType.isViewedAsDeprecated())
                return true;
        }
        break;
    case Scope.CLASS_SCOPE:
        ReferenceBinding context = ((ClassScope) this).referenceType().binding;
        if (context != null) {
            context.initializeDeprecatedAnnotationTagBits(); // may not have been resolved until then
            if (context.isViewedAsDeprecated())
                return true;
        }
        break;
    case Scope.COMPILATION_UNIT_SCOPE:
        // consider import as being deprecated if first type is itself deprecated (123522)
        CompilationUnitDeclaration unit = referenceCompilationUnit();
        if (unit.types != null && unit.types.length > 0) {
            SourceTypeBinding type = unit.types[0].binding;
            if (type != null) {
                type.initializeDeprecatedAnnotationTagBits(); // may not have been resolved until then
                if (type.isViewedAsDeprecated())
                    return true;
            }
        }
    }
    return false;
}

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.ast.LiftingTypeReference.java

License:Open Source License

@Override
public TypeBinding resolveType(BlockScope scope, boolean checkBounds) {
    TypeBinding baseType = this.resolvedType = this.baseReference.resolveType(scope);
    if (this.roleReference.getTypeName().length > 1) {
        scope.problemReporter().qualifiedLiftingType(this.roleReference, scope.enclosingSourceType());
        return invalidate(baseType);
    }/*ww w .j a v  a  2  s  .c  o m*/
    TypeBinding roleType = this.roleReference.resolveType(scope);
    if (scope.kind != Scope.BLOCK_SCOPE) { // not a catch block?
        if (!TeamModel.isAnyTeam(scope.enclosingSourceType())) {
            scope.problemReporter().liftingTypeNotAllowedHere(scope.methodScope().referenceContext, this);
            return invalidate(roleType);
        }
    }
    if (baseType == null || baseType instanceof MissingTypeBinding)
        return invalidate(roleType);
    if (roleType == null || roleType instanceof MissingTypeBinding)
        return invalidate(baseType);
    if (roleType.isArrayType()) {
        baseType = baseType.leafComponentType();
        roleType = roleType.leafComponentType();
    }
    if (roleType.isBaseType()) {
        scope.problemReporter().primitiveTypeNotAllowedForLifting(scope.referenceType(), this.roleReference,
                roleType);
        return invalidate(roleType);
    }
    if (baseType.isBaseType()) {
        scope.problemReporter().primitiveTypeNotAllowedForLifting(scope.referenceType(), this.baseReference,
                baseType);
        return invalidate(roleType);
    }

    ReferenceBinding roleRefType = (ReferenceBinding) roleType;
    if (!roleRefType.isValidBinding()) // already reported.
        return invalidate(roleType);

    if (!roleRefType.isDirectRole()) {
        scope.problemReporter().needRoleInLiftingType(scope.referenceType(), this.roleReference, roleType);
        return invalidate(roleType);
    }
    if (roleRefType.isSynthInterface())
        roleRefType = roleRefType.getRealClass();

    if (roleRefType.roleModel.hasBaseclassProblem()) {// already reported for the role class.
        scope.referenceContext().tagAsHavingErrors(); // -> mark method as erroneous, too.
        return invalidate(roleType);
    }

    // TODO (SH): maybe look for bound sub-type?
    // Note (SH): calling baseclass() requires STATE_LENV_DONE_FIELDS_AND_METHODS:
    Dependencies.ensureBindingState(roleRefType, ITranslationStates.STATE_LENV_DONE_FIELDS_AND_METHODS);

    if (baseType.isTypeVariable() && ((TypeVariableBinding) baseType).roletype != null) {
        // resolving "<B base R> as R":
        roleRefType = ((TypeVariableBinding) baseType).roletype;
        // ambiguity is handled by _OT$lift_dynamic which may or may not declare LiftingFailedException
    } else if ((baseType.tagBits & TagBits.HierarchyHasProblems) != 0) {
        // already reported (?)
    } else {
        // static adjustment (OTJLD 2.3.2(a)):
        roleRefType = (ReferenceBinding) TeamModel.getRoleToLiftTo(scope, baseType, roleRefType, true, this);
        if (roleRefType == null)
            roleRefType = (ReferenceBinding) roleType; // revert unsuccessful adjustment
        if (roleRefType.baseclass() == null
                || RoleModel.hasTagBit(roleRefType, RoleModel.BaseclassHasProblems)) {
            scope.problemReporter().roleNotBoundCantLift(scope.referenceType(), this.roleReference, roleType);
            return invalidate(roleType);
        }
        if (baseType.isRole()) // see http://trac.objectteams.org/ot/ticket/73
            baseType = RoleTypeCreator.maybeWrapUnqualifiedRoleType(baseType, scope.enclosingReceiverType());
        if (baseType == null)
            return invalidate(roleType);
        Config oldConfig = Config.createOrResetConfig(this);
        try {
            // fetch role's base class and perform substitutions:
            ReferenceBinding roleBase = roleRefType.baseclass();
            if (roleType.isParameterizedType()) {
                ParameterizedTypeBinding parameterizedRole = (ParameterizedTypeBinding) roleType;
                TypeBinding[] typeArgs = parameterizedRole.arguments;
                ITeamAnchor anchor = null;
                if (roleRefType.baseclass() instanceof RoleTypeBinding)
                    anchor = ((RoleTypeBinding) roleRefType.baseclass())._teamAnchor;
                roleBase = parameterizedRole.environment.createParameterizedType(
                        (ReferenceBinding) roleBase.original(), typeArgs, anchor, -1, roleBase.enclosingType(),
                        roleBase.getTypeAnnotations());
            }
            // THE compatibility check:
            if (!baseType.isCompatibleWith(roleBase) || Config.getLoweringRequired()) {
                scope.problemReporter().incompatibleBaseForRole(scope.referenceType(), this, roleType,
                        baseType);
                return invalidate(roleType);
            }
        } finally {
            Config.removeOrRestore(oldConfig, this);
        }
    }
    return this.resolvedType;
}

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.ast.TypeAnchorReference.java

License:Open Source License

private ITeamAnchor findVariable(Scope scope, char[] token, boolean isStaticScope, int start, int end) {
    if (scope instanceof ClassScope && ((ClassScope) scope).superTypeReference != null) {
        scope.problemReporter().extendingExternalizedRole(((ClassScope) scope).superTypeReference);
        return null;
    }/*from  w w w  . j av  a2s. c o  m*/
    VariableBinding anchorBinding = null;
    switch (scope.kind) {
    case Scope.METHOD_SCOPE:
        // check arguments for possible anchor:
        AbstractMethodDeclaration method = ((MethodScope) scope).referenceMethod();
        if (method != null) {
            Argument[] arguments = method.arguments;
            if (arguments != null)
                for (int i = 0; i < arguments.length; i++)
                    if (CharOperation.equals(arguments[i].name, token))
                        return RoleTypeCreator.resolveTypeAnchoredToArgument(method, i);
        }

        //$FALL-THROUGH$
    case Scope.BLOCK_SCOPE:
    case Scope.BINDING_SCOPE:
        anchorBinding = scope.findVariable(token);
        break;
    }
    if (anchorBinding == null) {
        Binding binding = scope.getBinding(token, Binding.VARIABLE, this, true);
        if (binding instanceof VariableBinding)
            anchorBinding = (VariableBinding) binding;
    }
    return checkAnchor(scope, this.anchor, token, start, end, anchorBinding);
}

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.lookup.AnchorMapping.java

License:Open Source License

@SuppressWarnings("nls")
private String computeScopeKind(Scope scope) {
    String result;//from  w  w w  . ja v a2  s.  co m
    if (scope == null) {
        return "scope is null";
    }
    switch (scope.kind) {
    case Scope.BLOCK_SCOPE:
        result = "BlockScope";
        break;
    case Scope.METHOD_SCOPE:
        result = "MethodScope";
        break;
    case Scope.CLASS_SCOPE:
        result = "ClassScope";
        break;
    case Scope.COMPILATION_UNIT_SCOPE:
        result = "CompilationUnitScope";
        break;
    case Scope.BINDING_SCOPE:
        result = "BindingScope";
        break;
    default:
        result = "unknown scope type";
    }
    return result;
}