Example usage for org.eclipse.jdt.internal.compiler.util HashtableOfObject put

List of usage examples for org.eclipse.jdt.internal.compiler.util HashtableOfObject put

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.util HashtableOfObject put.

Prototype

public Object put(char[] key, Object value) 

Source Link

Usage

From source file:org.codehaus.groovy.eclipse.codeassist.processors.GroovyProposalTypeSearchRequestor.java

License:Apache License

/**
 * This method is called after all types have been accepted by
 * this requestor.  Converts each type into an {@link ICompletionProposal}
 * @return list of all {@link ICompletionProposal}s applicable for this
 * content assist request./*from www  . j a  v a 2  s.  co m*/
 */
List<ICompletionProposal> processAcceptedTypes() {

    this.checkCancel();

    if (this.acceptedTypes == null)
        return Collections.EMPTY_LIST;

    int length = this.acceptedTypes.size();

    if (length == 0)
        return Collections.EMPTY_LIST;

    HashtableOfObject onDemandFound = new HashtableOfObject();
    String thisPackageName = module.getPackageName() == null ? "" : module.getPackageName();

    List<ICompletionProposal> proposals = new LinkedList<ICompletionProposal>();
    try {
        next: for (int i = 0; i < length; i++) {

            // does not check cancellation for every types to avoid
            // performance loss
            if ((i % CHECK_CANCEL_FREQUENCY) == 0) {
                checkCancel();
            }

            AcceptedType acceptedType = (AcceptedType) this.acceptedTypes.elementAt(i);
            char[] packageName = acceptedType.packageName;
            char[] simpleTypeName = acceptedType.simpleTypeName;
            char[][] enclosingTypeNames = acceptedType.enclosingTypeNames;
            int modifiers = acceptedType.modifiers;
            int accessibility = acceptedType.accessibility;

            char[] typeName;
            char[] flatEnclosingTypeNames;
            if (enclosingTypeNames == null || enclosingTypeNames.length == 0) {
                flatEnclosingTypeNames = null;
                typeName = simpleTypeName;
            } else {
                flatEnclosingTypeNames = CharOperation.concatWith(acceptedType.enclosingTypeNames, '.');
                typeName = CharOperation.concat(flatEnclosingTypeNames, simpleTypeName, '.');
            }
            char[] fullyQualifiedName = CharOperation.concat(packageName, typeName, '.');

            // get this imports from the module node
            if (!this.importCachesInitialized) {
                initializeImportCaches();
            }

            // check to see if this type is imported explicitly
            for (int j = 0; j < imports.length; j++) {
                char[][] importName = imports[j];
                if (CharOperation.equals(typeName, importName[0])) {
                    // potentially use fully qualified type name
                    // if there is already something else with the same
                    // simple
                    // name imported
                    proposals.add(proposeType(packageName, simpleTypeName, modifiers, accessibility, typeName,
                            fullyQualifiedName, !CharOperation.equals(fullyQualifiedName, importName[1])));
                    continue next;
                }
            }

            if ((enclosingTypeNames == null || enclosingTypeNames.length == 0)
                    && CharOperation.equals(thisPackageName.toCharArray(), packageName)) {
                proposals.add(proposeType(packageName, simpleTypeName, modifiers, accessibility, typeName,
                        fullyQualifiedName, false));
                continue next;
            } else {
                char[] fullyQualifiedEnclosingTypeOrPackageName = null;

                if (((AcceptedType) onDemandFound.get(simpleTypeName)) == null) {
                    for (int j = 0; j < this.onDemandimports.length; j++) {
                        char[] importFlatName = onDemandimports[j];

                        if (fullyQualifiedEnclosingTypeOrPackageName == null) {
                            if (enclosingTypeNames != null && enclosingTypeNames.length != 0) {
                                fullyQualifiedEnclosingTypeOrPackageName = CharOperation.concat(packageName,
                                        flatEnclosingTypeNames, '.');
                            } else {
                                fullyQualifiedEnclosingTypeOrPackageName = packageName;
                            }
                        }
                        if (CharOperation.equals(fullyQualifiedEnclosingTypeOrPackageName, importFlatName)) {
                            // assume not static import
                            // if(importBinding.isStatic()) {
                            // if((modifiers &
                            // ClassFileConstants.AccStatic) != 0) {
                            // acceptedType.qualifiedTypeName =
                            // typeName;
                            // acceptedType.fullyQualifiedName =
                            // fullyQualifiedName;
                            // onDemandFound.put(
                            // simpleTypeName,
                            // acceptedType);
                            // continue next;
                            // }
                            // } else {
                            acceptedType.qualifiedTypeName = typeName;
                            acceptedType.fullyQualifiedName = fullyQualifiedName;
                            onDemandFound.put(simpleTypeName, acceptedType);
                            continue next;
                            // }
                        }
                    }
                    proposals.add(proposeType(
                            fullyQualifiedEnclosingTypeOrPackageName != null
                                    ? fullyQualifiedEnclosingTypeOrPackageName
                                    : packageName,
                            simpleTypeName, modifiers, accessibility, typeName, fullyQualifiedName, true));
                }
            }
        }
        char[][] keys = onDemandFound.keyTable;
        Object[] values = onDemandFound.valueTable;
        int max = keys.length;
        for (int i = 0; i < max; i++) {
            if ((i % CHECK_CANCEL_FREQUENCY) == 0)
                checkCancel();
            if (keys[i] != null) {
                AcceptedType value = (AcceptedType) values[i];
                if (value != null) {
                    proposals.add(proposeType(value.packageName, value.simpleTypeName, value.modifiers,
                            value.accessibility, value.qualifiedTypeName, value.fullyQualifiedName,
                            value.mustBeQualified));
                }
            }
        }
    } finally {
        this.acceptedTypes = null; // reset
    }
    return proposals;
}

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

License:Open Source License

final Binding getTypeOrPackage(char[] name, int mask, boolean needResolve) {
    Scope scope = this;
    ReferenceBinding foundType = null;//from w  w w  . j  av a 2  s . c o  m
    boolean insideStaticContext = false;
    boolean insideTypeAnnotation = false;
    if ((mask & Binding.TYPE) == 0) {
        Scope next = scope;
        while ((next = scope.parent) != null)
            scope = next;
    } else {
        boolean inheritedHasPrecedence = compilerOptions().complianceLevel >= ClassFileConstants.JDK1_4;
        done: while (true) { // done when a COMPILATION_UNIT_SCOPE is found
            switch (scope.kind) {
            case METHOD_SCOPE:
                MethodScope methodScope = (MethodScope) scope;
                AbstractMethodDeclaration methodDecl = methodScope.referenceMethod();
                if (methodDecl != null) {
                    if (methodDecl.binding != null) {
                        TypeVariableBinding typeVariable = methodDecl.binding.getTypeVariable(name);
                        if (typeVariable != null)
                            return typeVariable;
                    } else {
                        // use the methodDecl's typeParameters to handle problem cases when the method binding doesn't exist
                        TypeParameter[] params = methodDecl.typeParameters();
                        for (int i = params == null ? 0 : params.length; --i >= 0;)
                            if (CharOperation.equals(params[i].name, name))
                                if (params[i].binding != null && params[i].binding.isValidBinding())
                                    return params[i].binding;
                    }
                }
                insideStaticContext |= methodScope.isStatic;
                insideTypeAnnotation = methodScope.insideTypeAnnotation;
                //$FALL-THROUGH$
            case BLOCK_SCOPE:
                ReferenceBinding localType = ((BlockScope) scope).findLocalType(name); // looks in this scope only
                if (localType != null) {
                    if (foundType != null && foundType != localType)
                        return new ProblemReferenceBinding(new char[][] { name }, foundType,
                                ProblemReasons.InheritedNameHidesEnclosingName);
                    return localType;
                }
                break;
            case CLASS_SCOPE:
                SourceTypeBinding sourceType = ((ClassScope) scope).referenceContext.binding;
                if (scope == this && (sourceType.tagBits & TagBits.TypeVariablesAreConnected) == 0) {
                    // type variables take precedence over the source type, ex. class X <X> extends X == class X <Y> extends Y
                    // but not when we step out to the enclosing type
                    TypeVariableBinding typeVariable = sourceType.getTypeVariable(name);
                    if (typeVariable != null)
                        return typeVariable;
                    if (CharOperation.equals(name, sourceType.sourceName))
                        return sourceType;
                    insideStaticContext |= sourceType.isStatic();
                    break;
                }
                // member types take precedence over type variables
                if (!insideTypeAnnotation) {
                    // 6.5.5.1 - member types have precedence over top-level type in same unit
                    ReferenceBinding memberType = findMemberType(name, sourceType);
                    if (memberType != null) { // skip it if we did not find anything
                        if (memberType.problemId() == ProblemReasons.Ambiguous) {
                            if (foundType == null || foundType.problemId() == ProblemReasons.NotVisible)
                                // supercedes any potential InheritedNameHidesEnclosingName problem
                                return memberType;
                            // make the user qualify the type, likely wants the first inherited type
                            return new ProblemReferenceBinding(new char[][] { name }, foundType,
                                    ProblemReasons.InheritedNameHidesEnclosingName);
                        }
                        if (memberType.isValidBinding()) {
                            if (sourceType == memberType.enclosingType() || inheritedHasPrecedence) {
                                if (insideStaticContext && !memberType.isStatic() && sourceType.isGenericType())
                                    return new ProblemReferenceBinding(new char[][] { name }, memberType,
                                            ProblemReasons.NonStaticReferenceInStaticContext);
                                // found a valid type in the 'immediate' scope (i.e. not inherited)
                                // OR in 1.4 mode (inherited visible shadows enclosing)
                                if (foundType == null || (inheritedHasPrecedence
                                        && foundType.problemId() == ProblemReasons.NotVisible))
                                    return memberType;
                                // if a valid type was found, complain when another is found in an 'immediate' enclosing type (i.e. not inherited)
                                if (foundType.isValidBinding() && foundType != memberType)
                                    return new ProblemReferenceBinding(new char[][] { name }, foundType,
                                            ProblemReasons.InheritedNameHidesEnclosingName);
                            }
                        }
                        if (foundType == null || (foundType.problemId() == ProblemReasons.NotVisible
                                && memberType.problemId() != ProblemReasons.NotVisible))
                            // only remember the memberType if its the first one found or the previous one was not visible & memberType is...
                            foundType = memberType;
                    }
                }
                TypeVariableBinding typeVariable = sourceType.getTypeVariable(name);
                if (typeVariable != null) {
                    if (insideStaticContext) // do not consider this type modifiers: access is legite within same type
                        return new ProblemReferenceBinding(new char[][] { name }, typeVariable,
                                ProblemReasons.NonStaticReferenceInStaticContext);
                    return typeVariable;
                }
                insideStaticContext |= sourceType.isStatic();
                insideTypeAnnotation = false;
                if (CharOperation.equals(sourceType.sourceName, name)) {
                    if (foundType != null && foundType != sourceType
                            && foundType.problemId() != ProblemReasons.NotVisible)
                        return new ProblemReferenceBinding(new char[][] { name }, foundType,
                                ProblemReasons.InheritedNameHidesEnclosingName);
                    return sourceType;
                }
                break;
            case COMPILATION_UNIT_SCOPE:
                break done;
            }
            scope = scope.parent;
        }
        if (foundType != null && foundType.problemId() != ProblemReasons.NotVisible)
            return foundType;
    }

    // at this point the scope is a compilation unit scope
    CompilationUnitScope unitScope = (CompilationUnitScope) scope;
    HashtableOfObject typeOrPackageCache = unitScope.typeOrPackageCache;
    if (typeOrPackageCache != null) {
        Binding cachedBinding = (Binding) typeOrPackageCache.get(name);
        if (cachedBinding != null) { // can also include NotFound ProblemReferenceBindings if we already know this name is not found
            if (cachedBinding instanceof ImportBinding) { // single type import cached in faultInImports(), replace it in the cache with the type
                ImportReference importReference = ((ImportBinding) cachedBinding).reference;
                if (importReference != null) {
                    importReference.bits |= ASTNode.Used;
                }
                if (cachedBinding instanceof ImportConflictBinding)
                    typeOrPackageCache.put(name,
                            cachedBinding = ((ImportConflictBinding) cachedBinding).conflictingTypeBinding); // already know its visible
                else
                    typeOrPackageCache.put(name,
                            cachedBinding = ((ImportBinding) cachedBinding).resolvedImport); // already know its visible
            }
            if ((mask & Binding.TYPE) != 0) {
                if (foundType != null && foundType.problemId() != ProblemReasons.NotVisible
                        && cachedBinding.problemId() != ProblemReasons.Ambiguous)
                    return foundType; // problem type from above supercedes NotFound type but not Ambiguous import case
                if (cachedBinding instanceof ReferenceBinding)
                    return cachedBinding; // cached type found in previous walk below
            }
            if ((mask & Binding.PACKAGE) != 0 && cachedBinding instanceof PackageBinding)
                return cachedBinding; // cached package found in previous walk below
        }
    }

    // ask for the imports + name
    if ((mask & Binding.TYPE) != 0) {
        ImportBinding[] imports = unitScope.imports;
        if (imports != null && typeOrPackageCache == null) { // walk single type imports since faultInImports() has not run yet
            nextImport: for (int i = 0, length = imports.length; i < length; i++) {
                ImportBinding importBinding = imports[i];
                if (!importBinding.onDemand) {
                    // GROOVY start
                    /* old {
                    if (CharOperation.equals(importBinding.compoundName[importBinding.compoundName.length - 1], name)) {
                    } new */
                    if (CharOperation.equals(getSimpleName(importBinding), name)) {
                        // GROOVY end
                        Binding resolvedImport = unitScope.resolveSingleImport(importBinding, Binding.TYPE);
                        if (resolvedImport == null)
                            continue nextImport;
                        if (resolvedImport instanceof TypeBinding) {
                            ImportReference importReference = importBinding.reference;
                            if (importReference != null)
                                importReference.bits |= ASTNode.Used;
                            return resolvedImport; // already know its visible
                        }
                    }
                }
            }
        }

        // check if the name is in the current package, skip it if its a sub-package
        PackageBinding currentPackage = unitScope.fPackage;
        unitScope.recordReference(currentPackage.compoundName, name);
        Binding binding = currentPackage.getTypeOrPackage(name);
        if (binding instanceof ReferenceBinding) {
            ReferenceBinding referenceType = (ReferenceBinding) binding;
            if ((referenceType.tagBits & TagBits.HasMissingType) == 0) {
                if (typeOrPackageCache != null)
                    typeOrPackageCache.put(name, referenceType);
                return referenceType; // type is always visible to its own package
            }
        }

        // check on demand imports
        if (imports != null) {
            boolean foundInImport = false;
            ReferenceBinding type = null;
            for (int i = 0, length = imports.length; i < length; i++) {
                ImportBinding someImport = imports[i];
                if (someImport.onDemand) {
                    Binding resolvedImport = someImport.resolvedImport;
                    ReferenceBinding temp = null;
                    if (resolvedImport instanceof PackageBinding) {
                        temp = findType(name, (PackageBinding) resolvedImport, currentPackage);
                    } else if (someImport.isStatic()) {
                        temp = findMemberType(name, (ReferenceBinding) resolvedImport); // static imports are allowed to see inherited member types
                        if (temp != null && !temp.isStatic())
                            temp = null;
                    } else {
                        temp = findDirectMemberType(name, (ReferenceBinding) resolvedImport);
                    }
                    if (temp != type && temp != null) {
                        if (temp.isValidBinding()) {
                            // GROOVY - start - allow for imports expressed in source to override 'default' imports - GRECLIPSE-945
                            boolean conflict = true; // do we need to continue checking
                            if (this.parent != null && foundInImport) {
                                CompilationUnitScope cuScope = compilationUnitScope();
                                if (cuScope != null) {
                                    ReferenceBinding chosenBinding = cuScope.selectBinding(temp, type,
                                            someImport.reference != null);
                                    if (chosenBinding != null) {
                                        // The new binding was selected as a valid answer
                                        conflict = false;
                                        foundInImport = true;
                                        type = chosenBinding;
                                    }
                                }
                            }
                            if (conflict) {
                                // GROOVY - end
                                ImportReference importReference = someImport.reference;
                                if (importReference != null) {
                                    importReference.bits |= ASTNode.Used;
                                }
                                if (foundInImport) {
                                    // Answer error binding -- import on demand conflict; name found in two import on demand packages.
                                    temp = new ProblemReferenceBinding(new char[][] { name }, type,
                                            ProblemReasons.Ambiguous);
                                    if (typeOrPackageCache != null)
                                        typeOrPackageCache.put(name, temp);
                                    return temp;
                                }
                                type = temp;
                                foundInImport = true;
                                // GROOVY - start
                            }
                            // GROOVY - end
                        } else if (foundType == null) {
                            foundType = temp;
                        }
                    }
                }
            }
            if (type != null) {
                if (typeOrPackageCache != null)
                    typeOrPackageCache.put(name, type);
                return type;
            }
        }
    }

    unitScope.recordSimpleReference(name);
    if ((mask & Binding.PACKAGE) != 0) {
        PackageBinding packageBinding = unitScope.environment.getTopLevelPackage(name);
        if (packageBinding != null) {
            if (typeOrPackageCache != null)
                typeOrPackageCache.put(name, packageBinding);
            return packageBinding;
        }
    }

    // Answer error binding -- could not find name
    if (foundType == null) {
        char[][] qName = new char[][] { name };
        ReferenceBinding closestMatch = null;
        if ((mask & Binding.PACKAGE) != 0) {
            if (needResolve) {
                closestMatch = environment().createMissingType(unitScope.fPackage, qName);
            }
        } else {
            PackageBinding packageBinding = unitScope.environment.getTopLevelPackage(name);
            if (packageBinding == null || !packageBinding.isValidBinding()) {
                if (needResolve) {
                    closestMatch = environment().createMissingType(unitScope.fPackage, qName);
                }
            }
        }
        foundType = new ProblemReferenceBinding(qName, closestMatch, ProblemReasons.NotFound);
        if (typeOrPackageCache != null && (mask & Binding.PACKAGE) != 0) { // only put NotFound type in cache if you know its not a package
            typeOrPackageCache.put(name, foundType);
        }
    } else if ((foundType.tagBits & TagBits.HasMissingType) != 0) {
        char[][] qName = new char[][] { name };
        foundType = new ProblemReferenceBinding(qName, foundType, ProblemReasons.NotFound);
        if (typeOrPackageCache != null && (mask & Binding.PACKAGE) != 0) // only put NotFound type in cache if you know its not a package
            typeOrPackageCache.put(name, foundType);
    }
    return foundType;
}

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

License:Open Source License

/** Resolve a list of type parameters, searching for type value parameters.
 * @param typeParameters parameters to investigate
 * @param scope/*from w  w w  .j ava  2s .  co  m*/
 * @param bindings array of field bindings with enough space to hold all natural fields
 *        plus all fields generated from type value parameters
 * @param knownFieldNames used for storing generated fields, too.
 */
public static void resolveValueParameters(TypeParameter[] typeParameters, ClassScope scope,
        FieldBinding[] bindings, HashtableOfObject knownFieldNames) {
    int count = 0;
    for (int i = 0; i < typeParameters.length; i++) {
        if (typeParameters[i] instanceof TypeValueParameter) {
            FieldBinding resolvedField = ((TypeValueParameter) typeParameters[i])
                    .resolveAsValueParameter(scope);
            knownFieldNames.put(typeParameters[i].name, resolvedField);
            bindings[count++] = resolvedField;
        }
    }
}