com.github.gkutiel.flip.processor.Utils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of flip-processor Show documentation
Show all versions of flip-processor Show documentation
A new way to build web applications
package com.github.gkutiel.flip.processor;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.annotation.processing.Messager;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.SimpleElementVisitor7;
import javax.lang.model.util.SimpleTypeVisitor7;
import com.github.gkutiel.flip.processor.pub.annotation.Filter;
public enum Utils {
;
public static Set filters(final Messager messager, final Element element) {
final Set filters = new HashSet<>();
element.accept(new SimpleElementVisitor7() {
@Override
public Void visitType(final TypeElement e, final Void p) {
final List extends Element> allMembers = Processor.ELEMENTS.getAllMembers(e);
for (final Element method : allMembers) {
final Filter filter = method.getAnnotation(Filter.class);
if (filter != null) filters.add(new MethodVisitor(messager, method).getMethod());
}
return null;
}
}, null);
return filters;
}
static boolean instanceOf(final Element element, final String type) {
if (element == null) return false;
if (element.asType().toString().equals(type)) return true;
return element.accept(new SimpleElementVisitor7() {
@Override
protected Boolean defaultAction(final Element e, final Void p) {
return false;
}
@Override
public Boolean visitType(final TypeElement element, final Void p) {
final TypeMirror superclass = element.getSuperclass();
if (superclass.getKind().equals(TypeKind.NONE)) return false;
return superclass.accept(new SimpleTypeVisitor7() {
@Override
public Boolean visitDeclared(final DeclaredType t, final Void p) {
return instanceOf(t.asElement(), type);
}
}, null);
}
}, null);
}
static Set interfaces(final Element element) {
final Set interfaces = new HashSet<>();
if (element != null) element.accept(new SimpleElementVisitor7() {
@Override
public Void visitType(final TypeElement e, final Void p) {
interfaces.addAll(interfaces(e.getInterfaces()));
final TypeMirror superclass = e.getSuperclass();
if (superclass.getKind().equals(TypeKind.NONE)) return null;
return superclass.accept(new SimpleTypeVisitor7() {
@Override
public Void visitDeclared(final DeclaredType t, final Void p) {
interfaces.addAll(interfaces(t.asElement()));
return null;
}
}, null);
}
}, null);
return interfaces;
}
private static Set interfaces(final List extends TypeMirror> typeMirrors) {
final Set interfaces = new HashSet<>();
for (final TypeMirror typeMirror : typeMirrors)
interfaces.add(typeMirror.toString());
return interfaces;
}
public static boolean isImplements(final Element element, final String interfaceFullName) {
final Set interfaces = interfaces(element);
return interfaces.contains(interfaceFullName);
}
}