Skip to content

Commit

Permalink
Added concept of declared members which are guaranteed to describe a …
Browse files Browse the repository at this point in the history
…list or field as they are declared by a type. Refactored the member specific lists to allow the representation of either sort.
  • Loading branch information
raphw committed Jul 18, 2015
1 parent 1e5f8c9 commit 371dc26
Show file tree
Hide file tree
Showing 36 changed files with 323 additions and 242 deletions.
Expand Up @@ -1807,7 +1807,7 @@ public static Builder forType(TypeDescription annotationType) {
* @return A builder with the additional, given property. * @return A builder with the additional, given property.
*/ */
public Builder define(String property, AnnotationValue<?, ?> value) { public Builder define(String property, AnnotationValue<?, ?> value) {
MethodList methodDescriptions = annotationType.getDeclaredMethods().filter(named(nonNull(property))); MethodList<?> methodDescriptions = annotationType.getDeclaredMethods().filter(named(nonNull(property)));
if (methodDescriptions.isEmpty()) { if (methodDescriptions.isEmpty()) {
throw new IllegalArgumentException(annotationType + " does not define a property named " + property); throw new IllegalArgumentException(annotationType + " does not define a property named " + property);
} else if (!methodDescriptions.getOnly().getReturnType().asRawType().isAnnotationValue(value.resolve())) { } else if (!methodDescriptions.getOnly().getReturnType().asRawType().isAnnotationValue(value.resolve())) {
Expand Down
Expand Up @@ -4,6 +4,7 @@
import net.bytebuddy.description.NamedElement; import net.bytebuddy.description.NamedElement;
import net.bytebuddy.description.annotation.AnnotationDescription; import net.bytebuddy.description.annotation.AnnotationDescription;
import net.bytebuddy.description.annotation.AnnotationList; import net.bytebuddy.description.annotation.AnnotationList;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription; import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.description.type.generic.GenericTypeDescription; import net.bytebuddy.description.type.generic.GenericTypeDescription;
import net.bytebuddy.matcher.ElementMatcher; import net.bytebuddy.matcher.ElementMatcher;
Expand Down Expand Up @@ -48,6 +49,22 @@ public interface FieldDescription extends ByteCodeElement, NamedElement.WithGene
*/ */
Token asToken(ElementMatcher<? super TypeDescription> targetTypeMatcher); Token asToken(ElementMatcher<? super TypeDescription> targetTypeMatcher);


InDeclaredForm asDeclared();

interface InDeclaredForm extends FieldDescription {

@Override
TypeDescription getDeclaringType();

abstract class AbstractBase extends AbstractFieldDescription implements InDeclaredForm {

@Override
public InDeclaredForm asDeclared() {
return this;
}
}
}

/** /**
* An abstract base implementation of a field description. * An abstract base implementation of a field description.
*/ */
Expand Down Expand Up @@ -141,7 +158,7 @@ public String toString() {
/** /**
* An implementation of a field description for a loaded field. * An implementation of a field description for a loaded field.
*/ */
class ForLoadedField extends AbstractFieldDescription { class ForLoadedField extends InDeclaredForm.AbstractBase {


/** /**
* The represented loaded field. * The represented loaded field.
Expand Down Expand Up @@ -173,7 +190,7 @@ public String getName() {
} }


@Override @Override
public GenericTypeDescription getDeclaringType() { public TypeDescription getDeclaringType() {
return new TypeDescription.ForLoadedType(field.getDeclaringClass()); return new TypeDescription.ForLoadedType(field.getDeclaringClass());
} }


Expand All @@ -192,12 +209,12 @@ public boolean isSynthetic() {
* A latent field description describes a field that is not attached to a declaring * A latent field description describes a field that is not attached to a declaring
* {@link TypeDescription}. * {@link TypeDescription}.
*/ */
class Latent extends AbstractFieldDescription { class Latent extends InDeclaredForm.AbstractBase {


/** /**
* The type for which this field is defined. * The type for which this field is defined.
*/ */
private final GenericTypeDescription declaringType; private final TypeDescription declaringType;


/** /**
* The name of the field. * The name of the field.
Expand Down Expand Up @@ -225,7 +242,7 @@ class Latent extends AbstractFieldDescription {
* @param declaringType The declaring type of the field. * @param declaringType The declaring type of the field.
* @param token A token representing the field's shape. * @param token A token representing the field's shape.
*/ */
public Latent(GenericTypeDescription declaringType, FieldDescription.Token token) { public Latent(TypeDescription declaringType, FieldDescription.Token token) {
this(declaringType, this(declaringType,
token.getName(), token.getName(),
token.getModifiers(), token.getModifiers(),
Expand All @@ -242,7 +259,7 @@ public Latent(GenericTypeDescription declaringType, FieldDescription.Token token
* @param modifiers The type of the field. * @param modifiers The type of the field.
* @param declaredAnnotations The annotations of this field. * @param declaredAnnotations The annotations of this field.
*/ */
public Latent(GenericTypeDescription declaringType, public Latent(TypeDescription declaringType,
String fieldName, String fieldName,
int modifiers, int modifiers,
GenericTypeDescription fieldType, GenericTypeDescription fieldType,
Expand Down Expand Up @@ -270,7 +287,7 @@ public String getName() {
} }


@Override @Override
public GenericTypeDescription getDeclaringType() { public TypeDescription getDeclaringType() {
return declaringType; return declaringType;
} }


Expand Down Expand Up @@ -339,6 +356,11 @@ public int getModifiers() {
public String getName() { public String getName() {
return fieldDescription.getName(); return fieldDescription.getName();
} }

@Override
public InDeclaredForm asDeclared() {
return fieldDescription.asDeclared();
}
} }


/** /**
Expand Down
Expand Up @@ -17,7 +17,7 @@
/** /**
* Implementations represent a list of field descriptions. * Implementations represent a list of field descriptions.
*/ */
public interface FieldList extends FilterableList<FieldDescription, FieldList> { public interface FieldList<T extends FieldDescription> extends FilterableList<T, FieldList<T>> {


/** /**
* Transforms the list of field descriptions into a list of detached tokens. * Transforms the list of field descriptions into a list of detached tokens.
Expand All @@ -38,7 +38,7 @@ public interface FieldList extends FilterableList<FieldDescription, FieldList> {
/** /**
* An abstract base implementation of a {@link FieldList}. * An abstract base implementation of a {@link FieldList}.
*/ */
abstract class AbstractBase extends FilterableList.AbstractBase<FieldDescription, FieldList> implements FieldList { abstract class AbstractBase<S extends FieldDescription> extends FilterableList.AbstractBase<S, FieldList<S>> implements FieldList<S> {


@Override @Override
public ByteCodeElement.Token.TokenList<FieldDescription.Token> asTokenList() { public ByteCodeElement.Token.TokenList<FieldDescription.Token> asTokenList() {
Expand All @@ -55,15 +55,15 @@ public ByteCodeElement.Token.TokenList<FieldDescription.Token> asTokenList(Eleme
} }


@Override @Override
protected FieldList wrap(List<FieldDescription> values) { protected FieldList<S> wrap(List<S> values) {
return new Explicit(values); return new Explicit<S>(values);
} }
} }


/** /**
* An implementation of a field list for an array of loaded fields. * An implementation of a field list for an array of loaded fields.
*/ */
class ForLoadedField extends AbstractBase { class ForLoadedField extends AbstractBase<FieldDescription.InDeclaredForm> {


/** /**
* The loaded fields this field list representedBy. * The loaded fields this field list representedBy.
Expand All @@ -89,7 +89,7 @@ public ForLoadedField(List<? extends Field> fields) {
} }


@Override @Override
public FieldDescription get(int index) { public FieldDescription.InDeclaredForm get(int index) {
return new FieldDescription.ForLoadedField(fields.get(index)); return new FieldDescription.ForLoadedField(fields.get(index));
} }


Expand All @@ -102,24 +102,24 @@ public int size() {
/** /**
* A wrapper implementation of a field list for a given list of field descriptions. * A wrapper implementation of a field list for a given list of field descriptions.
*/ */
class Explicit extends AbstractBase { class Explicit<S extends FieldDescription> extends AbstractBase<S> {


/** /**
* The list of field descriptions this list representedBy. * The list of field descriptions this list representedBy.
*/ */
private final List<? extends FieldDescription> fieldDescriptions; private final List<? extends S> fieldDescriptions;


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


@Override @Override
public FieldDescription get(int index) { public S get(int index) {
return fieldDescriptions.get(index); return fieldDescriptions.get(index);
} }


Expand All @@ -132,7 +132,7 @@ public int size() {
/** /**
* A list of field descriptions for a list of detached tokens. For the returned fields, each token is attached to its field representation. * 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 { class ForTokens extends AbstractBase<FieldDescription.InDeclaredForm> {


/** /**
* The declaring type of the represented fields. * The declaring type of the represented fields.
Expand All @@ -156,7 +156,7 @@ public ForTokens(TypeDescription declaringType, List<? extends FieldDescription.
} }


@Override @Override
public FieldDescription get(int index) { public FieldDescription.InDeclaredForm get(int index) {
return new FieldDescription.Latent(declaringType, tokens.get(index)); return new FieldDescription.Latent(declaringType, tokens.get(index));
} }


Expand All @@ -169,7 +169,7 @@ public int size() {
/** /**
* A list of field descriptions that yields {@link net.bytebuddy.description.field.FieldDescription.TypeSubstituting}. * A list of field descriptions that yields {@link net.bytebuddy.description.field.FieldDescription.TypeSubstituting}.
*/ */
class TypeSubstituting extends AbstractBase { class TypeSubstituting extends AbstractBase<FieldDescription> {


/** /**
* The field's actual declaring type. * The field's actual declaring type.
Expand Down Expand Up @@ -215,7 +215,8 @@ public int size() {
/** /**
* An implementation of an empty field list. * An implementation of an empty field list.
*/ */
class Empty extends FilterableList.Empty<FieldDescription, FieldList> implements FieldList { class Empty extends FilterableList.Empty<FieldDescription.InDeclaredForm, FieldList<FieldDescription.InDeclaredForm>>
implements FieldList<FieldDescription.InDeclaredForm> {


@Override @Override
public ByteCodeElement.Token.TokenList<FieldDescription.Token> asTokenList() { public ByteCodeElement.Token.TokenList<FieldDescription.Token> asTokenList() {
Expand Down

0 comments on commit 371dc26

Please sign in to comment.