Example usage for org.eclipse.jdt.internal.compiler.ast ImportReference isStatic

List of usage examples for org.eclipse.jdt.internal.compiler.ast ImportReference isStatic

Introduction

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

Prototype

public boolean isStatic() 

Source Link

Usage

From source file:com.codenvy.ide.ext.java.server.internal.codeassist.SelectionEngine.java

License:Open Source License

/**
 * Ask the engine to compute the selection at the specified position
 * of the given compilation unit.//from w ww .ja  v  a2s .  c  o  m
        
 *  @param sourceUnit org.eclipse.jdt.internal.compiler.env.ICompilationUnit
 *      the source of the current compilation unit.
 *
 *  @param selectionSourceStart int
 *  @param selectionSourceEnd int
 *      a range in the source where the selection is.
 */
public void select(ICompilationUnit sourceUnit, int selectionSourceStart, int selectionSourceEnd) {

    char[] source = sourceUnit.getContents();

    if (DEBUG) {
        System.out.print("SELECTION IN "); //$NON-NLS-1$
        System.out.print(sourceUnit.getFileName());
        System.out.print(" FROM "); //$NON-NLS-1$
        System.out.print(selectionSourceStart);
        System.out.print(" TO "); //$NON-NLS-1$
        System.out.println(selectionSourceEnd);
        System.out.println("SELECTION - Source :"); //$NON-NLS-1$
        System.out.println(source);
    }
    if (!checkSelection(source, selectionSourceStart, selectionSourceEnd)) {
        return;
    }
    if (DEBUG) {
        System.out.print("SELECTION - Checked : \""); //$NON-NLS-1$
        System.out.print(new String(source, this.actualSelectionStart,
                this.actualSelectionEnd - this.actualSelectionStart + 1));
        System.out.println('"');
    }
    try {
        this.acceptedAnswer = false;
        CompilationResult result = new CompilationResult(sourceUnit, 1, 1,
                this.compilerOptions.maxProblemsPerUnit);
        CompilationUnitDeclaration parsedUnit = this.parser.dietParse(sourceUnit, result,
                this.actualSelectionStart, this.actualSelectionEnd);

        if (parsedUnit != null) {
            if (DEBUG) {
                System.out.println("SELECTION - Diet AST :"); //$NON-NLS-1$
                System.out.println(parsedUnit.toString());
            }

            // scan the package & import statements first
            if (parsedUnit.currentPackage instanceof SelectionOnPackageReference) {
                char[][] tokens = ((SelectionOnPackageReference) parsedUnit.currentPackage).tokens;
                this.noProposal = false;
                this.requestor.acceptPackage(CharOperation.concatWith(tokens, '.'));
                return;
            }
            ImportReference[] imports = parsedUnit.imports;
            if (imports != null) {
                for (int i = 0, length = imports.length; i < length; i++) {
                    ImportReference importReference = imports[i];
                    if (importReference instanceof SelectionOnImportReference) {
                        char[][] tokens = ((SelectionOnImportReference) importReference).tokens;
                        this.noProposal = false;
                        this.requestor.acceptPackage(CharOperation.concatWith(tokens, '.'));
                        this.nameEnvironment.findTypes(CharOperation.concatWith(tokens, '.'), false, false,
                                IJavaSearchConstants.TYPE, this);

                        this.lookupEnvironment.buildTypeBindings(parsedUnit, null /*no access restriction*/);
                        if ((this.unitScope = parsedUnit.scope) != null) {
                            int tokenCount = tokens.length;
                            char[] lastToken = tokens[tokenCount - 1];
                            char[][] qualifierTokens = CharOperation.subarray(tokens, 0, tokenCount - 1);

                            if (qualifierTokens != null && qualifierTokens.length > 0) {
                                Binding binding = this.unitScope.getTypeOrPackage(qualifierTokens);
                                if (binding != null && binding instanceof ReferenceBinding) {
                                    ReferenceBinding ref = (ReferenceBinding) binding;
                                    selectMemberTypeFromImport(parsedUnit, lastToken, ref,
                                            importReference.isStatic());
                                    if (importReference.isStatic()) {
                                        selectStaticFieldFromStaticImport(parsedUnit, lastToken, ref);
                                        selectStaticMethodFromStaticImport(parsedUnit, lastToken, ref);
                                    }
                                }
                            }
                        }

                        // accept qualified types only if no unqualified type was accepted
                        if (!this.acceptedAnswer) {
                            acceptQualifiedTypes();
                            if (!this.acceptedAnswer) {
                                this.nameEnvironment.findTypes(this.selectedIdentifier, false, false,
                                        IJavaSearchConstants.TYPE, this);
                                // try with simple type name
                                if (!this.acceptedAnswer) {
                                    acceptQualifiedTypes();
                                }
                            }
                        }
                        if (this.noProposal && this.problem != null) {
                            this.requestor.acceptError(this.problem);
                        }
                        return;
                    }
                }
            }
            if (parsedUnit.types != null || parsedUnit.isPackageInfo()) {
                if (selectDeclaration(parsedUnit))
                    return;
                this.lookupEnvironment.buildTypeBindings(parsedUnit, null /*no access restriction*/);
                if ((this.unitScope = parsedUnit.scope) != null) {
                    try {
                        this.lookupEnvironment.completeTypeBindings(parsedUnit, true);

                        CompilationUnitDeclaration previousUnitBeingCompleted = this.lookupEnvironment.unitBeingCompleted;
                        this.lookupEnvironment.unitBeingCompleted = parsedUnit;
                        parsedUnit.scope.faultInTypes();
                        this.lookupEnvironment.unitBeingCompleted = previousUnitBeingCompleted;
                        ASTNode node = null;
                        if (parsedUnit.types != null)
                            node = parseBlockStatements(parsedUnit, selectionSourceStart);
                        if (DEBUG) {
                            System.out.println("SELECTION - AST :"); //$NON-NLS-1$
                            System.out.println(parsedUnit.toString());
                        }
                        parsedUnit.resolve();
                        if (node != null) {
                            selectLocalDeclaration(node);
                        }
                    } catch (SelectionNodeFound e) {
                        if (e.binding != null) {
                            if (DEBUG) {
                                System.out.println("SELECTION - Selection binding:"); //$NON-NLS-1$
                                System.out.println(e.binding.toString());
                            }
                            // if null then we found a problem in the selection node
                            selectFrom(e.binding, parsedUnit, sourceUnit, e.isDeclaration);
                        }
                    }
                }
            }
        }
        // only reaches here if no selection could be derived from the parsed tree
        // thus use the selected source and perform a textual type search
        if (!this.acceptedAnswer) {
            this.nameEnvironment.findTypes(this.selectedIdentifier, false, false, IJavaSearchConstants.TYPE,
                    this);

            // accept qualified types only if no unqualified type was accepted
            if (!this.acceptedAnswer) {
                acceptQualifiedTypes();

                // accept types from all the workspace only if no type was found in the project scope
                if (this.noProposal) {
                    findAllTypes(this.selectedIdentifier);
                }
            }
        }
        if (this.noProposal && this.problem != null) {
            this.requestor.acceptError(this.problem);
        }
    } catch (IndexOutOfBoundsException e) { // work-around internal failure - 1GEMF6D
        if (DEBUG) {
            System.out.println("Exception caught by SelectionEngine:"); //$NON-NLS-1$
            e.printStackTrace(System.out);
        }
    } catch (AbortCompilation e) { // ignore this exception for now since it typically means we cannot find java.lang.Object
        if (DEBUG) {
            System.out.println("Exception caught by SelectionEngine:"); //$NON-NLS-1$
            e.printStackTrace(System.out);
        }
    } finally {
        reset(true);
    }
}

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

License:Open Source License

public int match(ASTNode node, MatchingNodeSet nodeSet) {
    int declarationsLevel = IMPOSSIBLE_MATCH;
    if (this.pattern.findReferences) {
        if (node instanceof ImportReference) {
            // With static import, we can have static field reference in import reference
            ImportReference importRef = (ImportReference) node;
            int length = importRef.tokens.length - 1;
            if (importRef.isStatic() && ((importRef.bits & ASTNode.OnDemand) == 0)
                    && matchesName(this.pattern.name, importRef.tokens[length])) {
                char[][] compoundName = new char[length][];
                System.arraycopy(importRef.tokens, 0, compoundName, 0, length);
                FieldPattern fieldPattern = (FieldPattern) this.pattern;
                char[] declaringType = CharOperation.concat(fieldPattern.declaringQualification,
                        fieldPattern.declaringSimpleName, '.');
                if (matchesName(declaringType, CharOperation.concatWith(compoundName, '.'))) {
                    declarationsLevel = this.pattern.mustResolve ? POSSIBLE_MATCH : ACCURATE_MATCH;
                }//from   w ww .  j  a  v a  2 s . c o  m
            }
        }
    }
    return nodeSet.addMatch(node, declarationsLevel);
}

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

License:Open Source License

protected void matchLevelAndReportImportRef(ImportReference importRef, Binding binding, MatchLocator locator)
        throws CoreException {
    if (importRef.isStatic() && binding instanceof FieldBinding) {
        super.matchLevelAndReportImportRef(importRef, binding, locator);
    }/*from   w ww  .  java  2s .  com*/
}

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

License:Open Source License

/**
 * Visit the given resolved parse tree and report the nodes that match the search pattern.
 *//*from w  ww. j  a v  a2  s  . c  o  m*/
protected void reportMatching(CompilationUnitDeclaration unit, boolean mustResolve) throws CoreException {
    MatchingNodeSet nodeSet = this.currentPossibleMatch.nodeSet;
    boolean locatorMustResolve = this.patternLocator.mustResolve;
    if (nodeSet.mustResolve)
        this.patternLocator.mustResolve = true;
    if (BasicSearchEngine.VERBOSE) {
        System.out.println("Report matching: "); //$NON-NLS-1$
        int size = nodeSet.matchingNodes == null ? 0 : nodeSet.matchingNodes.elementSize;
        System.out.print("   - node set: accurate=" + size); //$NON-NLS-1$
        size = nodeSet.possibleMatchingNodesSet == null ? 0 : nodeSet.possibleMatchingNodesSet.elementSize;
        System.out.println(", possible=" + size); //$NON-NLS-1$
        System.out.print("   - must resolve: " + mustResolve); //$NON-NLS-1$
        System.out.print(" (locator: " + this.patternLocator.mustResolve); //$NON-NLS-1$
        System.out.println(", nodeSet: " + nodeSet.mustResolve + ')'); //$NON-NLS-1$
        System.out.println("   - fine grain flags=" //$NON-NLS-1$
                + JavaSearchPattern.getFineGrainFlagString(this.patternLocator.fineGrain()));
    }
    if (mustResolve) {
        this.unitScope = unit.scope.compilationUnitScope();
        // move the possible matching nodes that exactly match the search pattern to the matching nodes set
        Object[] nodes = nodeSet.possibleMatchingNodesSet.values;
        for (int i = 0, l = nodes.length; i < l; i++) {
            ASTNode node = (ASTNode) nodes[i];
            if (node == null)
                continue;
            if (node instanceof ImportReference) {
                // special case for import refs: they don't know their binding
                // import ref cannot be in the hierarchy of a type
                if (this.hierarchyResolver != null)
                    continue;

                ImportReference importRef = (ImportReference) node;
                Binding binding = (importRef.bits & ASTNode.OnDemand) != 0
                        ? this.unitScope.getImport(
                                CharOperation.subarray(importRef.tokens, 0, importRef.tokens.length), true,
                                importRef.isStatic())
                        : this.unitScope.getImport(importRef.tokens, false, importRef.isStatic());
                this.patternLocator.matchLevelAndReportImportRef(importRef, binding, this);
            } else {
                nodeSet.addMatch(node, this.patternLocator.resolveLevel(node));
            }
        }
        nodeSet.possibleMatchingNodesSet = new SimpleSet(3);
        if (BasicSearchEngine.VERBOSE) {
            int size = nodeSet.matchingNodes == null ? 0 : nodeSet.matchingNodes.elementSize;
            System.out.print("   - node set: accurate=" + size); //$NON-NLS-1$
            size = nodeSet.possibleMatchingNodesSet == null ? 0 : nodeSet.possibleMatchingNodesSet.elementSize;
            System.out.println(", possible=" + size); //$NON-NLS-1$
        }
    } else {
        this.unitScope = null;
    }

    if (nodeSet.matchingNodes.elementSize == 0)
        return; // no matching nodes were found
    this.methodHandles = new HashSet();

    boolean matchedUnitContainer = (this.matchContainer & PatternLocator.COMPILATION_UNIT_CONTAINER) != 0;

    // report references in javadoc
    if (unit.javadoc != null) {
        ASTNode[] nodes = nodeSet.matchingNodes(unit.javadoc.sourceStart, unit.javadoc.sourceEnd);
        if (nodes != null) {
            if (!matchedUnitContainer) {
                for (int i = 0, l = nodes.length; i < l; i++)
                    nodeSet.matchingNodes.removeKey(nodes[i]);
            } else {
                IJavaElement element = createPackageDeclarationHandle(unit);
                for (int i = 0, l = nodes.length; i < l; i++) {
                    ASTNode node = nodes[i];
                    Integer level = (Integer) nodeSet.matchingNodes.removeKey(node);
                    if (encloses(element)) {
                        this.patternLocator.matchReportReference(node, element, null, null, null/*no binding*/,
                                level.intValue(), this);
                    }
                }
            }
        }
    }

    if (matchedUnitContainer) {
        ImportReference pkg = unit.currentPackage;
        if (pkg != null && pkg.annotations != null) {
            IJavaElement element = createPackageDeclarationHandle(unit);
            if (element != null) {
                reportMatching(pkg.annotations, element, null, null, nodeSet, true, encloses(element));
            }
        }

        ImportReference[] imports = unit.imports;
        if (imports != null) {
            for (int i = 0, l = imports.length; i < l; i++) {
                ImportReference importRef = imports[i];
                Integer level = (Integer) nodeSet.matchingNodes.removeKey(importRef);
                if (level != null) {
                    this.patternLocator.matchReportImportRef(importRef, null /*no binding*/,
                            createImportHandle(importRef), level.intValue(), this);
                }
            }
        }
    }

    TypeDeclaration[] types = unit.types;
    if (types != null) {
        for (int i = 0, l = types.length; i < l; i++) {
            if (nodeSet.matchingNodes.elementSize == 0)
                return; // reported all the matching nodes
            TypeDeclaration type = types[i];
            Integer level = (Integer) nodeSet.matchingNodes.removeKey(type);
            int accuracy = (level != null && matchedUnitContainer) ? level.intValue() : -1;
            reportMatching(type, null, accuracy, nodeSet, 1);
        }
    }

    // Clear handle cache
    this.methodHandles = null;
    this.bindings.removeKey(this.pattern);
    this.patternLocator.mustResolve = locatorMustResolve;
}

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

License:Open Source License

public int match(ASTNode node, MatchingNodeSet nodeSet) {
    int declarationsLevel = IMPOSSIBLE_MATCH;
    if (this.pattern.findReferences) {
        if (node instanceof ImportReference) {
            // With static import, we can have static method reference in import reference
            ImportReference importRef = (ImportReference) node;
            int length = importRef.tokens.length - 1;
            if (importRef.isStatic() && ((importRef.bits & ASTNode.OnDemand) == 0)
                    && matchesName(this.pattern.selector, importRef.tokens[length])) {
                char[][] compoundName = new char[length][];
                System.arraycopy(importRef.tokens, 0, compoundName, 0, length);
                char[] declaringType = CharOperation.concat(this.pattern.declaringQualification,
                        this.pattern.declaringSimpleName, '.');
                if (matchesName(declaringType, CharOperation.concatWith(compoundName, '.'))) {
                    declarationsLevel = this.pattern.mustResolve ? POSSIBLE_MATCH : ACCURATE_MATCH;
                }/*from  www .  j  av a2 s.c om*/
            }
        }
    }
    return nodeSet.addMatch(node, declarationsLevel);
}

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

License:Open Source License

protected void matchLevelAndReportImportRef(ImportReference importRef, Binding binding, MatchLocator locator)
        throws CoreException {
    if (importRef.isStatic() && binding instanceof MethodBinding) {
        super.matchLevelAndReportImportRef(importRef, binding, locator);
    }/*from w  ww .  j a v a  2  s  .  co m*/
}

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

License:Open Source License

protected void matchLevelAndReportImportRef(ImportReference importRef, Binding binding, MatchLocator locator)
        throws CoreException {

    // for static import, binding can be a field binding or a member type binding
    // verify that in this case binding is static and use declaring class for fields
    Binding refBinding = binding;
    if (importRef.isStatic()) {
        if (binding instanceof FieldBinding) {
            FieldBinding fieldBinding = (FieldBinding) binding;
            if (!fieldBinding.isStatic())
                return;
            refBinding = fieldBinding.declaringClass;
        } else if (binding instanceof MethodBinding) {
            MethodBinding methodBinding = (MethodBinding) binding;
            if (!methodBinding.isStatic())
                return;
            refBinding = methodBinding.declaringClass;
        } else if (binding instanceof MemberTypeBinding) {
            MemberTypeBinding memberBinding = (MemberTypeBinding) binding;
            if (!memberBinding.isStatic())
                return;
        }//from  www  .j  av a 2 s. c  o m
    }

    // Look for closest pattern
    PatternLocator closestPattern = null;
    int level = IMPOSSIBLE_MATCH;
    for (int i = 0, length = this.patternLocators.length; i < length; i++) {
        PatternLocator patternLocator = this.patternLocators[i];
        int newLevel = patternLocator.referenceType() == 0 ? IMPOSSIBLE_MATCH
                : patternLocator.resolveLevel(refBinding);
        if (newLevel > level) {
            closestPattern = patternLocator;
            if (newLevel == ACCURATE_MATCH)
                break;
            level = newLevel;
        }
    }
    if (closestPattern != null) {
        closestPattern.matchLevelAndReportImportRef(importRef, binding, locator);
    }
}

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

License:Open Source License

protected void matchLevelAndReportImportRef(ImportReference importRef, Binding binding, MatchLocator locator)
        throws CoreException {
    Binding refBinding = binding;
    if (importRef.isStatic()) {
        // for static import, binding can be a field binding or a member type binding
        // verify that in this case binding is static and use declaring class for fields
        if (binding instanceof FieldBinding) {
            FieldBinding fieldBinding = (FieldBinding) binding;
            if (!fieldBinding.isStatic())
                return;
            refBinding = fieldBinding.declaringClass;
        } else if (binding instanceof MethodBinding) {
            MethodBinding methodBinding = (MethodBinding) binding;
            if (!methodBinding.isStatic())
                return;
            refBinding = methodBinding.declaringClass;
        } else if (binding instanceof MemberTypeBinding) {
            MemberTypeBinding memberBinding = (MemberTypeBinding) binding;
            if (!memberBinding.isStatic())
                return;
        }/*  w w w .  j a va  2s  . c  o  m*/
    }
    super.matchLevelAndReportImportRef(importRef, refBinding, locator);
}

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

License:Open Source License

protected int matchLevel(ImportReference importRef) {
    if (this.pattern.qualification == null) {
        if (this.pattern.simpleName == null)
            return ACCURATE_MATCH;
        char[][] tokens = importRef.tokens;
        boolean onDemand = (importRef.bits & ASTNode.OnDemand) != 0;
        final boolean isStatic = importRef.isStatic();
        if (!isStatic && onDemand) {
            return IMPOSSIBLE_MATCH;
        }/* w  w  w. j  av  a 2  s .  c o  m*/
        int length = tokens.length;
        if (matchesName(this.pattern.simpleName, tokens[length - 1])) {
            return ACCURATE_MATCH;
        }
        if (isStatic && !onDemand && length > 1) {
            if (matchesName(this.pattern.simpleName, tokens[length - 2])) {
                return ACCURATE_MATCH;
            }
        }
    } else {
        char[][] tokens = importRef.tokens;
        char[] qualifiedPattern = this.pattern.simpleName == null ? this.pattern.qualification
                : CharOperation.concat(this.pattern.qualification, this.pattern.simpleName, '.');
        char[] qualifiedTypeName = CharOperation.concatWith(tokens, '.');
        if (qualifiedPattern == null)
            return ACCURATE_MATCH; // null is as if it was "*"
        if (qualifiedTypeName == null)
            return IMPOSSIBLE_MATCH; // cannot match null name
        if (qualifiedTypeName.length == 0) { // empty name
            if (qualifiedPattern.length == 0) { // can only matches empty pattern
                return ACCURATE_MATCH;
            }
            return IMPOSSIBLE_MATCH;
        }
        boolean matchFirstChar = !this.isCaseSensitive || (qualifiedPattern[0] == qualifiedTypeName[0]);
        switch (this.matchMode) {
        case SearchPattern.R_EXACT_MATCH:
        case SearchPattern.R_PREFIX_MATCH:
            if (CharOperation.prefixEquals(qualifiedPattern, qualifiedTypeName, this.isCaseSensitive)) {
                return POSSIBLE_MATCH;
            }
            break;

        case SearchPattern.R_PATTERN_MATCH:
            if (CharOperation.match(qualifiedPattern, qualifiedTypeName, this.isCaseSensitive)) {
                return POSSIBLE_MATCH;
            }
            break;

        case SearchPattern.R_REGEXP_MATCH:
            // TODO (frederic) implement regular expression match
            break;
        case SearchPattern.R_CAMELCASE_MATCH:
            if (matchFirstChar && CharOperation.camelCaseMatch(qualifiedPattern, qualifiedTypeName, false)) {
                return POSSIBLE_MATCH;
            }
            // only test case insensitive as CamelCase already verified prefix case sensitive
            if (!this.isCaseSensitive
                    && CharOperation.prefixEquals(qualifiedPattern, qualifiedTypeName, false)) {
                return POSSIBLE_MATCH;
            }
            break;
        case SearchPattern.R_CAMELCASE_SAME_PART_COUNT_MATCH:
            if (matchFirstChar && CharOperation.camelCaseMatch(qualifiedPattern, qualifiedTypeName, true)) {
                return POSSIBLE_MATCH;
            }
            break;
        }
    }
    return IMPOSSIBLE_MATCH;
}

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

License:Open Source License

protected void matchLevelAndReportImportRef(ImportReference importRef, Binding binding, MatchLocator locator)
        throws CoreException {
    Binding refBinding = binding;
    if (importRef.isStatic()) {
        // for static import, binding can be a field binding or a member type binding
        // verify that in this case binding is static and use declaring class for fields
        if (binding instanceof FieldBinding) {
            FieldBinding fieldBinding = (FieldBinding) binding;
            if (!fieldBinding.isStatic())
                return;
            refBinding = fieldBinding.declaringClass;
        } else if (binding instanceof MethodBinding) {
            MethodBinding methodBinding = (MethodBinding) binding;
            if (!methodBinding.isStatic())
                return;
            refBinding = methodBinding.declaringClass;
        } else if (binding instanceof MemberTypeBinding) {
            MemberTypeBinding memberBinding = (MemberTypeBinding) binding;
            if (!memberBinding.isStatic())
                return;
        }/*w  ww  .  j  av  a 2  s  .  co  m*/
        // resolve and report
        int level = resolveLevel(refBinding);
        if (level >= INACCURATE_MATCH) {
            matchReportImportRef(importRef, binding, locator.createImportHandle(importRef),
                    level == ACCURATE_MATCH ? SearchMatch.A_ACCURATE : SearchMatch.A_INACCURATE, locator);
        }
        return;
    }
    super.matchLevelAndReportImportRef(importRef, refBinding, locator);
}