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.andmore.android.codeutils.db.utils.DatabaseUtils.java

License:Apache License

/**
 * Formats the code using the Eclipse Java settings if possible, otherwise
 * returns original document not indented.
 * //from  w  w w .  j  a v a2 s. co m
 * @param destinationFile
 *            Destination file.
 * @param databaseHelperText
 *            Text to generate.
 * @param monitor
 *            A progress monitor to be used to show operation status.
 * @return Created document.
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static IDocument formatCode(IFile destinationFile, String databaseHelperText, IProgressMonitor monitor) {
    IDocument document = new Document();
    File file = new File(destinationFile.getLocation().toOSString());

    try {
        document.set(databaseHelperText);

        try {
            IJavaProject p = JavaCore.create(destinationFile.getProject());
            Map mapOptions = p.getOptions(true);

            TextEdit textEdit = CodeFormatterUtil.format2(
                    CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, document.get(), 0,
                    System.getProperty("line.separator"), mapOptions);

            if (textEdit != null) {
                textEdit.apply(document);
            }
        } catch (Exception ex) {
            // do nothing
        }

        BufferedWriter out = new BufferedWriter(new FileWriter(file));
        try {
            out.write(document.get());
            out.flush();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                /* ignore */
            }
        }
        // the refresh is needed in order to avoid the user to have to press
        // F5
        destinationFile.getParent().refreshLocal(IResource.DEPTH_INFINITE, monitor);
    } catch (Exception e) {
        String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorFormattingSourceCode,
                destinationFile.getName());
        AndmoreLogger.error(DatabaseUtils.class, errMsg, e);
    }
    return document;

}

From source file:org.eclipse.andmore.internal.wizards.templates.TemplateHandler.java

License:Open Source License

private static String format(IProject project, String contents, IPath to) {
    String name = to.lastSegment();
    if (name.endsWith(DOT_XML)) {
        XmlFormatStyle formatStyle = EclipseXmlPrettyPrinter.getForFile(to);
        EclipseXmlFormatPreferences prefs = EclipseXmlFormatPreferences.create();
        return EclipseXmlPrettyPrinter.prettyPrint(contents, prefs, formatStyle, null);
    } else if (name.endsWith(DOT_JAVA)) {
        Map<?, ?> options = null;
        if (project != null && project.isAccessible()) {
            try {
                IJavaProject javaProject = BaseProjectHelper.getJavaProject(project);
                if (javaProject != null) {
                    options = javaProject.getOptions(true);
                }//from ww  w. j  a  v a2s . c om
            } catch (CoreException e) {
                AndmoreAndroidPlugin.log(e, null);
            }
        }
        if (options == null) {
            options = JavaCore.getOptions();
        }

        CodeFormatter formatter = ToolFactory.createCodeFormatter(options);

        try {
            IDocument doc = new org.eclipse.jface.text.Document();
            // format the file (the meat and potatoes)
            doc.set(contents);
            TextEdit edit = formatter.format(
                    CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, contents, 0,
                    contents.length(), 0, null);
            if (edit != null) {
                edit.apply(doc);
            }

            return doc.get();
        } catch (Exception e) {
            AndmoreAndroidPlugin.log(e, null);
        }
    }

    return contents;
}

From source file:org.eclipse.che.jdt.internal.core.search.indexing.IndexManager.java

License:Open Source License

public SourceElementParser getSourceElementParser(IJavaProject project, ISourceElementRequestor requestor) {
    // disable task tags to speed up parsing
    Map options = project.getOptions(true);
    options.put(JavaCore.COMPILER_TASK_TAGS, ""); //$NON-NLS-1$
    try {/* w w w . j a  v a  2 s  .c om*/
        SourceElementParser parser = new IndexingParser(requestor,
                new DefaultProblemFactory(Locale.getDefault()), new CompilerOptions(options), true, // index local declarations
                true, // optimize string literals
                false); // do not use source javadoc parser to speed up parsing
        parser.reportOnlyOneSyntaxError = true;

        // Always check javadoc while indexing
        parser.javadocParser.checkDocComment = true;
        parser.javadocParser.reportProblems = false;

        return parser;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:org.eclipse.che.jdt.javadoc.JavadocContentAccess2.java

License:Open Source License

private static CompilationUnit createAST(IJavaElement element, String cuSource) {
    Assert.isNotNull(element);/*from   w w w .  j  av a2s .  com*/
    CheASTParser parser = CheASTParser.newParser(AST.JLS8);

    IJavaProject javaProject = element.getJavaProject();
    parser.setProject(javaProject);
    Map<String, String> options = javaProject.getOptions(true);
    options.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED); // workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=212207
    parser.setCompilerOptions(options);

    parser.setSource(cuSource.toCharArray());
    return (CompilationUnit) parser.createAST(null);
}

From source file:org.eclipse.che.jdt.refactoring.ccp.MoveTest.java

License:Open Source License

@Test
public void testDestination_yes_cuFromRoot() throws Exception {
    ParticipantTesting.reset();//  ww w. j  av a  2 s .c om

    //import statement with type from default package - only <= java 1.3
    IJavaProject javaProject = getRoot().getJavaProject();
    Map originalOptions = javaProject.getOptions(false);
    Map newOptions = javaProject.getOptions(false);
    newOptions.put(JavaCore.COMPILER_COMPLIANCE, "1.3");
    newOptions.put(JavaCore.COMPILER_SOURCE, "1.3");
    javaProject.setOptions(newOptions);

    String oldD = "import org.test.Reference;public class Default {Reference ref;}";
    String oldRef = "package org.test;import Default;public class Reference{Default d;}";
    String newD = "package org;\nimport org.test.Reference;public class Default {Reference ref;}";
    String newRef = "package org.test;import org.Default;\npublic class Reference{Default d;}";
    ICompilationUnit cuD = getRoot().getPackageFragment("").createCompilationUnit("Default.java", oldD, false,
            new NullProgressMonitor());
    IPackageFragment orgTest = getRoot().createPackageFragment("org.test", false, new NullProgressMonitor());
    ICompilationUnit cuRef = orgTest.createCompilationUnit("Reference.java", oldRef, false,
            new NullProgressMonitor());
    IPackageFragment org = getRoot().getPackageFragment("org");
    ICompilationUnit newCuD = org.getCompilationUnit(cuD.getElementName());
    try {
        IJavaElement[] javaElements = { cuD };
        IResource[] resources = {};
        String[] handles = ParticipantTesting
                .createHandles(new Object[] { cuD, cuD.getTypes()[0], cuD.getResource() });
        JavaMoveProcessor ref = verifyEnabled(resources, javaElements, createReorgQueries());

        verifyValidDestination(ref, org);

        assertTrue("source file Default.java does not exist before moving", cuD.exists());
        assertTrue("source file Reference.java does not exist before moving", cuRef.exists());
        RefactoringStatus status = performRefactoring(ref, true);
        assertEquals(null, status);
        assertTrue("source file Default.java exists after moving", !cuD.exists());
        assertTrue("new file Default.java does not exist after moving", newCuD.exists());
        assertTrue("source file Reference.java does not exist after moving", cuRef.exists());
        assertEqualLines("Default.java differs", newD, newCuD.getSource());
        assertEqualLines("Reference.java differs", newRef, cuRef.getSource());

        ParticipantTesting.testMove(handles,
                new MoveArguments[] { new MoveArguments(org, ref.getUpdateReferences()),
                        new MoveArguments(org, ref.getUpdateReferences()),
                        new MoveArguments(org.getResource(), ref.getUpdateReferences()) });
    } finally {
        javaProject.setOptions(originalOptions);
    }
}

From source file:org.eclipse.che.jdt.refactoring.ccp.MoveTest.java

License:Open Source License

@Test
public void testDestination_no_cuFromRoot() throws Exception {
    //import statement with type from default package - only <= java 1.3
    IJavaProject javaProject = getRoot().getJavaProject();
    Map originalOptions = javaProject.getOptions(false);
    Map newOptions = javaProject.getOptions(false);
    newOptions.put(JavaCore.COMPILER_COMPLIANCE, "1.4"); //will cause error (potential match)
    newOptions.put(JavaCore.COMPILER_SOURCE, "1.4"); //will cause error (potential match)
    javaProject.setOptions(newOptions);//from  w w  w  .  j a va 2s.  com

    String oldD = "import org.test.Reference;public class Default {Reference ref;}";
    String oldRef = "package org.test;import Default;public class Reference{Default d;}";
    String newD = "package org;\nimport org.test.Reference;public class Default {Reference ref;}";
    String newRef = "package org.test;import org.Default;\npublic class Reference{Default d;}";
    ICompilationUnit cuD = getRoot().getPackageFragment("").createCompilationUnit("Default.java", oldD, false,
            new NullProgressMonitor());
    IPackageFragment orgTest = getRoot().createPackageFragment("org.test", false, new NullProgressMonitor());
    ICompilationUnit cuRef = orgTest.createCompilationUnit("Reference.java", oldRef, false,
            new NullProgressMonitor());
    IPackageFragment org = getRoot().getPackageFragment("org");
    ICompilationUnit newCuD = org.getCompilationUnit(cuD.getElementName());
    try {
        IJavaElement[] javaElements = { cuD };
        IResource[] resources = {};
        JavaMoveProcessor ref = verifyEnabled(resources, javaElements, createReorgQueries());

        verifyValidDestination(ref, org);

        assertTrue("source file Default.java does not exist before moving", cuD.exists());
        assertTrue("source file Reference.java does not exist before moving", cuRef.exists());
        RefactoringStatus status = performRefactoring(ref, false);
        assertEquals(RefactoringStatus.ERROR, status.getSeverity());
        assertTrue("source file Default.java exists after moving", !cuD.exists());
        assertTrue("new file Default.java does not exist after moving", newCuD.exists());
        assertTrue("source file Reference.java does not exist after moving", cuRef.exists());
        assertEqualLines("Default.java differs", newD, newCuD.getSource());
        assertEqualLines("Reference.java differs", newRef, cuRef.getSource());

    } finally {
        javaProject.setOptions(originalOptions);
    }
}

From source file:org.eclipse.che.jdt.refactoring.RenamePackageTest.java

License:Open Source License

/**
 * 2 Projects with a root each: Project RenamePack2 (root: srcTest) requires project RenamePack1
 * (root: srcPrg).// w ww  . ja  v  a  2s . c  o  m
 *
 * @param packageNames package names per root
 * @param newPackageName the new package name for packageNames[0][0]
 * @param cuNames cu names per package
 * @throws Exception if one of the resources cannot be created
 */
private void helperProjectsPrgTest(String[][] packageNames, String newPackageName, String[][][] cuNames)
        throws Exception {
    IJavaProject projectPrg = null;
    IJavaProject projectTest = null;
    try {
        projectPrg = JavaProjectHelper.createJavaProject("RenamePack1", "bin");
        assertNotNull(JavaProjectHelper.addRTJar(projectPrg));
        IPackageFragmentRoot srcPrg = JavaProjectHelper.addSourceContainer(projectPrg, "srcPrg");
        Map optionsPrg = projectPrg.getOptions(false);
        JavaProjectHelper.set15CompilerOptions(optionsPrg);
        projectPrg.setOptions(optionsPrg);

        projectTest = JavaProjectHelper.createJavaProject("RenamePack2", "bin");
        assertNotNull(JavaProjectHelper.addRTJar(projectTest));
        IPackageFragmentRoot srcTest = JavaProjectHelper.addSourceContainer(projectTest, "srcTest");
        Map optionsTest = projectTest.getOptions(false);
        JavaProjectHelper.set15CompilerOptions(optionsTest);
        projectTest.setOptions(optionsTest);

        JavaProjectHelper.addRequiredProject(projectTest, projectPrg);

        helperMultiProjects(new IPackageFragmentRoot[] { srcPrg, srcTest }, packageNames, newPackageName,
                cuNames);
    } finally {
        JavaProjectHelper.delete(projectPrg);
        JavaProjectHelper.delete(projectTest);
    }
}

From source file:org.eclipse.che.jdt.rest.CompilerSetupService.java

License:Open Source License

@GET
@Path("/all")
@Consumes(APPLICATION_JSON)/*from w w w . j a va2s  .  c o  m*/
@Produces(APPLICATION_JSON)
public Map<String, String> getAllParameters(@QueryParam("projectpath") String projectPath) {
    IJavaProject project = JAVA_MODEL.getJavaProject(projectPath);

    //noinspection unchecked
    Map<String, String> map = project.getOptions(true);

    CompilerOptions options = new CompilerOptions(map);

    //noinspection unchecked
    return options.getMap();
}

From source file:org.eclipse.che.jdt.testplugin.JavaProjectHelper.java

License:Open Source License

/**
 * Sets the compiler options to 1.8 for the given project.
 *
 * @param project the java project/*from w  ww.  ja  va2s .c  om*/
 * @since 3.10
 */
public static void set18CompilerOptions(IJavaProject project) {
    Map options = project.getOptions(false);
    set18CompilerOptions(options);
    project.setOptions(options);
}

From source file:org.eclipse.che.jdt.testplugin.JavaProjectHelper.java

License:Open Source License

/**
 * Sets the compiler options to 1.7 for the given project.
 * @param project the java project//from   w w  w .  jav a  2 s. co m
 */
public static void set17CompilerOptions(IJavaProject project) {
    Map options = project.getOptions(false);
    JavaProjectHelper.set17CompilerOptions(options);
    project.setOptions(options);
}