com.github.gkutiel.flip.plugin.Processor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of flip-apt Show documentation
Show all versions of flip-apt Show documentation
A new way to build web applications
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, cmd;
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");
cmd = env.getFiler().createSourceFile("com.github.gkutie.flip.web.Cmd");
messager = env.getMessager();
ELEMENTS = env.getElementUtils();
} catch (final IOException e) {
throw new RuntimeException(e);
}
}
@Override
public boolean process(final Set extends TypeElement> annotations, final RoundEnvironment env) {
try {
this.env = env;
Activator.info("processing... ");
if (annotations.isEmpty()) return true;
try (final Writer flipOut = flip.openWriter(); final Writer cmdOut = cmd.openWriter()) {
final List commands = commands();
flipOut.write(Template.flip(servlets(commands)));
cmdOut.write(Template.cmds(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 CommandMethod cm : cms)
cmds.add(Template.cmd(cm));
return cmds;
}
private List commands() {
final List commands = new ArrayList<>();
for (final Element method : env.getElementsAnnotatedWith(Command.class)) {
Activator.info("processing... " + method.getSimpleName().toString());
final Element clazz = method.getEnclosingElement();
System.out.println(Utils.filters(messager, clazz));
//@formatter:off
commands.add(new CommandMethod(
clazz.asType(),
Utils.isImplements(clazz, "com.github.gkutiel.flip.Fliplet"),
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;
}
private List servlets(final List commands) {
final List servlets = new ArrayList<>();
for (final CommandMethod command : commands)
servlets.add(Template.command(command));
return servlets;
}
}