Example usage for org.eclipse.jdt.core IJavaModel refreshExternalArchives

List of usage examples for org.eclipse.jdt.core IJavaModel refreshExternalArchives

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaModel refreshExternalArchives.

Prototype

void refreshExternalArchives(IJavaElement[] elementsScope, IProgressMonitor monitor) throws JavaModelException;

Source Link

Document

Triggers an update of the JavaModel with respect to the referenced external archives.

Usage

From source file:it.unibz.instasearch.indexing.WorkspaceIndexerJDT.java

License:Open Source License

/**
 * @param doc//from  www .ja v  a 2 s . com
 * @return
 * @throws JavaModelException 
 */
private IStorage getNonJavaResource(SearchResultDoc doc) throws JavaModelException {

    IWorkspaceRoot workspaceRoot = InstaSearchPlugin.getWorkspaceRoot();
    IJavaModel javaModel = JavaCore.create(workspaceRoot);

    String javaProjectName = doc.getProject().segment(0);
    IJavaProject javaProj = javaModel.getJavaProject(javaProjectName);

    if (!javaProj.isOpen())
        javaProj.open(new NullProgressMonitor());

    javaModel.refreshExternalArchives(new IJavaElement[] { javaProj }, new NullProgressMonitor());

    String jarName = doc.getJarName();

    IPackageFragmentRoot[] roots = javaProj.getPackageFragmentRoots();
    IPackageFragmentRoot jar = null;

    for (IPackageFragmentRoot root : roots) {
        if (root.isArchive() && root.getSourceAttachmentPath() != null) {

            String name = root.getElementName();
            if (name.equals(jarName)) {
                jar = root;
                break;
            }
        }
    }

    if (jar == null)
        return null;

    String filePath = doc.getFilePath();
    IPath path = new Path(filePath);

    IJarEntryResource res = null;
    for (String segment : path.segments()) {
        if (res == null)
            res = findJarEntry(jar, segment);
        else
            res = findJarEntry(res.getChildren(), segment);
    }

    return res;
}

From source file:it.unibz.instasearch.indexing.WorkspaceIndexerJDT.java

License:Open Source License

/**
 * @param doc//from w ww.  j a  v a 2  s  .  c om
 * @return
 * @throws JavaModelException 
 */
private IClassFile getClassFile(SearchResultDoc doc) throws Exception {

    IWorkspaceRoot workspaceRoot = InstaSearchPlugin.getWorkspaceRoot();
    IJavaModel javaModel = JavaCore.create(workspaceRoot);

    String javaProjectName = doc.getProject().segment(0);
    IJavaProject proj = javaModel.getJavaProject(javaProjectName);

    if (proj == null)
        throw new Exception("Project " + javaProjectName + " not found");

    if (!proj.isOpen())
        proj.open(new NullProgressMonitor());

    javaModel.refreshExternalArchives(new IJavaElement[] { proj }, new NullProgressMonitor());

    IPath filePath = new Path(doc.getFilePath());
    String fileName = filePath.lastSegment();

    IPath jarPath = filePath.removeLastSegments(2); // remove pkg and filename
    IPackageFragmentRoot jar = null;
    IResource jarFile = workspaceRoot.findMember(jarPath);

    if (jarFile != null)
        jar = proj.getPackageFragmentRoot(jarFile);
    else
        jar = proj.getPackageFragmentRoot(jarPath.toString()); // external archive

    if (jar == null)
        throw new Exception("Jar " + jarPath + " not found in project " + doc.getProjectName());

    IPath pkgPath = filePath.removeLastSegments(1); // remove filename
    String pkgName = pkgPath.lastSegment();

    IPackageFragment pkg = jar.getPackageFragment(pkgName);

    if (pkg == null)
        throw new Exception("Package " + pkgName + " not found  in " + doc.getProjectName());

    IClassFile classFile = pkg.getClassFile(fileName);

    return classFile;
}