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

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

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

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.apt.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 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;
	}

	public static boolean isImplements(final Element element, final String interfaceFullName) {
		final Set interfaces = interfaces(element);
		return interfaces.contains(interfaceFullName);
	}

	private static Set interfaces(final List typeMirrors) {
		final Set interfaces = new HashSet<>();

		for (final TypeMirror typeMirror : typeMirrors)
			interfaces.add(typeMirror.toString());

		return interfaces;
	}

	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
			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);
			}

			@Override
			protected Boolean defaultAction(final Element e, final Void p) {
				return false;
			}
		}, 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;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy