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

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

Introduction

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

Prototype

IPath getOutputLocation() throws JavaModelException;

Source Link

Document

Returns the default output location for this project as a workspace- relative absolute path.

Usage

From source file:org.sonar.ide.eclipse.jdt.internal.JavaProjectConfiguratorTest.java

License:Open Source License

@Test
public void shouldConfigureJavaSourceAndTarget() throws JavaModelException, IOException {
    IJavaProject project = mock(IJavaProject.class);
    Properties sonarProperties = new Properties();

    when(project.getOption(JavaCore.COMPILER_SOURCE, true)).thenReturn("1.6");
    when(project.getOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, true)).thenReturn("1.6");
    when(project.getResolvedClasspath(true)).thenReturn(new IClasspathEntry[] {});
    when(project.getOutputLocation()).thenReturn(new Path(temp.newFolder("output").getAbsolutePath()));

    configurator.configureJavaProject(project, sonarProperties, true);

    assertTrue(sonarProperties.containsKey("sonar.java.source"));
    assertThat(sonarProperties.getProperty("sonar.java.source"), is("1.6"));
    assertTrue(sonarProperties.containsKey("sonar.java.target"));
    assertThat(sonarProperties.getProperty("sonar.java.target"), is("1.6"));
}

From source file:org.sonar.ide.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();//from   www  .j a  v a2 s.co 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);
    Properties sonarProperties = new Properties();

    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, true);

    // 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.JavaProjectConfigurator.java

License:Open Source License

/**
 * Adds the classpath of an eclipse project to the sonarProject recursively, i.e
 * it iterates all dependent projects. Libraries and output folders of dependent projects
 * are added, but no source folders.//w ww .  j av  a  2 s.  c  o  m
 * @param javaProject the eclipse project to get the classpath from
 * @param sonarProjectProperties the sonar project properties to add the classpath to
 * @param context
 * @param topProject indicate we are working on the project to be analysed and not on a dependent project
 * @throws JavaModelException see {@link IJavaProject#getResolvedClasspath(boolean)}
 */
private void addClassPathToSonarProject(IJavaProject javaProject, JavaProjectConfiguration context,
        boolean topProject) throws JavaModelException {
    IClasspathEntry[] classPath = javaProject.getResolvedClasspath(true);
    for (IClasspathEntry entry : classPath) {
        switch (entry.getEntryKind()) {
        case IClasspathEntry.CPE_SOURCE:
            if (!isSourceExcluded(entry)) {
                processSourceEntry(entry, javaProject, context, topProject);
            }
            break;
        case IClasspathEntry.CPE_LIBRARY:
            if (topProject || entry.isExported()) {
                final String libPath = resolveLibrary(javaProject, entry);
                if (libPath != null) {
                    context.libraries().add(libPath);
                }
            }
            break;
        case IClasspathEntry.CPE_PROJECT:
            IJavaModel javaModel = javaProject.getJavaModel();
            IJavaProject referredProject = javaModel.getJavaProject(entry.getPath().segment(0));
            if (!context.dependentProjects().contains(referredProject)) {
                context.dependentProjects().add(referredProject);
                addClassPathToSonarProject(referredProject, context, false);
            }
            break;
        default:
            SonarLintCorePlugin.getDefault().info("Unhandled ClassPathEntry : " + entry);
            break;
        }
    }

    processOutputDir(javaProject.getOutputLocation(), context, topProject);
}

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

License:Open Source License

@Test
public void shouldConfigureJavaSourceAndTarget() throws JavaModelException, IOException {
    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.getResolvedClasspath(true)).thenReturn(new IClasspathEntry[] {});
    when(project.getOutputLocation()).thenReturn(new Path(temp.newFolder("output").getAbsolutePath()));

    configurator.configureJavaProject(project, sonarProperties);

    assertThat(sonarProperties).containsKey("sonar.java.source");
    assertThat(sonarProperties.get("sonar.java.source")).isEqualTo("1.6");
    assertThat(sonarProperties).containsKey("sonar.java.target");
    assertThat(sonarProperties.get("sonar.java.target")).isEqualTo("1.6");
}

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();//w  w w  . j av a2 s  . c om
    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.JdtUtils.java

License:Open Source License

/**
 * Adds the classpath of an eclipse project to the sonarProject recursively, i.e
 * it iterates all dependent projects. Libraries and output folders of dependent projects
 * are added, but no source folders./*from   w ww.  j  av a2s.c  o m*/
 * @param javaProject the eclipse project to get the classpath from
 * @param sonarProjectProperties the sonar project properties to add the classpath to
 * @param context
 * @param topProject indicate we are working on the project to be analyzed and not on a dependent project
 * @throws JavaModelException see {@link IJavaProject#getResolvedClasspath(boolean)}
 */
private static void addClassPathToSonarProject(IJavaProject javaProject, JavaProjectConfiguration context,
        boolean topProject) throws JavaModelException {
    IClasspathEntry[] classPath = javaProject.getResolvedClasspath(true);
    for (IClasspathEntry entry : classPath) {
        switch (entry.getEntryKind()) {
        case IClasspathEntry.CPE_SOURCE:
            processSourceEntry(entry, context, topProject);
            break;
        case IClasspathEntry.CPE_LIBRARY:
            processLibraryEntry(entry, javaProject, context, topProject);
            break;
        case IClasspathEntry.CPE_PROJECT:
            processProjectEntry(entry, javaProject, context);
            break;
        default:
            SonarLintLogger.get().info("Unhandled ClassPathEntry : " + entry);
            break;
        }
    }

    processOutputDir(javaProject.getOutputLocation(), context, topProject);
}

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

License:Open Source License

@Test
public void shouldConfigureJavaSourceAndTarget() throws JavaModelException, IOException {
    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.getResolvedClasspath(true)).thenReturn(new IClasspathEntry[] {});
    when(project.getOutputLocation()).thenReturn(new Path(temp.newFolder("output").getAbsolutePath()));

    jdtUtils.configureJavaProject(project, context);

    verify(context).setAnalysisProperty("sonar.java.source", "1.6");
    verify(context).setAnalysisProperty("sonar.java.target", "1.6");
}

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();/*from w w w .  j  a  v  a2 s. 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);
    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();/*from   www  .  ja  v 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.springframework.ide.eclipse.beans.core.model.locate.ProjectScanningBeansConfigLocator.java

License:Open Source License

/**
 * Filters out every {@link IFile} which is has unknown root elements in its
 * XML content.//  w  w w.  j av  a  2  s  .co m
 */
@Override
protected Set<IFile> filterMatchingFiles(Set<IFile> files) {
    // if project is a java project remove bin dirs from the list
    Set<String> outputDirectories = new HashSet<String>();
    IJavaProject javaProject = JdtUtils.getJavaProject(project);
    if (javaProject != null) {
        try {
            // add default output directory
            outputDirectories.add(javaProject.getOutputLocation().toString());

            // add source folder specific output directories
            for (IClasspathEntry entry : javaProject.getRawClasspath()) {
                if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE && entry.getOutputLocation() != null) {
                    outputDirectories.add(entry.getOutputLocation().toString());
                }
            }
        } catch (JavaModelException e) {
            BeansCorePlugin.log(e);
        }
    }

    Set<IFile> detectedFiles = new LinkedHashSet<IFile>();
    for (IFile file : files) {
        boolean skip = false;
        // first check if the file sits in an output directory
        String path = file.getFullPath().toString();
        for (String outputDirectory : outputDirectories) {
            if (path.startsWith(outputDirectory)) {
                skip = true;
            }
        }
        if (skip) {
            continue;
        }

        // check if the file is known Spring xml file
        IStructuredModel model = null;
        try {
            try {
                model = StructuredModelManager.getModelManager().getExistingModelForRead(file);
            } catch (RuntimeException e) {
                // sometimes WTP throws a NPE in concurrency situations
            }
            if (model == null) {
                model = StructuredModelManager.getModelManager().getModelForRead(file);
            }
            if (model != null) {
                IDOMDocument document = ((DOMModelImpl) model).getDocument();
                if (document != null && document.getDocumentElement() != null) {
                    String namespaceUri = document.getDocumentElement().getNamespaceURI();
                    if (applyNamespaceFilter(file, namespaceUri)) {
                        detectedFiles.add(file);
                    }
                }
            }
        } catch (IOException e) {
            BeansCorePlugin.log(e);
        } catch (CoreException e) {
            BeansCorePlugin.log(e);
        } finally {
            if (model != null) {
                model.releaseFromRead();
            }
        }
    }
    return detectedFiles;
}