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

jedi.annotation.processor.model.JediMethod Maven / Gradle / Ivy

package jedi.annotation.processor.model;

import static jedi.functional.FirstOrderLogic.invert;
import static jedi.functional.FunctionalPrimitives.select;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import jedi.annotation.writer.JavaWriter;
import jedi.functional.Filter;

public class JediMethod extends AbstractAnnotateable {
	private final Set cutParameterNames;

	public JediMethod(MemberDeclaration declaration, Class annotationClass) {
		this(declaration, annotationClass, null);
	}

	public JediMethod(MemberDeclaration declaration, Class annotationClass, String factoryMethodPrefix) {
		this(declaration, annotationClass, factoryMethodPrefix, null);
	}

	public JediMethod(MemberDeclaration declaration, Class annotationClass, String cutName, Set cutParameterNames) {
		super(declaration, annotationClass, cutName);
		this.cutParameterNames = (cutParameterNames == null ? new HashSet() : cutParameterNames);
	}

	public List getUncutParameters() {
		return selectParameters(invert(createCutParameterFilter()));
	}

	public List getCutParameters() {
		return selectParameters(createCutParameterFilter());
	}

	private List selectParameters(final Filter filter) {
		return select(declaration.getParameters(), filter);
	}

	private Filter createCutParameterFilter() {
		return new Filter() {
			public Boolean execute(Attribute value) {
				return cutParameterNames.contains(value.getName());
			}
		};
	}

	@Override
	public boolean equals(Object obj) {
		return super.equals(obj) && ((JediMethod) obj).cutParameterNames.equals(cutParameterNames);
	}

	private String removeNamePrefix(String prefix) {
		return Character.toString(Character.toLowerCase(name.charAt(prefix.length()))) + name.substring(prefix.length() + 1);
	}

	private boolean isNamePrefixedWith(String prefix) {
		return name.startsWith(prefix) && name.length() > prefix.length() && Character.isUpperCase(name.charAt(prefix.length()));
	}

	public void writeInvocation(JavaWriter writer, String receiverName) {
		writer.print(receiverName + "." + getOriginalName());
		writer.printSimpleNamesAsActualParameterList(declaration.getParameters());
	}

	public String getName(boolean simplified) {
		return simplified ? getSimplifiedName() : name;
	}

	private String getSimplifiedName() {
		if (isNamePrefixedWith("get")) {
			return removeNamePrefix("get");
		}
		if (isNamePrefixedWith("is")) {
			return removeNamePrefix("is");
		}
		return name;
	}

	public void writeGenericTypeParameters(JavaWriter writer) {
		writer.print(declaration.renderGenericTypeParameters());
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy