Example usage for org.eclipse.jdt.internal.compiler.impl CompilerOptions OPTION_Source

List of usage examples for org.eclipse.jdt.internal.compiler.impl CompilerOptions OPTION_Source

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.impl CompilerOptions OPTION_Source.

Prototype

String OPTION_Source

To view the source code for org.eclipse.jdt.internal.compiler.impl CompilerOptions OPTION_Source.

Click Source Link

Usage

From source file:br.com.objectos.code.JdtSourceVersion.java

License:Apache License

public void set(ImmutableMap.Builder<String, Object> optionMap) {
    optionMap.put(CompilerOptions.OPTION_TargetPlatform, version);
    optionMap.put(CompilerOptions.OPTION_Compliance, version);
    optionMap.put(CompilerOptions.OPTION_Source, version);
}

From source file:com.bsiag.eclipse.jdt.java.formatter.CommentsPreparator.java

License:Open Source License

private DefaultCodeFormatter getCommentCodeFormatter() {
    if (this.commentCodeFormatter == null) {
        Map<String, String> options2 = this.options.getMap();
        options2.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, String.valueOf(
                this.options.comment_line_length - this.commentIndent - COMMENT_LINE_SEPARATOR_LENGTH));
        options2.put(DefaultCodeFormatterConstants.FORMATTER_LINE_SPLIT,
                String.valueOf(this.options.page_width - this.commentIndent - COMMENT_LINE_SEPARATOR_LENGTH));
        options2.put(CompilerOptions.OPTION_Source, this.sourceLevel);
        this.commentCodeFormatter = new DefaultCodeFormatter(options2);
    }/*from   w w w  . ja v  a2  s .  com*/
    return this.commentCodeFormatter;
}

From source file:com.bsiag.eclipse.jdt.java.formatter.DefaultCodeFormatter.java

License:Open Source License

private void initOptions(DefaultCodeFormatterOptions defaultCodeFormatterOptions, Map<String, String> options) {
    if (options != null) {
        this.originalOptions = new DefaultCodeFormatterOptions(options);
        this.workingOptions = new DefaultCodeFormatterOptions(options);
        this.oldCommentFormatOption = getOldCommentFormatOption(options);
        String compilerSource = options.get(CompilerOptions.OPTION_Source);
        this.sourceLevel = compilerSource != null ? compilerSource : CompilerOptions.VERSION_1_8;
    } else {// w  w  w .  j  av a 2  s. co m
        Map<String, String> settings = DefaultCodeFormatterConstants.getJavaConventionsSettings();
        this.originalOptions = new DefaultCodeFormatterOptions(settings);
        this.workingOptions = new DefaultCodeFormatterOptions(settings);
        this.oldCommentFormatOption = DefaultCodeFormatterConstants.TRUE;
        this.sourceLevel = CompilerOptions.VERSION_1_8;
    }
    if (defaultCodeFormatterOptions != null) {
        this.originalOptions.set(defaultCodeFormatterOptions.getMap());
        this.workingOptions.set(defaultCodeFormatterOptions.getMap());
    }
}

From source file:com.bsiag.eclipse.jdt.java.formatter.DefaultCodeFormatter.java

License:Open Source License

private ASTNode parseSourceCode(int kind) {
    ASTParser parser = ASTParser.newParser(AST.JLS8);
    Map<String, String> parserOptions = JavaCore.getOptions();
    parserOptions.put(CompilerOptions.OPTION_Source, this.sourceLevel);
    parser.setCompilerOptions(parserOptions);

    switch (kind & K_MASK) {
    case K_COMPILATION_UNIT:
        return parseSourceCode(parser, ASTParser.K_COMPILATION_UNIT, true);
    case K_CLASS_BODY_DECLARATIONS:
        return parseSourceCode(parser, ASTParser.K_CLASS_BODY_DECLARATIONS, false);
    case K_STATEMENTS:
        return parseSourceCode(parser, ASTParser.K_STATEMENTS, false);
    case K_EXPRESSION:
        return parseSourceCode(parser, ASTParser.K_EXPRESSION, false);
    case K_UNKNOWN:
        int[] parserModes = { ASTParser.K_COMPILATION_UNIT, ASTParser.K_EXPRESSION,
                ASTParser.K_CLASS_BODY_DECLARATIONS, ASTParser.K_STATEMENTS };
        for (int parserMode : parserModes) {
            ASTNode astNode = parseSourceCode(parser, parserMode, false);
            if (astNode != null)
                return astNode;
            parser.setCompilerOptions(parserOptions); // parser loses compiler options after every use
        }//from  w ww.j  a v  a2 s .c om
        return null;
    default:
        throw new IllegalArgumentException();
    }
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.MatchLocator.java

License:Open Source License

private boolean filterEnum(SearchMatch match) {

    // filter org.apache.commons.lang.enum package for projects above 1.5
    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=317264
    IJavaElement element = (IJavaElement) match.getElement();
    PackageFragment pkg = (PackageFragment) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
    if (pkg != null) {
        // enum was found in org.apache.commons.lang.enum at index 5
        if (pkg.names.length == 5 && pkg.names[4].equals("enum")) { //$NON-NLS-1$
            if (this.options == null) {
                IJavaProject proj = (IJavaProject) pkg.getAncestor(IJavaElement.JAVA_PROJECT);
                String complianceStr = proj.getOption(CompilerOptions.OPTION_Source, true);
                if (CompilerOptions.versionToJdkLevel(complianceStr) >= ClassFileConstants.JDK1_5)
                    return true;
            } else if (this.options.sourceLevel >= ClassFileConstants.JDK1_5) {
                return true;
            }//from w w w . ja  v a2s .  c o  m
        }
    }
    return false;
}

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

License:Open Source License

private IType createTypeFromJar(String resourcePath, int separatorIndex) throws JavaModelException {
    // path to a class file inside a jar
    // Optimization: cache package fragment root handle and package handles
    if (this.lastPkgFragmentRootPath == null || this.lastPkgFragmentRootPath.length() > resourcePath.length()
            || !resourcePath.startsWith(this.lastPkgFragmentRootPath)) {
        String jarPath = resourcePath.substring(0, separatorIndex);
        IPackageFragmentRoot root = ((AbstractJavaSearchScope) this.scope).packageFragmentRoot(resourcePath,
                separatorIndex, jarPath);
        if (root == null)
            return null;
        this.lastPkgFragmentRootPath = jarPath;
        this.lastPkgFragmentRoot = root;
        this.packageHandles = new HashtableOfArrayToObject(5);
    }//ww w .  ja  v a  2  s .co  m
    // create handle
    String classFilePath = resourcePath.substring(separatorIndex + 1);
    String[] simpleNames = new Path(classFilePath).segments();
    String[] pkgName;
    int length = simpleNames.length - 1;
    if (length > 0) {
        pkgName = new String[length];
        System.arraycopy(simpleNames, 0, pkgName, 0, length);
    } else {
        pkgName = CharOperation.NO_STRINGS;
    }
    IPackageFragment pkgFragment = (IPackageFragment) this.packageHandles.get(pkgName);
    if (pkgFragment == null) {
        pkgFragment = ((PackageFragmentRoot) this.lastPkgFragmentRoot).getPackageFragment(pkgName);
        // filter org.apache.commons.lang.enum package for projects above 1.5 
        // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=317264
        if (length == 5 && pkgName[4].equals("enum")) { //$NON-NLS-1$
            IJavaProject proj = (IJavaProject) pkgFragment.getAncestor(IJavaElement.JAVA_PROJECT);
            if (!proj.equals(this.lastProject)) {
                String complianceStr = proj.getOption(CompilerOptions.OPTION_Source, true);
                this.complianceValue = CompilerOptions.versionToJdkLevel(complianceStr);
                this.lastProject = proj;
            }
            if (this.complianceValue >= ClassFileConstants.JDK1_5)
                return null;
        }
        this.packageHandles.put(pkgName, pkgFragment);
    }
    return pkgFragment.getClassFile(simpleNames[length]).getType();
}

From source file:com.mysema.codegen.ECJEvaluatorFactory.java

License:Apache License

public static CompilerOptions getDefaultCompilerOptions() {
    String javaSpecVersion = System.getProperty("java.specification.version");
    if (javaSpecVersion.equals("1.8")) {
        javaSpecVersion = "1.7";
    }/* w w w.j  av a 2 s . c o m*/
    Map<String, Object> settings = Maps.newHashMap();
    settings.put(CompilerOptions.OPTION_Source, javaSpecVersion);
    settings.put(CompilerOptions.OPTION_TargetPlatform, javaSpecVersion);
    settings.put(CompilerOptions.OPTION_ReportDeprecation, CompilerOptions.IGNORE);
    return new CompilerOptions(settings);
}

From source file:com.tsc9526.monalisa.plugin.eclipse.jdt.JDTCompiler.java

License:Open Source License

public CompilerOptions getCompilerOptions() {
    Map<String, String> settings = new HashMap<String, String>();
    settings.put(CompilerOptions.OPTION_ReportMissingSerialVersion, CompilerOptions.IGNORE);
    settings.put(CompilerOptions.OPTION_LineNumberAttribute, CompilerOptions.GENERATE);
    settings.put(CompilerOptions.OPTION_SourceFileAttribute, CompilerOptions.GENERATE);
    settings.put(CompilerOptions.OPTION_ReportDeprecation, CompilerOptions.IGNORE);
    settings.put(CompilerOptions.OPTION_ReportUnusedImport, CompilerOptions.IGNORE);
    settings.put(CompilerOptions.OPTION_Encoding, "UTF-8");
    settings.put(CompilerOptions.OPTION_LocalVariableAttribute, CompilerOptions.GENERATE);
    String javaVersion = CompilerOptions.VERSION_1_6;

    if (System.getProperty("java.version").startsWith("1.5")) {
        javaVersion = CompilerOptions.VERSION_1_5;
    } else if (System.getProperty("java.version").startsWith("1.7")) {
        javaVersion = CompilerOptions.VERSION_1_7;
    } else if (System.getProperty("java.version").startsWith("1.8")) {
        javaVersion = CompilerOptions.VERSION_1_8;
    }//w  w w.  j ava2  s .  com

    settings.put(CompilerOptions.OPTION_Source, javaVersion);
    settings.put(CompilerOptions.OPTION_TargetPlatform, javaVersion);
    settings.put(CompilerOptions.OPTION_PreserveUnusedLocal, CompilerOptions.PRESERVE);
    settings.put(CompilerOptions.OPTION_Compliance, javaVersion);
    return new CompilerOptions(settings);
}

From source file:com.wavemaker.tools.compiler.WaveMakerJavaCompiler.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public CompilationTask getTask(Writer out, JavaFileManager fileManager,
        DiagnosticListener<? super JavaFileObject> diagnosticListener, Iterable<String> options,
        Iterable<String> classes, Iterable<? extends JavaFileObject> compilationUnits) {

    if (fileManager == null) {
        fileManager = this.getStandardFileManager(diagnosticListener, null, null);
    }//from w  w  w.j av  a  2s  .  co  m

    PrintWriter writer = createPrintWriter(out);

    EclipseBatchCompiler batchCompiler = new EclipseBatchCompiler(writer, writer, false);

    batchCompiler.options.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_6);
    batchCompiler.options.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_6);
    batchCompiler.options.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_6);
    batchCompiler.diagnosticListener = diagnosticListener;
    batchCompiler.fileManager = fileManager;
    batchCompiler.setCompilationUnits(compilationUnits);

    if (options != null) {
        for (Iterator<String> iterator = options.iterator(); iterator.hasNext();) {
            batchCompiler.fileManager.handleOption(iterator.next(), iterator);
        }
    }
    batchCompiler.configure(getArgumentsForBatchCompiler(asList(options), asList(classes)));
    if (fileManager instanceof StandardJavaFileManager) {
        Iterable<? extends File> location = ((StandardJavaFileManager) fileManager)
                .getLocation(StandardLocation.CLASS_OUTPUT);
        if (location != null) {
            batchCompiler.setDestinationPath(location.iterator().next().getAbsolutePath());
        }
    }

    return new CompilationTaskImpl(batchCompiler);
}

From source file:com.xqbase.compiler.eclipse.EclipseJavaCompiler.java

License:Open Source License

public CompilerResult performCompile(CompilerConfiguration config) throws CompilerException {

    // added by xqbase-compiler-eclipse
    if (config.getGeneratedSourcesDirectory() != null) {
        config.getGeneratedSourcesDirectory().mkdirs();
    }/*from   w  ww  .java2  s .c om*/

    List<CompilerMessage> errors = new LinkedList<CompilerMessage>();

    List<String> classpathEntries = config.getClasspathEntries();

    URL[] urls = new URL[1 + classpathEntries.size()];

    int i = 0;

    try {
        urls[i++] = new File(config.getOutputLocation()).toURL();

        for (String entry : classpathEntries) {
            urls[i++] = new File(entry).toURL();
        }
    } catch (MalformedURLException e) {
        throw new CompilerException("Error while converting the classpath entries to URLs.", e);
    }

    ClassLoader classLoader = new URLClassLoader(urls);

    SourceCodeLocator sourceCodeLocator = new SourceCodeLocator(config.getSourceLocations());

    INameEnvironment env = new EclipseCompilerINameEnvironment(sourceCodeLocator, classLoader, errors);

    IErrorHandlingPolicy policy = DefaultErrorHandlingPolicies.proceedWithAllProblems();

    // ----------------------------------------------------------------------
    // Build settings from configuration
    // ----------------------------------------------------------------------

    Map<String, String> settings = new HashMap<String, String>();

    if (config.isDebug()) {
        settings.put(CompilerOptions.OPTION_LocalVariableAttribute, CompilerOptions.GENERATE);
        settings.put(CompilerOptions.OPTION_LineNumberAttribute, CompilerOptions.GENERATE);
        settings.put(CompilerOptions.OPTION_SourceFileAttribute, CompilerOptions.GENERATE);
    }

    if (!config.isShowWarnings()) {
        Map opts = new CompilerOptions().getMap();
        for (Object optKey : opts.keySet()) {
            if (opts.get(optKey).equals(CompilerOptions.WARNING)) {
                settings.put((String) optKey, CompilerOptions.IGNORE);
            }
        }
    }

    String sourceVersion = decodeVersion(config.getSourceVersion());

    if (sourceVersion != null) {
        settings.put(CompilerOptions.OPTION_Source, sourceVersion);
    }

    String targetVersion = decodeVersion(config.getTargetVersion());

    if (targetVersion != null) {
        settings.put(CompilerOptions.OPTION_TargetPlatform, targetVersion);

        if (config.isOptimize()) {
            settings.put(CompilerOptions.OPTION_Compliance, targetVersion);
        }
    }

    if (StringUtils.isNotEmpty(config.getSourceEncoding())) {
        settings.put(CompilerOptions.OPTION_Encoding, config.getSourceEncoding());
    }

    if (config.isShowDeprecation()) {
        settings.put(CompilerOptions.OPTION_ReportDeprecation, CompilerOptions.WARNING);
    } else {
        settings.put(CompilerOptions.OPTION_ReportDeprecation, CompilerOptions.IGNORE);
    }

    // ----------------------------------------------------------------------
    // Set Eclipse-specific options
    // ----------------------------------------------------------------------

    settings.put(CompilerOptions.OPTION_LineNumberAttribute, CompilerOptions.GENERATE);
    settings.put(CompilerOptions.OPTION_SourceFileAttribute, CompilerOptions.GENERATE);

    // compiler-specific extra options override anything else in the config object...
    Map<String, String> extras = config.getCustomCompilerArguments();
    if (extras != null && !extras.isEmpty()) {
        settings.putAll(extras);
    }

    if (settings.containsKey("-properties")) {
        initializeWarnings(settings.get("-properties"), settings);
        settings.remove("-properties");
    }

    IProblemFactory problemFactory = new DefaultProblemFactory(Locale.getDefault());

    ICompilerRequestor requestor = new EclipseCompilerICompilerRequestor(config.getOutputLocation(), errors);

    List<CompilationUnit> compilationUnits = new ArrayList<CompilationUnit>();

    for (String sourceRoot : config.getSourceLocations()) {
        Set<String> sources = getSourceFilesForSourceRoot(config, sourceRoot);

        for (String source : sources) {
            CompilationUnit unit = new CompilationUnit(source, makeClassName(source, sourceRoot), errors,
                    config.getSourceEncoding());

            compilationUnits.add(unit);
        }
    }

    // ----------------------------------------------------------------------
    // Compile!
    // ----------------------------------------------------------------------

    CompilerOptions options = new CompilerOptions(settings);
    Compiler compiler = new Compiler(env, policy, options, requestor, problemFactory);

    ICompilationUnit[] units = compilationUnits.toArray(new ICompilationUnit[compilationUnits.size()]);

    compiler.compile(units);

    CompilerResult compilerResult = new CompilerResult().compilerMessages(errors);

    for (CompilerMessage compilerMessage : errors) {
        if (compilerMessage.isError()) {
            compilerResult.setSuccess(false);
            continue;
        }
    }

    return compilerResult;
}