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

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

There is a newer version: 5-RELEASE
Show newest version
package com.github.gkutiel.flip.plugin;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
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.util.Elements;
import javax.tools.JavaFileObject;

import com.github.gkutiel.flip.apt.Command;
import com.github.gkutiel.flip.apt.Path;
import com.github.gkutiel.flip.apt.Response;

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

	private JavaFileObject flip;

	private Messager messager;

	private RoundEnvironment env;

	static Elements ELEMENTS;

	@Override
	public synchronized void init(final ProcessingEnvironment env) {
		try {
			flip = env.getFiler().createSourceFile("com.github.gkutie.flip.web.Flip");
			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... " + annotations);
			if (annotations.isEmpty()) return true;
			try (final Writer out = flip.openWriter()) {
				out.write(Template.flip(commands()));
			}
		} catch (final Exception e) {
			throw new RuntimeException(e);
		}

		return true;
	}

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

		for (final Element method : env.getElementsAnnotatedWith(Command.class)) {
			final Element clazz = method.getEnclosingElement();

			System.out.println(Utils.filters(messager, clazz));

			//@formatter:off
			commands.add(Template.command(
				clazz.asType(), 
				Utils.isImplements(clazz, "com.github.gkutiel.flip.processor.pub.Flipper"),
				Utils.filters(messager, clazz),
				new MethodVisitor(messager, method).getMethod(), 
				clazz.getAnnotation(Path.class),
				method.getAnnotation(Command.class),
				method.getAnnotation(Response.class)
			));
			//@formatter:on
		}

		return commands;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy