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:at.bestsolution.efxclipse.tooling.converter.AbstractConverterHandler.java

License:Open Source License

public static Object createCodeFormatter(IProject project) {
    IJavaProject javaProject = JavaCore.create(project);
    Map options = javaProject.getOptions(true);
    return ToolFactory.createCodeFormatter(options);
}

From source file:at.bestsolution.fxide.jdt.editor.JDTJavaDocSupport.java

License:Open Source License

private static CompilationUnit createAST(IJavaElement element, String cuSource) {
    ASTParser parser = ASTParser.newParser(AST.JLS8);

    IJavaProject javaProject = element.getJavaProject();
    parser.setProject(javaProject);//from  w  w w  .  j a v a2s  .  com
    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:at.bestsolution.fxide.jdt.text.javadoc.JavadocContentAccess2.java

License:Open Source License

private static CompilationUnit createAST(IJavaElement element, String cuSource) {
    Assert.isNotNull(element);/*from ww w  .  j  a  v a 2s .  c  o m*/
    ASTParser parser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);

    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:at.bestsolution.javafx.ide.jdt.internal.jdt.BuildPathSupport.java

License:Open Source License

public static void setEEComplianceOptions(IJavaProject javaProject, String eeID, String newProjectCompliance) {
    IExecutionEnvironment ee = JavaRuntime.getExecutionEnvironmentsManager().getEnvironment(eeID);
    if (ee != null) {
        Map<String, String> options = javaProject.getOptions(false);
        Map<String, String> eeOptions = getEEOptions(ee);
        if (eeOptions != null) {
            for (int i = 0; i < PREFS_COMPLIANCE.length; i++) {
                String option = PREFS_COMPLIANCE[i];
                options.put(option, eeOptions.get(option));
            }//from  www. j a v a2s.  c o m

            if (newProjectCompliance != null) {
                JavaModelUtil.setDefaultClassfileOptions(options, newProjectCompliance); // complete compliance options
            }

            String option = JavaCore.COMPILER_CODEGEN_INLINE_JSR_BYTECODE;
            String inlineJSR = eeOptions.get(option);
            if (inlineJSR != null) {
                options.put(option, inlineJSR);
            }

            javaProject.setOptions(options);
        }
    }
}

From source file:at.bestsolution.javafx.ide.jdt.internal.NewJavaProjectService.java

License:Open Source License

public static void flush(List<CPListElement> classPathEntries, IPath outputLocation, IJavaProject javaProject,
        String newProjectCompliance, IProgressMonitor monitor)
        throws CoreException, OperationCanceledException {
    IProject project = javaProject.getProject();
    IPath projPath = project.getFullPath();

    IPath oldOutputLocation;/*from  w  w  w  .ja  va  2s  . com*/
    try {
        oldOutputLocation = javaProject.getOutputLocation();
    } catch (CoreException e) {
        oldOutputLocation = projPath.append(
                "bin"/*PreferenceConstants.getPreferenceStore().getString(PreferenceConstants.SRCBIN_BINNAME)*/);
    }

    if (oldOutputLocation.equals(projPath) && !outputLocation.equals(projPath)) {
        if (BuildPathsBlock.hasClassfiles(project)) {
            //            if (BuildPathsBlock.getRemoveOldBinariesQuery(JavaPlugin.getActiveWorkbenchShell()).doQuery(false, projPath)) {
            BuildPathsBlock.removeOldClassfiles(project);
            //            }
        }
    } else if (!outputLocation.equals(oldOutputLocation)) {
        IFolder folder = ResourcesPlugin.getWorkspace().getRoot().getFolder(oldOutputLocation);
        if (folder.exists()) {
            if (folder.members().length == 0) {
                BuildPathsBlock.removeOldClassfiles(folder);
            } else {
                //               if (BuildPathsBlock.getRemoveOldBinariesQuery(JavaPlugin.getActiveWorkbenchShell()).doQuery(folder.isDerived(), oldOutputLocation)) {
                BuildPathsBlock.removeOldClassfiles(folder);
                //               }
            }
        }
    }

    monitor.worked(1);

    IWorkspaceRoot fWorkspaceRoot = ResourcesPlugin.getWorkspace().getRoot();

    //create and set the output path first
    if (!fWorkspaceRoot.exists(outputLocation)) {
        IFolder folder = fWorkspaceRoot.getFolder(outputLocation);
        CoreUtility.createDerivedFolder(folder, true, true, new SubProgressMonitor(monitor, 1));
    } else {
        monitor.worked(1);
    }
    if (monitor.isCanceled()) {
        throw new OperationCanceledException();
    }

    int nEntries = classPathEntries.size();
    IClasspathEntry[] classpath = new IClasspathEntry[nEntries];
    int i = 0;

    for (Iterator<CPListElement> iter = classPathEntries.iterator(); iter.hasNext();) {
        CPListElement entry = iter.next();
        classpath[i] = entry.getClasspathEntry();
        i++;

        IResource res = entry.getResource();
        //1 tick
        if (res instanceof IFolder && entry.getLinkTarget() == null && !res.exists()) {
            CoreUtility.createFolder((IFolder) res, true, true, new SubProgressMonitor(monitor, 1));
        } else {
            monitor.worked(1);
        }

        //3 ticks
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            IPath folderOutput = (IPath) entry.getAttribute(CPListElement.OUTPUT);
            if (folderOutput != null && folderOutput.segmentCount() > 1) {
                IFolder folder = fWorkspaceRoot.getFolder(folderOutput);
                CoreUtility.createDerivedFolder(folder, true, true, new SubProgressMonitor(monitor, 1));
            } else {
                monitor.worked(1);
            }

            IPath path = entry.getPath();
            if (projPath.equals(path)) {
                monitor.worked(2);
                continue;
            }

            if (projPath.isPrefixOf(path)) {
                path = path.removeFirstSegments(projPath.segmentCount());
            }
            IFolder folder = project.getFolder(path);
            IPath orginalPath = entry.getOrginalPath();
            if (orginalPath == null) {
                if (!folder.exists()) {
                    //New source folder needs to be created
                    if (entry.getLinkTarget() == null) {
                        CoreUtility.createFolder(folder, true, true, new SubProgressMonitor(monitor, 2));
                    } else {
                        folder.createLink(entry.getLinkTarget(), IResource.ALLOW_MISSING_LOCAL,
                                new SubProgressMonitor(monitor, 2));
                    }
                }
            } else {
                if (projPath.isPrefixOf(orginalPath)) {
                    orginalPath = orginalPath.removeFirstSegments(projPath.segmentCount());
                }
                IFolder orginalFolder = project.getFolder(orginalPath);
                if (entry.getLinkTarget() == null) {
                    if (!folder.exists()) {
                        //Source folder was edited, move to new location
                        IPath parentPath = entry.getPath().removeLastSegments(1);
                        if (projPath.isPrefixOf(parentPath)) {
                            parentPath = parentPath.removeFirstSegments(projPath.segmentCount());
                        }
                        if (parentPath.segmentCount() > 0) {
                            IFolder parentFolder = project.getFolder(parentPath);
                            if (!parentFolder.exists()) {
                                CoreUtility.createFolder(parentFolder, true, true,
                                        new SubProgressMonitor(monitor, 1));
                            } else {
                                monitor.worked(1);
                            }
                        } else {
                            monitor.worked(1);
                        }
                        orginalFolder.move(entry.getPath(), true, true, new SubProgressMonitor(monitor, 1));
                    }
                } else {
                    if (!folder.exists() || !entry.getLinkTarget().equals(entry.getOrginalLinkTarget())) {
                        orginalFolder.delete(true, new SubProgressMonitor(monitor, 1));
                        folder.createLink(entry.getLinkTarget(), IResource.ALLOW_MISSING_LOCAL,
                                new SubProgressMonitor(monitor, 1));
                    }
                }
            }
        } else {
            if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                IPath path = entry.getPath();
                if (!path.equals(entry.getOrginalPath())) {
                    String eeID = JavaRuntime.getExecutionEnvironmentId(path);
                    if (eeID != null) {
                        BuildPathSupport.setEEComplianceOptions(javaProject, eeID, newProjectCompliance);
                        newProjectCompliance = null; // don't set it again below
                    }
                }
                if (newProjectCompliance != null) {
                    Map<String, String> options = javaProject.getOptions(false);
                    JavaModelUtil.setComplianceOptions(options, newProjectCompliance);
                    JavaModelUtil.setDefaultClassfileOptions(options, newProjectCompliance); // complete compliance options
                    javaProject.setOptions(options);
                }
            }
            monitor.worked(3);
        }
        if (monitor.isCanceled()) {
            throw new OperationCanceledException();
        }
    }

    javaProject.setRawClasspath(classpath, outputLocation, new SubProgressMonitor(monitor, 2));
}

From source file:ca.mcgill.cs.swevo.ppa.ui.PPAJavaProjectHelper.java

License:Open Source License

public void createProject() throws CoreException, InterruptedException {
    try {//from  ww  w.  ja va  2s  .c  o m
        if (fCurrProject == null) {
            updateProject();
        }
        configureJavaProject();

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

From source file:com.android.ide.eclipse.adt.internal.project.ProjectHelper.java

License:Open Source License

/**
 * Makes the given project use JDK 6 (or more specifically,
 * {@link AdtConstants#COMPILER_COMPLIANCE_PREFERRED} as the compilation
 * target, regardless of what the default IDE JDK level is, provided a JRE
 * of the given level is installed./*ww  w.  ja  v a  2  s.c  o m*/
 *
 * @param javaProject the Java project
 * @throws CoreException if the IDE throws an exception setting the compiler
 *             level
 */
@SuppressWarnings("restriction") // JDT API for setting compliance options
public static void enforcePreferredCompilerCompliance(@NonNull IJavaProject javaProject) throws CoreException {
    String compliance = javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true);
    if (compliance == null || JavaModelUtil.isVersionLessThan(compliance, COMPILER_COMPLIANCE_PREFERRED)) {
        IVMInstallType[] types = JavaRuntime.getVMInstallTypes();
        for (int i = 0; i < types.length; i++) {
            IVMInstallType type = types[i];
            IVMInstall[] installs = type.getVMInstalls();
            for (int j = 0; j < installs.length; j++) {
                IVMInstall install = installs[j];
                if (install instanceof IVMInstall2) {
                    IVMInstall2 install2 = (IVMInstall2) install;
                    // Java version can be 1.6.0, and preferred is 1.6
                    if (install2.getJavaVersion().startsWith(COMPILER_COMPLIANCE_PREFERRED)) {
                        Map<String, String> options = javaProject.getOptions(false);
                        JavaCore.setComplianceOptions(COMPILER_COMPLIANCE_PREFERRED, options);
                        JavaModelUtil.setDefaultClassfileOptions(options, COMPILER_COMPLIANCE_PREFERRED);
                        javaProject.setOptions(options);
                        return;
                    }
                }
            }
        }
    }
}

From source file:com.android.ide.eclipse.adt.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   www.j  a  va  2  s  .  c  o  m*/
            } catch (CoreException e) {
                AdtPlugin.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) {
            AdtPlugin.log(e, null);
        }
    }

    return contents;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.CompilationUnit.java

License:Open Source License

protected boolean buildStructure(OpenableElementInfo info, final IProgressMonitor pm, Map newElements,
        File underlyingResource) throws JavaModelException {
    CompilationUnitElementInfo unitInfo = (CompilationUnitElementInfo) info;

    // ensure buffer is opened
    IBuffer buffer = getBufferManager().getBuffer(CompilationUnit.this);
    if (buffer == null) {
        openBuffer(pm, unitInfo); // open buffer independently from the info, since we are building the info
    }//from   w w w  . j  a  v  a  2  s  .c o  m

    // generate structure and compute syntax problems if needed
    CompilationUnitStructureRequestor requestor = new CompilationUnitStructureRequestor(this, unitInfo,
            newElements, manager);
    JavaModelManager.PerWorkingCopyInfo perWorkingCopyInfo = getPerWorkingCopyInfo();
    IJavaProject project = getJavaProject();

    boolean createAST;
    boolean resolveBindings;
    int reconcileFlags;
    HashMap problems;
    if (info instanceof ASTHolderCUInfo) {
        ASTHolderCUInfo astHolder = (ASTHolderCUInfo) info;
        createAST = astHolder.astLevel != NO_AST;
        resolveBindings = astHolder.resolveBindings;
        reconcileFlags = astHolder.reconcileFlags;
        problems = astHolder.problems;
    } else {
        createAST = false;
        resolveBindings = true;
        reconcileFlags = 0;
        problems = null;
    }
    boolean computeProblems = false;
    //   boolean computeProblems = perWorkingCopyInfo != null && perWorkingCopyInfo.isActive() && project != null && JavaProject
    //         .hasJavaNature(project.getProject());
    IProblemFactory problemFactory = new DefaultProblemFactory();
    Map options = project == null ? JavaCore.getOptions() : project.getOptions(true);
    if (!computeProblems) {
        // disable task tags checking to speed up parsing
        options.put(JavaCore.COMPILER_TASK_TAGS, ""); //$NON-NLS-1$
    }
    CompilerOptions compilerOptions = new CompilerOptions(options);
    compilerOptions.ignoreMethodBodies = (reconcileFlags & ICompilationUnit.IGNORE_METHOD_BODIES) != 0;
    SourceElementParser parser = new SourceElementParser(requestor, problemFactory, compilerOptions,
            true/*report local declarations*/,
            !createAST /*optimize string literals only if not creating a DOM AST*/);
    parser.reportOnlyOneSyntaxError = !computeProblems;
    parser.setMethodsFullRecovery(true);
    parser.setStatementsRecovery((reconcileFlags & ICompilationUnit.ENABLE_STATEMENTS_RECOVERY) != 0);

    if (!computeProblems && !resolveBindings && !createAST) // disable javadoc parsing if not computing problems, not resolving and not creating ast
        parser.javadocParser.checkDocComment = false;
    requestor.parser = parser;

    // update timestamp (might be IResource.NULL_STAMP if original does not exist)
    if (underlyingResource == null) {
        underlyingResource = resource();
    }
    // underlying resource is null in the case of a working copy on a class file in a jar
    if (underlyingResource != null)
        unitInfo.timestamp = (underlyingResource).lastModified();

    // compute other problems if needed
    CompilationUnitDeclaration compilationUnitDeclaration = null;
    CompilationUnit source = cloneCachingContents();
    try {
        if (computeProblems) {
            //         if (problems == null) {
            //            // report problems to the problem requestor
            //            problems = new HashMap();
            //            compilationUnitDeclaration = CompilationUnitProblemFinder
            //                  .process(source, parser, this.owner, problems, createAST, reconcileFlags, pm);
            //            try {
            //               perWorkingCopyInfo.beginReporting();
            //               for (Iterator iteraror = problems.values().iterator(); iteraror.hasNext();) {
            //                  CategorizedProblem[] categorizedProblems = (CategorizedProblem[]) iteraror.next();
            //                  if (categorizedProblems == null) continue;
            //                  for (int i = 0, length = categorizedProblems.length; i < length; i++) {
            //                     perWorkingCopyInfo.acceptProblem(categorizedProblems[i]);
            //                  }
            //               }
            //            } finally {
            //               perWorkingCopyInfo.endReporting();
            //            }
            //         } else {
            //            // collect problems
            //            compilationUnitDeclaration = CompilationUnitProblemFinder
            //                  .process(source, parser, this.owner, problems, createAST, reconcileFlags, pm);
            //         }
        } else {
            compilationUnitDeclaration = parser.parseCompilationUnit(source,
                    true /*full parse to find local elements*/, pm);
        }

        if (createAST) {
            //         int astLevel = ((ASTHolderCUInfo) info).astLevel;
            //         org.eclipse.jdt.core.dom.CompilationUnit cu = AST
            //               .convertCompilationUnit(astLevel, compilationUnitDeclaration, options, computeProblems, source, reconcileFlags, pm);
            //         ((ASTHolderCUInfo) info).ast = cu;
        }
    } finally {
        if (compilationUnitDeclaration != null) {
            unitInfo.hasFunctionalTypes = compilationUnitDeclaration.hasFunctionalTypes();
            compilationUnitDeclaration.cleanUp();
        }
    }

    return unitInfo.isStructureKnown();
}

From source file:com.codenvy.ide.ext.java.server.javadoc.JavadocContentAccess2.java

License:Open Source License

private static CompilationUnit createAST(IJavaElement element, String cuSource) {
    Assert.isNotNull(element);/*w  ww .j a v a 2 s  . co m*/
    ASTParser parser = ASTParser.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);
}