public CompilationResult compile()

in src/main/java/org/apache/sling/commons/compiler/impl/EclipseJavaCompiler.java [161:237]


    public CompilationResult compile(final CompilationUnit[] units,
                                     final Options compileOptions) {
        // make sure we have an options object (to avoid null checks all over the place)
        final Options options = (compileOptions != null ? compileOptions : EMPTY_OPTIONS);

        // get classloader and classloader writer
        final ClassLoaderWriter writer = this.getClassLoaderWriter(options);
        if ( writer == null ) {
            return new CompilationResultImpl("Class loader writer for compilation is not available.");
        }
        final ClassLoader loader = this.getClassLoader(options, writer);
        if ( loader == null ) {
            return new CompilationResultImpl("Class loader for compilation is not available.");
        }

        // check sources for compilation
        boolean needsCompilation = isForceCompilation(options);
        if ( !needsCompilation ) {
            for(final CompilationUnit unit : units) {
                if ( this.isOutDated(unit, writer) ) {
                    needsCompilation = true;
                    break;
                }
            }
        }
        if ( !needsCompilation ) {
            logger.debug("All source files are recent - no compilation required.");
            return new CompilationResultImpl(writer);
        }

        // delete old class files
        for(final CompilationUnit unit : units) {
            final String name = '/' + unit.getMainClassName().replace('.', '/') + ".class";
            writer.delete(name);
        }

        // create properties for the settings object
        final Map<String, String> props = new HashMap<>();
        if (options.isGenerateDebugInfo()) {
            props.put(CompilerOptions.OPTION_LocalVariableAttribute, "generate");
            props.put(CompilerOptions.OPTION_LineNumberAttribute, "generate");
            props.put(CompilerOptions.OPTION_SourceFileAttribute, "generate");
        }
        if (options.getSourceVersion() != null) {
            props.put(CompilerOptions.OPTION_Source, adjustJavaVersion(options.getSourceVersion()));
            props.put(CompilerOptions.OPTION_Compliance, adjustJavaVersion(options.getSourceVersion()));
        }
        if (options.getTargetVersion() != null) {
            props.put(CompilerOptions.OPTION_TargetPlatform, adjustJavaVersion(options.getTargetVersion()));
        }
        props.put(CompilerOptions.OPTION_Encoding, "UTF8");

        // create the settings
        final CompilerOptions settings = new CompilerOptions(props);
        logger.debug("Compiling with settings {}.", settings);

        // create the result
        final CompilationResultImpl result = new CompilationResultImpl(isIgnoreWarnings(options), writer);
        // create the context
        final CompileContext context = new CompileContext(units, result, writer, loader);

        // create the compiler
        final org.eclipse.jdt.internal.compiler.Compiler compiler =
                new org.eclipse.jdt.internal.compiler.Compiler(
                        context,
                        this.policy,
                        settings,
                        context,
                        this.problemFactory,
                        null,
                        null);

        // compile
        compiler.compile(context.getSourceUnits());

        return result;
    }