com.fitbur.bytebuddy.description.field.FieldList Maven / Gradle / Ivy
The newest version!
package com.fitbur.bytebuddy.description.field;
import com.fitbur.bytebuddy.description.ByteCodeElement;
import com.fitbur.bytebuddy.description.type.TypeDescription;
import com.fitbur.bytebuddy.matcher.ElementMatcher;
import com.fitbur.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 com.fitbur.bytebuddy.dynamic.TargetType}.
*
* @param matcher A matcher that indicates type substitution.
* @return The transformed token list.
*/
ByteCodeElement.Token.TokenList asTokenList(ElementMatcher super TypeDescription> 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 {
@Override
public ByteCodeElement.Token.TokenList asTokenList(ElementMatcher super TypeDescription> matcher) {
List tokens = new ArrayList(size());
for (FieldDescription fieldDescription : this) {
tokens.add(fieldDescription.asToken(matcher));
}
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 ForLoadedFields 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 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 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 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 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 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 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 com.fitbur.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 extends FieldDescription> fieldDescriptions;
/**
* The visitor to apply to a field description.
*/
private final TypeDescription.Generic.Visitor extends TypeDescription.Generic> 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 extends FieldDescription> fieldDescriptions,
TypeDescription.Generic.Visitor extends TypeDescription.Generic> visitor) {
this.declaringType = declaringType;
this.fieldDescriptions = fieldDescriptions;
this.visitor = visitor;
}
@Override
public FieldDescription.InGenericShape 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.
*
* @param The type of parameter descriptions represented by this list.
*/
class Empty extends FilterableList.Empty> implements FieldList {
@Override
public ByteCodeElement.Token.TokenList asTokenList(ElementMatcher super TypeDescription> matcher) {
return new ByteCodeElement.Token.TokenList();
}
@Override
@SuppressWarnings("unchecked")
public FieldList asDefined() {
return (FieldList) this;
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy