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

net.bytebuddy.description.field.FieldList Maven / Gradle / Ivy

Go to download

Byte Buddy is a Java library for creating Java classes at run time. This artifact is a build of Byte Buddy with a remaining dependency onto ASM. You should never depend on this module without repackaging Byte Buddy and ASM into your own namespace.

There is a newer version: 1.15.10
Show newest version
/*
 * Copyright 2014 - Present 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.field;

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.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

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

    /**
     * Transforms the list of field 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 asTokenList(ElementMatcher matcher);

    /**
     * Returns this list of these field descriptions resolved to their defined shape.
     *
     * @return A list of fields in their defined shape.
     */
    FieldList asDefined();

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

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

        /**
         * {@inheritDoc}
         */
        public FieldList asDefined() {
            List declaredForms = new ArrayList(size());
            for (FieldDescription fieldDescription : this) {
                declaredForms.add(fieldDescription.asDefined());
            }
            return new Explicit(declaredForms);
        }

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

    /**
     * An implementation of a field list for an array of loaded fields.
     */
    class ForLoadedFields extends AbstractBase {

        /**
         * The loaded fields this field list represents.
         */
        private final List fields;

        /**
         * Creates a new immutable field list that represents an array of loaded field.
         *
         * @param field An array of fields to be represented by this field list.
         */
        public ForLoadedFields(Field... field) {
            this(Arrays.asList(field));
        }

        /**
         * Creates a new immutable field list that represents an array of loaded field.
         *
         * @param fields An array of fields to be represented by this field list.
         */
        public ForLoadedFields(List fields) {
            this.fields = fields;
        }

        /**
         * {@inheritDoc}
         */
        public FieldDescription.InDefinedShape get(int index) {
            return new FieldDescription.ForLoadedField(fields.get(index));
        }

        /**
         * {@inheritDoc}
         */
        public int size() {
            return fields.size();
        }
    }

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

        /**
         * The list of field descriptions this list represents.
         */
        private final List fieldDescriptions;

        /**
         * Creates a new immutable wrapper field list.
         *
         * @param fieldDescription The list of fields to be represented by this field list.
         */
        @SuppressWarnings("unchecked")
        public Explicit(S... fieldDescription) {
            this(Arrays.asList(fieldDescription));
        }

        /**
         * Creates a new immutable wrapper field list.
         *
         * @param fieldDescriptions The list of fields to be represented by this field list.
         */
        public Explicit(List fieldDescriptions) {
            this.fieldDescriptions = fieldDescriptions;
        }

        /**
         * {@inheritDoc}
         */
        public S get(int index) {
            return fieldDescriptions.get(index);
        }

        /**
         * {@inheritDoc}
         */
        public int size() {
            return fieldDescriptions.size();
        }
    }

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

        /**
         * The declaring type of the represented fields.
         */
        private final TypeDescription declaringType;

        /**
         * A list of the represented fields' tokens.
         */
        private final List tokens;

        /**
         * Creates a new field list from a list of field tokens.
         *
         * @param declaringType The declaring type of the represented fields.
         * @param token         A list of the represented fields' tokens.
         */
        public ForTokens(TypeDescription declaringType, FieldDescription.Token... token) {
            this(declaringType, Arrays.asList(token));
        }

        /**
         * Creates a new field list from a list of field tokens.
         *
         * @param declaringType The declaring type of the represented fields.
         * @param tokens        A list of the represented fields' tokens.
         */
        public ForTokens(TypeDescription declaringType, List tokens) {
            this.declaringType = declaringType;
            this.tokens = tokens;
        }

        /**
         * {@inheritDoc}
         */
        public FieldDescription.InDefinedShape get(int index) {
            return new FieldDescription.Latent(declaringType, tokens.get(index));
        }

        /**
         * {@inheritDoc}
         */
        public int size() {
            return tokens.size();
        }
    }

    /**
     * A list of field descriptions that yields {@link net.bytebuddy.description.field.FieldDescription.TypeSubstituting}.
     */
    class TypeSubstituting extends AbstractBase {

        /**
         * The field's actual declaring type.
         */
        private final TypeDescription.Generic declaringType;

        /**
         * The field descriptions to be transformed.
         */
        private final List fieldDescriptions;

        /**
         * The visitor to apply to a field description.
         */
        private final TypeDescription.Generic.Visitor visitor;

        /**
         * Creates a new type substituting field list.
         *
         * @param declaringType     The field's actual declaring type.
         * @param fieldDescriptions The field descriptions to be transformed.
         * @param visitor           The visitor to apply to a field description.
         */
        public TypeSubstituting(TypeDescription.Generic declaringType,
                                List fieldDescriptions,
                                TypeDescription.Generic.Visitor visitor) {
            this.declaringType = declaringType;
            this.fieldDescriptions = fieldDescriptions;
            this.visitor = visitor;
        }

        /**
         * {@inheritDoc}
         */
        public FieldDescription.InGenericShape get(int index) {
            return new FieldDescription.TypeSubstituting(declaringType, fieldDescriptions.get(index), visitor);
        }

        /**
         * {@inheritDoc}
         */
        public int size() {
            return fieldDescriptions.size();
        }
    }

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

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

        /**
         * {@inheritDoc}
         */
        @SuppressWarnings("unchecked")
        public FieldList asDefined() {
            return (FieldList) this;
        }
    }
}