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

com.github.eduardovalentim.easymath.processor.AbstractAnnotationProcessor Maven / Gradle / Ivy

There is a newer version: 0.1.17
Show newest version
package com.github.eduardovalentim.easymath.processor;

import java.io.IOException;
import java.io.Writer;
import java.util.Properties;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Filer;
import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.SourceVersion;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
import javax.tools.JavaFileObject;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * The abstract processor
 * 
 * @author eduardovalentim
 */
public abstract class AbstractAnnotationProcessor extends AbstractProcessor {

	private static final Logger LOGGER = LoggerFactory.getLogger(AbstractAnnotationProcessor.class);

	public static final String ENCODING = "UTF-8";

	protected Filer filer;
	protected Messager messager;
	protected Types types;
	protected Elements elements;

	/**
	 * Public default constructor
	 */
	public AbstractAnnotationProcessor() {
		super();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public SourceVersion getSupportedSourceVersion() {
		LOGGER.trace("Entering... ");
		SourceVersion sv = SourceVersion.latestSupported();

		LOGGER.trace("Exiting... ");
		return sv;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public synchronized void init(ProcessingEnvironment processingEnv) {
		LOGGER.trace("Entering...  ");

		super.init(processingEnv);

		Velocity.init(getVelocityProperties());

		types = processingEnv.getTypeUtils();
		elements = processingEnv.getElementUtils();
		filer = processingEnv.getFiler();
		messager = processingEnv.getMessager();

		LOGGER.trace("Exiting...  ");
	}

	protected void generate(CharSequence name, Template template, VelocityContext context) {
		LOGGER.trace("Entering...");
		try {
			JavaFileObject jfo = filer.createSourceFile(name);
			try (Writer writer = jfo.openWriter()) {
				LOGGER.debug("Merging objects to template...");
				template.merge(context, writer);
				LOGGER.debug("Merging finished!");
			}
		} catch (IOException ex) {
			throw new IllegalStateException(ex);
		}
		LOGGER.trace("Exiting...");
	}

	private Properties getVelocityProperties() {
		LOGGER.trace("Entering...");

		Properties properties = new Properties();
		properties.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
		properties.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());

		LOGGER.trace("Exiting...");
		return properties;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy