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

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

Introduction

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

Prototype

String getOption(String optionName, boolean inheritJavaCoreOptions);

Source Link

Document

Helper method for returning one option value only.

Usage

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  ww . j  a  va  2 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 ww  w . j ava 2 s  .co 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.sonarlint.eclipse.jdt.internal.JdtUtilsTest.java

License:Open Source License

@Test
public void shouldConfigureProjectsWithCircularDependencies() throws CoreException, IOException {
    // the bug appeared when at least 3 projects were involved: the first project depends on the second one which has a circular dependency
    // towards the second one
    IPreAnalysisContext context = mock(IPreAnalysisContext.class);
    // mock three projects that depend on each other
    final String project1Name = "project1";
    final String project2Name = "project2";
    final String project3Name = "project3";
    IJavaProject project1 = mock(IJavaProject.class, Mockito.RETURNS_DEEP_STUBS);
    IJavaProject project2 = mock(IJavaProject.class, Mockito.RETURNS_DEEP_STUBS);
    IJavaProject project3 = mock(IJavaProject.class, Mockito.RETURNS_DEEP_STUBS);
    // these are required during the call to configureJavaProject
    when(project1.getOption(JavaCore.COMPILER_SOURCE, true)).thenReturn("1.6");
    when(project1.getOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, true)).thenReturn("1.6");
    when(project1.getProject().getName()).thenReturn(project1Name);
    when(project2.getProject().getName()).thenReturn(project2Name);
    when(project3.getProject().getName()).thenReturn(project3Name);

    // create three classpathEntries, one for each Project
    IClasspathEntry entryProject1 = mock(IClasspathEntry.class, Mockito.RETURNS_DEEP_STUBS);
    IClasspathEntry entryProject2 = mock(IClasspathEntry.class, Mockito.RETURNS_DEEP_STUBS);
    IClasspathEntry entryProject3 = mock(IClasspathEntry.class, Mockito.RETURNS_DEEP_STUBS);
    when(entryProject1.getEntryKind()).thenReturn(IClasspathEntry.CPE_PROJECT);
    when(entryProject1.getPath().segment(0)).thenReturn(project1Name);
    when(entryProject2.getEntryKind()).thenReturn(IClasspathEntry.CPE_PROJECT);
    when(entryProject2.getPath().segment(0)).thenReturn(project2Name);
    when(entryProject3.getEntryKind()).thenReturn(IClasspathEntry.CPE_PROJECT);
    when(entryProject3.getPath().segment(0)).thenReturn(project3Name);
    // project1 depends on project2, which depends on project3, which depends on project2
    IClasspathEntry[] classpath1 = new IClasspathEntry[] { entryProject2 };
    IClasspathEntry[] classpath2 = new IClasspathEntry[] { entryProject3 };
    IClasspathEntry[] classpath3 = new IClasspathEntry[] { entryProject2 };
    when(project1.getResolvedClasspath(true)).thenReturn(classpath1);
    when(project2.getResolvedClasspath(true)).thenReturn(classpath2);
    when(project3.getResolvedClasspath(true)).thenReturn(classpath3);

    // mock the JavaModel
    IJavaModel javaModel = mock(IJavaModel.class);
    when(javaModel.getJavaProject(project1Name)).thenReturn(project1);
    when(javaModel.getJavaProject(project2Name)).thenReturn(project2);
    when(javaModel.getJavaProject(project3Name)).thenReturn(project3);

    when(project1.getJavaModel()).thenReturn(javaModel);
    when(project2.getJavaModel()).thenReturn(javaModel);
    when(project3.getJavaModel()).thenReturn(javaModel);

    // this call should not fail (StackOverFlowError before patch)
    jdtUtils.configureJavaProject(project1, context);

}

From source file:org.talend.designer.maven.tools.creator.CreateMavenCodeProject.java

License:Open Source License

/**
 * Clear compliance settings from project, and set them into Eclipse compliance settings
 * /*from  ww  w. j a v  a  2  s  .  c  om*/
 * @param javaProject
 */
private static void clearProjectIndenpendComplianceSettings(IJavaProject javaProject) {

    Map<String, String> projectComplianceOptions = javaProject.getOptions(false);
    if (projectComplianceOptions == null || projectComplianceOptions.isEmpty()) {
        return;
    }
    String compilerCompliance = javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, false);
    // clear compliance settings from project
    Set<String> keySet = projectComplianceOptions.keySet();
    for (String key : keySet) {
        javaProject.setOption(key, null);
    }

    IEclipsePreferences pluginPreferences = InstanceScope.INSTANCE.getNode(DesignerMavenPlugin.PLUGIN_ID);
    boolean isAlreadySetEclipsePreferences = pluginPreferences.getBoolean(IS_ALREADY_SET_ECLIPSE_COMPLIANCE,
            false);
    // if already setted them, then can't modify them anymore since user can customize them.
    if (!isAlreadySetEclipsePreferences) {
        pluginPreferences.putBoolean(IS_ALREADY_SET_ECLIPSE_COMPLIANCE, true);
        if (compilerCompliance != null) {
            IEclipsePreferences eclipsePreferences = InstanceScope.INSTANCE.getNode(JavaCore.PLUGIN_ID);
            // set compliance settings to Eclipse
            Map<String, String> complianceOptions = new HashMap<String, String>();
            JavaCore.setComplianceOptions(compilerCompliance, complianceOptions);
            if (!complianceOptions.isEmpty()) {
                Set<Entry<String, String>> entrySet = complianceOptions.entrySet();
                for (Entry<String, String> entry : entrySet) {
                    eclipsePreferences.put(entry.getKey(), entry.getValue());
                }
            }
            try {
                // save changes
                eclipsePreferences.flush();
                pluginPreferences.flush();
            } catch (BackingStoreException e) {
                ExceptionHandler.process(e);
            }
        }
    }
}

From source file:repast.simphony.systemdynamics.handlers.MDLImportWizardPage.java

License:Open Source License

private String[] getSourceComplianceLevels(IJavaProject javaProject) {

    if (javaProject != null) {
        return new String[] { javaProject.getOption(JavaCore.COMPILER_SOURCE, true),
                javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true) };
    }/* w  w w. j av a2s . c  om*/

    return new String[] { JavaCore.getOption(JavaCore.COMPILER_SOURCE),
            JavaCore.getOption(JavaCore.COMPILER_COMPLIANCE) };
}