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 Aug 3, 2015
1 parent 7d12f3e commit f095ef0
Show file tree
Hide file tree
Showing 46 changed files with 1,813 additions and 650 deletions.
32 changes: 23 additions & 9 deletions byte-buddy-dep/src/main/java/net/bytebuddy/ByteBuddy.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@
import net.bytebuddy.description.annotation.AnnotationList; import net.bytebuddy.description.annotation.AnnotationList;
import net.bytebuddy.description.field.FieldDescription; import net.bytebuddy.description.field.FieldDescription;
import net.bytebuddy.description.method.MethodDescription; import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.method.ParameterDescription;
import net.bytebuddy.description.method.ParameterList;
import net.bytebuddy.description.modifier.*; import net.bytebuddy.description.modifier.*;
import net.bytebuddy.description.type.PackageDescription; import net.bytebuddy.description.type.PackageDescription;
import net.bytebuddy.description.type.TypeDescription; import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.description.type.TypeList; import net.bytebuddy.description.type.TypeList;
import net.bytebuddy.description.type.generic.GenericTypeDescription;
import net.bytebuddy.description.type.generic.GenericTypeList; import net.bytebuddy.description.type.generic.GenericTypeList;
import net.bytebuddy.dynamic.ClassFileLocator; import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.dynamic.DynamicType; import net.bytebuddy.dynamic.DynamicType;
Expand Down Expand Up @@ -118,6 +115,9 @@ public class ByteBuddy {
*/ */
protected final Definable<Integer> modifiers; protected final Definable<Integer> modifiers;


/**
* The method graph compiler to use.
*/
protected final MethodGraph.Compiler methodGraphCompiler; protected final MethodGraph.Compiler methodGraphCompiler;


/** /**
Expand Down Expand Up @@ -180,6 +180,7 @@ public ByteBuddy(ClassFileVersion classFileVersion) {
* @param methodRegistry The currently valid method registry. * @param methodRegistry The currently valid method registry.
* @param modifiers The modifiers to define for any implementation process. * @param modifiers The modifiers to define for any implementation process.
* @param typeAttributeAppender The type attribute appender to apply to any implementation process. * @param typeAttributeAppender The type attribute appender to apply to any implementation process.
* @param methodGraphCompiler The method graph compiler to use.
* @param defaultFieldAttributeAppenderFactory The field attribute appender to apply as a default for any field * @param defaultFieldAttributeAppenderFactory The field attribute appender to apply as a default for any field
* definition. * definition.
* @param defaultMethodAttributeAppenderFactory The method attribute appender to apply as a default for any * @param defaultMethodAttributeAppenderFactory The method attribute appender to apply as a default for any
Expand Down Expand Up @@ -274,6 +275,11 @@ public Definable<Integer> getModifiers() {
return modifiers; return modifiers;
} }


/**
* Returns the method graph compiler that is used.
*
* @return The method graph compiler that is used.
*/
public MethodGraph.Compiler getMethodGraphCompiler() { public MethodGraph.Compiler getMethodGraphCompiler() {
return methodGraphCompiler; return methodGraphCompiler;
} }
Expand Down Expand Up @@ -1060,8 +1066,7 @@ public ByteBuddy withIgnoredMethods(ElementMatcher<? super MethodDescription> ig
* that are to be applied onto any creation process of a dynamic type. * that are to be applied onto any creation process of a dynamic type.
* *
* @param classVisitorWrapper The class visitor wrapper to ba appended to the current chain of class visitor wrappers. * @param classVisitorWrapper The class visitor wrapper to ba appended to the current chain of class visitor wrappers.
* @return The same configuration with the given class visitor wrapper to be applied onto any creation process of a dynamic * @return The same configuration with the given class visitor wrapper to be applied onto any creation process of a dynamic type.
* type.
*/ */
public ByteBuddy withClassVisitor(ClassVisitorWrapper classVisitorWrapper) { public ByteBuddy withClassVisitor(ClassVisitorWrapper classVisitorWrapper) {
return new ByteBuddy(classFileVersion, return new ByteBuddy(classFileVersion,
Expand All @@ -1078,6 +1083,12 @@ public ByteBuddy withClassVisitor(ClassVisitorWrapper classVisitorWrapper) {
defaultMethodAttributeAppenderFactory); defaultMethodAttributeAppenderFactory);
} }


/**
* Defines a new method graph compiler to be used for extracting a type's invokable methods.
*
* @param methodGraphCompiler The method graph compiler to use.
* @return The same configuration with the given method graph compiler to be applied onto any creation process of a dynamic type.
*/
public ByteBuddy withMethodGraphCompiler(MethodGraph.Compiler methodGraphCompiler) { public ByteBuddy withMethodGraphCompiler(MethodGraph.Compiler methodGraphCompiler) {
return new ByteBuddy(classFileVersion, return new ByteBuddy(classFileVersion,
namingStrategy, namingStrategy,
Expand Down Expand Up @@ -1148,7 +1159,7 @@ public ByteBuddy withDefaultMethodAttributeAppender(MethodAttributeAppender.Fact
* @return A matched method interception for the given selection. * @return A matched method interception for the given selection.
*/ */
public MatchedMethodInterception invokable(ElementMatcher<? super MethodDescription> methodMatcher) { public MatchedMethodInterception invokable(ElementMatcher<? super MethodDescription> methodMatcher) {
return invokeable(new LatentMethodMatcher.Resolved(nonNull(methodMatcher))); return invokable(new LatentMethodMatcher.Resolved(nonNull(methodMatcher)));
} }


/** /**
Expand All @@ -1157,7 +1168,7 @@ public MatchedMethodInterception invokable(ElementMatcher<? super MethodDescript
* @param methodMatcher The latent method matcher representing all byte code methods to intercept. * @param methodMatcher The latent method matcher representing all byte code methods to intercept.
* @return A matched method interception for the given selection. * @return A matched method interception for the given selection.
*/ */
public MatchedMethodInterception invokeable(LatentMethodMatcher methodMatcher) { public MatchedMethodInterception invokable(LatentMethodMatcher methodMatcher) {
return new MatchedMethodInterception(nonNull(methodMatcher)); return new MatchedMethodInterception(nonNull(methodMatcher));
} }


Expand Down Expand Up @@ -1424,6 +1435,7 @@ public static class MethodAnnotationTarget extends Proxy {
* @param methodRegistry The currently valid method registry. * @param methodRegistry The currently valid method registry.
* @param modifiers The modifiers to define for any implementation process. * @param modifiers The modifiers to define for any implementation process.
* @param typeAttributeAppender The type attribute appender to apply to any implementation process. * @param typeAttributeAppender The type attribute appender to apply to any implementation process.
* @param methodGraphCompiler The method graph compiler to use.
* @param defaultFieldAttributeAppenderFactory The field attribute appender to apply as a default for any field * @param defaultFieldAttributeAppenderFactory The field attribute appender to apply as a default for any field
* definition. * definition.
* @param defaultMethodAttributeAppenderFactory The method attribute appender to apply as a default for any * @param defaultMethodAttributeAppenderFactory The method attribute appender to apply as a default for any
Expand Down Expand Up @@ -1654,6 +1666,7 @@ public static class OptionalMethodInterception extends ByteBuddy implements Meth
* @param methodRegistry The currently valid method registry. * @param methodRegistry The currently valid method registry.
* @param modifiers The modifiers to define for any implementation process. * @param modifiers The modifiers to define for any implementation process.
* @param typeAttributeAppender The type attribute appender to apply to any implementation process. * @param typeAttributeAppender The type attribute appender to apply to any implementation process.
* @param methodGraphCompiler The method graph compiler to use.
* @param defaultFieldAttributeAppenderFactory The field attribute appender to apply as a default for any field * @param defaultFieldAttributeAppenderFactory The field attribute appender to apply as a default for any field
* definition. * definition.
* @param defaultMethodAttributeAppenderFactory The method attribute appender to apply as a default for any * @param defaultMethodAttributeAppenderFactory The method attribute appender to apply as a default for any
Expand Down Expand Up @@ -1761,6 +1774,7 @@ protected abstract static class Proxy extends ByteBuddy {
* @param modifiers The modifiers to define for any implementation process. * @param modifiers The modifiers to define for any implementation process.
* @param typeAttributeAppender The type attribute appender to apply to any implementation * @param typeAttributeAppender The type attribute appender to apply to any implementation
* process. * process.
* @param methodGraphCompiler The method graph compiler to use.
* @param defaultFieldAttributeAppenderFactory The field attribute appender to apply as a default for any * @param defaultFieldAttributeAppenderFactory The field attribute appender to apply as a default for any
* field definition. * field definition.
* @param defaultMethodAttributeAppenderFactory The method attribute appender to apply as a default for any * @param defaultMethodAttributeAppenderFactory The method attribute appender to apply as a default for any
Expand Down Expand Up @@ -2082,8 +2096,8 @@ public ByteBuddy withTypeAnnotation(AnnotationDescription... annotation) {
} }


@Override @Override
public MatchedMethodInterception invokeable(LatentMethodMatcher methodMatcher) { public MatchedMethodInterception invokable(LatentMethodMatcher methodMatcher) {
return materialize().invokeable(methodMatcher); return materialize().invokable(methodMatcher);
} }


@Override @Override
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -44,12 +44,35 @@ public interface ByteCodeElement extends NamedElement.WithRuntimeName, ModifierR
*/ */
boolean isVisibleTo(TypeDescription typeDescription); boolean isVisibleTo(TypeDescription typeDescription);


/**
* A type dependant describes an element that is an extension of a type definition, i.e. a field, method or method parameter.
*
* @param <T> The type dependant's type.
* @param <S> The type dependant's token type.
*/
interface TypeDependant<T extends TypeDependant<?, S>, S extends ByteCodeElement.Token<S>> { interface TypeDependant<T extends TypeDependant<?, S>, S extends ByteCodeElement.Token<S>> {


/**
* Returns this type dependant in its defined shape, i.e. the form it is declared in and without its type variable's resolved.
*
* @return This type dependant in its defined shape.
*/
T asDefined(); T asDefined();


/**
* Returns a token representative of this type dependant.
*
* @return A token representative of this type dependant.
*/
S asToken(); S asToken();


/**
* Returns a token representative of this type dependant. All types that are matched by the supplied matcher are replaced by
* {@link net.bytebuddy.dynamic.TargetType} descriptions.
*
* @param targetTypeMatcher A matcher to identify types to be replaced by {@link net.bytebuddy.dynamic.TargetType} descriptions.
* @return A token representative of this type dependant.
*/
S asToken(ElementMatcher<? super GenericTypeDescription> targetTypeMatcher); S asToken(ElementMatcher<? super GenericTypeDescription> targetTypeMatcher);
} }


Expand All @@ -60,7 +83,13 @@ interface TypeDependant<T extends TypeDependant<?, S>, S extends ByteCodeElement
*/ */
interface Token<T extends Token<T>> { interface Token<T extends Token<T>> {


boolean isIdenticalTo(T t); /**
* Checks if this token is fully identical to the provided token.
*
* @param token The token to compare this token with.
* @return {@code true} if this token is identical to the given token.
*/
boolean isIdenticalTo(T token);


/** /**
* Transforms the types represented by this token by applying the given visitor to them. * Transforms the types represented by this token by applying the given visitor to them.
Expand All @@ -70,8 +99,21 @@ interface Token<T extends Token<T>> {
*/ */
T accept(GenericTypeDescription.Visitor<? extends GenericTypeDescription> visitor); T accept(GenericTypeDescription.Visitor<? extends GenericTypeDescription> visitor);


/**
* Returns this token with the given modifiers.
*
* @param modifiers The modifiers to set for this token.
* @return A version of this token with the given modifiers.
*/
T withModifiers(int modifiers); T withModifiers(int modifiers);


/**
* Returns this token with the given modifiers.
*
* @param modifiers The modifiers to add for this token's modifiers.
* @param mask The modifiers to clear for this token's modifiers.
* @return A version of this token with the given modifiers.
*/
T withModifiers(int modifiers, int mask); T withModifiers(int modifiers, int mask);


/** /**
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ public interface AnnotationDescription {
*/ */
<T extends Annotation> Loadable<T> prepare(Class<T> annotationType); <T extends Annotation> Loadable<T> prepare(Class<T> annotationType);


/**
* Returns this annotation's retention policy.
*
* @return This annotation's retention policy.
*/
RetentionPolicy getRetention(); RetentionPolicy getRetention();


/** /**
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public interface FieldDescription extends ByteCodeElement,
NamedElement.WithGenericName, NamedElement.WithGenericName,
ByteCodeElement.TypeDependant<FieldDescription.InDefinedShape, FieldDescription.Token> { ByteCodeElement.TypeDependant<FieldDescription.InDefinedShape, FieldDescription.Token> {


/**
* A representative of a field's non-set default value.
*/
Object NO_DEFAULT_VALUE = null; Object NO_DEFAULT_VALUE = null;


/** /**
Expand All @@ -35,11 +38,17 @@ public interface FieldDescription extends ByteCodeElement,
*/ */
GenericTypeDescription getType(); GenericTypeDescription getType();


/**
* Represents a field in its defined shape, i.e. in the form it is defined by a class without its type variables being resolved.
*/
interface InDefinedShape extends FieldDescription { interface InDefinedShape extends FieldDescription {


@Override @Override
TypeDescription getDeclaringType(); TypeDescription getDeclaringType();


/**
* An abstract base implementation of a field description in its defined shape.
*/
abstract class AbstractBase extends FieldDescription.AbstractBase implements InDefinedShape { abstract class AbstractBase extends FieldDescription.AbstractBase implements InDefinedShape {


@Override @Override
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@


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


Expand All @@ -35,10 +37,17 @@ public interface FieldList<T extends FieldDescription> extends FilterableList<T,
*/ */
ByteCodeElement.Token.TokenList<FieldDescription.Token> asTokenList(ElementMatcher<? super GenericTypeDescription> targetTypeMatcher); ByteCodeElement.Token.TokenList<FieldDescription.Token> 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<FieldDescription.InDefinedShape> asDefined(); FieldList<FieldDescription.InDefinedShape> asDefined();


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


Expand Down Expand Up @@ -112,6 +121,8 @@ 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.
*
* @param <S> The type of field descriptions represented by this list.
*/ */
class Explicit<S extends FieldDescription> extends AbstractBase<S> { class Explicit<S extends FieldDescription> extends AbstractBase<S> {


Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ public interface MethodDescription extends TypeVariableSource,
*/ */
boolean represents(Constructor<?> constructor); boolean represents(Constructor<?> constructor);


/**
* Verifies if this method describes a virtual method, i.e. a method that is inherited by a sub type of this type.
*
* @return {@code true} if this method is virtual.
*/
boolean isVirtual(); boolean isVirtual();


/** /**
Expand Down Expand Up @@ -221,8 +226,16 @@ public interface MethodDescription extends TypeVariableSource,
*/ */
boolean isDefaultValue(Object value); boolean isDefaultValue(Object value);


/**
* Returns a type token that represents this method's raw return and parameter types.
*
* @return A type token that represents this method's raw return and parameter types.
*/
TypeToken asTypeToken(); TypeToken asTypeToken();


/**
* Represents a method in its defined shape, i.e. in the form it is defined by a class without its type variables being resolved.
*/
interface InDefinedShape extends MethodDescription { interface InDefinedShape extends MethodDescription {


@Override @Override
Expand All @@ -231,6 +244,9 @@ interface InDefinedShape extends MethodDescription {
@Override @Override
ParameterList<ParameterDescription.InDefinedShape> getParameters(); ParameterList<ParameterDescription.InDefinedShape> getParameters();


/**
* An abstract base implementation of a method description in its defined shape.
*/
abstract class AbstractBase extends MethodDescription.AbstractBase implements InDefinedShape { abstract class AbstractBase extends MethodDescription.AbstractBase implements InDefinedShape {


@Override @Override
Expand Down Expand Up @@ -1438,6 +1454,19 @@ public Object getDefaultValue() {
return defaultValue; return defaultValue;
} }


/**
* Transforms this method token into a type token.
*
* @return A type token representing the type's of this method token.
*/
public TypeToken asTypeToken() {
List<TypeDescription> parameterTypes = new ArrayList<TypeDescription>(getParameterTokens().size());
for (ParameterDescription.Token parameterToken : getParameterTokens()) {
parameterTypes.add(parameterToken.getType().asRawType());
}
return new TypeToken(getReturnType().asRawType(), parameterTypes);
}

@Override @Override
public Token withModifiers(int modifiers, int mask) { public Token withModifiers(int modifiers, int mask) {
return withModifiers((getModifiers() & ~mask) | modifiers); return withModifiers((getModifiers() & ~mask) | modifiers);
Expand Down Expand Up @@ -1518,31 +1547,48 @@ public String toString() {
", defaultValue=" + defaultValue + ", defaultValue=" + defaultValue +
'}'; '}';
} }

public TypeToken asTypeToken() {
List<TypeDescription> parameterTypes = new ArrayList<TypeDescription>(getParameterTokens().size());
for (ParameterDescription.Token parameterToken : getParameterTokens()) {
parameterTypes.add(parameterToken.getType().asRawType());
}
return new TypeToken(getReturnType().asRawType(), parameterTypes);
}
} }


/**
* A token representing a method's raw type.
*/
class TypeToken { class TypeToken {


/**
* The represented method's raw return type.
*/
private final TypeDescription returnType; private final TypeDescription returnType;


/**
* The represented method's raw parameter types.
*/
private final List<? extends TypeDescription> parameterTypes; private final List<? extends TypeDescription> parameterTypes;


/**
* Creates a new type token.
*
* @param returnType The represented method's raw return type.
* @param parameterTypes The represented method's raw parameter types.
*/
public TypeToken(TypeDescription returnType, List<? extends TypeDescription> parameterTypes) { public TypeToken(TypeDescription returnType, List<? extends TypeDescription> parameterTypes) {
this.returnType = returnType; this.returnType = returnType;
this.parameterTypes = parameterTypes; this.parameterTypes = parameterTypes;
} }


/**
* Returns this token's return type.
*
* @return This token's return type.
*/
public TypeDescription getReturnType() { public TypeDescription getReturnType() {
return returnType; return returnType;
} }


/**
* Returns this token's parameter types.
*
* @return This token's parameter types.
*/
public List<TypeDescription> getParameterTypes() { public List<TypeDescription> getParameterTypes() {
return new ArrayList<TypeDescription>(parameterTypes); return new ArrayList<TypeDescription>(parameterTypes);
} }
Expand Down
Loading

0 comments on commit f095ef0

Please sign in to comment.