Example usage for org.eclipse.jdt.internal.core JavaElementRequestor getPackageFragments

List of usage examples for org.eclipse.jdt.internal.core JavaElementRequestor getPackageFragments

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.core JavaElementRequestor getPackageFragments.

Prototype

public IPackageFragment[] getPackageFragments() 

Source Link

Usage

From source file:com.codenvy.ide.ext.java.server.internal.core.NameLookup.java

License:Open Source License

/**
 * Find type. Considering secondary types and waiting for indexes depends on given corresponding parameters.
 *///w  ww . j a  va2  s  .co m
public Answer findType(String typeName, String packageName, boolean partialMatch, int acceptFlags,
        boolean considerSecondaryTypes, boolean waitForIndexes, boolean checkRestrictions,
        IProgressMonitor monitor) {
    if (packageName == null || packageName.length() == 0) {
        packageName = IPackageFragment.DEFAULT_PACKAGE_NAME;
    } else if (typeName.length() > 0 && ScannerHelper.isLowerCase(typeName.charAt(0))) {
        // see if this is a known package and not a type
        if (findPackageFragments(packageName + "." + typeName, false) != null) //$NON-NLS-1$
            return null;
    }

    // Look for concerned package fragments
    JavaElementRequestor elementRequestor = new JavaElementRequestor();
    seekPackageFragments(packageName, false, elementRequestor);
    IPackageFragment[] packages = elementRequestor.getPackageFragments();

    // Try to find type in package fragments list
    IType type = null;
    int length = packages.length;
    HashSet projects = null;
    IJavaProject javaProject = null;
    Answer suggestedAnswer = null;
    for (int i = 0; i < length; i++) {
        type = findType(typeName, packages[i], partialMatch, acceptFlags, waitForIndexes,
                considerSecondaryTypes);
        if (type != null) {
            AccessRestriction accessRestriction = null;
            if (checkRestrictions) {
                accessRestriction = getViolatedRestriction(typeName, packageName, type, accessRestriction);
            }
            Answer answer = new Answer(type, accessRestriction);
            if (!answer.ignoreIfBetter()) {
                if (answer.isBetter(suggestedAnswer))
                    return answer;
            } else if (answer.isBetter(suggestedAnswer))
                // remember suggestion and keep looking
                suggestedAnswer = answer;
        } else if (suggestedAnswer == null && considerSecondaryTypes) {
            if (javaProject == null) {
                javaProject = packages[i].getJavaProject();
            } else if (projects == null) {
                if (!javaProject.equals(packages[i].getJavaProject())) {
                    projects = new HashSet(3);
                    projects.add(javaProject);
                    projects.add(packages[i].getJavaProject());
                }
            } else {
                projects.add(packages[i].getJavaProject());
            }
        }
    }
    if (suggestedAnswer != null)
        // no better answer was found
        return suggestedAnswer;

    // If type was not found, try to find it as secondary in source folders
    if (considerSecondaryTypes && javaProject != null) {
        if (projects == null) {
            type = findSecondaryType(packageName, typeName, javaProject, waitForIndexes, monitor);
        } else {
            Iterator allProjects = projects.iterator();
            while (type == null && allProjects.hasNext()) {
                type = findSecondaryType(packageName, typeName, (IJavaProject) allProjects.next(),
                        waitForIndexes, monitor);
            }
        }
    }
    if (type != null) {
        ICompilationUnit unit = type.getCompilationUnit();
        if (unit != null && unit.isWorkingCopy()) { // https://bugs.eclipse.org/bugs/show_bug.cgi?id=421902
            IType[] types = null;
            try {
                types = unit.getTypes();
            } catch (JavaModelException e) {
                return null;
            }
            boolean typeFound = false;
            for (int i = 0, typesLength = types == null ? 0 : types.length; i < typesLength; i++) {
                if (types[i].getElementName().equals(typeName)) {
                    typeFound = true;
                    break;
                }
            }
            if (!typeFound)
                type = null;
        }
    }
    return type == null ? null : new Answer(type, null);
}