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

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

Introduction

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

Prototype

Map<String, String> getOptions(boolean inheritJavaCoreOptions);

Source Link

Document

Returns the table of the current custom options for this project.

Usage

From source file:org.eclipse.jet.internal.taglib.java.JavaFormatTag.java

License:Open Source License

private Map getJavaCoreOptions(String projectName, JET2Context context) throws CoreException {
    if (DEBUG)/*from ww w . ja va2  s.c o  m*/
        System.out.println("JavaFormatTag.getJavaCoreOptions - project specified=" + projectName); //$NON-NLS-1$
    Map options = null;

    if (projectName != null) {
        IWorkspaceRoot wsroot = ResourcesPlugin.getWorkspace().getRoot();
        IProject project = wsroot.getProject(projectName);
        if (project.isAccessible() && project.hasNature(JavaCore.NATURE_ID)) {
            IJavaProject javaProject = JavaCore.create(project);

            if (DEBUG)
                System.out.println(
                        "JavaFormatTag.getJavaCoreOptions - using Options from java project " + projectName); //$NON-NLS-1$
            options = javaProject.getOptions(true);
        }
    }

    if (options == null) {
        if (DEBUG)
            System.out.println("JavaFormatTag.getJavaCoreOptions - Falling back to generic JavaCore options"); //$NON-NLS-1$
        options = JavaCore.getOptions();
    }

    return options;
}

From source file:org.eclipse.jst.common.project.facet.core.internal.JavaFacetUtil.java

License:Open Source License

public static void setCompilerLevel(final IProject project, final String level)

        throws CoreException

{
    final IJavaProject jproj = JavaCore.create(project);
    final Map<String, String> options = jproj.getOptions(false);
    JavaCore.setComplianceOptions(level, options);
    jproj.setOptions(options);//  ww  w.  j  av a 2  s . com
}

From source file:org.eclipse.m2m.internal.qvt.oml.ui.wizards.project.NewProjectCreationOperation.java

License:Open Source License

@SuppressWarnings("unchecked")
private static void setComplianceOptions(IJavaProject project, String eeId, boolean overrideExisting) {
    Map<String, String> projectMap = project.getOptions(false);
    IExecutionEnvironment ee = null;// w  w  w  . j  a  va  2s .c  o  m
    Map<String, String> options = null;
    if (eeId != null) {
        ee = JavaRuntime.getExecutionEnvironmentsManager().getEnvironment(eeId);
        if (ee != null) {
            options = ee.getComplianceOptions();
        }
    }
    if (options == null) {
        if (overrideExisting && projectMap.size() > 0) {
            projectMap.remove(JavaCore.COMPILER_COMPLIANCE);
            projectMap.remove(JavaCore.COMPILER_SOURCE);
            projectMap.remove(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM);
            projectMap.remove(JavaCore.COMPILER_PB_ASSERT_IDENTIFIER);
            projectMap.remove(JavaCore.COMPILER_PB_ENUM_IDENTIFIER);
        } else {
            return;
        }
    } else {
        String compliance = options.get(JavaCore.COMPILER_COMPLIANCE);
        Iterator<?> iterator = options.entrySet().iterator();
        while (iterator.hasNext()) {
            Entry<?, ?> entry = (Entry<?, ?>) iterator.next();
            String option = (String) entry.getKey();
            String value = (String) entry.getValue();
            if (JavaCore.VERSION_1_3.equals(compliance) || JavaCore.VERSION_1_4.equals(compliance)) {
                if (JavaCore.COMPILER_PB_ASSERT_IDENTIFIER.equals(option)
                        || JavaCore.COMPILER_PB_ENUM_IDENTIFIER.equals(option)) {
                    // for 1.3 & 1.4 projects, only override the existing setting if the default setting
                    // is a greater severity than the existing setting
                    setMinimumCompliance(projectMap, option, value, overrideExisting);
                } else {
                    setCompliance(projectMap, option, value, overrideExisting);
                }
            } else {
                setCompliance(projectMap, option, value, overrideExisting);
            }
        }
    }

    project.setOptions(projectMap);
}

From source file:org.eclipse.objectteams.otdt.tests.otmodel.OTReconcilerTests.java

License:Open Source License

@SuppressWarnings("unchecked") // options in a raw map
public void testLocalInRoFi() throws CoreException, InterruptedException {
    try {//from ww  w.  j  av a  2 s  .co  m
        // Resources creation
        IJavaProject p = createOTJavaProject("P", new String[] { "" }, new String[] { "JCL15_LIB" }, "bin");
        // set compliance to 1.6 to force generated of a stack map attribute.
        Map options = p.getOptions(true);
        options.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_6);
        options.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_6);
        options.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_6);
        p.setOptions(options);
        IProject project = p.getProject();
        IProjectDescription prjDesc = project.getDescription();
        prjDesc.setNatureIds(OTDTPlugin.createProjectNatures(prjDesc));
        prjDesc.setBuildSpec(OTDTPlugin.createProjectBuildCommands(prjDesc));
        project.setDescription(prjDesc, null);

        OTREContainer.initializeOTJProject(project);

        this.createFile("/P/Plain.java", "public class Plain {\n" + "    public Object getVal() {\n"
                + "        return \"Plain\";\n" + "    }\n" + "}\n");
        String sourceTeam = "public team class Foo {\n" + "    String foo, blub, bar, dings, wurgs, zork;\n"
                + "    public static void main(String[] args) {\n" + "        new Foo().new Role().test();\n"
                + "    }\n" + "}\n";
        this.createFile("/P/Foo.java", sourceTeam);
        this.createFolder("/P/Foo");

        String sourceRole1 = "team package Foo;\n" + "@SuppressWarnings(\"unused\")\n"
                + "public class Role1 {\n" + "   String s1, s2, s3, s4, s5, s6;\n" + "}\n";
        this.createFile("/P/Foo/Role1.java", sourceRole1);

        String sourceRole2 = "team package Foo;\n" + "@SuppressWarnings(\"unused\")\n"
                + "public class Role2 extends Role1 {\n" + "        protected void test() {\n"
                + "            Plain isub = new Plain() {\n" + "                public Object getVal() {\n"
                + "                    Object edits = super.getVal();\n"
                + "                    if (edits instanceof String) {\n"
                + "                        String string = (String)edits;\n"
                + "                        int l = string.length();\n" + "                    }\n"
                + "                    return edits;\n" + "                }\n" + "            };\n"
                + "            Object v = isub.getVal();\n" + "        }\n" + "}\n";
        this.createFile("/P/Foo/Role2.java", sourceRole2);

        char[] sourceChars = sourceRole2.toCharArray();
        this.problemRequestor.initialize(sourceChars);

        ICompilationUnit wc = getCompilationUnit("/P/Foo/Role2.java").getWorkingCopy(this.wcOwner, null);
        wc.reconcile(AST.JLS3, ICompilationUnit.FORCE_PROBLEM_DETECTION
                | ICompilationUnit.ENABLE_STATEMENTS_RECOVERY | ICompilationUnit.ENABLE_BINDINGS_RECOVERY,
                wc.getOwner(), null);

        assertProblems("Unexpected problems",
                "----------\n" + "----------\n" + "----------\n" + "----------\n");
    } finally {
        deleteProject("P");
    }
}

From source file:org.eclipse.objectteams.otdt.tests.otmodel.OTReconcilerTests.java

License:Open Source License

@SuppressWarnings("unchecked") // options in a raw map
public void testRemoveRole() throws CoreException, InterruptedException, UnsupportedEncodingException {
    try {/*from www. jav  a 2s . c  om*/
        // Resources creation
        IJavaProject p = createOTJavaProject("P", new String[] { "" }, new String[] { "JCL15_LIB" }, "bin");
        // set compliance to 1.6 to force generated of a stack map attribute.
        Map options = p.getOptions(true);
        options.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_6);
        options.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_6);
        options.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_6);
        p.setOptions(options);
        IProject project = p.getProject();
        IProjectDescription prjDesc = project.getDescription();
        prjDesc.setNatureIds(OTDTPlugin.createProjectNatures(prjDesc));
        prjDesc.setBuildSpec(OTDTPlugin.createProjectBuildCommands(prjDesc));
        project.setDescription(prjDesc, null);

        OTREContainer.initializeOTJProject(project);

        String sourceTeam = "public team class Foo {\n" + "   public class Role1 {\n"
                + "      String s1, s2, s3, s4, s5, s6;\n" + "   }\n"
                + "   String foo, blub, bar, dings, wurgs, zork;\n"
                + "   public static void main(String[] args) {\n" + "       new Foo().new Role1().test();\n"
                + "   }\n" + "}\n";
        this.createFile("/P/Foo.java", sourceTeam);

        this.problemRequestor.initialize(sourceTeam.toCharArray());

        ICompilationUnit wc = getCompilationUnit("/P/Foo.java").getWorkingCopy(this.wcOwner, null);
        IType itype = wc.getType("Foo");
        IOTType ottype = OTModelManager.getOTElement(itype);
        IType rt = ottype.getRoleType("Role1");
        IOTType roleType = OTModelManager.getOTElement(rt); // this caches the role type
        assertTrue("Role should initially exist", rt.exists());
        assertTrue("RoleType should initially exist", roleType.exists());

        // delete Role1
        wc.applyTextEdit(new DeleteEdit(("public team class Foo {\n").length(),
                ("   public class Role1 {\n" + "      String s1, s2, s3, s4, s5, s6;\n" + "   }\n").length()),
                null);
        wc.reconcile(AST.JLS3, ICompilationUnit.FORCE_PROBLEM_DETECTION
                | ICompilationUnit.ENABLE_STATEMENTS_RECOVERY | ICompilationUnit.ENABLE_BINDINGS_RECOVERY,
                wc.getOwner(), null);

        // watch that
        // ... the JavaElement no longer exists:
        rt = itype.getType("Role1");
        assertFalse("Role should no longer exist", rt.exists());
        // ... and OTModel no longer has a RoleType:
        ottype = OTModelManager.getOTElement(itype);
        rt = ottype.getRoleType("Role1");
        assertNull("Role should be null", rt);
    } finally {
        deleteProject("P");
    }
}

From source file:org.eclipse.objectteams.otdt.tests.otmodel.OTReconcilerTests.java

License:Open Source License

public void testAnchoredType01() throws CoreException, InterruptedException {
    try {/*from  ww w  .  j  a  v  a 2  s . co m*/
        // Resources creation
        IJavaProject p = createOTJavaProject("P", new String[] { "" }, new String[] { "JCL15_LIB" }, "bin");
        IProject project = p.getProject();
        IProjectDescription prjDesc = project.getDescription();
        //prjDesc.setNatureIds(OTDTPlugin.createProjectNatures(prjDesc));
        prjDesc.setBuildSpec(OTDTPlugin.createProjectBuildCommands(prjDesc));
        project.setDescription(prjDesc, null);

        OTREContainer.initializeOTJProject(project);

        this.createFile("/P/BaseTeam.java",
                "public team class BaseTeam {\n" + "   public class Role {}\n" + "}\n");

        String sourceFoo = "public team class Foo {\n" + "   protected team class Mid playedBy BaseTeam {\n"
                + "      public class Inner1 playedBy Role<@Mid.base> {}\n"
                + "      protected class Inner2 playedBy Role<@base> {}\n"
                + "      @SuppressWarnings(\"roletypesyntax\")\n"
                + "      protected class Inner3 playedBy base.Role {}\n" + "   }\n" + "}\n";
        this.createFile("/P/Foo.java", sourceFoo);

        char[] sourceChars = sourceFoo.toCharArray();
        this.problemRequestor.initialize(sourceChars);

        ICompilationUnit fooWC = getCompilationUnit("/P/Foo.java").getWorkingCopy(this.wcOwner, null);
        IType foo = fooWC.getType("Foo");

        CompilerOptions compilerOptions = new CompilerOptions(p.getOptions(true));
        ProblemReporter problemReporter = new ProblemReporter(
                DefaultErrorHandlingPolicies.proceedWithAllProblems(), compilerOptions,
                new DefaultProblemFactory());

        // force usage of type converter:
        CompilationUnitDeclaration parsedUnit = SourceTypeConverter.buildCompilationUnit(
                new ISourceType[] { (ISourceType) ((SourceType) foo).getElementInfo() },
                SourceTypeConverter.FIELD_AND_METHOD | SourceTypeConverter.MEMBER_TYPE, problemReporter,
                new CompilationResult("Foo.java".toCharArray(), 1, 1, 90));

        // force resolving:
        process(parsedUnit, p, compilerOptions, problemReporter, ITranslationStates.STATE_RESOLVED);

        // evaluate result:
        String result = "";
        CategorizedProblem[] problems = parsedUnit.compilationResult().problems;
        assertNotNull(problems);
        for (IProblem problem : problems)
            if (problem != null && problem.isError())
                result += problem;
        assertEquals("", result);
    } finally {
        deleteProject("P");
    }
}

From source file:org.eclipse.objectteams.otdt.tests.otmodel.OTReconcilerTests.java

License:Open Source License

public void testAnchoredType02() throws CoreException, InterruptedException {
    try {/*www . jav  a2 s.  co m*/
        // Resources creation
        IJavaProject p = createOTJavaProject("P", new String[] { "" }, new String[] { "JCL15_LIB" }, "bin");
        IProject project = p.getProject();
        IProjectDescription prjDesc = project.getDescription();
        //prjDesc.setNatureIds(OTDTPlugin.createProjectNatures(prjDesc));
        prjDesc.setBuildSpec(OTDTPlugin.createProjectBuildCommands(prjDesc));
        project.setDescription(prjDesc, null);

        OTREContainer.initializeOTJProject(project);

        this.createFile("/P/BaseTeam.java",
                "public team class BaseTeam {\n" + "   public class Role {}\n" + "   Role baseField;" + "}\n");

        String sourceFoo = "public team class Foo {\n" + "   protected team class Mid playedBy BaseTeam {\n"
                + "      public class Inner playedBy Role<@Mid.base> {}\n"
                + "       Inner get1() -> get Role<@Mid.base> baseField;\n"
                + "       Inner get2() -> get Role<@base> baseField;\n"
                + "      @SuppressWarnings(\"roletypesyntax\")\n"
                + "       Inner get3() -> get base.Role baseField;\n" + "   }\n" + "}\n";
        this.createFile("/P/Foo.java", sourceFoo);

        char[] sourceChars = sourceFoo.toCharArray();
        this.problemRequestor.initialize(sourceChars);

        ICompilationUnit fooWC = getCompilationUnit("/P/Foo.java").getWorkingCopy(this.wcOwner, null);
        IType foo = fooWC.getType("Foo");

        CompilerOptions compilerOptions = new CompilerOptions(p.getOptions(true));
        ProblemReporter problemReporter = new ProblemReporter(
                DefaultErrorHandlingPolicies.proceedWithAllProblems(), compilerOptions,
                new DefaultProblemFactory());

        // force usage of type converter:
        CompilationUnitDeclaration parsedUnit = SourceTypeConverter.buildCompilationUnit(
                new ISourceType[] { (ISourceType) ((SourceType) foo).getElementInfo() },
                SourceTypeConverter.FIELD_AND_METHOD | SourceTypeConverter.MEMBER_TYPE, problemReporter,
                new CompilationResult("Foo.java".toCharArray(), 1, 1, 90));

        // force resolving:
        process(parsedUnit, p, compilerOptions, problemReporter, ITranslationStates.STATE_RESOLVED);

        // evaluate result:
        String result = "";
        CategorizedProblem[] problems = parsedUnit.compilationResult().problems;
        assertNotNull(problems);
        for (IProblem problem : problems)
            if (problem != null && problem.isError())
                result += problem;
        assertEquals("", result);
    } finally {
        deleteProject("P");
    }
}

From source file:org.eclipse.pde.internal.core.builders.BuildErrorReporter.java

License:Open Source License

/**
 * Checks that if the project has java compiler settings that build.properties contains a use project settings
 * entry so that the compiler picks up the settings using the .pref file.
 * /*from w w  w  .  jav a  2 s .co m*/
 * @param useJavaProjectSettings a build entry for using the project's compiler warning preferences file
 */
private void validateJavaCompilerSettings(IBuildEntry useJavaProjectSettings) {
    // Check if the project has compiler warnings set
    IJavaProject project = JavaCore.create(fProject);
    if (project.exists()) {
        Map<?, ?> options = project.getOptions(false);
        // If project specific options are turned on, all options will be stored.  Only need to check if at least one compiler option is set. Currently using the second option on the property page.
        if (options.containsKey(JavaCore.COMPILER_PB_INDIRECT_STATIC_ACCESS)) {
            if (useJavaProjectSettings != null) {
                boolean entryCorrect = false;
                String[] tokens = useJavaProjectSettings.getTokens();
                if (tokens != null && tokens.length == 1) {
                    if (Boolean.TRUE.toString().equalsIgnoreCase(tokens[0])) {
                        // True is valid if the bundle root is the default (the project)
                        entryCorrect = fProject.equals(PDEProject.getBundleRoot(fProject));
                    } else {
                        IPath prefFile = null;
                        prefFile = new Path(tokens[0]);
                        if (prefFile.isAbsolute()) {
                            entryCorrect = prefFile.toFile().exists();
                        } else {
                            IContainer root = PDEProject.getBundleRoot(fProject);
                            entryCorrect = root.getFile(prefFile).exists();
                        }
                    }
                }
                if (!entryCorrect) {
                    String token = null;
                    String message = null;
                    IContainer root = PDEProject.getBundleRoot(fProject);
                    if (fProject.equals(root)) {
                        // Default project root, just use 'true'
                        token = Boolean.TRUE.toString();
                        message = NLS.bind(PDECoreMessages.BuildErrorReporter_buildEntryMissingValidPath,
                                PROPERTY_PROJECT_SETTINGS);
                    } else {
                        // Non default bundle root, make a relative path
                        IPath prefFile = fProject.getFullPath().append(".settings") //$NON-NLS-1$
                                .append(JavaCore.PLUGIN_ID + ".prefs"); //$NON-NLS-1$
                        prefFile = prefFile.makeRelativeTo(root.getFullPath());
                        token = prefFile.toString();
                        message = NLS.bind(
                                PDECoreMessages.BuildErrorReporter_buildEntryMissingValidRelativePath,
                                PROPERTY_PROJECT_SETTINGS);
                    }
                    prepareError(PROPERTY_PROJECT_SETTINGS, token, message, PDEMarkerFactory.B_REPLACE,
                            fJavaCompilerSeverity, PDEMarkerFactory.CAT_EE);
                }
            } else {
                String token = null;
                IContainer root = PDEProject.getBundleRoot(fProject);
                if (fProject.equals(root)) {
                    // Default project root, just use 'true'
                    token = Boolean.TRUE.toString();
                } else {
                    // Non default bundle root, make a relative path
                    IPath prefFile = fProject.getFullPath().append(".settings") //$NON-NLS-1$
                            .append(JavaCore.PLUGIN_ID + ".prefs"); //$NON-NLS-1$
                    prefFile = prefFile.makeRelativeTo(root.getFullPath());
                    token = prefFile.toString();
                }
                String message = NLS.bind(
                        PDECoreMessages.BuildErrorReporter_buildEntryMissingProjectSpecificSettings,
                        PROPERTY_PROJECT_SETTINGS);
                prepareError(PROPERTY_PROJECT_SETTINGS, token, message, PDEMarkerFactory.B_JAVA_ADDDITION,
                        fJavaCompilerSeverity, PDEMarkerFactory.CAT_EE);
            }
        } else if (useJavaProjectSettings != null) {
            String message = NLS.bind(PDECoreMessages.BuildErrorReporter_buildEntryInvalidWhenNoProjectSettings,
                    PROPERTY_PROJECT_SETTINGS);
            prepareError(PROPERTY_PROJECT_SETTINGS, null, message, PDEMarkerFactory.B_REMOVAL,
                    fJavaCompilerSeverity, PDEMarkerFactory.CAT_EE);
        }
    }
}

From source file:org.eclipse.pde.internal.core.ClasspathComputer.java

License:Open Source License

/**
 * Sets compiler compliance options on the given project to match the default compliance settings
 * for the specified execution environment. Only sets options that do not already have an explicit
 * setting based on the given override flag.
 * <p>/* w  w w. j  a  v a2 s . c  om*/
 * If the specified execution environment is <code>null</code> and override is <code>true</code>,
 * all compliance options are removed from the options map before applying to the project.
 * </p>
 * @param project project to set compiler compliance options for
 * @param eeId execution environment identifier, or <code>null</code>
 * @param overrideExisting whether to override a setting if already present
 */
public static void setComplianceOptions(IJavaProject project, String eeId, boolean overrideExisting) {
    @SuppressWarnings("unchecked")
    Map<String, String> projectMap = project.getOptions(false);
    IExecutionEnvironment ee = null;
    Map<?, ?> options = null;
    if (eeId != null) {
        ee = JavaRuntime.getExecutionEnvironmentsManager().getEnvironment(eeId);
        if (ee != null) {
            options = ee.getComplianceOptions();
        }
    }
    if (options == null) {
        if (overrideExisting && projectMap.size() > 0) {
            projectMap.remove(JavaCore.COMPILER_COMPLIANCE);
            projectMap.remove(JavaCore.COMPILER_SOURCE);
            projectMap.remove(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM);
            projectMap.remove(JavaCore.COMPILER_PB_ASSERT_IDENTIFIER);
            projectMap.remove(JavaCore.COMPILER_PB_ENUM_IDENTIFIER);
        } else {
            return;
        }
    } else {
        String compliance = (String) options.get(JavaCore.COMPILER_COMPLIANCE);
        Iterator<?> iterator = options.entrySet().iterator();
        while (iterator.hasNext()) {
            Entry<?, ?> entry = (Entry<?, ?>) iterator.next();
            String option = (String) entry.getKey();
            String value = (String) entry.getValue();
            if (JavaCore.VERSION_1_3.equals(compliance) || JavaCore.VERSION_1_4.equals(compliance)) {
                if (JavaCore.COMPILER_PB_ASSERT_IDENTIFIER.equals(option)
                        || JavaCore.COMPILER_PB_ENUM_IDENTIFIER.equals(option)) {
                    // for 1.3 & 1.4 projects, only override the existing setting if the default setting
                    // is a greater severity than the existing setting
                    setMinimumCompliance(projectMap, option, value, overrideExisting);
                } else {
                    setCompliance(projectMap, option, value, overrideExisting);
                }
            } else {
                setCompliance(projectMap, option, value, overrideExisting);
            }
        }
    }

    project.setOptions(projectMap);

}

From source file:org.eclipse.virgo.ide.eclipse.wizards.NewJavaProjectWizardPageTwoCOPY.java

License:Open Source License

/**
 * Called from the wizard on finish.//from  w  w w  . jav  a 2 s.  c o m
 *
 * @param monitor the progress monitor
 * @throws CoreException thrown when the project creation or configuration failed
 * @throws InterruptedException thrown when the user cancelled the project creation
 */
@SuppressWarnings("unchecked")
public void performFinish(IProgressMonitor monitor) throws CoreException, InterruptedException {
    try {
        monitor.beginTask(NewJavaProjectWizardConstants.NewJavaProjectWizardPageTwo_operation_create, 3);
        if (this.fCurrProject == null) {
            updateProject(new SubProgressMonitor(monitor, 1));
        }
        configureJavaProject(new SubProgressMonitor(monitor, 2));

        if (!this.fKeepContent) {
            String compliance = this.fFirstPage.getCompilerCompliance();
            if (compliance != null) {
                IJavaProject project = JavaCore.create(this.fCurrProject);
                Map options = project.getOptions(false);
                // JavaModelUtil.setCompilanceOptions(options, compliance);
                JavaCore.setComplianceOptions(compliance, options);
                JavaModelUtil.setDefaultClassfileOptions(options, compliance); // complete
                // compliance
                // options
                project.setOptions(options);
            }
        }
    } finally {
        monitor.done();
        this.fCurrProject = null;
        if (this.fIsAutobuild != null) {
            setAutoBuilding(this.fIsAutobuild.booleanValue());
            this.fIsAutobuild = null;
        }
    }
}