Example usage for org.eclipse.jdt.internal.core JavaModel getJavaProject

List of usage examples for org.eclipse.jdt.internal.core JavaModel getJavaProject

Introduction

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

Prototype

public IJavaProject getJavaProject(IResource resource) 

Source Link

Document

Returns the active Java project associated with the specified resource, or null if no Java project yet exists for the resource.

Usage

From source file:org.eclipse.che.jdt.rest.SearchService.java

License:Open Source License

@POST
@Consumes(APPLICATION_JSON)//from w w  w . ja  v  a 2  s .c  o m
@Produces(APPLICATION_JSON)
@Path("find/usages")
public FindUsagesResponse findUsages(FindUsagesRequest request) throws SearchException {
    JavaModel javaModel = JavaModelManager.getJavaModelManager().getJavaModel();
    IJavaProject javaProject = javaModel.getJavaProject(request.getProjectPath());
    return manager.findUsage(javaProject, request.getFQN(), request.getOffset());
}

From source file:org.eclipse.che.plugin.java.server.rest.SearchJsonRpcService.java

License:Open Source License

private FindUsagesResponse findUsages(FindUsagesRequest request) {
    try {/*from w  w  w.j  ava 2 s . c  o m*/
        JavaModel javaModel = JavaModelManager.getJavaModelManager().getJavaModel();
        IJavaProject javaProject = javaModel.getJavaProject(request.getProjectPath());
        return manager.findUsage(javaProject, request.getFQN(), request.getOffset());
    } catch (SearchException e) {
        throw new JsonRpcException(-27000, e.getMessage());
    }
}

From source file:org.eclipse.che.plugin.java.testing.ProjectClasspathProvider.java

License:Open Source License

/**
 * Builds classpath for the java project.
 *
 * @param javaProject java project/* w  w w  .  j  ava  2s.c o  m*/
 * @return set of resources which are included to the classpath
 */
public Set<String> getProjectClassPath(IJavaProject javaProject) {
    try {
        IClasspathEntry[] resolvedClasspath = javaProject.getResolvedClasspath(false);
        Set<String> result = new HashSet<>();
        for (IClasspathEntry classpathEntry : resolvedClasspath) {
            switch (classpathEntry.getEntryKind()) {
            case IClasspathEntry.CPE_LIBRARY:
                IPath path = classpathEntry.getPath();
                result.add(path.toOSString());
                break;

            case IClasspathEntry.CPE_SOURCE:
                IPath outputLocation = classpathEntry.getOutputLocation();
                if (outputLocation != null) {
                    result.add(workspacePath + outputLocation.toOSString());
                }
                break;

            case IClasspathEntry.CPE_PROJECT:
                IPath projectPath = classpathEntry.getPath();
                JavaModel javaModel = JavaModelManager.getJavaModelManager().getJavaModel();
                IJavaProject project = javaModel.getJavaProject(projectPath.toOSString());
                result.addAll(getProjectClassPath(project));
                break;
            }
        }
        return result;
    } catch (JavaModelException e) {
        LOG.debug(e.getMessage(), e);
    }

    return Collections.emptySet();
}

From source file:org.eclipse.che.plugin.java.testing.ProjectClasspathProviderTest.java

License:Open Source License

@Test
public void classpathProviderShouldProvideClasspathPathsWithAnotherProject() throws Exception {
    JavaModel model = mock(JavaModel.class);
    Field javaModel = JavaModelManager.class.getDeclaredField("javaModel");
    javaModel.setAccessible(true);//from  w  w w.  ja v  a2s  .com
    javaModel.set(JavaModelManager.getJavaModelManager(), model);
    IClasspathEntry entry = mockClasspathEntry(IClasspathEntry.CPE_SOURCE, "/anotherProject/src",
            "/anotherProject/target/classes");

    IJavaProject anotherProject = mock(IJavaProject.class);
    when(anotherProject.getResolvedClasspath(false)).thenReturn(new IClasspathEntry[] { entry });
    when(model.getJavaProject("/anotherProject")).thenReturn(anotherProject);

    IClasspathEntry classpathEntry = mockClasspathEntry(IClasspathEntry.CPE_SOURCE, "",
            "/testProject/target/classes");
    IClasspathEntry jarClasspathEntry = mockClasspathEntry(IClasspathEntry.CPE_LIBRARY,
            "/absolute/path/to/jar.file", null);
    IClasspathEntry projectEntry = mockClasspathEntry(IClasspathEntry.CPE_PROJECT, "/anotherProject", null);

    IClasspathEntry[] entries = new IClasspathEntry[] { classpathEntry, jarClasspathEntry, projectEntry };
    when(javaProject.getResolvedClasspath(false)).thenReturn(entries);

    Set<String> classPath = classpathProvider.getProjectClassPath(javaProject);

    assertThat(classPath).isNotNull().isNotEmpty().contains(PROJECTS_PATH + "/testProject/target/classes",
            "/absolute/path/to/jar.file", PROJECTS_PATH + "/anotherProject/target/classes");
}

From source file:org.eclipse.imp.wizards.IMPWizard.java

License:Open Source License

public static String getProjectSourceLocation(IProject project) {
    try {// w  ww .  ja  v a  2 s  .  c  om
        if (project == null)
            return null;
        JavaModel jm = JavaModelManager.getJavaModelManager().getJavaModel();
        IJavaProject jp = jm.getJavaProject(project);
        if (jp == null)
            return null;
        else {
            IPackageFragmentRoot[] roots = jp.getPackageFragmentRoots();
            for (int i = 0; i < roots.length; i++) {
                if (roots[i].getCorrespondingResource() instanceof IFolder) {
                    IPath lcnPath = roots[i].getPath();
                    lcnPath = lcnPath.removeFirstSegments(1);
                    String lcn = lcnPath.toString();
                    if (lcn.startsWith("/"))
                        lcn = lcn.substring(1);
                    if (!lcn.endsWith("/"))
                        lcn = lcn + "/";
                    return lcn;
                }
            }
        }
    } catch (JavaModelException e) {

    }
    return null;
}

From source file:org.eclipse.imp.wizards.WizardUtilities.java

License:Open Source License

/**
 * Returns the string representation of the workbench relative
 * path to the source location of the given project.
 * //from  w  w  w  .  j ava 2s  .c o m
 * Note:  This method effectively duplicates one defined in IMPWizard.
 * That one is intended for use by wizards, where it is commonly needed.
 * This one is intended for users that are not wizards.
 * 
 * @param project   An IProject that also presumably represents
 *                an IJavaProject
 * @return         The string representation of the path, relative
 *                to the workbench, to the project source location
 */
public static String getProjectSourceLocation(IProject project) {
    try {
        if (project == null)
            return null;
        JavaModel jm = JavaModelManager.getJavaModelManager().getJavaModel();
        IJavaProject jp = jm.getJavaProject(project);
        if (jp == null)
            return null;
        else {
            IPackageFragmentRoot[] roots = jp.getPackageFragmentRoots();
            for (int i = 0; i < roots.length; i++) {
                if (roots[i].getCorrespondingResource() instanceof IFolder) {
                    IPath lcnPath = roots[i].getPath();
                    lcnPath = lcnPath.removeFirstSegments(1);
                    String lcn = lcnPath.toString();
                    if (lcn.startsWith("/"))
                        lcn = lcn.substring(1);
                    if (!lcn.endsWith("/"))
                        lcn = lcn + "/";
                    return lcn;
                }
            }
        }
    } catch (JavaModelException e) {

    }
    return null;
}

From source file:org.eclipse.jdt.internal.core.search.indexing.IndexManager.java

License:Open Source License

/**
 * Trigger addition of the entire content of a project
 * Note: the actual operation is performed in background
 *///from w  w w  . j  a  v  a  2 s . com
public void indexAll(IProject project) {
    if (JavaCore.getPlugin() == null)
        return;

    // Also request indexing of binaries on the classpath
    // determine the new children
    try {
        JavaModel model = JavaModelManager.getJavaModelManager().getJavaModel();
        JavaProject javaProject = (JavaProject) model.getJavaProject(project);
        // only consider immediate libraries - each project will do the same
        // NOTE: force to resolve CP variables before calling indexer - 19303, so that initializers
        // will be run in the current thread.
        IClasspathEntry[] entries = javaProject.getResolvedClasspath();
        for (int i = 0; i < entries.length; i++) {
            IClasspathEntry entry = entries[i];
            if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY)
                indexLibrary(entry.getPath(), project);
        }
    } catch (JavaModelException e) { // cannot retrieve classpath info
    }

    // check if the same request is not already in the queue
    IndexRequest request = new IndexAllProject(project, this);
    if (!isJobWaiting(request))
        request(request);
}