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

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

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

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.processor.pub.annotation.Command;
import com.github.gkutiel.flip.processor.pub.annotation.Response;

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

	private JavaFileObject flip;

	private Messager messager;

	private RoundEnvironment env;

	static Elements ELEMENTS;

	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(), 
				method.getAnnotation(Command.class),
				method.getAnnotation(Response.class)
			));
			//@formatter:on
		}

		return commands;
	}

	@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...");
			if (annotations.isEmpty()) return true;
			try (final Writer out = flip.openWriter()) {
				System.out.println("processing...");
				out.write(Template.flip(commands()));
			}
		} catch (final Exception e) {
			throw new RuntimeException(e);
		}

		return true;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy