Example usage for org.eclipse.jdt.internal.compiler.ast ASTNode OnDemand

List of usage examples for org.eclipse.jdt.internal.compiler.ast ASTNode OnDemand

Introduction

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

Prototype

int OnDemand

To view the source code for org.eclipse.jdt.internal.compiler.ast ASTNode OnDemand.

Click Source Link

Usage

From source file:com.android.tools.lint.psi.EcjPsiBuilder.java

License:Apache License

private EcjPsiImport toImport(EcjPsiImportList parent, ImportReference importReference) {
    String typeName = getTypeName(importReference.getImportName());
    boolean onDemand = (importReference.bits & ASTNode.OnDemand) != 0;
    EcjPsiImport ref;/*  www. j  a  v a2  s.  c o  m*/
    if ((importReference.modifiers & ClassFileConstants.AccStatic) != 0) {
        ref = new EcjPsiStaticImport(mManager, importReference, typeName, onDemand);
    } else {
        ref = new EcjPsiImport(mManager, importReference, typeName, onDemand);
    }
    ref.setReference(toImportReference(ref, importReference));
    parent.adoptChild(ref);
    return ref;
}

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  ava 2 s.co  m
            }
        }
    }
    return nodeSet.addMatch(node, declarationsLevel);
}

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

License:Open Source License

/**
 * Creates an IImportDeclaration from the given import statement
 *///  w  ww  .  j av  a  2  s.c  o m
protected IJavaElement createImportHandle(ImportReference importRef) {
    char[] importName = CharOperation.concatWith(importRef.getImportName(), '.');
    if ((importRef.bits & ASTNode.OnDemand) != 0)
        importName = CharOperation.concat(importName, ".*".toCharArray()); //$NON-NLS-1$
    Openable openable = this.currentPossibleMatch.openable;
    if (openable instanceof CompilationUnit)
        return ((CompilationUnit) openable).getImport(new String(importName));

    // binary types do not contain import statements so just answer the top-level type as the element
    IType binaryType = ((ClassFile) openable).getType();
    String typeName = binaryType.getElementName();
    int lastDollar = typeName.lastIndexOf('$');
    if (lastDollar == -1)
        return binaryType;
    return createTypeHandle(typeName.substring(0, lastDollar));
}

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 w w  .  j a v a2 s .  c  om
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 w ww.ja 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.PackageReferenceLocator.java

License:Open Source License

protected void matchReportReference(ASTNode reference, IJavaElement element, IJavaElement localElement,
        IJavaElement[] otherElements, Binding elementBinding, int accuracy, MatchLocator locator)
        throws CoreException {
    long[] positions = null;
    int last = -1;
    if (reference instanceof ImportReference) {
        ImportReference importRef = (ImportReference) reference;
        positions = importRef.sourcePositions;
        last = (importRef.bits & ASTNode.OnDemand) != 0 ? positions.length : positions.length - 1;
    } else {//w  ww  .ja  v  a  2  s  .  com
        TypeBinding typeBinding = null;
        if (reference instanceof QualifiedNameReference) {
            QualifiedNameReference qNameRef = (QualifiedNameReference) reference;
            positions = qNameRef.sourcePositions;
            switch (qNameRef.bits & ASTNode.RestrictiveFlagMASK) {
            case Binding.FIELD: // reading a field
                typeBinding = qNameRef.actualReceiverType;
                break;
            case Binding.TYPE: //=============only type ==============
                if (qNameRef.binding instanceof TypeBinding)
                    typeBinding = (TypeBinding) qNameRef.binding;
                break;
            case Binding.VARIABLE: //============unbound cases===========
            case Binding.TYPE | Binding.VARIABLE:
                Binding binding = qNameRef.binding;
                if (binding instanceof TypeBinding) {
                    typeBinding = (TypeBinding) binding;
                } else if (binding instanceof ProblemFieldBinding) {
                    typeBinding = qNameRef.actualReceiverType;
                    last = qNameRef.tokens.length
                            - (qNameRef.otherBindings == null ? 2 : qNameRef.otherBindings.length + 2);
                } else if (binding instanceof ProblemBinding) {
                    ProblemBinding pbBinding = (ProblemBinding) binding;
                    typeBinding = pbBinding.searchType;
                    last = CharOperation.occurencesOf('.', pbBinding.name);
                }
                break;
            }
        } else if (reference instanceof QualifiedTypeReference) {
            QualifiedTypeReference qTypeRef = (QualifiedTypeReference) reference;
            positions = qTypeRef.sourcePositions;
            typeBinding = qTypeRef.resolvedType;
        } else if (reference instanceof JavadocSingleTypeReference) {
            JavadocSingleTypeReference jsTypeRef = (JavadocSingleTypeReference) reference;
            positions = new long[1];
            positions[0] = (((long) jsTypeRef.sourceStart) << 32) + jsTypeRef.sourceEnd;
            typeBinding = jsTypeRef.resolvedType;
        }
        if (positions == null)
            return;
        if (typeBinding instanceof ArrayBinding)
            typeBinding = ((ArrayBinding) typeBinding).leafComponentType;
        if (typeBinding instanceof ProblemReferenceBinding)
            typeBinding = ((ProblemReferenceBinding) typeBinding).closestMatch();
        if (typeBinding instanceof ReferenceBinding) {
            PackageBinding pkgBinding = ((ReferenceBinding) typeBinding).fPackage;
            if (pkgBinding != null)
                last = pkgBinding.compoundName.length;
        }
        // Do not report qualified references which are only enclosing type
        // (see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=91078)
        ReferenceBinding enclosingType = typeBinding == null ? null : typeBinding.enclosingType();
        if (enclosingType != null) {
            int length = positions.length;
            while (enclosingType != null && length > 0) {
                length--;
                enclosingType = enclosingType.enclosingType();
            }
            if (length <= 1)
                return;
        }
    }
    if (last == -1) {
        last = this.pattern.segments.length;
    }
    if (last == 0)
        return;
    if (last > positions.length)
        last = positions.length;
    int sourceStart = (int) (positions[0] >>> 32);
    int sourceEnd = ((int) positions[last - 1]);
    PackageReferenceMatch packageReferenceMatch = locator.newPackageReferenceMatch(element, accuracy,
            sourceStart, sourceEnd - sourceStart + 1, reference);
    packageReferenceMatch.setLocalElement(localElement);
    this.match = packageReferenceMatch;
    locator.report(this.match);
}

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;
        }/*from  ww w.  ja v a  2s. c  om*/
        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 matchReportImportRef(ImportReference importRef, Binding binding, IJavaElement element,
        int accuracy, MatchLocator locator) throws CoreException {
    if (this.isDeclarationOfReferencedTypesPattern) {
        if ((element = findElement(element, accuracy)) != null) {
            SimpleSet knownTypes = ((DeclarationOfReferencedTypesPattern) this.pattern).knownTypes;
            while (binding instanceof ReferenceBinding) {
                ReferenceBinding typeBinding = (ReferenceBinding) binding;
                reportDeclaration(typeBinding, 1, locator, knownTypes);
                binding = typeBinding.enclosingType();
            }/*w  w w. j av  a 2s  . c  o m*/
        }
        return;
    }

    // return if this is not necessary to report
    if (this.pattern.hasTypeArguments() && !this.isEquivalentMatch && !this.isErasureMatch) {
        return;
    }

    // Return if fine grain is on and does not concern import reference
    if ((this.pattern.fineGrain != 0
            && (this.pattern.fineGrain & IJavaSearchConstants.IMPORT_DECLARATION_TYPE_REFERENCE) == 0)) {
        return;
    }

    // Create search match
    this.match = locator.newTypeReferenceMatch(element, binding, accuracy, importRef);

    // set match raw flag and rule
    this.match.setRaw(true);
    if (this.pattern.hasTypeArguments()) {
        // binding is raw => only compatible erasure if pattern has type arguments
        this.match.setRule(this.match.getRule() & (~SearchPattern.R_FULL_MATCH));
    }

    // Try to find best selection for match
    TypeBinding typeBinding = null;
    boolean lastButOne = false;
    if (binding instanceof ReferenceBinding) {
        typeBinding = (ReferenceBinding) binding;
    } else if (binding instanceof FieldBinding) { // may happen for static import
        typeBinding = ((FieldBinding) binding).declaringClass;
        lastButOne = importRef.isStatic() && ((importRef.bits & ASTNode.OnDemand) == 0);
    } else if (binding instanceof MethodBinding) { // may happen for static import
        typeBinding = ((MethodBinding) binding).declaringClass;
        lastButOne = importRef.isStatic() && ((importRef.bits & ASTNode.OnDemand) == 0);
    }
    if (typeBinding != null) {
        int lastIndex = importRef.tokens.length - 1;
        if (lastButOne) {
            // for field or method static import, use last but one token
            lastIndex--;
        }
        if (typeBinding instanceof ProblemReferenceBinding) {
            ProblemReferenceBinding pbBinding = (ProblemReferenceBinding) typeBinding;
            typeBinding = pbBinding.closestMatch();
            lastIndex = pbBinding.compoundName.length - 1;
        }
        // try to match all enclosing types for which the token matches as well.
        while (typeBinding != null && lastIndex >= 0) {
            if (resolveLevelForType(typeBinding) != IMPOSSIBLE_MATCH) {
                if (locator.encloses(element)) {
                    long[] positions = importRef.sourcePositions;
                    // index now depends on pattern type signature
                    int index = lastIndex;
                    if (this.pattern.qualification != null) {
                        index = lastIndex - this.pattern.segmentsSize;
                    }
                    if (index < 0)
                        index = 0;
                    int start = (int) ((positions[index]) >>> 32);
                    int end = (int) positions[lastIndex];
                    // report match
                    this.match.setOffset(start);
                    this.match.setLength(end - start + 1);
                    locator.report(this.match);
                }
                return;
            }
            lastIndex--;
            typeBinding = typeBinding.enclosingType();
        }
    }
    locator.reportAccurateTypeReference(this.match, importRef, this.pattern.simpleName);
}

From source file:lombok.eclipse.EclipseImportList.java

License:Open Source License

@Override
public String getFullyQualifiedNameForSimpleName(String unqualified) {
    if (imports != null) {
        outer: for (ImportReference imp : imports) {
            if ((imp.bits & ASTNode.OnDemand) != 0)
                continue;
            char[][] tokens = imp.tokens;
            char[] token = tokens.length == 0 ? new char[0] : tokens[tokens.length - 1];
            int len = token.length;
            if (len != unqualified.length())
                continue;
            for (int i = 0; i < len; i++)
                if (token[i] != unqualified.charAt(i))
                    continue outer;
            return LombokInternalAliasing.processAliases(toQualifiedName(tokens));
        }/*w w  w .  j av  a2  s .c  om*/
    }
    return null;
}

From source file:lombok.eclipse.EclipseImportList.java

License:Open Source License

@Override
public boolean hasStarImport(String packageName) {
    if (isEqual(packageName, pkg))
        return true;
    if ("java.lang".equals(packageName))
        return true;

    if (pkg != null && pkg.tokens != null && pkg.tokens.length == 0) {
        for (Map.Entry<String, Collection<String>> e : LombokInternalAliasing.IMPLIED_EXTRA_STAR_IMPORTS
                .entrySet()) {/*w w  w. j  ava  2  s. c  o m*/
            if (isEqual(e.getKey(), pkg) && e.getValue().contains(packageName))
                return true;
        }
    }

    if (imports != null)
        for (ImportReference imp : imports) {
            if ((imp.bits & ASTNode.OnDemand) == 0)
                continue;
            if (imp.isStatic())
                continue;
            if (isEqual(packageName, imp))
                return true;
            for (Map.Entry<String, Collection<String>> e : LombokInternalAliasing.IMPLIED_EXTRA_STAR_IMPORTS
                    .entrySet()) {
                if (isEqual(e.getKey(), imp) && e.getValue().contains(packageName))
                    return true;
            }

        }
    return false;
}