Example usage for org.eclipse.jdt.core IJavaProject getPath

List of usage examples for org.eclipse.jdt.core IJavaProject getPath

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaProject getPath.

Prototype

IPath getPath();

Source Link

Document

Returns the path to the innermost resource enclosing this element.

Usage

From source file:org.sonarlint.eclipse.jdt.internal.JavaProjectConfiguratorTest.java

License:Open Source License

@Test
public void shouldConfigureSimpleProject() throws JavaModelException, IOException {
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    File workspaceRoot = root.getLocation().toFile();
    File projectRoot = new File(workspaceRoot, "myProject");
    projectRoot.mkdir();/*  www.  j  av  a2s . com*/
    File sourceFolder = new File(projectRoot, "src");
    sourceFolder.mkdir();
    File testFolder = new File(projectRoot, "test");
    testFolder.mkdir();
    File outputFolder = new File(projectRoot, "bin");
    outputFolder.mkdir();

    IJavaProject project = mock(IJavaProject.class);
    Map<String, String> sonarProperties = new HashMap<>();

    when(project.getOption(JavaCore.COMPILER_SOURCE, true)).thenReturn("1.6");
    when(project.getOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, true)).thenReturn("1.6");
    when(project.getPath()).thenReturn(new Path(projectRoot.getAbsolutePath()));

    IClasspathEntry[] cpes = new IClasspathEntry[] { createCPE(IClasspathEntry.CPE_SOURCE, sourceFolder),
            createCPE(IClasspathEntry.CPE_SOURCE, testFolder) };

    when(project.getResolvedClasspath(true)).thenReturn(cpes);
    when(project.getOutputLocation()).thenReturn(new Path(outputFolder.getAbsolutePath()));

    configurator.configureJavaProject(project, sonarProperties);

    // TODO Find a way to mock a project inside Eclipse

    // assertTrue(sonarProperties.containsKey("sonar.sources"));
    // assertThat(sonarProperties.getProperty("sonar.sources"), is(sourceFolder.getPath()));
    // assertTrue(sonarProperties.containsKey("sonar.tests"));
    // assertThat(sonarProperties.getProperty("sonar.tests"), is(testFolder.getPath()));
    // assertTrue(sonarProperties.containsKey("sonar.binaries"));
    // assertThat(sonarProperties.getProperty("sonar.binaries"), is(outputFolder.getPath()));
}

From source file:org.sonarlint.eclipse.jdt.internal.JdtUtilsTest.java

License:Open Source License

@Test
public void shouldConfigureSimpleProject() throws JavaModelException, IOException {
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    File workspaceRoot = root.getLocation().toFile();
    File projectRoot = new File(workspaceRoot, "myProject");
    projectRoot.mkdir();// w  ww  . j  av  a2s. c  o  m
    File sourceFolder = new File(projectRoot, "src");
    sourceFolder.mkdir();
    File testFolder = new File(projectRoot, "test");
    testFolder.mkdir();
    File outputFolder = new File(projectRoot, "bin");
    outputFolder.mkdir();

    IJavaProject project = mock(IJavaProject.class);
    IPreAnalysisContext context = mock(IPreAnalysisContext.class);

    when(project.getOption(JavaCore.COMPILER_SOURCE, true)).thenReturn("1.6");
    when(project.getOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, true)).thenReturn("1.6");
    when(project.getPath()).thenReturn(new Path(projectRoot.getAbsolutePath()));

    IClasspathEntry[] cpes = new IClasspathEntry[] {
            createCPE(IClasspathEntry.CPE_SOURCE, sourceFolder, outputFolder),
            createCPE(IClasspathEntry.CPE_SOURCE, testFolder, outputFolder) };

    when(project.getResolvedClasspath(true)).thenReturn(cpes);
    when(project.getOutputLocation()).thenReturn(new Path(outputFolder.getAbsolutePath()));

    jdtUtils.configureJavaProject(project, context);

    ArgumentCaptor<Collection<String>> captor = ArgumentCaptor.forClass(Collection.class);
    verify(context).setAnalysisProperty(ArgumentMatchers.eq("sonar.java.binaries"), captor.capture());

    assertThat(captor.getValue())
            .containsExactlyInAnyOrder(outputFolder.getAbsolutePath().replaceAll(Pattern.quote("\\"), "/"));
}

From source file:org.sonarlint.eclipse.jdt.internal.JdtUtilsTest.java

License:Open Source License

@Test
public void doNotAddNonExistingPaths() throws JavaModelException, IOException {
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    File workspaceRoot = root.getLocation().toFile();
    File projectRoot = new File(workspaceRoot, "myProjectMissingOut");
    projectRoot.mkdir();/*  w  w w.  j  av a 2  s.  c o  m*/
    File sourceFolder = new File(projectRoot, "src");
    sourceFolder.mkdir();
    File outputFolder = new File(projectRoot, "bin");

    IJavaProject project = mock(IJavaProject.class);
    IPreAnalysisContext context = mock(IPreAnalysisContext.class);

    when(project.getOption(JavaCore.COMPILER_SOURCE, true)).thenReturn("1.6");
    when(project.getOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, true)).thenReturn("1.6");
    when(project.getPath()).thenReturn(new Path(projectRoot.getAbsolutePath()));

    IClasspathEntry[] cpes = new IClasspathEntry[] {
            createCPE(IClasspathEntry.CPE_SOURCE, sourceFolder, outputFolder) };

    when(project.getResolvedClasspath(true)).thenReturn(cpes);
    when(project.getOutputLocation()).thenReturn(new Path(outputFolder.getAbsolutePath()));

    jdtUtils.configureJavaProject(project, context);

    ArgumentCaptor<Collection<String>> captor = ArgumentCaptor.forClass(Collection.class);
    verify(context).setAnalysisProperty(ArgumentMatchers.eq("sonar.java.binaries"), captor.capture());

    assertThat(captor.getValue()).isEmpty();
}

From source file:org.sonatype.m2e.webby.internal.launch.WebbySourcePathProvider.java

License:Open Source License

public IRuntimeClasspathEntry[] resolveClasspath(IRuntimeClasspathEntry[] entries,
        ILaunchConfiguration configuration) throws CoreException {
    IProgressMonitor monitor = new NullProgressMonitor();
    int scope = IClasspathManager.CLASSPATH_RUNTIME;
    Set<IRuntimeClasspathEntry> all = new LinkedHashSet<IRuntimeClasspathEntry>(entries.length);
    for (IRuntimeClasspathEntry entry : entries) {
        if (entry.getType() == IRuntimeClasspathEntry.CONTAINER
                && MavenClasspathHelpers.isMaven2ClasspathContainer(entry.getPath())) {
            addMavenClasspathEntries(all, entry, configuration, scope, monitor);
        } else if (entry.getType() == IRuntimeClasspathEntry.PROJECT) {
            IJavaProject javaProject = JavaRuntime.getJavaProject(configuration);
            if (javaProject.getPath().equals(entry.getPath())) {
                addProjectEntries(all, entry.getPath());
            } else {
                addStandardClasspathEntries(all, entry, configuration);
            }//  ww w .  ja v a  2 s.  c  o m
        } else {
            addStandardClasspathEntries(all, entry, configuration);
        }
    }
    return all.toArray(new IRuntimeClasspathEntry[all.size()]);
}

From source file:org.switchyard.tools.ui.JavaUtil.java

License:Open Source License

/**
 * Returns the first source folder in the project. If the project is a maven
 * project, the first source folder configured will be used.
 * // w  w w  .ja v  a 2 s.c  o  m
 * @param project the Java project
 * 
 * @return the source root; may be null.
 */
public static IPackageFragmentRoot getFirstJavaSourceRoot(IJavaProject project) {
    if (project == null) {
        return null;
    }
    try {
        IPackageFragmentRoot sourceRoot = null;
        IMavenProjectFacade facade = MavenPlugin.getMavenProjectRegistry().getProject(project.getProject());
        if (facade == null) {
            for (IPackageFragmentRoot frag : project.getPackageFragmentRoots()) {
                if (frag.getKind() == IPackageFragmentRoot.K_SOURCE) {
                    sourceRoot = frag;
                    break;
                }
            }
        } else {
            IPath projectPath = project.getPath();
            for (IPath sourcePath : facade.getCompileSourceLocations()) {
                IPackageFragmentRoot frag = project.findPackageFragmentRoot(projectPath.append(sourcePath));
                if (frag != null) {
                    sourceRoot = frag;
                    break;
                }
            }
        }
        return sourceRoot;
    } catch (JavaModelException e) {
        return null;
    }
}

From source file:org.whole.lang.visitors.WorkspaceArtifactsGeneratorVisitor.java

License:Open Source License

public void visitFolderArtifact(FolderArtifact entity) {
    env().wEnterScope();/*w w w. j ava  2  s  .  c  o m*/
    if (!Matcher.matchImplAndBind(ArtifactsEntityDescriptorEnum.Name, entity.getName(), env(), "folderName"))
        throw new VisitException("No Folder name");

    entity.getMetadata().accept(this);

    try {
        IPath path;
        if (env().wIsSet("folder")) {
            IFolder parentFolder = (IFolder) env().wGetValue("folder");
            path = parentFolder.getFullPath();
        } else {
            IProject project = (IProject) env().wGetValue("project");
            path = project.getFullPath();
        }

        IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
        IFolder folder = null;
        String[] folderPath = env().wStringValue("folderName").split("/");
        for (int i = 0; i < folderPath.length; i++) {
            path = path.append(folderPath[i]);

            folder = workspaceRoot.getFolder(path);

            if (!folder.exists())
                folder.create(true, true, progressMonitor());
        }

        env().wDefValue("folder", folder);

        if (env().wIsSet("derived"))
            folder.setDerived(true, progressMonitor());
        if (env().wIsSet("readonly")) {
            ResourceAttributes attributes = folder.getResourceAttributes();
            if (attributes != null) {
                attributes.setReadOnly(true);
                folder.setResourceAttributes(attributes);
            }
        }

        if (env().wLocalNames().contains("source") && env().wIsSet("javaProject")) {
            List<IClasspathEntry> classpathEntries = new ArrayList<IClasspathEntry>();

            IJavaProject javaProject = (IJavaProject) env().wGetValue("javaProject");
            classpathEntries.addAll(Arrays.asList(javaProject.getRawClasspath()));

            IClasspathEntry sourceEntry = JavaCore.newSourceEntry(folder.getFullPath());
            if (classpathEntries.size() == 1
                    && ((IClasspathEntry) classpathEntries.get(0)).getPath().equals(javaProject.getPath()))
                classpathEntries.remove(0);
            else
                for (Iterator<IClasspathEntry> i = classpathEntries.iterator(); i.hasNext();) {
                    IClasspathEntry entry = i.next();
                    if (entry.getPath().equals(sourceEntry.getPath()))
                        i.remove();
                }
            classpathEntries.add(0, sourceEntry);

            javaProject.setRawClasspath(
                    (IClasspathEntry[]) classpathEntries.toArray(new IClasspathEntry[classpathEntries.size()]),
                    progressMonitor());
        }
    } catch (CoreException e) {
        throw new VisitException(e);
        //      } catch (NullPointerException e) {
        //         throw new VisitException("No Project");
    }

    entity.getArtifacts().accept(this);
    env().wExitScope();
}

From source file:org.yakindu.sct.generator.builder.efs.ClasspathChanger.java

License:Open Source License

public void addFolderToClassPath(IProject project, String folder) {

    if (!isPartOfClassPath(project, folder)) {
        IJavaProject javaProject = JavaCore.create(project);
        IClasspathEntry[] entries = null;

        try {//from ww  w.  j a v  a  2s.c o m
            entries = javaProject.getRawClasspath();
        } catch (JavaModelException e) {
            e.printStackTrace();
        }

        IClasspathEntry[] newEntries = new IClasspathEntry[entries.length + 1];
        System.arraycopy(entries, 0, newEntries, 0, entries.length);

        IPath srcPath = javaProject.getPath().append(folder);
        IClasspathEntry srcEntry = JavaCore.newSourceEntry(srcPath, null);

        newEntries[entries.length] = JavaCore.newSourceEntry(srcEntry.getPath());

        try {
            javaProject.setRawClasspath(newEntries, null);
        } catch (JavaModelException e) {
            e.printStackTrace();
        }
    }
}

From source file:repast.simphony.agents.designer.core.AgentBuilderPlugin.java

License:Open Source License

/**
 * TODO/*  w w w . j a  va  2s . c  o m*/
 * 
 * @param javaProject
 * @return
 */
public static IProject getProject(IJavaProject javaProject) {
    return getWorkspaceProject(javaProject.getPath());
}

From source file:repast.simphony.relogo.ide.wizards.NetlogoImportWizard.java

License:LGPL

private IPath checkAndGetSourcePath(String srcPathName, IProject project, IJavaProject javaProject,
        IProgressMonitor monitor) throws CoreException {
    // Create src-gen directory
    IPath srcPath = javaProject.getPath().append(srcPathName + "/");
    // project relative
    IFolder srcGenfolder = project.getFolder(srcPathName);

    if (!srcGenfolder.exists()) {
        // creates within the project
        srcGenfolder.create(true, true, monitor);
        IClasspathEntry[] entries = javaProject.getRawClasspath();
        boolean found = false;
        for (IClasspathEntry entry : entries) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE && entry.getPath().equals(srcPath)) {
                found = true;/*  ww w .ja  v  a 2 s .c  om*/
                break;
            }
        }

        if (!found) {
            IClasspathEntry[] newEntries = new IClasspathEntry[entries.length + 1];
            System.arraycopy(entries, 0, newEntries, 0, entries.length);
            IClasspathEntry srcEntry = JavaCore.newSourceEntry(srcPath, null);
            newEntries[entries.length] = srcEntry;
            javaProject.setRawClasspath(newEntries, null);
        }

    }

    return srcPath;
}