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

com.slimgears.apt.data.MethodInfo Maven / Gradle / Ivy

There is a newer version: 0.7.58
Show newest version
package com.slimgears.apt.data;

import com.google.auto.value.AutoValue;

import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.ExecutableType;
import javax.lang.model.type.TypeMirror;
import java.util.List;
import java.util.stream.IntStream;

@AutoValue
public abstract class MethodInfo implements HasName, HasEnclosingType, HasAnnotations, HasParameters, HasTypeParameters {
    public abstract TypeInfo returnType();

    public boolean hasReturnType() {
        return !TypeInfo.of(void.class).equals(returnType());
    }

    public static Builder builder() {
        return new AutoValue_MethodInfo.Builder();
    }

    public static MethodInfo of(ExecutableElement executableElement) {
        Builder builder = builder()
                .name(executableElement.getSimpleName().toString())
                .paramsFromMethod(executableElement)
                .typeParamsFromMethod(executableElement)
                .returnType(TypeInfo.of(executableElement.getReturnType()));

        return builder.build();
    }

    public static MethodInfo create(ExecutableElement element, DeclaredType containingType) {
        ExecutableType executableType = (ExecutableType)Environment.instance().types().asMemberOf(containingType, element);
        Builder builder = builder()
                .name(element.getSimpleName().toString())
                .annotationsFromElement(element)
                .typeParamsFromMethod(executableType)
                .returnType(TypeInfo.of(executableType.getReturnType()));

        List paramElements = element.getParameters();
        List paramTypes = executableType.getParameterTypes();

        IntStream.range(0, paramElements.size())
                .mapToObj(i -> ParamInfo
                        .builder()
                        .name(paramElements.get(i).getSimpleName().toString())
                        .type(TypeInfo.of(paramTypes.get(i)))
                        .annotationsFromElement(paramElements.get(i))
                        .build())
                .forEach(builder::addParam);

        return builder.build();
    }

    @AutoValue.Builder
    public interface Builder extends
            InfoBuilder,
            HasName.Builder,
            HasEnclosingType.Builder,
            HasParameters.Builder,
            HasAnnotations.Builder,
            HasTypeParameters.Builder {
        Builder returnType(TypeInfo type);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy