/*
 * Copyright 2014 - 2020 Rafael Winterhalter
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.bytebuddy.description.method;

import net.bytebuddy.description.ByteCodeElement;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import net.bytebuddy.matcher.FilterableList;

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

Implementations represent a list of method descriptions.
Type parameters:
  • <T> – The type of method descriptions represented by this list.
/** * Implementations represent a list of method descriptions. * * @param <T> The type of method descriptions represented by this list. */
public interface MethodList<T extends MethodDescription> extends FilterableList<T, MethodList<T>> {
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 TargetType.
Params:
  • matcher – A matcher that indicates type substitution.
Returns:The transformed token list.
/** * 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 net.bytebuddy.dynamic.TargetType}. * * @param matcher A matcher that indicates type substitution. * @return The transformed token list. */
ByteCodeElement.Token.TokenList<MethodDescription.Token> asTokenList(ElementMatcher<? super TypeDescription> matcher);
Returns a list of signature tokens for this list of methods.
Returns:A list of signature tokens for this list of methods.
/** * Returns a list of signature tokens for this list of methods. * * @return A list of signature tokens for this list of methods. */
List<MethodDescription.SignatureToken> asSignatureTokenList();
Returns a list of signature tokens for this list of methods.
Params:
  • matcher – A matcher for resolving methods to Tokens.
  • typeDescription – The type description to resolve the SignatureTokens to.
Returns:A list of signature tokens for this list of methods.
/** * Returns a list of signature tokens for this list of methods. * * @param matcher A matcher for resolving methods to {@link net.bytebuddy.description.method.MethodDescription.Token}s. * @param typeDescription The type description to resolve the {@link net.bytebuddy.description.method.MethodDescription.SignatureToken}s to. * @return A list of signature tokens for this list of methods. */
List<MethodDescription.SignatureToken> asSignatureTokenList(ElementMatcher<? super TypeDescription> matcher, TypeDescription typeDescription);
Returns this list of these method descriptions resolved to their defined shape.
Returns:A list of methods in their defined shape.
/** * Returns this list of these method descriptions resolved to their defined shape. * * @return A list of methods in their defined shape. */
MethodList<MethodDescription.InDefinedShape> asDefined();
A base implementation of a MethodList.
Type parameters:
  • <S> – The type of method descriptions represented by this list.
/** * A base implementation of a {@link MethodList}. * * @param <S> The type of method descriptions represented by this list. */
abstract class AbstractBase<S extends MethodDescription> extends FilterableList.AbstractBase<S, MethodList<S>> implements MethodList<S> { @Override protected MethodList<S> wrap(List<S> values) { return new Explicit<S>(values); }
{@inheritDoc}
/** * {@inheritDoc} */
public ByteCodeElement.Token.TokenList<MethodDescription.Token> asTokenList(ElementMatcher<? super TypeDescription> matcher) { List<MethodDescription.Token> tokens = new ArrayList<MethodDescription.Token>(size()); for (MethodDescription methodDescription : this) { tokens.add(methodDescription.asToken(matcher)); } return new ByteCodeElement.Token.TokenList<MethodDescription.Token>(tokens); }
{@inheritDoc}
/** * {@inheritDoc} */
public List<MethodDescription.SignatureToken> asSignatureTokenList() { List<MethodDescription.SignatureToken> tokens = new ArrayList<MethodDescription.SignatureToken>(size()); for (MethodDescription methodDescription : this) { tokens.add(methodDescription.asSignatureToken()); } return tokens; }
{@inheritDoc}
/** * {@inheritDoc} */
public List<MethodDescription.SignatureToken> asSignatureTokenList(ElementMatcher<? super TypeDescription> matcher, TypeDescription typeDescription) { List<MethodDescription.SignatureToken> tokens = new ArrayList<MethodDescription.SignatureToken>(size()); for (MethodDescription methodDescription : this) { tokens.add(methodDescription.asToken(matcher).asSignatureToken(typeDescription)); } return tokens; }
{@inheritDoc}
/** * {@inheritDoc} */
public MethodList<MethodDescription.InDefinedShape> asDefined() { List<MethodDescription.InDefinedShape> declaredForms = new ArrayList<MethodDescription.InDefinedShape>(size()); for (MethodDescription methodDescription : this) { declaredForms.add(methodDescription.asDefined()); } return new Explicit<MethodDescription.InDefinedShape>(declaredForms); } }
A method list implementation that returns all loaded byte code methods (methods and constructors) that are declared for a given type.
/** * A method list implementation that returns all loaded byte code methods (methods and constructors) that * are declared for a given type. */
class ForLoadedMethods extends AbstractBase<MethodDescription.InDefinedShape> {
The loaded methods that are represented by this method list.
/** * The loaded methods that are represented by this method list. */
private final List<? extends Method> methods;
The loaded constructors that are represented by this method list.
/** * The loaded constructors that are represented by this method list. */
private final List<? extends Constructor<?>> constructors;
Creates a new list for a loaded type. Method descriptions are created on demand.
Params:
  • type – The type to be represented by this method list.
/** * 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 ForLoadedMethods(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.
Params:
  • constructor – The constructors to be represented by the method list.
  • method – The methods to be represented by the method list.
/** * 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 ForLoadedMethods(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.
Params:
  • constructors – The constructors to be represented by the method list.
  • methods – The methods to be represented by the method list.
/** * 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 ForLoadedMethods(List<? extends Constructor<?>> constructors, List<? extends Method> methods) { this.constructors = constructors; this.methods = methods; }
{@inheritDoc}
/** * {@inheritDoc} */
public MethodDescription.InDefinedShape get(int index) { return index < constructors.size() ? new MethodDescription.ForLoadedConstructor(constructors.get(index)) : new MethodDescription.ForLoadedMethod(methods.get(index - constructors.size())); }
{@inheritDoc}
/** * {@inheritDoc} */
public int size() { return constructors.size() + methods.size(); } }
A method list that is a wrapper for a given list of method descriptions.
Type parameters:
  • <S> – The type of method descriptions represented by this list.
/** * A method list that is a wrapper for a given list of method descriptions. * * @param <S> The type of method descriptions represented by this list. */
class Explicit<S extends MethodDescription> extends AbstractBase<S> {
The list of methods that is represented by this method list.
/** * The list of methods that is represented by this method list. */
private final List<? extends S> methodDescriptions;
Creates a new wrapper for a given list of methods.
Params:
  • methodDescription – The underlying list of methods used for this method list.
/** * 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.
Params:
  • methodDescriptions – The underlying list of methods used for this method list.
/** * 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<? extends S> methodDescriptions) { this.methodDescriptions = methodDescriptions; }
{@inheritDoc}
/** * {@inheritDoc} */
public S get(int index) { return methodDescriptions.get(index); }
{@inheritDoc}
/** * {@inheritDoc} */
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.
/** * 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<MethodDescription.InDefinedShape> {
The method's declaring type.
/** * The method's declaring type. */
private final TypeDescription declaringType;
The list of method tokens to represent.
/** * The list of method tokens to represent. */
private final List<? extends MethodDescription.Token> tokens;
Creates a new list of method descriptions for a list of detached tokens.
Params:
  • declaringType – The method's declaring type.
  • token – The list of method tokens to represent.
/** * 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.
Params:
  • declaringType – The method's declaring type.
  • tokens – The list of method tokens to represent.
/** * 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<? extends MethodDescription.Token> tokens) { this.declaringType = declaringType; this.tokens = tokens; }
{@inheritDoc}
/** * {@inheritDoc} */
public MethodDescription.InDefinedShape get(int index) { return new MethodDescription.Latent(declaringType, tokens.get(index)); }
{@inheritDoc}
/** * {@inheritDoc} */
public int size() { return tokens.size(); } }
A list of method descriptions that yields TypeSubstituting.
/** * A list of method descriptions that yields {@link net.bytebuddy.description.method.MethodDescription.TypeSubstituting}. */
class TypeSubstituting extends AbstractBase<MethodDescription.InGenericShape> {
The methods' declaring type.
/** * The methods' declaring type. */
protected final TypeDescription.Generic declaringType;
The list of method descriptions to represent.
/** * The list of method descriptions to represent. */
protected final List<? extends MethodDescription> methodDescriptions;
The visitor to apply to each method description before returning it.
/** * The visitor to apply to each method description before returning it. */
protected final TypeDescription.Generic.Visitor<? extends TypeDescription.Generic> visitor;
Creates a new type substituting method list.
Params:
  • declaringType – The methods' declaring type.
  • methodDescriptions – The list of method descriptions to represent.
  • visitor – The visitor to apply to each method description before returning it.
/** * 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<? extends MethodDescription> methodDescriptions, TypeDescription.Generic.Visitor<? extends TypeDescription.Generic> visitor) { this.declaringType = declaringType; this.methodDescriptions = methodDescriptions; this.visitor = visitor; }
{@inheritDoc}
/** * {@inheritDoc} */
public MethodDescription.InGenericShape get(int index) { return new MethodDescription.TypeSubstituting(declaringType, methodDescriptions.get(index), visitor); }
{@inheritDoc}
/** * {@inheritDoc} */
public int size() { return methodDescriptions.size(); } }
An implementation of an empty method list.
Type parameters:
  • <S> – The type of parameter descriptions represented by this list.
/** * An implementation of an empty method list. * * @param <S> The type of parameter descriptions represented by this list. */
class Empty<S extends MethodDescription> extends FilterableList.Empty<S, MethodList<S>> implements MethodList<S> {
{@inheritDoc}
/** * {@inheritDoc} */
public ByteCodeElement.Token.TokenList<MethodDescription.Token> asTokenList(ElementMatcher<? super TypeDescription> matcher) { return new ByteCodeElement.Token.TokenList<MethodDescription.Token>(); }
{@inheritDoc}
/** * {@inheritDoc} */
public List<MethodDescription.SignatureToken> asSignatureTokenList() { return Collections.<MethodDescription.SignatureToken>emptyList(); }
{@inheritDoc}
/** * {@inheritDoc} */
public List<MethodDescription.SignatureToken> asSignatureTokenList(ElementMatcher<? super TypeDescription> matcher, TypeDescription typeDescription) { return Collections.<MethodDescription.SignatureToken>emptyList(); }
{@inheritDoc}
/** * {@inheritDoc} */
@SuppressWarnings("unchecked") public MethodList<MethodDescription.InDefinedShape> asDefined() { return (MethodList<MethodDescription.InDefinedShape>) this; } } }