
net.bytebuddy.description.field.FieldList Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of byte-buddy Show documentation
Show all versions of byte-buddy Show documentation
Byte Buddy is a Java library for creating Java classes at run time.
This artifact is a build of Byte Buddy with all ASM dependencies repackaged into its own name space.
package net.bytebuddy.description.field;
import net.bytebuddy.description.ByteCodeElement;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.description.type.generic.GenericTypeDescription;
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.Collections;
import java.util.List;
import static net.bytebuddy.matcher.ElementMatchers.none;
/**
* 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.
*
* @return The transformed token list.
*/
ByteCodeElement.Token.TokenList asTokenList();
/**
* 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 targetTypeMatcher A matcher that indicates type substitution.
* @return The transformed token list.
*/
ByteCodeElement.Token.TokenList asTokenList(ElementMatcher super GenericTypeDescription> targetTypeMatcher);
/**
* 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 {
@Override
public ByteCodeElement.Token.TokenList asTokenList() {
return asTokenList(none());
}
@Override
public ByteCodeElement.Token.TokenList asTokenList(ElementMatcher super GenericTypeDescription> targetTypeMatcher) {
List tokens = new ArrayList(size());
for (FieldDescription fieldDescription : this) {
tokens.add(fieldDescription.asToken(targetTypeMatcher));
}
return new ByteCodeElement.Token.TokenList(tokens);
}
@Override
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 ForLoadedField extends AbstractBase {
/**
* The loaded fields this field list represents.
*/
private final List extends Field> 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 ForLoadedField(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 ForLoadedField(List extends Field> fields) {
this.fields = fields;
}
@Override
public FieldDescription.InDefinedShape get(int index) {
return new FieldDescription.ForLoadedField(fields.get(index));
}
@Override
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 extends S> fieldDescriptions;
/**
* Creates a new immutable wrapper field list.
*
* @param fieldDescriptions The list of fields to be represented by this field list.
*/
public Explicit(List extends S> fieldDescriptions) {
this.fieldDescriptions = fieldDescriptions;
}
@Override
public S get(int index) {
return fieldDescriptions.get(index);
}
@Override
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 extends FieldDescription.Token> tokens;
/**
* 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 extends FieldDescription.Token> tokens) {
this.declaringType = declaringType;
this.tokens = tokens;
}
@Override
public FieldDescription.InDefinedShape get(int index) {
return new FieldDescription.Latent(declaringType, tokens.get(index));
}
@Override
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 GenericTypeDescription declaringType;
/**
* The field descriptions to be transformed.
*/
private final List extends FieldDescription> fieldDescriptions;
/**
* The visitor to apply to a field description.
*/
private final GenericTypeDescription.Visitor extends GenericTypeDescription> 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(GenericTypeDescription declaringType,
List extends FieldDescription> fieldDescriptions,
GenericTypeDescription.Visitor extends GenericTypeDescription> visitor) {
this.declaringType = declaringType;
this.fieldDescriptions = fieldDescriptions;
this.visitor = visitor;
}
@Override
public FieldDescription get(int index) {
return new FieldDescription.TypeSubstituting(declaringType, fieldDescriptions.get(index), visitor);
}
@Override
public int size() {
return fieldDescriptions.size();
}
}
/**
* An implementation of an empty field list.
*/
class Empty extends FilterableList.Empty>
implements FieldList {
@Override
public ByteCodeElement.Token.TokenList asTokenList() {
return new ByteCodeElement.Token.TokenList(Collections.emptyList());
}
@Override
public ByteCodeElement.Token.TokenList asTokenList(ElementMatcher super GenericTypeDescription> targetTypeMatcher) {
return new ByteCodeElement.Token.TokenList(Collections.emptyList());
}
@Override
public FieldList asDefined() {
return this;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy