All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.objectos.code.testing.CompilationTaskDsl Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2014-2020 Objectos Software LTDA.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package br.com.objectos.code.testing;

import static br.com.objectos.comuns.collections.Factory.emptyImmutableList;
import static br.com.objectos.comuns.collections.Factory.emptyImmutableSet;
import static br.com.objectos.comuns.collections.Factory.newGrowableList;
import static br.com.objectos.comuns.lang.Preconditions.checkNotNull;

import br.com.objectos.comuns.collections.GrowableList;
import java.io.StringWriter;
import java.util.Map;
import javax.annotation.processing.Processor;
import javax.tools.JavaCompiler;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.JavaFileObject;
import javax.tools.ToolProvider;

public class CompilationTaskDsl extends Compilation.Builder {

  private static final String LINE_SEPARATOR = System.getProperty("line.separator");

  private final JavaCompiler compiler;
  private final CompilationJavaFileManager fileManager;
  private final GrowableList javaFiles = newGrowableList();
  private final GrowableList processors = newGrowableList();
  private final StringWriter stdout = new StringWriter();

  private CompilationTaskDsl(JavaCompiler compiler) {
    this.compiler = compiler;
    fileManager = new CompilationJavaFileManager(compiler);
  }

  static CompilationTaskDsl withSystemJavaCompiler() {
    return new CompilationTaskDsl(ToolProvider.getSystemJavaCompiler());
  }

  public final Compilation compile() {
    return build();
  }

  public final CompilationTaskDsl withCompilationUnit(
      String qualifiedName, String source) {
    checkNotNull(qualifiedName, "qualifiedName == null");
    checkNotNull(source, "source == null");
    javaFiles.add(new StringJavaFileObject(qualifiedName, source));
    return this;
  }

  public final CompilationTaskDsl withCompilationUnit(
      String qualifiedName, String... lines) {
    checkNotNull(qualifiedName, "qualifiedName == null");
    checkNotNull(lines, "lines == null");
    return withCompilationUnit(qualifiedName, String.join(LINE_SEPARATOR, lines));
  }

  public final CompilationTaskDsl withProcessor(Processor processor) {
    processors.addWithNullMessage(processor, "processor == null");
    return this;
  }

  @Override
  final boolean successful() {
    CompilationTask task = compiler.getTask(
        stdout,
        fileManager,
        null,
        emptyImmutableSet(),
        emptyImmutableList(),
        javaFiles);
    task.setProcessors(processors);
    return task.call();
  }

  @Override
  final String message() {
    return stdout.toString();
  }

  @Override
  final Map generatedClassFiles() {
    return fileManager.generatedClassFiles;
  }

  @Override
  final Map generatedJavaFiles() {
    return fileManager.generatedJavaFiles;
  }

  @Override
  final Map generatedResources() {
    return fileManager.generatedResources;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy