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

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

Introduction

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

Prototype

void setOptions(Map<String, String> newOptions);

Source Link

Document

Sets the project custom options.

Usage

From source file:edu.illinois.keshmesh.detector.tests.TestSetupHelper.java

License:Open Source License

/**
 * Creates a new project in Eclipse and sets up its dependencies on JRE.
 * /*from   w  w  w .j  a  v  a2  s. c o m*/
 * @param projectName
 * @param baseProjectName
 * @return
 * @throws CoreException
 */
@SuppressWarnings("rawtypes")
static IJavaProject createAndInitializeProject(String projectName, String baseProjectName)
        throws CoreException {
    IJavaProject project;
    project = JavaProjectHelper.createJavaProject(projectName, "bin");
    addJREContainer(project);
    // set compiler options on projectOriginal
    Map options = project.getOptions(false);
    JavaProjectHelper.set15CompilerOptions(options);
    project.setOptions(options);
    JavaProjectHelper.addLibrary(project, new Path(Activator.getDefault()
            .getFileInPlugin(new Path(join("lib", "annotations.jar"))).getAbsolutePath()));
    return project;
}

From source file:io.sarl.tests.api.WorkbenchTestHelper.java

License:Apache License

/** Make the given project compliant for the given version.
 *
 * @param javaProject the project./* ww  w . j  a  v  a 2 s. co  m*/
 * @param javaVersion the Java version.
 */
public static void makeCompliantFor(IJavaProject javaProject, JavaVersion javaVersion) {
    Map<String, String> options = javaProject.getOptions(false);
    String jreLevel;
    switch (javaVersion) {
    case JAVA8:
        jreLevel = JavaCore.VERSION_1_8;
        break;
    case JAVA6:
        jreLevel = JavaCore.VERSION_1_6;
        break;
    case JAVA5:
        jreLevel = JavaCore.VERSION_1_5;
        break;
    case JAVA7:
    default:
        jreLevel = JavaCore.VERSION_1_7;
    }
    options.put(JavaCore.COMPILER_COMPLIANCE, jreLevel);
    options.put(JavaCore.COMPILER_SOURCE, jreLevel);
    options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, jreLevel);
    options.put(JavaCore.COMPILER_PB_ASSERT_IDENTIFIER, JavaCore.ERROR);
    options.put(JavaCore.COMPILER_PB_ENUM_IDENTIFIER, JavaCore.ERROR);
    options.put(JavaCore.COMPILER_CODEGEN_INLINE_JSR_BYTECODE, JavaCore.ENABLED);
    options.put(JavaCore.COMPILER_LOCAL_VARIABLE_ATTR, JavaCore.GENERATE);
    options.put(JavaCore.COMPILER_LINE_NUMBER_ATTR, JavaCore.GENERATE);
    options.put(JavaCore.COMPILER_SOURCE_FILE_ATTR, JavaCore.GENERATE);
    options.put(JavaCore.COMPILER_CODEGEN_UNUSED_LOCAL, JavaCore.PRESERVE);
    javaProject.setOptions(options);
}

From source file:it.wallgren.android.platform.project.AndroidPlatformProject.java

License:Apache License

private void addJavaNature(IProject project, IProgressMonitor monitor) throws CoreException {
    if (project == null) {
        throw new IllegalStateException("Project must be created before giving it a Java nature");
    }/* www  .  ja  v  a2s  .  c  o  m*/
    final IFolder repoLink = createRepoLink(monitor, project, repoPath);
    IFile classpath = repoLink.getFile("development/ide/eclipse/.classpath");
    IFile classpathDestination = project.getFile(".classpath");
    if (classpathDestination.exists()) {
        classpathDestination.delete(true, monitor);
    }
    classpath.copy(classpathDestination.getFullPath(), true, monitor);
    final IProjectDescription description = project.getDescription();
    final String[] natures = description.getNatureIds();
    final String[] newNatures = Arrays.copyOf(natures, natures.length + 1);
    newNatures[natures.length] = JavaCore.NATURE_ID;
    description.setNatureIds(newNatures);
    project.setDescription(description, null);
    final IJavaProject javaProject = JavaCore.create(project);
    @SuppressWarnings("rawtypes")
    final Map options = javaProject.getOptions(true);
    // Compliance level need to be 1.6
    JavaCore.setComplianceOptions(JavaCore.VERSION_1_6, options);
    javaProject.setOptions(options);

    IClasspathEntry[] classPath = mangleClasspath(javaProject.getRawClasspath(), project, repoLink);
    javaProject.setRawClasspath(classPath, monitor);
    javaProject.setOutputLocation(javaProject.getPath().append("out"), monitor);
}

From source file:it.wallgren.android.platform.project.PackagesProject.java

License:Apache License

private void addJavaNature(IClasspathEntry[] classPath, IProject project, IProgressMonitor monitor)
        throws CoreException {
    if (project == null) {
        throw new IllegalStateException("Project must be created before giving it a Java nature");
    }/*from   www  .j ava 2s . c o m*/

    final IProjectDescription description = project.getDescription();
    final String[] natures = description.getNatureIds();
    final String[] newNatures = Arrays.copyOf(natures, natures.length + 1);
    newNatures[natures.length] = JavaCore.NATURE_ID;
    description.setNatureIds(newNatures);
    project.setDescription(description, null);
    final IJavaProject javaProject = JavaCore.create(project);
    @SuppressWarnings("rawtypes")
    final Map options = javaProject.getOptions(true);
    // Compliance level need to be 1.6
    JavaCore.setComplianceOptions(JavaCore.VERSION_1_6, options);
    javaProject.setOptions(options);
    javaProject.setRawClasspath(classPath, monitor);
    javaProject.setOutputLocation(javaProject.getPath().append("out"), monitor);
}

From source file:net.rim.ejde.internal.ui.wizards.BasicBlackBerryProjectWizardPageTwo.java

License:Open Source License

/**
 * Initialize java compiler./* ww w  . j a va2 s . co m*/
 */
private void initializeJavaCompiler(IJavaProject eclipseProject) {
    final Map<String, String> map = eclipseProject.getOptions(false);

    if (map.size() > 0) {
        map.remove(JavaCore.COMPILER_COMPLIANCE);
        map.remove(JavaCore.COMPILER_SOURCE);
        map.remove(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM);
    }

    map.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_3);
    map.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_4);
    map.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_2);

    eclipseProject.setOptions(map);
}

From source file:net.rim.ejde.internal.util.ImportUtils.java

License:Open Source License

/**
 * Sets a whole bunch of project specific settings.
 * <p>/*from  w  ww .j a  v a2s.  c om*/
 * http://help.eclipse.org/help31/topic/org.eclipse.jdt.doc.isv/reference/
 * api/org/eclipse/jdt/core/JavaCore.html#getDefaultOptions()
 *
 * @param project
 */
@SuppressWarnings("unchecked")
static public void initializeProjectOptions(IJavaProject javaProject) {
    if (null == javaProject)
        throw new IllegalArgumentException();

    final Map map = javaProject.getOptions(false);

    if (map.size() > 0) {
        map.remove(JavaCore.COMPILER_COMPLIANCE);
        map.remove(JavaCore.COMPILER_SOURCE);
        map.remove(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM);
    }

    map.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_3);
    // DPI 221069 --> Bugzilla id=250185
    map.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_4);
    map.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_2);

    javaProject.setOptions(map);
}

From source file:nz.ac.auckland.ptjava.builder.PTJavaFileBuilderNature.java

License:Open Source License

/**
 * Add the nature to the specified project if it does not already have it.
 * /*from w  ww .  ja  v  a 2s  .  c o  m*/
 * @param project the project to be modified
 */
public static void addNature(IProject project) {
    // Cannot modify closed projects.
    if (!project.isOpen())
        return;

    // Get the description.
    IProjectDescription description;
    try {
        description = project.getDescription();
    } catch (CoreException e) {
        PTJavaLog.logError(e);
        return;
    }

    // Determine if the project already has the nature.
    List<String> newIds = new ArrayList<String>();
    newIds.addAll(Arrays.asList(description.getNatureIds()));
    int index = newIds.indexOf(NATURE_ID);
    if (index != -1)
        return;

    // Add the nature
    newIds.add(NATURE_ID);
    description.setNatureIds(newIds.toArray(new String[newIds.size()]));

    try {
        // Save the description.
        project.setDescription(description, null);

        if (project.hasNature(JavaCore.NATURE_ID)) {
            IJavaProject javaProject = JavaCore.create(project);
            // set up compiler to not copy .ptjava files to output
            Map options = javaProject.getOptions(false);
            options.put(JavaCore.CORE_JAVA_BUILD_RESOURCE_COPY_FILTER, "*.ptjava");
            javaProject.setOptions(options);
        } else {
            PTJavaLog.logInfo("The project to add ParaTask nature does not have Java nature!");
        }
    } catch (CoreException e) {
        PTJavaLog.logError(e);
    }
}

From source file:org.cubictest.ui.wizards.NewCubicTestProjectWizard.java

License:Open Source License

private IProject createProject(IProgressMonitor monitor, boolean launchNewTestWizard) {

    try {//from w w  w. j  a va2  s .  c o m
        IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
        IProject project = root.getProject(namePage.getProjectName());

        IProjectDescription description = ResourcesPlugin.getWorkspace()
                .newProjectDescription(project.getName());

        if (!Platform.getLocation().equals(namePage.getLocationPath()))
            description.setLocation(namePage.getLocationPath());

        description.setNatureIds(new String[] { JavaCore.NATURE_ID });
        ICommand buildCommand = description.newCommand();
        buildCommand.setBuilderName(JavaCore.BUILDER_ID);
        description.setBuildSpec(new ICommand[] { buildCommand });
        project.create(description, monitor);
        project.open(monitor);

        IJavaProject javaProject = JavaCore.create(project);
        Map<?, ?> java15options = javaProject.getOptions(true);
        JavaCore.setComplianceOptions("1.5", java15options);
        javaProject.setOptions(java15options);

        IFolder testFolder = project.getFolder(TESTS_FOLDER_NAME);
        testFolder.create(false, true, monitor);

        IFolder suiteFolder = project.getFolder(TEST_SUITES_FOLDER_NAME);
        suiteFolder.create(false, true, monitor);

        project.getFolder("src").create(false, true, monitor);
        project.getFolder("src/main").create(false, true, monitor);
        project.getFolder("src/test").create(false, true, monitor);

        IFolder javaSrcFolder = project.getFolder("src/main/java");
        javaSrcFolder.create(false, true, monitor);

        IFolder javaTestFolder = project.getFolder("src/test/java");
        javaTestFolder.create(false, true, monitor);

        IFolder customTestSuites = project.getFolder("src/test/java/customTestSuites");
        customTestSuites.create(false, true, monitor);

        IFolder binFolder = project.getFolder("bin");
        binFolder.create(false, true, monitor);

        IFolder libFolder = project.getFolder("lib");
        libFolder.create(false, true, monitor);

        WizardUtils.copyPom(project.getLocation().toFile());
        WizardUtils.copySampleCustomTestSuite(customTestSuites.getRawLocation().toFile());
        WizardUtils.copySettings(project.getLocation().toFile());

        //construct build path for the new project
        List<IClasspathEntry> classpathlist = new ArrayList<IClasspathEntry>();
        classpathlist.add(JavaCore.newSourceEntry(javaSrcFolder.getFullPath()));
        classpathlist.add(JavaCore.newSourceEntry(javaTestFolder.getFullPath()));
        classpathlist.add(JavaCore.newContainerEntry(new Path("org.eclipse.jdt.launching.JRE_CONTAINER")));

        javaProject.setOutputLocation(binFolder.getFullPath(), monitor);

        javaProject.setRawClasspath(classpathlist.toArray(new IClasspathEntry[classpathlist.size()]),
                binFolder.getFullPath(), monitor);

        ResourceNavigator navigator = null;
        IViewPart viewPart = workbench.getActiveWorkbenchWindow().getActivePage().getViewReferences()[0]
                .getView(false);

        if (viewPart instanceof ResourceNavigator) {
            navigator = (ResourceNavigator) viewPart;
        }

        if (launchNewTestWizard) {
            launchNewTestWizard(testFolder);
            if (navigator != null && testFolder.members().length > 0) {
                navigator.selectReveal(new StructuredSelection(testFolder.members()[0]));
            }
        }

        handlePluginBuildPathSupporters(javaProject, libFolder, classpathlist);

        project.refreshLocal(IResource.DEPTH_INFINITE, null);

        return project;

    } catch (Exception e) {
        ErrorHandler.logAndShowErrorDialogAndRethrow(e);
        return null;
    }
}

From source file:org.ebayopensource.turmeric.repositorysystem.imp.impl.TurmericProjectConfigurer.java

License:Open Source License

private void mavenizeAndCleanUp(final IProject project, final ProjectMavenizationRequest request,
        final IProgressMonitor monitor)
        throws CoreException, MavenEclipseApiException, IOException, InterruptedException, TemplateException {
    if (SOALogger.DEBUG)
        logger.entering(project, request);
    final IMavenEclipseApi api = MavenCoreUtils.mavenEclipseAPI();
    try {/*from   ww  w .  j  a v a 2s  .  co  m*/
        //         It seems that the pom.xml is not updated in the Eclipse Workspace by the mavenize project operation
        //         Util.refresh( project.getFile( "pom.xml" ) );
        //         Project Mavenization doesn't seem to care that the output directory is set here and set in the pom.xml
        //         it will use the maven default of target-eclipse no matter what.
        IJavaProject javaProject = null;
        try {
            api.mavenizeProject(request, monitor);
            logger.info("Mavenization finished->" + project);
            ProgressUtil.progressOneStep(monitor);
            javaProject = JavaCore.create(project);
            //javaProject.setOutputLocation( project.getFolder( SOAProjectConstants.FOLDER_OUTPUT_DIR).getFullPath(), null );
            FileUtils.deleteDirectory(project.getFolder("target-eclipse").getLocation().toFile());
            //Util.delete( project.getFolder( "target-eclipse" ), null );
            final Map<?, ?> options = javaProject.getOptions(false);
            options.clear();
            javaProject.setOptions(options);
        } catch (NullPointerException e) {
            throw new CoreException(EclipseMessageUtils.createErrorStatus(
                    "NPE occured during projects mavenization.\n\n"
                            + "The possible cause is that the M2Eclipse Maven indexes in your current workspace might be corrupted. "
                            + "Please remove folder {workspace}/.metadata/.plugins/org.maven.ide.eclipse/, and then restart your IDE.",
                    e));
        }
        //         The Mavenization also seemed to take the META-SRC src directory and add an exclusion pattern along with
        //         setting its output location to itself.  Maybe they wanted to do a class library folder?  Either way its
        //         no good for us.  Secondly, we are setting the Maven classpath container to have its entries exported by default.
        //         Finally, we are adding the classpath entry attribute 'org.eclipse.jst.component.dependency' in case the project
        //         is a WTP web project so that WTP will use the maven classpath container when deploying to its runtime servers.
        boolean changed = false;
        final List<IClasspathEntry> newEntries = ListUtil.list();
        final IPath containerPath = new Path(SOAMavenConstants.MAVEN_CLASSPATH_CONTAINER_ID);
        for (final IClasspathEntry cpEntry : JDTUtil.rawClasspath(javaProject, true)) {
            if (cpEntry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                if (cpEntry.getPath().equals(containerPath)) {
                    newEntries.add(JavaCore.newContainerEntry(containerPath, true));
                    changed |= true;
                }
            } else if (cpEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                if (!ArrayUtils.isEmpty(cpEntry.getExclusionPatterns())
                        || cpEntry.getOutputLocation() != null) {
                    newEntries.add(JavaCore.newSourceEntry(cpEntry.getPath()));
                    changed |= true;
                } else {
                    newEntries.add(cpEntry);
                }
            } else {
                newEntries.add(cpEntry);
            }
        }
        ProgressUtil.progressOneStep(monitor, 15);
        if (changed) {
            javaProject.setRawClasspath(newEntries.toArray(new IClasspathEntry[0]), null);
            ProgressUtil.progressOneStep(monitor);
            //This is a hot fix for the Maven classpath container issue,
            //should be removed as soon as the M2Eclipse guys fix it
            int count = 0; //we only go to sleep 5 times.
            while (MavenCoreUtils.getMaven2ClasspathContainer(javaProject).getClasspathEntries().length == 0
                    && count < 5) {
                logger.warning("Maven Classpath is empty->", SOAMavenConstants.MAVEN_CLASSPATH_CONTAINER_ID,
                        ", going to sleep...");
                Thread.sleep(1000);
                ProgressUtil.progressOneStep(monitor, 10);
                count++;
            }
        }
    } finally {
        if (SOALogger.DEBUG)
            logger.exiting();
    }
}

From source file:org.eclim.plugin.jdt.preference.OptionHandler.java

License:Open Source License

@Override
public void setOption(IProject project, String name, String value) throws Exception {
    IJavaProject javaProject = JavaCore.create(project);
    if (!javaProject.exists()) {
        throw new IllegalArgumentException(Services.getMessage("project.not.found", project.getName()));
    }//from   ww  w  .  j a  v a  2s  . co  m

    @SuppressWarnings("unchecked")
    Map<String, String> global = javaProject.getOptions(true);
    @SuppressWarnings("unchecked")
    Map<String, String> options = javaProject.getOptions(false);

    Object current = global.get(name);
    if (current == null || !current.equals(value)) {
        if (name.equals(JavaCore.COMPILER_SOURCE)) {
            JavaUtils.setCompilerSourceCompliance(javaProject, value);
        } else if (name.equals(PROJECT_JAVADOC)) {
            if (value != null && value.trim().length() == 0) {
                value = null;
            }
            try {
                JavaUI.setProjectJavadocLocation(javaProject, value != null ? new URL(value) : null);
            } catch (MalformedURLException mue) {
                throw new IllegalArgumentException(
                        PROJECT_JAVADOC + ": Invalid javadoc url: " + mue.getMessage());
            }
        } else if (name.startsWith(JavaUI.ID_PLUGIN)) {
            Preferences.getInstance().setPreference(JavaUI.ID_PLUGIN, project, name, value);
        } else {
            options.put(name, value);
            javaProject.setOptions(options);
        }
    }
}