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

com.fitbur.mockito.bytebuddy.description.ByteCodeElement Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
package com.fitbur.mockito.bytebuddy.description;

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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Implementations describe an element represented in byte code, i.e. a type, a field or a method or a constructor.
 */
public interface ByteCodeElement extends NamedElement.WithRuntimeName, ModifierReviewable, DeclaredByType, AnnotatedCodeElement {

    /**
     * The generic type signature of a non-generic byte code element.
     */
    String NON_GENERIC_SIGNATURE = null;

    /**
     * Returns the descriptor of this byte code element.
     *
     * @return The descriptor of this byte code element.
     */
    String getDescriptor();

    /**
     * Returns the generic signature of this byte code element. If this element does not reference generic types
     * or references malformed generic types, {@code null} is returned as a signature.
     *
     * @return The generic signature or {@code null} if this element is not generic.
     */
    String getGenericSignature();

    /**
     * 

* Checks if this element is visible from a given type. *

*

* Note: A method or field might define a signature that includes types that are not visible to a type. Such methods can be * legally invoked from this type and can even be implemented as bridge methods by this type. It is however not legal to declare * a method with invisible types in its signature that are not bridges what might require additional validation. *

*

* Important: Virtual byte code elements, i.e. virtual methods, are only considered visible if the type they are invoked upon * is visible to a given type. The visibility of such virtual members can therefore not be determined by only investigating the invoked * method but requires an additional check of the target type. *

* * @param typeDescription The type which is checked for its access of this element. * @return {@code true} if this element is visible for {@code typeDescription}. */ boolean isVisibleTo(TypeDescription typeDescription); /** * A type dependant describes an element that is an extension of a type definition, i.e. a field, method or method parameter. * * @param The type dependant's type. * @param The type dependant's token type. */ interface TypeDependant, S extends ByteCodeElement.Token> { /** * Returns this type dependant in its defined shape, i.e. the form it is declared in and without its type variable's resolved. * * @return This type dependant in its defined shape. */ T asDefined(); /** * Returns a token representative of this type dependant. All types that are matched by the supplied matcher are replaced by * {@link com.fitbur.mockito.bytebuddy.dynamic.TargetType} descriptions. * * @param matcher A matcher to identify types to be replaced by {@link com.fitbur.mockito.bytebuddy.dynamic.TargetType} descriptions. * @return A token representative of this type dependant. */ S asToken(ElementMatcher matcher); } /** * A token representing a byte code element. * * @param The type of the implementation. */ interface Token> { /** * Transforms the types represented by this token by applying the given visitor to them. * * @param visitor The visitor to transform all types that are represented by this token. * @return This token with all of its represented types transformed by the supplied visitor. */ T accept(TypeDescription.Generic.Visitor visitor); /** * A list of tokens. * * @param The actual token type. */ class TokenList> extends FilterableList.AbstractBase> { /** * The tokens that this list represents. */ private final List tokens; /** * Creates a list of tokens. * * @param token The tokens that this list represents. */ @SuppressWarnings("unchecked") public TokenList(S... token) { this(Arrays.asList(token)); } /** * Creates a list of tokens. * * @param tokens The tokens that this list represents. */ public TokenList(List tokens) { this.tokens = tokens; } /** * Transforms all tokens that are represented by this list. * * @param visitor The visitor to apply to all tokens. * @return A list containing the transformed tokens. */ public TokenList accept(TypeDescription.Generic.Visitor visitor) { List tokens = new ArrayList(this.tokens.size()); for (S token : this.tokens) { tokens.add(token.accept(visitor)); } return new TokenList(tokens); } @Override protected TokenList wrap(List values) { return new TokenList(values); } @Override public S get(int index) { return tokens.get(index); } @Override public int size() { return tokens.size(); } } } }