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

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

Introduction

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

Prototype

public static void setComplianceOptions(String compliance, Map options) 

Source Link

Document

Sets the default compiler options inside the given options map according to the given compliance.

Usage

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

License:Open Source License

public static void setComplianceOptions(Map<String, String> map, String compliance) {
    JavaCore.setComplianceOptions(compliance, map);
}

From source file:boa.datagen.scm.AbstractCommit.java

License:Apache License

private boolean parseJavaScriptFile(final String path, final ChangedFile.Builder fb, final String content,
        final String compliance, final int astLevel, final boolean storeOnError, Writer astWriter, String key) {
    try {// w  ww.  j  av a2s . c o  m
        //System.out.println("parsing=" + (++count) + "\t" + path);
        final org.eclipse.wst.jsdt.core.dom.ASTParser parser = org.eclipse.wst.jsdt.core.dom.ASTParser
                .newParser(astLevel);
        parser.setKind(ASTParser.K_COMPILATION_UNIT);
        parser.setResolveBindings(true);
        parser.setSource(content.toCharArray());

        final Map options = JavaCore.getOptions();
        JavaCore.setComplianceOptions(compliance, options);
        parser.setCompilerOptions(options);

        JavaScriptUnit cu;
        try {
            cu = (JavaScriptUnit) parser.createAST(null);
        } catch (java.lang.IllegalArgumentException ex) {
            return false;
        }

        final JavaScriptErrorCheckVisitor errorCheck = new JavaScriptErrorCheckVisitor();
        cu.accept(errorCheck);

        if (!errorCheck.hasError || storeOnError) {
            final ASTRoot.Builder ast = ASTRoot.newBuilder();
            // final CommentsRoot.Builder comments =
            // CommentsRoot.newBuilder();
            final JavaScriptVisitor visitor = new JavaScriptVisitor(content);
            try {
                ast.addNamespaces(visitor.getNamespaces(cu));
                // for (final String s : visitor.getImports())
                // ast.addImports(s);
                /*
                 * for (final Comment c : visitor.getComments())
                 * comments.addComments(c);
                 */
            } catch (final UnsupportedOperationException e) {
                return false;
            } catch (final Exception e) {
                if (debug)
                    System.err.println("Error visiting: " + path);
                //e.printStackTrace();
                return false;
            }

            if (astWriter != null) {
                try {
                    //   System.out.println("writing=" + count + "\t" + path);
                    astWriter.append(new Text(key), new BytesWritable(ast.build().toByteArray()));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else
                fb.setAst(ast);
            // fb.setComments(comments);
        }

        return !errorCheck.hasError;
    } catch (final Exception e) {
        e.printStackTrace();
        return false;
    }
}

From source file:boa.datagen.scm.AbstractCommit.java

License:Apache License

private boolean parseJavaFile(final String path, final ChangedFile.Builder fb, final String content,
        final String compliance, final int astLevel, final boolean storeOnError, Writer astWriter, String key) {
    try {//from w  ww .ja  v  a2s. c om
        final ASTParser parser = ASTParser.newParser(astLevel);
        parser.setKind(ASTParser.K_COMPILATION_UNIT);
        parser.setResolveBindings(true);
        parser.setSource(content.toCharArray());

        final Map options = JavaCore.getOptions();
        JavaCore.setComplianceOptions(compliance, options);
        parser.setCompilerOptions(options);

        final CompilationUnit cu = (CompilationUnit) parser.createAST(null);

        final JavaErrorCheckVisitor errorCheck = new JavaErrorCheckVisitor();
        cu.accept(errorCheck);

        if (!errorCheck.hasError || storeOnError) {
            final ASTRoot.Builder ast = ASTRoot.newBuilder();
            //final CommentsRoot.Builder comments = CommentsRoot.newBuilder();
            final JavaVisitor visitor = new JavaVisitor(content, connector.nameIndices);
            try {
                ast.addNamespaces(visitor.getNamespaces(cu));
                for (final String s : visitor.getImports())
                    ast.addImports(s);
                /*for (final Comment c : visitor.getComments())
                   comments.addComments(c);*/
            } catch (final UnsupportedOperationException e) {
                return false;
            } catch (final Exception e) {
                if (debug)
                    System.err.println("Error visiting: " + path);
                e.printStackTrace();
                return false;
            }

            if (astWriter != null) {
                try {
                    astWriter.append(new Text(key), new BytesWritable(ast.build().toByteArray()));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else
                fb.setAst(ast);
            //fb.setComments(comments);
        }

        return !errorCheck.hasError;
    } catch (final Exception e) {
        e.printStackTrace();
        return false;
    }
}

From source file:boa.test.datagen.Java7BaseTest.java

License:Apache License

protected static String parseJava(final String content) {
    final ASTParser parser = ASTParser.newParser(astLevel);
    parser.setKind(ASTParser.K_COMPILATION_UNIT);
    parser.setSource(content.toCharArray());

    final Map options = JavaCore.getOptions();
    JavaCore.setComplianceOptions(javaVersion, options);
    parser.setCompilerOptions(options);// w  w w .  ja v  a2s . c o  m

    final CompilationUnit cu = (CompilationUnit) parser.createAST(null);

    final ASTRoot.Builder ast = ASTRoot.newBuilder();
    try {
        ast.addNamespaces(visitor.getNamespaces(cu));
        for (final String s : visitor.getImports())
            ast.addImports(s);
    } catch (final Exception e) {
        System.err.println(e);
        e.printStackTrace();
        return "";
    }

    return JsonFormat.printToString(ast.build());
}

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  a  va2  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.facebook.buck.java.JavaFileParser.java

License:Apache License

private CompilationUnit makeCompilationUnitFromSource(String code) {
    ASTParser parser = ASTParser.newParser(jlsLevel);
    parser.setSource(code.toCharArray());
    parser.setKind(ASTParser.K_COMPILATION_UNIT);

    @SuppressWarnings("unchecked")
    Map<String, String> options = JavaCore.getOptions();
    JavaCore.setComplianceOptions(javaVersion, options);
    parser.setCompilerOptions(options);//from  w  w w  .j  ava 2 s. c o  m

    return (CompilationUnit) parser.createAST(/* monitor */ null);
}

From source file:com.facebook.buck.jvm.java.JavaFileParser.java

License:Apache License

private CompilationUnit makeCompilationUnitFromSource(String code) {
    ASTParser parser = ASTParser.newParser(jlsLevel);
    parser.setSource(code.toCharArray());
    parser.setKind(ASTParser.K_COMPILATION_UNIT);

    Map<String, String> options = JavaCore.getOptions();
    JavaCore.setComplianceOptions(javaVersion, options);
    parser.setCompilerOptions(options);/*from ww  w .jav  a2 s. co m*/

    return (CompilationUnit) parser.createAST(/* monitor */ null);
}

From source file:com.facebook.nuclide.debugger.EvaluationManager.java

License:Open Source License

private CompilationUnit compileSource(String source, String sourceFilePath, String unitName,
        StringBuilder errors) throws Exception {

    if (!unitName.endsWith(".java")) {
        // The AST compiler is surprisingly insistent about this.
        unitName += ".java";
    }//from  w w  w  . j a  v  a2s .co  m

    final File sourceFile = new File(sourceFilePath);
    final String directoryPath = sourceFile.getAbsoluteFile().getParent();
    Utils.logVerbose("compiling source for: " + directoryPath);
    final String[] sources = _contextManager.getSourceLocator().getSourceSearchPaths();
    final String[] classpath = _contextManager.getSourceLocator().getBinaryJarPaths();

    final ASTParser parser = ASTParser.newParser(AST.JLS8);
    final Map<String, String> options = JavaCore.getOptions();

    JavaCore.setComplianceOptions(JavaCore.VERSION_1_8, options);
    parser.setCompilerOptions(options);
    parser.setUnitName(unitName);
    parser.setResolveBindings(true);
    parser.setStatementsRecovery(true);
    parser.setBindingsRecovery(true);
    parser.setEnvironment(classpath, sources, null, true);
    parser.setSource(source.toCharArray());

    CompilationUnit unit = (CompilationUnit) parser.createAST(null);
    String errorMsg = checkUnitForProblems(unit);
    if (errorMsg != null) {
        errors.append(errorMsg);
    }

    return unit;
}

From source file:com.facebook.nuclide.shim.EclipseJavaElementShim.java

License:Open Source License

@Override
public ISourceRange getNameRange() throws JavaModelException {
    if (_sourceRange != null) {
        return _sourceRange;
    }/*from   w w w  . j  a v a2 s .c o m*/

    // We need to determine the location in the source at which this type
    // is declared. Compute an AST from the source file, and fine the declaring
    // node.
    ICompilationUnit unit = getCompilationUnit();
    ASTParser parser = ASTParser.newParser(AST.JLS8);
    Map<String, String> options = JavaCore.getOptions();
    JavaCore.setComplianceOptions(JavaCore.VERSION_1_8, options);
    parser.setCompilerOptions(options);

    try {
        parser.setSource(unit.getSource().toCharArray());
    } catch (Exception ex) {
        return new SourceRange(0, 0);
    }

    final String targetName = fixTargetName(_refType.name());
    CompilationUnit ast = (CompilationUnit) parser.createAST(null);
    ast.accept(new ASTVisitor(false) {
        public boolean visit(TypeDeclaration node) {
            SimpleName name = getNodeDeclName(node);
            if (name.getFullyQualifiedName().equals(targetName)) {
                _sourceRange = new SourceRange(name.getStartPosition(), name.getLength());

                // No need to continue processing, we've found what we're looking for.
                return false;
            }
            return true;
        }
    });

    if (_sourceRange != null) {
        return _sourceRange;
    }

    // Can't return null or the source generator will die.
    return new SourceRange(0, 1);
}

From source file:com.facebook.nuclide.shim.EclipseJavaProjectShim.java

License:Open Source License

@Override
public Map getOptions(boolean inheritJavaCoreOptions) {
    final Map<String, String> options = JavaCore.getOptions();
    JavaCore.setComplianceOptions(JavaCore.VERSION_1_8, options);
    return options;
}