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

guru.mocker.annotation.mixin.processor.helper.PoetHelper Maven / Gradle / Ivy

The newest version!
package guru.mocker.annotation.mixin.processor.helper;

import com.squareup.javapoet.*;

import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeParameterElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.util.Types;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

public class PoetHelper
{
    private PoetHelper()
    {
        throw new UnsupportedOperationException("Not intended to create an instance of this class.");
    }

    /**
     * The method is copied as it would be the member of the required enclosing class or interface. It means the type
     * parameters of the method and the parameters of the method are resolved upon this information. If the method
     * could not be a member of the enclosing class or interface then it will be copied without type parameter resolution.
     *
     * @param method to be copied
     * @param enclosing the required enclosing class or interface
     * @param types types helper tool
     * @return the method specification builder
     */
    public static MethodSpec.Builder copyMethod(ExecutableElement method, DeclaredType enclosing, Types types)
    {
        MethodSpec.Builder result = MethodSpec.overriding(method, enclosing, types);
        return removeOverrideAnnotation(result);
    }

    private static MethodSpec.Builder removeOverrideAnnotation(MethodSpec.Builder result)
    {
        return removeAnnotation(result, "java.lang.Override");
    }

    private static MethodSpec.Builder removeAnnotation(MethodSpec.Builder builder, String canonicalName)
    {
        MethodSpec.Builder result = builder.build().toBuilder();
        for (Iterator iterator = result.annotations.iterator(); iterator.hasNext(); ) {
            removeNextIfNameIs(canonicalName, iterator);
        }
        return result;
    }

    private static void removeNextIfNameIs(String canonicalName, Iterator iterator)
    {
        ClassName annotation = (ClassName) iterator.next().type;
        if (annotation.canonicalName().equals(canonicalName)) {
            iterator.remove();
        }
    }

    public static MethodSpec.Builder copyMethod(ExecutableElement method)
    {
        MethodSpec.Builder result = MethodSpec.overriding(method);
        return removeOverrideAnnotation(result);
    }

    public static AnnotationSpec buildAnnotationGenerated()
    {
        AnnotationSpec.Builder result = AnnotationSpec.builder(javax.annotation.Generated.class);
        result.addMember("value", "$S", "guru.mocker.annotation.mixin.processor.MixinProcessor");
        result.addMember("date", "$S", LocalDateTime.now());
        return result.build();
    }

    public static String buildMethodCall(ExecutableElement method, String instanceName)
    {
        return instanceName + "." + method.getSimpleName().toString() + "(" + createParametersForMethodCall(method) + ");";
    }

    static String createParametersForMethodCall(ExecutableElement method)
    {
        return parametersOf(method).stream().map(parameter -> parameter.name).collect(Collectors.joining(", "));
    }

    static List parametersOf(ExecutableElement method)
    {
        List result = new ArrayList<>();
        for (VariableElement parameter : method.getParameters()) {
            result.add(ParameterSpec.get(parameter));
        }
        return result;
    }

    public static Iterable asTypeVariableNames(List typeParameterElements)
    {
        return typeParameterElements.stream().map(TypeVariableName::get).collect(Collectors.toList());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy