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

com.github.gkutiel.flip.plugin.Processor Maven / Gradle / Ivy

The newest version!
package com.github.gkutiel.flip.plugin;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.MirroredTypeException;
import javax.lang.model.util.Elements;
import javax.tools.JavaFileObject;

import com.github.gkutiel.flip.apt.Cmd;
import com.github.gkutiel.flip.apt.Page;
import com.github.gkutiel.flip.apt.Path;
import com.github.gkutiel.flip.res.Xml;

@SupportedAnnotationTypes("com.github.gkutiel.flip.apt.*")
@SupportedSourceVersion(SourceVersion.RELEASE_7)
public class Processor extends AbstractProcessor {

	private JavaFileObject flip, to;
	private Messager messager;
	private RoundEnvironment env;

	static Elements ELEMENTS;

	public String className(final Cmd cmd) {
		try {
			return cmd.response().getCanonicalName();
		} catch (final MirroredTypeException e) {
			return e.getTypeMirror().toString();
		}
	}
	@Override
	public synchronized void init(final ProcessingEnvironment env) {
		try {
			flip = env.getFiler().createSourceFile("com.github.gkutie.flip.web.Flip");
			to = env.getFiler().createSourceFile("com.github.gkutie.flip.web.To");
			messager = env.getMessager();
			ELEMENTS = env.getElementUtils();
		} catch (final IOException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public boolean process(final Set annotations, final RoundEnvironment env) {
		try {
			this.env = env;
			Activator.info("processing... ");
			if (annotations.isEmpty()) return true;
			try (final Writer flipOut = flip.openWriter(); final Writer toOut = to.openWriter()) {
				final List commands = commands();
				commands.addAll(pages());

				flipOut.write(Template.flip(servlets(commands)));
				toOut.write(Template.tos(cmds(commands)));
			}
		} catch (final Exception e) {
			throw new RuntimeException(e);
		}

		return true;
	}

	private List cmds(final List cms) {
		final List cmds = new ArrayList<>();

		for (final CmdMethod cm : cms)
			cmds.add(Template.cmd(cm));

		return cmds;
	}

	private List commands() {
		final List commands = new ArrayList<>();

		for (final Element method : env.getElementsAnnotatedWith(Cmd.class)) {
			Activator.info("processing... " + method.getSimpleName().toString());
			final Element clazz = method.getEnclosingElement();

			final Cmd cmd = method.getAnnotation(Cmd.class);
			//@formatter:off
			commands.add(new CmdMethod(
				clazz.asType(), 
				Utils.isImplements(clazz, "com.github.gkutiel.flip.Fliplet"),
				Utils.filters(messager, clazz),
				new MethodVisitor(messager, method).getMethod(), 
				clazz.getAnnotation(Path.class),
				cmd.args(),
				cmd.mime(),
				cmd.path(),
				className(cmd)
			));
			//@formatter:on
		}

		return commands;
	}

	private Collection pages() {
		final List pages = new ArrayList<>();

		for (final Element method : env.getElementsAnnotatedWith(Page.class)) {
			Activator.info("processing... " + method.getSimpleName().toString());
			final Element clazz = method.getEnclosingElement();

			final Page page = method.getAnnotation(Page.class);
			// @formatter:off
			pages.add(new CmdMethod(
				clazz.asType(), 
				Utils.isImplements(clazz, "com.github.gkutiel.flip.Fliplet"),
				Utils.filters(messager, clazz),
				new MethodVisitor(messager, method).getMethod(), 
				clazz.getAnnotation(Path.class),
				new String[] {page.xsl()},
				null,
				page.path(),
				Xml.class.getCanonicalName()
			));
			//@formatter:on
		}

		return pages;
	}

	private List servlets(final List commands) {
		final List servlets = new ArrayList<>();

		for (final CmdMethod command : commands)
			servlets.add(Template.command(command));

		return servlets;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy