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

com.slimgears.apt.util.JavaUtils Maven / Gradle / Ivy

There is a newer version: 0.7.58
Show newest version
/**
 *
 */
package com.slimgears.apt.util;

import com.slimgears.apt.data.Environment;
import com.slimgears.apt.data.TypeInfo;
import com.slimgears.util.generic.ScopedInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.processing.ProcessingEnvironment;
import javax.tools.JavaFileObject;
import java.io.IOException;
import java.io.Writer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class JavaUtils extends TemplateUtils {
    private final static Logger log = LoggerFactory.getLogger(JavaUtils.class);
    public static Function imports(ImportTracker importTracker) {
        ScopedInstance.Closeable closeable = ImportTracker.withTracker(importTracker);
        return evaluator -> evaluator
                .variable("imports", importTracker)
                .postProcess(TemplateUtils.postProcessImports(importTracker))
                .postProcess(code -> addImports(importTracker, code, imp -> "import " + imp + ";"))
                .postProcess(code -> { closeable.close(); return code; });
    }

    public static Consumer fileWriter(ProcessingEnvironment environment, TypeInfo targetClass) {
        return code -> writeJavaFile(environment, targetClass, code);
    }

    private static String addImports(ImportTracker importTracker, String code, Function importSubstitutor) {
        String importsStr = Stream.of(importTracker.imports())
                .map(importSubstitutor)
                .collect(Collectors.joining("\n"));
        return code.replace(importTracker.toString(), importsStr);
    }

    private static void writeJavaFile(ProcessingEnvironment environment, TypeInfo targetClass, String code) {
        try {
            log.debug("Writing class: {}", targetClass.name());
            LogUtils.dumpContent(code);
            code = FileUtils.lineEndingsNormalizer().apply(code.trim() + "\n");
            JavaFileObject sourceFile = environment.getFiler().createSourceFile(targetClass.name());
            try (Writer writer = sourceFile.openWriter()) {
                writer.write(code);
                Environment.instance()
                        .fileListener()
                        .onFileWrite(targetClass.simpleName() + ".java", code);
            }
        } catch (IOException e) {
            log.error(code, e);
            throw new RuntimeException(e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy