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

com.fitbur.mockito.bytebuddy.description.method.MethodList Maven / Gradle / Ivy

The newest version!
package com.fitbur.mockito.bytebuddy.description.method;

import com.fitbur.mockito.bytebuddy.description.ByteCodeElement;
import com.fitbur.mockito.bytebuddy.description.type.TypeDescription;
import com.fitbur.mockito.bytebuddy.matcher.ElementMatcher;
import com.fitbur.mockito.bytebuddy.matcher.FilterableList;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Implementations represent a list of method descriptions.
 *
 * @param  The type of method descriptions represented by this list.
 */
public interface MethodList extends FilterableList> {

    /**
     * Transforms the list of method descriptions into a list of detached tokens. All types that are matched by the provided
     * target type matcher are substituted by {@link com.fitbur.mockito.bytebuddy.dynamic.TargetType}.
     *
     * @param matcher A matcher that indicates type substitution.
     * @return The transformed token list.
     */
    ByteCodeElement.Token.TokenList asTokenList(ElementMatcher matcher);

    /**
     * Returns this list of these method descriptions resolved to their defined shape.
     *
     * @return A list of methods in their defined shape.
     */
    MethodList asDefined();

    /**
     * A base implementation of a {@link MethodList}.
     *
     * @param  The type of method descriptions represented by this list.
     */
    abstract class AbstractBase extends FilterableList.AbstractBase> implements MethodList {

        @Override
        protected MethodList wrap(List values) {
            return new Explicit(values);
        }

        @Override
        public ByteCodeElement.Token.TokenList asTokenList(ElementMatcher matcher) {
            List tokens = new ArrayList(size());
            for (MethodDescription fieldDescription : this) {
                tokens.add(fieldDescription.asToken(matcher));
            }
            return new ByteCodeElement.Token.TokenList(tokens);
        }

        @Override
        public MethodList asDefined() {
            List declaredForms = new ArrayList(size());
            for (MethodDescription methodDescription : this) {
                declaredForms.add(methodDescription.asDefined());
            }
            return new Explicit(declaredForms);
        }
    }

    /**
     * A method list implementation that returns all loaded byte code methods (methods and constructors) that
     * are declared for a given type.
     */
    class ForLoadedType extends AbstractBase {

        /**
         * The loaded methods that are represented by this method list.
         */
        private final List methods;

        /**
         * The loaded constructors that are represented by this method list.
         */
        private final List> constructors;

        /**
         * Creates a new list for a loaded type. Method descriptions are created on demand.
         *
         * @param type The type to be represented by this method list.
         */
        public ForLoadedType(Class type) {
            this(type.getDeclaredConstructors(), type.getDeclaredMethods());
        }

        /**
         * Creates a method list that represents the given constructors and methods in their given order. The
         * constructors are assigned the indices before the methods.
         *
         * @param constructor The constructors to be represented by the method list.
         * @param method      The methods to be represented by the method list.
         */
        public ForLoadedType(Constructor[] constructor, Method[] method) {
            this(Arrays.asList(constructor), Arrays.asList(method));
        }

        /**
         * Creates a method list that represents the given constructors and methods in their given order. The
         * constructors are assigned the indices before the methods.
         *
         * @param constructors The constructors to be represented by the method list.
         * @param methods      The methods to be represented by the method list.
         */
        public ForLoadedType(List> constructors, List methods) {
            this.constructors = constructors;
            this.methods = methods;
        }

        @Override
        public MethodDescription.InDefinedShape get(int index) {
            return index < constructors.size()
                    ? new MethodDescription.ForLoadedConstructor(constructors.get(index))
                    : new MethodDescription.ForLoadedMethod(methods.get(index - constructors.size()));

        }

        @Override
        public int size() {
            return constructors.size() + methods.size();
        }
    }

    /**
     * A method list that is a wrapper for a given list of method descriptions.
     *
     * @param  The type of method descriptions represented by this list.
     */
    class Explicit extends AbstractBase {

        /**
         * The list of methods that is represented by this method list.
         */
        private final List methodDescriptions;

        /**
         * Creates a new wrapper for a given list of methods.
         *
         * @param methodDescription The underlying list of methods used for this method list.
         */
        @SuppressWarnings("unchecked")
        public Explicit(S... methodDescription) {
            this(Arrays.asList(methodDescription));
        }

        /**
         * Creates a new wrapper for a given list of methods.
         *
         * @param methodDescriptions The underlying list of methods used for this method list.
         */
        public Explicit(List methodDescriptions) {
            this.methodDescriptions = methodDescriptions;
        }

        @Override
        public S get(int index) {
            return methodDescriptions.get(index);
        }

        @Override
        public int size() {
            return methodDescriptions.size();
        }
    }

    /**
     * A list of method descriptions for a list of detached tokens. For the returned method, each token is attached to its method representation.
     */
    class ForTokens extends AbstractBase {

        /**
         * The method's declaring type.
         */
        private final TypeDescription declaringType;

        /**
         * The list of method tokens to represent.
         */
        private final List tokens;

        /**
         * Creates a new list of method descriptions for a list of detached tokens.
         *
         * @param declaringType The method's declaring type.
         * @param token         The list of method tokens to represent.
         */
        public ForTokens(TypeDescription declaringType, MethodDescription.Token... token) {
            this(declaringType, Arrays.asList(token));
        }

        /**
         * Creates a new list of method descriptions for a list of detached tokens.
         *
         * @param declaringType The method's declaring type.
         * @param tokens        The list of method tokens to represent.
         */
        public ForTokens(TypeDescription declaringType, List tokens) {
            this.declaringType = declaringType;
            this.tokens = tokens;
        }

        @Override
        public MethodDescription.InDefinedShape get(int index) {
            return new MethodDescription.Latent(declaringType, tokens.get(index));
        }

        @Override
        public int size() {
            return tokens.size();
        }
    }

    /**
     * A list of method descriptions that yields {@link com.fitbur.mockito.bytebuddy.description.method.MethodDescription.TypeSubstituting}.
     */
    class TypeSubstituting extends AbstractBase {

        /**
         * The methods' declaring type.
         */
        private final TypeDescription.Generic declaringType;

        /**
         * The list of method descriptions to represent.
         */
        private final List methodDescriptions;

        /**
         * The visitor to apply to each method description before returning it.
         */
        private final TypeDescription.Generic.Visitor visitor;

        /**
         * Creates a new type substituting method list.
         *
         * @param declaringType      The methods' declaring type.
         * @param methodDescriptions The list of method descriptions to represent.
         * @param visitor            The visitor to apply to each method description before returning it.
         */
        public TypeSubstituting(TypeDescription.Generic declaringType,
                                List methodDescriptions,
                                TypeDescription.Generic.Visitor visitor) {
            this.declaringType = declaringType;
            this.methodDescriptions = methodDescriptions;
            this.visitor = visitor;
        }

        @Override
        public MethodDescription.InGenericShape get(int index) {
            return new MethodDescription.TypeSubstituting(declaringType, methodDescriptions.get(index), visitor);
        }

        @Override
        public int size() {
            return methodDescriptions.size();
        }
    }

    /**
     * An implementation of an empty method list.
     *
     * @param  The type of parameter descriptions represented by this list.
     */
    class Empty extends FilterableList.Empty> implements MethodList {

        @Override
        public ByteCodeElement.Token.TokenList asTokenList(ElementMatcher matcher) {
            return new ByteCodeElement.Token.TokenList();
        }

        @Override
        @SuppressWarnings("unchecked")
        public MethodList asDefined() {
            return (MethodList) this;
        }
    }
}