Skip to content

Commit

Permalink
Added additional tests and documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafael Winterhalter committed Jan 5, 2016
1 parent e939783 commit 72136b8
Show file tree
Hide file tree
Showing 27 changed files with 478 additions and 83 deletions.
Expand Up @@ -68,38 +68,93 @@ interface ForParameter extends ModifierContributor {
/* marker interface */ /* marker interface */
} }


/**
* A resolver for Java modifiers represented by {@link ModifierContributor}s.
*
* @param <T> The type of the {@link ModifierContributor}s being resolved.
*/
class Resolver<T extends ModifierContributor> { class Resolver<T extends ModifierContributor> {


/**
* The modifier contributors to resolve.
*/
private final Collection<? extends T> modifierContributors; private final Collection<? extends T> modifierContributors;


/**
* Creates a new resolver.
*
* @param modifierContributors The modifier contributors to resolve.
*/
protected Resolver(Collection<? extends T> modifierContributors) { protected Resolver(Collection<? extends T> modifierContributors) {
this.modifierContributors = modifierContributors; this.modifierContributors = modifierContributors;
} }


/**
* Creates a new resolver for modifier contributors to a type.
*
* @param modifierContributor The modifier contributors to resolve.
* @return A resolver for the provided modifier contributors.
*/
public static Resolver<ForType> of(ForType... modifierContributor) { public static Resolver<ForType> of(ForType... modifierContributor) {
return of(Arrays.asList(modifierContributor)); return of(Arrays.asList(modifierContributor));
} }


/**
* Creates a new resolver for modifier contributors to a field.
*
* @param modifierContributor The modifier contributors to resolve.
* @return A resolver for the provided modifier contributors.
*/
public static Resolver<ForField> of(ForField... modifierContributor) { public static Resolver<ForField> of(ForField... modifierContributor) {
return of(Arrays.asList(modifierContributor)); return of(Arrays.asList(modifierContributor));
} }


/**
* Creates a new resolver for modifier contributors to a method.
*
* @param modifierContributor The modifier contributors to resolve.
* @return A resolver for the provided modifier contributors.
*/
public static Resolver<ForMethod> of(ForMethod... modifierContributor) { public static Resolver<ForMethod> of(ForMethod... modifierContributor) {
return of(Arrays.asList(modifierContributor)); return of(Arrays.asList(modifierContributor));
} }


/**
* Creates a new resolver for modifier contributors to a parameter.
*
* @param modifierContributor The modifier contributors to resolve.
* @return A resolver for the provided modifier contributors.
*/
public static Resolver<ForParameter> of(ForParameter... modifierContributor) { public static Resolver<ForParameter> of(ForParameter... modifierContributor) {
return Resolver.of(Arrays.asList(modifierContributor)); return Resolver.of(Arrays.asList(modifierContributor));
} }


/**
* Creates a new resolver for any modifier contributor of a given type.
*
* @param modifierContributors The modifier contributors to resolve.
* @param <S> The modifier contributors type.
* @return A resolver for the provided modifier contributors.
*/
public static <S extends ModifierContributor> Resolver<S> of(Collection<? extends S> modifierContributors) { public static <S extends ModifierContributor> Resolver<S> of(Collection<? extends S> modifierContributors) {
return new Resolver<S>(modifierContributors); return new Resolver<S>(modifierContributors);
} }


/**
* Resolves the modifier contributors based on a zero modifier.
*
* @return The resolved modifiers.
*/
public int resolve() { public int resolve() {
return resolve(EMPTY_MASK); return resolve(EMPTY_MASK);
} }


/**
* Resolves the modifier contributors based on a given modifier.
*
* @param modifiers The base modifiers.
* @return The resolved modifiers.
*/
public int resolve(int modifiers) { public int resolve(int modifiers) {
for (T modifierContributor : modifierContributors) { for (T modifierContributor : modifierContributors) {
modifiers = (modifiers & ~modifierContributor.getRange()) | modifierContributor.getMask(); modifiers = (modifiers & ~modifierContributor.getRange()) | modifierContributor.getMask();
Expand Down
Expand Up @@ -204,6 +204,13 @@ interface Generic extends FilterableList<TypeDescription.Generic, Generic> {
*/ */
Generic asRawTypes(); Generic asRawTypes();


/**
* Transforms a list of attached type variables into their tokenized form. Calling this method throws an {@link IllegalStateException}
* if any type in this list does not represent a type variable ({@link net.bytebuddy.description.type.TypeDefinition.Sort#VARIABLE}).
*
* @param visitor The visitor to use for detaching the type variable's bounds.
* @return A list of tokens representing the type variables contained in this list.
*/
ByteCodeElement.Token.TokenList<TypeVariableToken> asTokenList(TypeDescription.Generic.Visitor<? extends TypeDescription.Generic> visitor); ByteCodeElement.Token.TokenList<TypeVariableToken> asTokenList(TypeDescription.Generic.Visitor<? extends TypeDescription.Generic> visitor);


/** /**
Expand Down Expand Up @@ -459,19 +466,35 @@ public int size() {
} }


/** /**
* * A list of attached type variables represented by a list of type variable tokens.
*/ */
protected static class OfTypeVariables extends Generic.AbstractBase { protected static class OfTypeVariables extends Generic.AbstractBase {


/**
* The type variable's source.
*/
private final TypeVariableSource typeVariableSource; private final TypeVariableSource typeVariableSource;


/**
* A token representing the type variable in its detached state.
*/
private final List<? extends TypeVariableToken> detachedTypeVariables; private final List<? extends TypeVariableToken> detachedTypeVariables;


/**
* A visitor for attaching the type variable's bounds.
*/
private final TypeDescription.Generic.Visitor<? extends TypeDescription.Generic> visitor; private final TypeDescription.Generic.Visitor<? extends TypeDescription.Generic> visitor;


public OfTypeVariables(TypeVariableSource typeVariableSource, /**
List<? extends TypeVariableToken> detachedTypeVariables, * Creates a new list of attached type variables representing a list of type variable tokens.
TypeDescription.Generic.Visitor<? extends TypeDescription.Generic> visitor) { *
* @param typeVariableSource The type variable's source.
* @param detachedTypeVariables A token representing the type variable in its detached state.
* @param visitor A visitor for attaching the type variable's bounds.
*/
protected OfTypeVariables(TypeVariableSource typeVariableSource,
List<? extends TypeVariableToken> detachedTypeVariables,
TypeDescription.Generic.Visitor<? extends TypeDescription.Generic> visitor) {
this.typeVariableSource = typeVariableSource; this.typeVariableSource = typeVariableSource;
this.detachedTypeVariables = detachedTypeVariables; this.detachedTypeVariables = detachedTypeVariables;
this.visitor = visitor; this.visitor = visitor;
Expand All @@ -493,17 +516,27 @@ public int size() {
protected static class AttachedTypeVariable extends TypeDescription.Generic.OfTypeVariable { protected static class AttachedTypeVariable extends TypeDescription.Generic.OfTypeVariable {


/** /**
* The type variable source that defines the type variable. * The type variable's source.
*/ */
private final TypeVariableSource typeVariableSource; private final TypeVariableSource typeVariableSource;


/**
* A token representing the type variable in its detached state.
*/
private final TypeVariableToken typeVariableToken; private final TypeVariableToken typeVariableToken;


/** /**
* The visitor to apply onto the detached bounds for their attachment. * A visitor for attaching the type variable's bounds.
*/ */
private final TypeDescription.Generic.Visitor<? extends TypeDescription.Generic> visitor; private final TypeDescription.Generic.Visitor<? extends TypeDescription.Generic> visitor;


/**
* Creates a new attached type variable.
*
* @param typeVariableSource The type variable's source.
* @param typeVariableToken A token representing the type variable in its detached state.
* @param visitor A visitor for attaching the type variable's bounds.
*/
protected AttachedTypeVariable(TypeVariableSource typeVariableSource, protected AttachedTypeVariable(TypeVariableSource typeVariableSource,
TypeVariableToken typeVariableToken, TypeVariableToken typeVariableToken,
Visitor<? extends TypeDescription.Generic> visitor) { Visitor<? extends TypeDescription.Generic> visitor) {
Expand Down
Expand Up @@ -20,6 +20,12 @@ public class TypeVariableToken implements ByteCodeElement.Token<TypeVariableToke
*/ */
private final List<? extends TypeDescription.Generic> upperBounds; private final List<? extends TypeDescription.Generic> upperBounds;


/**
* Creates a new type variable token.
*
* @param symbol The type variable's symbol.
* @param upperBound The type variable's single upper bound.
*/
public TypeVariableToken(String symbol, TypeDescription.Generic upperBound) { public TypeVariableToken(String symbol, TypeDescription.Generic upperBound) {
this(symbol, Collections.singletonList(upperBound)); this(symbol, Collections.singletonList(upperBound));
} }
Expand All @@ -35,6 +41,13 @@ public TypeVariableToken(String symbol, List<? extends TypeDescription.Generic>
this.upperBounds = upperBounds; this.upperBounds = upperBounds;
} }


/**
* Transforms a type variable into a type variable token with its bounds detached.
*
* @param typeVariable A type variable in its attached state.
* @param visitor A visitor for detaching the type variable's upper bounds.
* @return A token representing the detached type variable.
*/
public static TypeVariableToken of(TypeDescription.Generic typeVariable, TypeDescription.Generic.Visitor<? extends TypeDescription.Generic> visitor) { public static TypeVariableToken of(TypeDescription.Generic typeVariable, TypeDescription.Generic.Visitor<? extends TypeDescription.Generic> visitor) {
return new TypeVariableToken(typeVariable.getSymbol(), typeVariable.getUpperBounds().accept(visitor)); return new TypeVariableToken(typeVariable.getSymbol(), typeVariable.getUpperBounds().accept(visitor));
} }
Expand Down
Expand Up @@ -60,6 +60,12 @@ public interface InstrumentedType extends TypeDescription {
*/ */
InstrumentedType withInterfaces(List<? extends Generic> interfaceTypes); InstrumentedType withInterfaces(List<? extends Generic> interfaceTypes);


/**
* Creates a new instrumented type with the given type variable defined.
*
* @param typeVariable The type variable to declare.
* @return A new instrumented type that is equal to this instrumented type but with the given type variable declared.
*/
InstrumentedType withTypeVariable(TypeVariableToken typeVariable); InstrumentedType withTypeVariable(TypeVariableToken typeVariable);


/** /**
Expand Down
Expand Up @@ -17,9 +17,9 @@ public interface FieldAttributeAppender {
/** /**
* Applies this attribute appender to a given field visitor. * Applies this attribute appender to a given field visitor.
* *
* @param fieldVisitor The field visitor to which the attributes that are represented by this attribute appender * @param fieldVisitor The field visitor to which the attributes that are represented by this attribute appender are written to.
* are written to. * @param fieldDescription The description of the field to which the field visitor belongs to.
* @param fieldDescription The description of the field to which the field visitor belongs to. * @param annotationValueFilter The annotation value filter to apply when writing annotations.
*/ */
void apply(FieldVisitor fieldVisitor, FieldDescription fieldDescription, AnnotationValueFilter annotationValueFilter); void apply(FieldVisitor fieldVisitor, FieldDescription fieldDescription, AnnotationValueFilter annotationValueFilter);


Expand Down Expand Up @@ -73,10 +73,20 @@ class Compound implements Factory {
*/ */
private final List<? extends Factory> factories; private final List<? extends Factory> factories;


/**
* Creates a new compound field attribute appender factory.
*
* @param factory The factories to represent in the order of their application.
*/
public Compound(Factory... factory) { public Compound(Factory... factory) {
this(Arrays.asList(factory)); this(Arrays.asList(factory));
} }


/**
* Creates a new compound field attribute appender factory.
*
* @param factories The factories to represent in the order of their application.
*/
public Compound(List<? extends Factory> factories) { public Compound(List<? extends Factory> factories) {
this.factories = factories; this.factories = factories;
} }
Expand Down Expand Up @@ -108,8 +118,14 @@ public String toString() {
} }
} }


/**
* An attribute appender that writes all annotations that are declared on a field.
*/
enum ForInstrumentedField implements FieldAttributeAppender, Factory { enum ForInstrumentedField implements FieldAttributeAppender, Factory {


/**
* The singleton instance.
*/
INSTANCE; INSTANCE;


@Override @Override
Expand Down Expand Up @@ -204,6 +220,12 @@ public Compound(FieldAttributeAppender... fieldAttributeAppender) {
this(Arrays.asList(fieldAttributeAppender)); this(Arrays.asList(fieldAttributeAppender));
} }


/**
* Creates a new compound field attribute appender.
*
* @param fieldAttributeAppenders The field attribute appenders that are to be combined by this compound appender
* in the order of their application.
*/
public Compound(List<? extends FieldAttributeAppender> fieldAttributeAppenders) { public Compound(List<? extends FieldAttributeAppender> fieldAttributeAppenders) {
this.fieldAttributeAppenders = fieldAttributeAppenders; this.fieldAttributeAppenders = fieldAttributeAppenders;
} }
Expand Down
Expand Up @@ -19,10 +19,11 @@ public interface MethodAttributeAppender {
/** /**
* Applies this attribute appender to a given method visitor. * Applies this attribute appender to a given method visitor.
* *
* @param methodVisitor The method visitor to which the attributes that are represented by this attribute * @param methodVisitor The method visitor to which the attributes that are represented by this attribute
* appender are written to. * appender are written to.
* @param methodDescription The description of the method for which the given method visitor creates an * @param methodDescription The description of the method for which the given method visitor creates an
* instrumentation for. * instrumentation for.
* @param annotationValueFilter The annotation value filter to apply when the annotations are written.
*/ */
void apply(MethodVisitor methodVisitor, MethodDescription methodDescription, AnnotationValueFilter annotationValueFilter); void apply(MethodVisitor methodVisitor, MethodDescription methodDescription, AnnotationValueFilter annotationValueFilter);


Expand Down Expand Up @@ -79,13 +80,17 @@ class Compound implements Factory {
/** /**
* Creates a new compound method attribute appender factory. * Creates a new compound method attribute appender factory.
* *
* @param factory The factories that are to be combined by this compound factory in the order of their * @param factory The factories that are to be combined by this compound factory in the order of their application.
* application.
*/ */
public Compound(Factory... factory) { public Compound(Factory... factory) {
this(Arrays.asList(factory)); this(Arrays.asList(factory));
} }


/**
* Creates a new compound method attribute appender factory.
*
* @param factories The factories that are to be combined by this compound factory in the order of their application.
*/
public Compound(List<? extends Factory> factories) { public Compound(List<? extends Factory> factories) {
this.factories = factories; this.factories = factories;
} }
Expand Down Expand Up @@ -123,6 +128,9 @@ public String toString() {
*/ */
enum ForInstrumentedMethod implements MethodAttributeAppender, Factory { enum ForInstrumentedMethod implements MethodAttributeAppender, Factory {


/**
* The singleton instance.
*/
INSTANCE; INSTANCE;


@Override @Override
Expand Down Expand Up @@ -186,11 +194,24 @@ public Explicit(List<? extends AnnotationDescription> annotations) {
this(Target.OnMethod.INSTANCE, annotations); this(Target.OnMethod.INSTANCE, annotations);
} }


/**
* Creates an explicit annotation appender for a either a method or one of its parameters..
*
* @param target The target to which the annotation should be written to.
* @param annotations The annotations to write.
*/
protected Explicit(Target target, List<? extends AnnotationDescription> annotations) { protected Explicit(Target target, List<? extends AnnotationDescription> annotations) {
this.target = target; this.target = target;
this.annotations = annotations; this.annotations = annotations;
} }


/**
* Creates a method attribute appender factory that writes all annotations of a given method, both the method
* annotations themselves and all annotations that are defined for every parameter.
*
* @param methodDescription The method from which to extract the annotations.
* @return A method attribute appender factory for an appender that writes all annotations of the supplied method.
*/
public static Factory of(MethodDescription methodDescription) { public static Factory of(MethodDescription methodDescription) {
ParameterList<?> parameters = methodDescription.getParameters(); ParameterList<?> parameters = methodDescription.getParameters();
List<MethodAttributeAppender.Factory> methodAttributeAppenders = new ArrayList<MethodAttributeAppender.Factory>(parameters.size() + 1); List<MethodAttributeAppender.Factory> methodAttributeAppenders = new ArrayList<MethodAttributeAppender.Factory>(parameters.size() + 1);
Expand Down Expand Up @@ -338,6 +359,12 @@ public Compound(MethodAttributeAppender... methodAttributeAppender) {
this(Arrays.asList(methodAttributeAppender)); this(Arrays.asList(methodAttributeAppender));
} }


/**
* Creates a new compound method attribute appender.
*
* @param methodAttributeAppenders The method attribute appenders that are to be combined by this compound appender
* in the order of their application.
*/
public Compound(List<? extends MethodAttributeAppender> methodAttributeAppenders) { public Compound(List<? extends MethodAttributeAppender> methodAttributeAppenders) {
this.methodAttributeAppenders = methodAttributeAppenders; this.methodAttributeAppenders = methodAttributeAppenders;
} }
Expand Down

0 comments on commit 72136b8

Please sign in to comment.