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

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

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

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.annotation.processing.Messager;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.Name;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.SimpleElementVisitor7;
import javax.tools.Diagnostic.Kind;

import com.github.gkutiel.flip.plugin.Method.Param;

class MethodVisitor extends SimpleElementVisitor7 {

	private final Element element;

	private TypeMirror returnType;
	private Name name;
	private List params;

	private final Messager messager;

	public MethodVisitor(final Messager messager, final Element element) {
		this.messager = messager;
		this.element = element;
	}

	private void checkModifiers(final ExecutableElement e) {
		final Set modifiers = e.getModifiers();
		if (!modifiers.contains(Modifier.PUBLIC)) messager.printMessage(Kind.ERROR, "method must be public", e);
	}

	public Method getMethod() {
		return element.accept(this, null);
	}

	public String getName() {
		return name.toString();
	}

	private List params(final ExecutableElement e) {
		final List params = new ArrayList<>();
		for (final VariableElement param : e.getParameters())
			params.add(new Param(param.asType(), param.getSimpleName()));
		return params;
	}

	@Override
	public Method visitExecutable(final ExecutableElement e, final Void p) {
		checkModifiers(e);
		returnType = e.getReturnType();
		name = e.getSimpleName();
		params = params(e);
		return new Method(returnType, name, params);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy