Example usage for org.eclipse.jdt.internal.core JavaModelManager createCompilationUnitFrom

List of usage examples for org.eclipse.jdt.internal.core JavaModelManager createCompilationUnitFrom

Introduction

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

Prototype

public static ICompilationUnit createCompilationUnitFrom(IFile file, IJavaProject project) 

Source Link

Document

Creates and returns a compilation unit element for the given .java file, its project being the given project.

Usage

From source file:org.eclipse.jdt.internal.core.JavaModelManager.java

License:Open Source License

/**
 * Add a secondary type in temporary indexing cache for a project got from given path.
 *
 * Current secondary types cache is not modified as we want to wait that indexing
 * was finished before taking new secondary types into account.
 *
 * Indexing cache is a specific entry in secondary types cache which key is
 * {@link #INDEXED_SECONDARY_TYPES } and value a map with same structure than
 * secondary types cache itself.//from ww w . j  ava  2s.  c  o m
 *
 * @see #secondaryTypes(IJavaProject, boolean, IProgressMonitor)
 */
public void secondaryTypeAdding(String path, char[] typeName, char[] packageName) {
    if (VERBOSE) {
        StringBuffer buffer = new StringBuffer("JavaModelManager.addSecondaryType("); //$NON-NLS-1$
        buffer.append(path);
        buffer.append(',');
        buffer.append('[');
        buffer.append(new String(packageName));
        buffer.append('.');
        buffer.append(new String(typeName));
        buffer.append(']');
        buffer.append(')');
        Util.verbose(buffer.toString());
    }
    IWorkspaceRoot wRoot = ResourcesPlugin.getWorkspace().getRoot();
    IResource resource = wRoot.findMember(path);
    if (resource != null) {
        if (org.eclipse.jdt.internal.core.util.Util.isJavaLikeFileName(path)
                && resource.getType() == IResource.FILE) {
            IProject project = resource.getProject();
            try {
                PerProjectInfo projectInfo = getPerProjectInfoCheckExistence(project);
                // Get or create map to cache secondary types while indexing (can be not synchronized as indexing insure a non-concurrent usage)
                HashMap indexedSecondaryTypes = null;
                if (projectInfo.secondaryTypes == null) {
                    projectInfo.secondaryTypes = new Hashtable(3);
                    indexedSecondaryTypes = new HashMap(3);
                    projectInfo.secondaryTypes.put(INDEXED_SECONDARY_TYPES, indexedSecondaryTypes);
                } else {
                    indexedSecondaryTypes = (HashMap) projectInfo.secondaryTypes.get(INDEXED_SECONDARY_TYPES);
                    if (indexedSecondaryTypes == null) {
                        indexedSecondaryTypes = new HashMap(3);
                        projectInfo.secondaryTypes.put(INDEXED_SECONDARY_TYPES, indexedSecondaryTypes);
                    }
                }
                // Store the secondary type in temporary cache (these are just handles => no problem to create it now...)
                HashMap allTypes = (HashMap) indexedSecondaryTypes.get(resource);
                if (allTypes == null) {
                    allTypes = new HashMap(3);
                    indexedSecondaryTypes.put(resource, allTypes);
                }
                ICompilationUnit unit = JavaModelManager.createCompilationUnitFrom((IFile) resource, null);
                if (unit != null) {
                    String typeString = new String(typeName);
                    IType type = unit.getType(typeString);
                    // String packageString = new String(packageName);
                    // use package fragment name instead of parameter as it may be invalid...
                    // see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=186781
                    String packageString = type.getPackageFragment().getElementName();
                    HashMap packageTypes = (HashMap) allTypes.get(packageString);
                    if (packageTypes == null) {
                        packageTypes = new HashMap(3);
                        allTypes.put(packageString, packageTypes);
                    }
                    packageTypes.put(typeString, type);
                }
                if (VERBOSE) {
                    Util.verbose("   - indexing cache:"); //$NON-NLS-1$
                    Iterator entries = indexedSecondaryTypes.entrySet().iterator();
                    while (entries.hasNext()) {
                        Map.Entry entry = (Map.Entry) entries.next();
                        IFile file = (IFile) entry.getKey();
                        Util.verbose("      + " + file.getFullPath() + ':' + entry.getValue()); //$NON-NLS-1$
                    }
                }
            } catch (JavaModelException jme) {
                // do nothing
            }
        }
    }
}

From source file:org.eclipse.jdt.internal.core.JavaModelManager.java

License:Open Source License

private Map secondaryTypesSearching(IJavaProject project, boolean waitForIndexes, IProgressMonitor monitor,
        final PerProjectInfo projectInfo) throws JavaModelException {
    if (VERBOSE || BasicSearchEngine.VERBOSE) {
        StringBuffer buffer = new StringBuffer("JavaModelManager.secondaryTypesSearch("); //$NON-NLS-1$
        buffer.append(project.getElementName());
        buffer.append(',');
        buffer.append(waitForIndexes);/*from  w  w  w  .ja va2 s .  c  om*/
        buffer.append(')');
        Util.verbose(buffer.toString());
    }

    final Hashtable secondaryTypes = new Hashtable(3);
    IRestrictedAccessTypeRequestor nameRequestor = new IRestrictedAccessTypeRequestor() {
        public void acceptType(int modifiers, char[] packageName, char[] simpleTypeName,
                char[][] enclosingTypeNames, String path, AccessRestriction access) {
            String key = packageName == null ? "" : new String(packageName); //$NON-NLS-1$
            HashMap types = (HashMap) secondaryTypes.get(key);
            if (types == null)
                types = new HashMap(3);
            types.put(new String(simpleTypeName), path);
            secondaryTypes.put(key, types);
        }
    };

    // Build scope using prereq projects but only source folders
    IPackageFragmentRoot[] allRoots = project.getAllPackageFragmentRoots();
    int length = allRoots.length, size = 0;
    IPackageFragmentRoot[] allSourceFolders = new IPackageFragmentRoot[length];
    for (int i = 0; i < length; i++) {
        if (allRoots[i].getKind() == IPackageFragmentRoot.K_SOURCE) {
            allSourceFolders[size++] = allRoots[i];
        }
    }
    if (size < length) {
        System.arraycopy(allSourceFolders, 0, allSourceFolders = new IPackageFragmentRoot[size], 0, size);
    }

    // Search all secondary types on scope
    new BasicSearchEngine().searchAllSecondaryTypeNames(allSourceFolders, nameRequestor, waitForIndexes,
            monitor);

    // Build types from paths
    Iterator packages = secondaryTypes.values().iterator();
    while (packages.hasNext()) {
        HashMap types = (HashMap) packages.next();
        Iterator names = types.entrySet().iterator();
        while (names.hasNext()) {
            Map.Entry entry = (Map.Entry) names.next();
            String typeName = (String) entry.getKey();
            String path = (String) entry.getValue();
            if (org.eclipse.jdt.internal.core.util.Util.isJavaLikeFileName(path)) {
                IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(path));
                ICompilationUnit unit = JavaModelManager.createCompilationUnitFrom(file, null);
                IType type = unit.getType(typeName);
                types.put(typeName, type); // replace stored path with type itself
            } else {
                names.remove();
            }
        }
    }

    // Store result in per project info cache if still null or there's still an indexing cache (may have been set by another thread...)
    if (projectInfo.secondaryTypes == null || projectInfo.secondaryTypes.get(INDEXED_SECONDARY_TYPES) != null) {
        projectInfo.secondaryTypes = secondaryTypes;
        if (VERBOSE || BasicSearchEngine.VERBOSE) {
            System.out.print(Thread.currentThread() + "   -> secondary paths stored in cache: "); //$NON-NLS-1$
            System.out.println();
            Iterator entries = secondaryTypes.entrySet().iterator();
            while (entries.hasNext()) {
                Map.Entry entry = (Map.Entry) entries.next();
                String qualifiedName = (String) entry.getKey();
                Util.verbose("      - " + qualifiedName + '-' + entry.getValue()); //$NON-NLS-1$
            }
        }
    }
    return projectInfo.secondaryTypes;
}