Example usage for org.eclipse.jdt.core JavaCore COMPILER_COMPLIANCE

List of usage examples for org.eclipse.jdt.core JavaCore COMPILER_COMPLIANCE

Introduction

In this page you can find the example usage for org.eclipse.jdt.core JavaCore COMPILER_COMPLIANCE.

Prototype

String COMPILER_COMPLIANCE

To view the source code for org.eclipse.jdt.core JavaCore COMPILER_COMPLIANCE.

Click Source Link

Document

Compiler option ID: Setting Compliance Level.

Usage

From source file:at.bestsolution.efxclipse.tooling.jdt.ui.internal.handler.AbstractAntHandler.java

License:Open Source License

protected BuildConfiguration prepareBuild(IFile f, AntTask task) {
    final IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    BuildConfiguration config = new BuildConfiguration();
    config.task = task;//w  w w. j  a v  a2s  .  c  o  m
    project = JavaCore.create(f.getProject());

    IPath[] paths = BuildPathSupport.getFxJarPath(project);
    if (paths != null) {
        config.jfxjar = paths[0].toFile().getAbsolutePath();
        config.jfxantjar = paths[2].toFile().getAbsolutePath();
    }

    if (task.getBuildDirectory() == null) {
        DirectoryDialog dialog = new DirectoryDialog(
                PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
        dialog.setText("Staging Directory");
        String directory = dialog.open();
        if (directory == null) {
            return null;
        }
        config.buildDirectory = directory;
    } else {
        config.buildDirectory = resolvePath(task.getBuildDirectory(), project.getProject());
    }

    // TODO uncomment
    // config.builderName = properties.getProperty( "jfx.eclipse.buildername" );
    if (task.getDeploy().getApplication().getName() != null) {
        config.projectName = task.getDeploy().getApplication().getName();
    } else {
        config.projectName = f.getProject().getName();
    }
    config.keyStore = task.getSignjar().getKeystore() != null
            ? resolvePath(task.getSignjar().getKeystore(), project.getProject())
            : null;

    try {
        config.projectEncoding = f.getProject().getDefaultCharset();
        config.sourceCompliance = project.getOption(JavaCore.COMPILER_SOURCE, true);
        config.targetCompliance = project.getOption(JavaCore.COMPILER_COMPLIANCE, true);
    } catch (CoreException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Set<IPath> listProjectSourceDirs = new HashSet<IPath>();
    Set<IPath> listRefProjectSourceDirs = new HashSet<IPath>();
    Set<IPath> listRefLibraries = new HashSet<IPath>();

    resolveDataProject(project, listProjectSourceDirs, listRefProjectSourceDirs, listRefLibraries);

    {
        Set<String> set = new HashSet<String>();
        Set<File> set2 = new HashSet<File>();
        for (IPath p : listRefLibraries) {
            set.add(p.lastSegment());
            IFile file = root.getFile(p);
            if (file != null && file.exists()) {
                p = file.getLocation();
            }
            set2.add(p.toFile());
        }
        config.externalLibs = set;
        config.origExternalLibs = set2;
    }

    {
        Set<String> set = new HashSet<String>();
        Set<SetupDirectory> set2 = new HashSet<SetupDirectory>();
        for (IPath p : listProjectSourceDirs) {
            IFolder t = root.getFolder(p);
            set.add(t.getProjectRelativePath().toString());
            set2.add(new SetupDirectory(t.getProject().getLocation().toFile(),
                    new File(t.getProjectRelativePath().toString())));
        }
        config.projectSourceDirs = set;
        config.origProjectSourceDirs = set2;
    }

    {
        Set<String> set = new HashSet<String>();
        Set<SetupDirectory> set2 = new HashSet<SetupDirectory>();
        for (IPath p : listRefProjectSourceDirs) {
            IFolder t = root.getFolder(p);
            set.add(t.getProject().getName() + "/" + t.getProjectRelativePath());
            set2.add(new SetupDirectory(t.getProject().getLocation().toFile().getParentFile(),
                    new File(t.getProject().getName() + "/" + t.getProjectRelativePath().toString())));

        }
        config.projectRefSourceDirs = set;
        config.origProjectRefSourceDirs = set2;
    }
    return config;
}

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

License:Open Source License

public static Map<String, String> getEEOptions(IExecutionEnvironment ee) {
    if (ee == null)
        return null;
    Map<String, String> eeOptions = ee.getComplianceOptions();
    if (eeOptions == null)
        return null;

    Object complianceOption = eeOptions.get(JavaCore.COMPILER_COMPLIANCE);
    if (!(complianceOption instanceof String))
        return null;

    // eeOptions can miss some options, make sure they are complete:
    HashMap<String, String> options = new HashMap<String, String>();
    JavaModelUtil.setComplianceOptions(options, (String) complianceOption);
    options.putAll(eeOptions);/*from   w  w w  .j av  a 2s.  c o  m*/
    return options;
}

From source file:br.uff.ic.gems.resources.ast.ASTExtractor.java

public void parser() throws IOException {
    ASTParser parser = ASTParser.newParser(AST.JLS3);
    File file = new File(filePath);

    String stringFile = FileUtils.readFileToString(file);
    parser.setSource(stringFile.toCharArray());
    parser.setKind(ASTParser.K_COMPILATION_UNIT);

    //Setting options
    Map options;//from   w ww  .j  ava 2s .  c  om
    options = JavaCore.getOptions();
    options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_7);
    options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_7);
    options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_7);
    parser.setCompilerOptions(options);

    final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
    Visitor visitor = new Visitor(cu);
    cu.accept(visitor);

    List<Comment> commentList = cu.getCommentList();

    for (Comment comment : commentList) {
        comment.accept(visitor);
    }

    languageConstructs = visitor.getLanguageConstructs();

}

From source file:br.uff.ic.mergeguider.javaparser.JavaParser.java

private Storage generateASTs(String path) {
    ASTParser parser = ASTParser.newParser(AST.JLS8);

    parser.setResolveBindings(true);/*from  w  ww  .j  av a 2 s .  c o  m*/
    parser.setBindingsRecovery(true);

    Map options = JavaCore.getOptions();

    options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_8);
    options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_8);
    options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_8);
    parser.setCompilerOptions(options);

    String[] srcDirs = FileUtils.getAllDirs(path);
    String[] javaFiles = FileUtils.getAllJavaFiles(path);

    parser.setEnvironment(null, srcDirs, null, true);

    Storage storage = new Storage();
    parser.createASTs(javaFiles, null, new String[0], storage, null);
    return storage;
}

From source file:ch.acanda.eclipse.pmd.java.resolution.ASTQuickFixTestCase.java

License:Open Source License

private CompilationUnit createAST(final org.eclipse.jface.text.Document document,
        final ASTQuickFix<ASTNode> quickFix) {
    final ASTParser astParser = ASTParser.newParser(AST.JLS4);
    astParser.setSource(document.get().toCharArray());
    astParser.setKind(ASTParser.K_COMPILATION_UNIT);
    astParser.setResolveBindings(quickFix.needsTypeResolution());
    astParser.setEnvironment(new String[0], new String[0], new String[0], true);
    final String name = last(params.pmdReferenceId.or("QuickFixTest").split("/"));
    astParser.setUnitName(format("{0}.java", name));
    final String version = last(params.language.or("java 1.7").split("\\s+"));
    astParser.setCompilerOptions(ImmutableMap.<String, String>builder().put(JavaCore.COMPILER_SOURCE, version)
            .put(JavaCore.COMPILER_COMPLIANCE, version).put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, version)
            .build());//from www .j  a  v  a  2s  .c o m
    final CompilationUnit ast = (CompilationUnit) astParser.createAST(null);
    ast.recordModifications();
    return ast;
}

From source file:ch.acanda.eclipse.pmd.java.resolution.PMDMarkerResolutionGenerator.java

License:Open Source License

private Version getCompilerCompliance(final IMarker marker) {
    final IJavaProject project = JavaCore.create(marker.getResource().getProject());
    final String compilerCompliance = project.getOption(JavaCore.COMPILER_COMPLIANCE, true);
    return new Version(compilerCompliance);
}

From source file:chibi.gumtreediff.gen.jdt.AbstractJdtTreeGenerator.java

License:Open Source License

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public TreeContext generate(Reader r) throws IOException {
    ASTParser parser = ASTParser.newParser(AST.JLS8);
    parser.setKind(ASTParser.K_COMPILATION_UNIT);
    Map pOptions = JavaCore.getOptions();
    pOptions.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_8);
    pOptions.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_8);
    pOptions.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_8);
    pOptions.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED);
    parser.setCompilerOptions(pOptions);
    parser.setSource(readerToCharArray(r));
    AbstractJdtVisitor v = createVisitor();
    CompilationUnit c = (CompilationUnit) parser.createAST(null);
    v.setTreeRoot(c);/*w w  w. jav  a2  s  . c  om*/
    c.accept(v);
    return v.getTreeContext();
}

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

License:Open Source License

/**
 * Checks the project compiler compliance level is supported.
 * @param javaProject The project to check
 * @return A pair with the first integer being an error code, and the second value
 *   being the invalid value found or null. The error code can be: <ul>
 * <li><code>COMPILER_COMPLIANCE_OK</code> if the project is properly configured</li>
 * <li><code>COMPILER_COMPLIANCE_LEVEL</code> for unsupported compiler level</li>
 * <li><code>COMPILER_COMPLIANCE_SOURCE</code> for unsupported source compatibility</li>
 * <li><code>COMPILER_COMPLIANCE_CODEGEN_TARGET</code> for unsupported .class format</li>
 * </ul>//from  ww w  .j a v a  2 s  .  c  o m
 */
public static final Pair<Integer, String> checkCompilerCompliance(IJavaProject javaProject) {
    // get the project compliance level option
    String compliance = javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true);

    // check it against a list of valid compliance level strings.
    if (!checkCompliance(javaProject, compliance)) {
        // if we didn't find the proper compliance level, we return an error
        return Pair.of(COMPILER_COMPLIANCE_LEVEL, compliance);
    }

    // otherwise we check source compatibility
    String source = javaProject.getOption(JavaCore.COMPILER_SOURCE, true);

    // check it against a list of valid compliance level strings.
    if (!checkCompliance(javaProject, source)) {
        // if we didn't find the proper compliance level, we return an error
        return Pair.of(COMPILER_COMPLIANCE_SOURCE, source);
    }

    // otherwise check codegen level
    String codeGen = javaProject.getOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, true);

    // check it against a list of valid compliance level strings.
    if (!checkCompliance(javaProject, codeGen)) {
        // if we didn't find the proper compliance level, we return an error
        return Pair.of(COMPILER_COMPLIANCE_CODEGEN_TARGET, codeGen);
    }

    return Pair.of(COMPILER_COMPLIANCE_OK, null);
}

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

License:Open Source License

/**
 * Checks, and fixes if needed, the compiler compliance level, and the source compatibility
 * level/* w  w  w .  ja v  a2 s .  com*/
 * @param javaProject The Java project to check and fix.
 */
public static final void checkAndFixCompilerCompliance(IJavaProject javaProject) {
    Pair<Integer, String> result = checkCompilerCompliance(javaProject);
    if (result.getFirst().intValue() != COMPILER_COMPLIANCE_OK) {
        // setup the preferred compiler compliance level.
        javaProject.setOption(JavaCore.COMPILER_COMPLIANCE, AdtConstants.COMPILER_COMPLIANCE_PREFERRED);
        javaProject.setOption(JavaCore.COMPILER_SOURCE, AdtConstants.COMPILER_COMPLIANCE_PREFERRED);
        javaProject.setOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM,
                AdtConstants.COMPILER_COMPLIANCE_PREFERRED);

        // clean the project to make sure we recompile
        try {
            javaProject.getProject().build(IncrementalProjectBuilder.CLEAN_BUILD, new NullProgressMonitor());
        } catch (CoreException e) {
            AdtPlugin.printErrorToConsole(javaProject.getProject(),
                    "Project compiler settings changed. Clean your project.");
        }
    }
}

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.//from   ww  w .j av a  2  s.co  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;
                    }
                }
            }
        }
    }
}