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

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

Introduction

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

Prototype

public void cacheZipFiles(Object owner) 

Source Link

Document

Starts caching ZipFiles.

Usage

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

License:Open Source License

/**
 * Locate the matches in the given files and report them using the search requestor.
 *///from w  ww.  j  av a 2 s .  c o m
public void locateMatches(SearchDocument[] searchDocuments) throws CoreException {
    if (this.patternLocator == null)
        return;
    int docsLength = searchDocuments.length;
    int progressLength = docsLength;
    if (BasicSearchEngine.VERBOSE) {
        System.out.println("Locating matches in documents ["); //$NON-NLS-1$
        for (int i = 0; i < docsLength; i++)
            System.out.println("\t" + searchDocuments[i]); //$NON-NLS-1$
        System.out.println("]"); //$NON-NLS-1$
    }
    IJavaProject[] javaModelProjects = null;
    if (this.searchPackageDeclaration) {
        javaModelProjects = JavaModelManager.getJavaModelManager().getJavaModel().getJavaProjects();
        progressLength += javaModelProjects.length;
    }

    // init infos for progress increasing
    int n = progressLength < 1000 ? Math.min(Math.max(progressLength / 200 + 1, 2), 4)
            : 5 * (progressLength / 1000);
    this.progressStep = progressLength < n ? 1 : progressLength / n; // step should not be 0
    this.progressWorked = 0;

    // extract working copies
    ArrayList copies = new ArrayList();
    for (int i = 0; i < docsLength; i++) {
        SearchDocument document = searchDocuments[i];
        if (document instanceof WorkingCopyDocument) {
            copies.add(((WorkingCopyDocument) document).workingCopy);
        }
    }
    int copiesLength = copies.size();
    this.workingCopies = new org.eclipse.jdt.core.ICompilationUnit[copiesLength];
    copies.toArray(this.workingCopies);

    JavaModelManager manager = JavaModelManager.getJavaModelManager();
    this.bindings = new SimpleLookupTable();
    try {
        // optimize access to zip files during search operation
        manager.cacheZipFiles(this);

        // initialize handle factory (used as a cache of handles so as to optimize space)
        if (this.handleFactory == null)
            this.handleFactory = new HandleFactory();

        if (this.progressMonitor != null) {
            this.progressMonitor.beginTask("", searchDocuments.length); //$NON-NLS-1$
        }

        // initialize pattern for polymorphic search (i.e. method reference pattern)
        this.patternLocator.initializePolymorphicSearch(this);

        JavaProject previousJavaProject = null;
        PossibleMatchSet matchSet = new PossibleMatchSet();
        Util.sort(searchDocuments, new Util.Comparer() {
            public int compare(Object a, Object b) {
                return ((SearchDocument) a).getPath().compareTo(((SearchDocument) b).getPath());
            }
        });
        int displayed = 0; // progress worked displayed
        String previousPath = null;
        SearchParticipant searchParticipant = null;
        for (int i = 0; i < docsLength; i++) {
            if (this.progressMonitor != null && this.progressMonitor.isCanceled()) {
                throw new OperationCanceledException();
            }

            // skip duplicate paths
            SearchDocument searchDocument = searchDocuments[i];
            if (searchParticipant == null) {
                searchParticipant = searchDocument.getParticipant();
            }
            searchDocuments[i] = null; // free current document
            String pathString = searchDocument.getPath();
            if (i > 0 && pathString.equals(previousPath)) {
                if (this.progressMonitor != null) {
                    this.progressWorked++;
                    if ((this.progressWorked % this.progressStep) == 0)
                        this.progressMonitor.worked(this.progressStep);
                }
                displayed++;
                continue;
            }
            previousPath = pathString;

            Openable openable;
            org.eclipse.jdt.core.ICompilationUnit workingCopy = null;
            if (searchDocument instanceof WorkingCopyDocument) {
                workingCopy = ((WorkingCopyDocument) searchDocument).workingCopy;
                openable = (Openable) workingCopy;
            } else {
                openable = this.handleFactory.createOpenable(pathString, this.scope);
            }
            //         if (openable == null) {
            //            if (this.progressMonitor != null) {
            //               this.progressWorked++;
            //               if ((this.progressWorked%this.progressStep)==0) this.progressMonitor.worked(this.progressStep);
            //            }
            //            displayed++;
            //            continue; // match is outside classpath
            //         }

            // create new parser and lookup environment if this is a new project
            IResource resource = null;
            JavaProject javaProject = (JavaProject) openable.getJavaProject();
            resource = workingCopy != null ? workingCopy.getResource() : openable.getResource();
            if (resource == null)
                resource = javaProject.getProject(); // case of a file in an external jar or external folder
            if (!javaProject.equals(previousJavaProject)) {
                // locate matches in previous project
                if (previousJavaProject != null) {
                    try {
                        locateMatches(previousJavaProject, matchSet, i - displayed);
                        displayed = i;
                    } catch (JavaModelException e) {
                        // problem with classpath in this project -> skip it
                    }
                    matchSet.reset();
                }
                previousJavaProject = javaProject;
            }
            matchSet.add(new PossibleMatch(this, resource, openable, searchDocument, this.pattern.mustResolve));
        }

        // last project
        if (previousJavaProject != null) {
            try {
                locateMatches(previousJavaProject, matchSet, docsLength - displayed);
            } catch (JavaModelException e) {
                // problem with classpath in last project -> ignore
            }
        }

        if (this.searchPackageDeclaration) {
            locatePackageDeclarations(searchParticipant, javaModelProjects);
        }

    } finally {
        if (this.progressMonitor != null)
            this.progressMonitor.done();
        if (this.nameEnvironment != null)
            this.nameEnvironment.cleanup();
        manager.flushZipFiles(this);
        this.bindings = null;
    }
}

From source file:org.codehaus.jdt.groovy.model.GroovyCompilationUnit.java

License:Open Source License

@Override
public org.eclipse.jdt.core.dom.CompilationUnit reconcile(int astLevel, int reconcileFlags,
        WorkingCopyOwner workingCopyOwner, IProgressMonitor monitor) throws JavaModelException {
    if (!isWorkingCopy())
        return null; // Reconciling is not supported on non working copies
    if (workingCopyOwner == null)
        workingCopyOwner = DefaultWorkingCopyOwner.PRIMARY;

    PerformanceStats stats = null;/*from   w  ww .  ja v a 2s  .co  m*/
    if (ReconcileWorkingCopyOperation.PERF) {
        stats = PerformanceStats.getStats(JavaModelManager.RECONCILE_PERF, this);
        stats.startRun(new String(this.getFileName()));
    }
    ReconcileWorkingCopyOperation op = new GroovyReconcileWorkingCopyOperation(this, astLevel, reconcileFlags,
            workingCopyOwner);
    JavaModelManager manager = JavaModelManager.getJavaModelManager();
    try {
        manager.cacheZipFiles(this); // cache zip files for performance (see
        // https://bugs.eclipse.org/bugs/show_bug.cgi?id=134172)
        op.runOperation(monitor);
    } finally {
        manager.flushZipFiles(this);
    }
    if (ReconcileWorkingCopyOperation.PERF) {
        stats.endRun();
    }
    return op.ast;
}

From source file:org.eclipse.che.jdt.internal.core.search.matching.MatchLocator.java

License:Open Source License

/**
* Locate the matches in the given files and report them using the search requestor.
*///from w  w  w . j  a  v a2 s. co  m
public void locateMatches(SearchDocument[] searchDocuments) throws CoreException {
    if (this.patternLocator == null)
        return;
    int docsLength = searchDocuments.length;
    int progressLength = docsLength;
    if (BasicSearchEngine.VERBOSE) {
        System.out.println("Locating matches in documents ["); //$NON-NLS-1$
        for (int i = 0; i < docsLength; i++)
            System.out.println("\t" + searchDocuments[i]); //$NON-NLS-1$
        System.out.println("]"); //$NON-NLS-1$
    }
    IJavaProject[] javaModelProjects = null;
    if (this.searchPackageDeclaration) {
        javaModelProjects = JavaModelManager.getJavaModelManager().getJavaModel().getJavaProjects();
        progressLength += javaModelProjects.length;
    }

    // init infos for progress increasing
    int n = progressLength < 1000 ? Math.min(Math.max(progressLength / 200 + 1, 2), 4)
            : 5 * (progressLength / 1000);
    this.progressStep = progressLength < n ? 1 : progressLength / n; // step should not be 0
    this.progressWorked = 0;

    // extract working copies
    ArrayList copies = new ArrayList();
    for (int i = 0; i < docsLength; i++) {
        SearchDocument document = searchDocuments[i];
        if (document instanceof WorkingCopyDocument) {
            copies.add(((WorkingCopyDocument) document).workingCopy);
        }
    }
    int copiesLength = copies.size();
    this.workingCopies = new org.eclipse.jdt.core.ICompilationUnit[copiesLength];
    copies.toArray(this.workingCopies);

    JavaModelManager manager = JavaModelManager.getJavaModelManager();
    this.bindings = new SimpleLookupTable();
    try {
        // optimize access to zip files during search operation
        manager.cacheZipFiles(this);

        // initialize handle factory (used as a cache of handles so as to optimize space)
        if (this.handleFactory == null)
            this.handleFactory = new HandleFactory();

        if (this.progressMonitor != null) {
            this.progressMonitor.beginTask("", searchDocuments.length); //$NON-NLS-1$
        }

        // initialize pattern for polymorphic search (i.e. method reference pattern)
        this.patternLocator.initializePolymorphicSearch(this);

        JavaProject previousJavaProject = null;
        PossibleMatchSet matchSet = new PossibleMatchSet();
        Util.sort(searchDocuments, new Util.Comparer() {
            public int compare(Object a, Object b) {
                return ((SearchDocument) a).getPath().compareTo(((SearchDocument) b).getPath());
            }
        });
        int displayed = 0; // progress worked displayed
        String previousPath = null;
        SearchParticipant searchParticipant = null;
        for (int i = 0; i < docsLength; i++) {
            if (this.progressMonitor != null && this.progressMonitor.isCanceled()) {
                throw new OperationCanceledException();
            }

            // skip duplicate paths
            SearchDocument searchDocument = searchDocuments[i];
            if (searchParticipant == null) {
                searchParticipant = searchDocument.getParticipant();
            }
            searchDocuments[i] = null; // free current document
            String pathString = searchDocument.getPath();
            if (i > 0 && pathString.equals(previousPath)) {
                if (this.progressMonitor != null) {
                    this.progressWorked++;
                    if ((this.progressWorked % this.progressStep) == 0)
                        this.progressMonitor.worked(this.progressStep);
                }
                displayed++;
                continue;
            }
            previousPath = pathString;

            Openable openable;
            org.eclipse.jdt.core.ICompilationUnit workingCopy = null;
            if (searchDocument instanceof WorkingCopyDocument) {
                workingCopy = ((WorkingCopyDocument) searchDocument).workingCopy;
                openable = (Openable) workingCopy;
            } else {
                openable = this.handleFactory.createOpenable(pathString, this.scope);
            }
            if (openable == null) {
                if (this.progressMonitor != null) {
                    this.progressWorked++;
                    if ((this.progressWorked % this.progressStep) == 0)
                        this.progressMonitor.worked(this.progressStep);
                }
                displayed++;
                continue; // match is outside classpath
            }

            // create new parser and lookup environment if this is a new project
            IResource resource = null;
            JavaProject javaProject = (JavaProject) openable.getJavaProject();
            resource = workingCopy != null ? workingCopy.getResource() : openable.getResource();
            if (resource == null)
                resource = javaProject.getProject(); // case of a file in an external jar or external folder
            if (!javaProject.equals(previousJavaProject)) {
                // locate matches in previous project
                if (previousJavaProject != null) {
                    try {
                        locateMatches(previousJavaProject, matchSet, i - displayed);
                        displayed = i;
                    } catch (JavaModelException e) {
                        // problem with classpath in this project -> skip it
                    }
                    matchSet.reset();
                }
                previousJavaProject = javaProject;
            }
            matchSet.add(new PossibleMatch(this, resource, openable, searchDocument, this.pattern.mustResolve));
        }

        // last project
        if (previousJavaProject != null) {
            try {
                locateMatches(previousJavaProject, matchSet, docsLength - displayed);
            } catch (JavaModelException e) {
                // problem with classpath in last project -> ignore
            }
        }

        if (this.searchPackageDeclaration) {
            locatePackageDeclarations(searchParticipant, javaModelProjects);
        }

    } finally {
        if (this.progressMonitor != null)
            this.progressMonitor.done();
        if (this.nameEnvironment != null)
            this.nameEnvironment.cleanup();
        this.unitScope = null;
        manager.flushZipFiles(this);
        this.bindings = null;
    }
}

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

License:Open Source License

public org.eclipse.jdt.core.dom.CompilationUnit reconcile(int astLevel, int reconcileFlags,
        WorkingCopyOwner workingCopyOwner, IProgressMonitor monitor) throws JavaModelException {

    if (!isWorkingCopy())
        return null; // Reconciling is not supported on non working copies
    if (workingCopyOwner == null)
        workingCopyOwner = DefaultWorkingCopyOwner.PRIMARY;

    PerformanceStats stats = null;/* w  ww.ja va 2  s . c  o m*/
    if (ReconcileWorkingCopyOperation.PERF) {
        stats = PerformanceStats.getStats(JavaModelManager.RECONCILE_PERF, this);
        stats.startRun(new String(getFileName()));
    }
    ReconcileWorkingCopyOperation op = new ReconcileWorkingCopyOperation(this, astLevel, reconcileFlags,
            workingCopyOwner);
    JavaModelManager manager = JavaModelManager.getJavaModelManager();
    try {
        manager.cacheZipFiles(this); // cache zip files for performance (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=134172)
        op.runOperation(monitor);
    } finally {
        manager.flushZipFiles(this);
    }
    if (ReconcileWorkingCopyOperation.PERF) {
        stats.endRun();
    }
    return op.ast;
}

From source file:org.eclipse.jdt.internal.core.search.matching.MatchLocator.java

License:Open Source License

/**
 * Locate the matches in the given files and report them using the search requestor.
 *///w w w .  j a  va 2s . c o m
public void locateMatches(SearchDocument[] searchDocuments) throws CoreException {
    if (this.patternLocator == null)
        return;
    int docsLength = searchDocuments.length;
    int progressLength = docsLength;
    if (BasicSearchEngine.VERBOSE) {
        System.out.println("Locating matches in documents ["); //$NON-NLS-1$
        for (int i = 0; i < docsLength; i++)
            System.out.println("\t" + searchDocuments[i]); //$NON-NLS-1$
        System.out.println("]"); //$NON-NLS-1$
    }
    IJavaProject[] javaModelProjects = null;
    if (this.searchPackageDeclaration) {
        javaModelProjects = JavaModelManager.getJavaModelManager().getJavaModel().getJavaProjects();
        progressLength += javaModelProjects.length;
    }

    // init infos for progress increasing
    int n = progressLength < 1000 ? Math.min(Math.max(progressLength / 200 + 1, 2), 4)
            : 5 * (progressLength / 1000);
    this.progressStep = progressLength < n ? 1 : progressLength / n; // step should not be 0
    this.progressWorked = 0;

    // extract working copies
    ArrayList copies = new ArrayList();
    for (int i = 0; i < docsLength; i++) {
        SearchDocument document = searchDocuments[i];
        if (document instanceof WorkingCopyDocument) {
            copies.add(((WorkingCopyDocument) document).workingCopy);
        }
    }
    int copiesLength = copies.size();
    this.workingCopies = new org.eclipse.jdt.core.ICompilationUnit[copiesLength];
    copies.toArray(this.workingCopies);

    JavaModelManager manager = JavaModelManager.getJavaModelManager();
    this.bindings = new SimpleLookupTable();
    try {
        // optimize access to zip files during search operation
        manager.cacheZipFiles(this);

        // initialize handle factory (used as a cache of handles so as to optimize space)
        if (this.handleFactory == null)
            this.handleFactory = new HandleFactory();

        if (this.progressMonitor != null) {
            this.progressMonitor.beginTask("", searchDocuments.length); //$NON-NLS-1$
        }

        // initialize pattern for polymorphic search (i.e. method reference pattern)
        this.patternLocator.initializePolymorphicSearch(this);

        JavaProject previousJavaProject = null;
        PossibleMatchSet matchSet = new PossibleMatchSet();
        Util.sort(searchDocuments, new Util.Comparer() {
            public int compare(Object a, Object b) {
                return ((SearchDocument) a).getPath().compareTo(((SearchDocument) b).getPath());
            }
        });
        int displayed = 0; // progress worked displayed
        String previousPath = null;
        SearchParticipant searchParticipant = null;
        for (int i = 0; i < docsLength; i++) {
            if (this.progressMonitor != null && this.progressMonitor.isCanceled()) {
                throw new OperationCanceledException();
            }

            // skip duplicate paths
            SearchDocument searchDocument = searchDocuments[i];
            if (searchParticipant == null) {
                searchParticipant = searchDocument.getParticipant();
            }
            searchDocuments[i] = null; // free current document
            String pathString = searchDocument.getPath();
            if (i > 0 && pathString.equals(previousPath)) {
                if (this.progressMonitor != null) {
                    this.progressWorked++;
                    if ((this.progressWorked % this.progressStep) == 0)
                        this.progressMonitor.worked(this.progressStep);
                }
                displayed++;
                continue;
            }
            previousPath = pathString;

            Openable openable;
            org.eclipse.jdt.core.ICompilationUnit workingCopy = null;
            if (searchDocument instanceof WorkingCopyDocument) {
                workingCopy = ((WorkingCopyDocument) searchDocument).workingCopy;
                openable = (Openable) workingCopy;
            } else {
                openable = this.handleFactory.createOpenable(pathString, this.scope);
            }
            if (openable == null) {
                if (this.progressMonitor != null) {
                    this.progressWorked++;
                    if ((this.progressWorked % this.progressStep) == 0)
                        this.progressMonitor.worked(this.progressStep);
                }
                displayed++;
                continue; // match is outside classpath
            }

            // create new parser and lookup environment if this is a new project
            IResource resource = null;
            JavaProject javaProject = (JavaProject) openable.getJavaProject();
            resource = workingCopy != null ? workingCopy.getResource() : openable.getResource();
            if (resource == null)
                resource = javaProject.getProject(); // case of a file in an external jar or external folder
            if (!javaProject.equals(previousJavaProject)) {
                // locate matches in previous project
                if (previousJavaProject != null) {
                    try {
                        locateMatches(previousJavaProject, matchSet, i - displayed);
                        displayed = i;
                    } catch (JavaModelException e) {
                        // problem with classpath in this project -> skip it
                    }
                    matchSet.reset();
                }
                previousJavaProject = javaProject;
            }
            matchSet.add(new PossibleMatch(this, resource, openable, searchDocument, this.pattern.mustResolve));
        }

        // last project
        if (previousJavaProject != null) {
            try {
                locateMatches(previousJavaProject, matchSet, docsLength - displayed);
            } catch (JavaModelException e) {
                // problem with classpath in last project -> ignore
            }
        }

        if (this.searchPackageDeclaration) {
            locatePackageDeclarations(searchParticipant, javaModelProjects);
        }

    } finally {
        if (this.progressMonitor != null)
            this.progressMonitor.done();
        if (this.nameEnvironment != null)
            this.nameEnvironment.cleanup();
        manager.flushZipFiles(this);
        this.bindings = null;
    }
}

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

License:Open Source License

/**
 * Locates and returns source code for the given (binary) type, in this
 * SourceMapper's ZIP file, or returns <code>null</code> if source
 * code cannot be found.//from  www .j av a  2  s  .c om
 * The given simpleSourceFileName is the .java file name (without the enclosing
 * folder) used to create the given type (e.g. "A.java" for x/y/A$Inner.class)
 */
public char[] findSource(IType type, String simpleSourceFileName) {
    long time = 0;
    if (VERBOSE) {
        time = System.currentTimeMillis();
    }
    PackageFragment pkgFrag = (PackageFragment) type.getPackageFragment();
    String name = org.eclipse.jdt.internal.core.util.Util.concatWith(pkgFrag.names, simpleSourceFileName, '/');

    char[] source = null;

    JavaModelManager javaModelManager = JavaModelManager.getJavaModelManager();
    try {
        javaModelManager.cacheZipFiles(this); // Cache any zip files we open during this operation

        if (this.rootPath != null) {
            source = getSourceForRootPath(this.rootPath, name);
        }

        if (source == null) {
            computeAllRootPaths(type);
            if (this.rootPaths != null) {
                loop: for (Iterator iterator = this.rootPaths.iterator(); iterator.hasNext();) {
                    String currentRootPath = (String) iterator.next();
                    if (!currentRootPath.equals(this.rootPath)) {
                        source = getSourceForRootPath(currentRootPath, name);
                        if (source != null) {
                            // remember right root path
                            this.rootPath = currentRootPath;
                            break loop;
                        }
                    }
                }
            }
        }
    } finally {
        javaModelManager.flushZipFiles(this); // clean up cached zip files.
    }
    if (VERBOSE) {
        System.out.println("spent " + (System.currentTimeMillis() - time) + "ms for " + type.getElementName()); //$NON-NLS-1$ //$NON-NLS-2$
    }
    return source;
}

From source file:org.jboss.tools.batch.internal.core.impl.BatchBuilder.java

License:Open Source License

private void buildJars(JarSet newJars) throws CoreException {
    IJavaProject jp = EclipseUtil.getJavaProject(getBatchProject().getProject());
    if (jp == null)
        return;//from   w ww .j  a v  a2 s  . c om

    JavaModelManager manager = JavaModelManager.getJavaModelManager();
    try {
        manager.cacheZipFiles(this);

        FileSet fileSet = new FileSet();
        for (String jar : newJars.getBatchModules()) {
            Path path = new Path(jar);
            IPackageFragmentRoot root = jp.getPackageFragmentRoot(jar);
            if (root == null)
                continue;
            if (!root.exists()) {
                IFile f = getFile(jar);
                if (f != null && f.exists()) {
                    root = jp.getPackageFragmentRoot(f);
                } else {
                    f = getFile(jar + "/META-INF/web-fragment.xml");
                    if (f != null && f.exists()) {
                        root = jp.getPackageFragmentRoot(f.getParent().getParent());
                    }
                }
            }
            if (root == null || !root.exists())
                continue;
            IJavaElement[] es = root.getChildren();
            for (IJavaElement e : es) {
                if (e instanceof IPackageFragment) {
                    IPackageFragment pf = (IPackageFragment) e;
                    IClassFile[] cs = pf.getClassFiles();
                    for (IClassFile c : cs) {
                        fileSet.add(path, c.getType());
                    }
                }
            }
        }
        build(fileSet, getBatchProject());

    } finally {
        manager.flushZipFiles(this);
    }
}