Example usage for org.eclipse.jdt.internal.compiler.apt.util EclipseFileManager EclipseFileManager

List of usage examples for org.eclipse.jdt.internal.compiler.apt.util EclipseFileManager EclipseFileManager

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.apt.util EclipseFileManager EclipseFileManager.

Prototype

public EclipseFileManager(Locale locale, Charset charset) 

Source Link

Usage

From source file:io.gige.compiler.EclipseCompiler.java

License:Apache License

protected StandardJavaFileManager newFileManager(Locale locale, Charset charset) {
    final String OS = "os.name";
    String current = System.getProperty(OS);
    try {/* ww  w .  ja va 2s.co m*/
        System.setProperty(OS, "xxx");
        return new EclipseFileManager(locale, charset) {
            @Override
            public void close() throws IOException {
                super.close();
                filesystem.cleanup();
                provided = null;
                filesystem = null;
            }
        };
    } finally {
        System.setProperty(OS, current);
    }
}

From source file:io.takari.maven.plugins.compile.jdt.CompilerJdt.java

License:Open Source License

@Override
public int compile() throws MojoExecutionException, IOException {
    Map<String, String> args = new HashMap<String, String>();
    // XXX figure out how to reuse source/target check from jdt
    // org.eclipse.jdt.internal.compiler.batch.Main.validateOptions(boolean)
    args.put(CompilerOptions.OPTION_TargetPlatform, getTarget()); // support 5/6/7 aliases
    args.put(CompilerOptions.OPTION_Compliance, getTarget()); // support 5/6/7 aliases
    args.put(CompilerOptions.OPTION_Source, getSource()); // support 5/6/7 aliases
    args.put(CompilerOptions.OPTION_ReportForbiddenReference, CompilerOptions.ERROR);
    Set<Debug> debug = getDebug();
    if (debug == null || debug.contains(Debug.all)) {
        args.put(CompilerOptions.OPTION_LocalVariableAttribute, CompilerOptions.GENERATE);
        args.put(CompilerOptions.OPTION_LineNumberAttribute, CompilerOptions.GENERATE);
        args.put(CompilerOptions.OPTION_SourceFileAttribute, CompilerOptions.GENERATE);
    } else if (debug.contains(Debug.none)) {
        args.put(CompilerOptions.OPTION_LocalVariableAttribute, CompilerOptions.DO_NOT_GENERATE);
        args.put(CompilerOptions.OPTION_LineNumberAttribute, CompilerOptions.DO_NOT_GENERATE);
        args.put(CompilerOptions.OPTION_SourceFileAttribute, CompilerOptions.DO_NOT_GENERATE);
    } else {/*w  w  w  . ja v  a2 s.c  om*/
        args.put(CompilerOptions.OPTION_LocalVariableAttribute, CompilerOptions.DO_NOT_GENERATE);
        args.put(CompilerOptions.OPTION_LineNumberAttribute, CompilerOptions.DO_NOT_GENERATE);
        args.put(CompilerOptions.OPTION_SourceFileAttribute, CompilerOptions.DO_NOT_GENERATE);
        for (Debug keyword : debug) {
            switch (keyword) {
            case lines:
                args.put(CompilerOptions.OPTION_LineNumberAttribute, CompilerOptions.GENERATE);
                break;
            case source:
                args.put(CompilerOptions.OPTION_SourceFileAttribute, CompilerOptions.GENERATE);
                break;
            case vars:
                args.put(CompilerOptions.OPTION_LocalVariableAttribute, CompilerOptions.GENERATE);
                break;
            default:
                throw new IllegalArgumentException();
            }
        }
    }

    class _CompilerOptions extends CompilerOptions {
        public void setShowWarnings(boolean showWarnings) {
            if (showWarnings) {
                warningThreshold = IrritantSet.COMPILER_DEFAULT_WARNINGS;
            } else {
                warningThreshold = new IrritantSet(0);
            }
        }
    }
    _CompilerOptions compilerOptions = new _CompilerOptions();

    compilerOptions.set(args);
    compilerOptions.performMethodsFullRecovery = false;
    compilerOptions.performStatementsRecovery = false;
    compilerOptions.verbose = isVerbose();
    compilerOptions.suppressWarnings = true;
    compilerOptions.setShowWarnings(isShowWarnings());
    compilerOptions.docCommentSupport = true;

    if (isProcEscalate() && strategy instanceof IncrementalCompilationStrategy) {
        strategy.enqueueAllSources();
        strategy = new FullCompilationStrategy();
    }

    Classpath namingEnvironment = strategy.createClasspath();
    IErrorHandlingPolicy errorHandlingPolicy = DefaultErrorHandlingPolicies.exitAfterAllProblems();
    IProblemFactory problemFactory = ProblemFactory.getProblemFactory(Locale.getDefault());
    Compiler compiler = new Compiler(namingEnvironment, errorHandlingPolicy, compilerOptions, this,
            problemFactory);
    compiler.options.produceReferenceInfo = true;

    EclipseFileManager fileManager = null;
    try {
        if (!isProcNone()) {
            fileManager = new EclipseFileManager(null, getSourceEncoding());
            fileManager.setLocation(StandardLocation.ANNOTATION_PROCESSOR_PATH, dependencies);
            fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Collections.singleton(getOutputDirectory()));
            fileManager.setLocation(StandardLocation.SOURCE_OUTPUT,
                    Collections.singleton(getGeneratedSourcesDirectory()));

            ProcessingEnvImpl processingEnv = new ProcessingEnvImpl(context, fileManager,
                    getAnnotationProcessorOptions(), compiler, this);

            compiler.annotationProcessorManager = new AnnotationProcessorManager(processingEnv, fileManager,
                    getAnnotationProcessors());
            compiler.options.storeAnnotations = true;
        }

        return strategy.compile(namingEnvironment, compiler);
    } finally {
        if (fileManager != null) {
            fileManager.flush();
            fileManager.close();
        }
    }
}