Skip to content

Commit

Permalink
Added improved unit tests for dynamic type builders.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafael Winterhalter committed Apr 13, 2015
1 parent 02c088e commit f7b3047
Show file tree
Hide file tree
Showing 10 changed files with 555 additions and 1,085 deletions.
60 changes: 41 additions & 19 deletions byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/DynamicType.java
Expand Up @@ -652,25 +652,29 @@ ExceptionDeclarableMethodInterception<T> defineConstructor(List<? extends TypeDe
* Defines an instrumentation for a method that was added to this instrumentation or a to method selection * Defines an instrumentation for a method that was added to this instrumentation or a to method selection
* of existing methods. * of existing methods.
* *
* @param <T> The most specific known loaded type that is implemented by the created dynamic type, usually the * @param <S> The most specific known loaded type that is implemented by the created dynamic type, usually the
* type itself, an interface or the direct super class. * type itself, an interface or the direct super class.
*/ */
interface MatchedMethodInterception<T> { interface MatchedMethodInterception<S> {


/** /**
* Intercepts the currently selected method by a given instrumentation. * Intercepts the currently selected method by a given instrumentation.
* *
* @param instrumentation An instrumentation to apply to the currently selected method. * @param instrumentation An instrumentation to apply to the currently selected method.
* @return A builder which will intercept the currently selected methods by the given instrumentation. * @return A builder which will intercept the currently selected methods by the given instrumentation.
*/ */
MethodAnnotationTarget<T> intercept(Instrumentation instrumentation); MethodAnnotationTarget<S> intercept(Instrumentation instrumentation);


/** /**
* Implements the currently selected methods as {@code abstract} methods. * Implements the currently selected methods as {@code abstract} methods.
* *
* @return A builder which will implement the currently selected methods as {@code abstract} methods. * @return A builder which will implement the currently selected methods as {@code abstract} methods.
*/ */
MethodAnnotationTarget<T> withoutCode(); MethodAnnotationTarget<S> withoutCode();

MethodAnnotationTarget<S> withDefaultValue(Object value, Class<?> type);

MethodAnnotationTarget<S> withDefaultValue(Object value);
} }


/** /**
Expand Down Expand Up @@ -699,10 +703,6 @@ interface ExceptionDeclarableMethodInterception<S> extends MatchedMethodIntercep
* types. * types.
*/ */
MatchedMethodInterception<S> throwing(TypeDescription... type); MatchedMethodInterception<S> throwing(TypeDescription... type);

MethodAnnotationTarget<S> withDefaultValue(Object value);

MethodAnnotationTarget<S> withUnloadedDefaultValue(Object value);
} }


/** /**
Expand Down Expand Up @@ -2469,6 +2469,23 @@ public MethodAnnotationTarget<S> withoutCode() {
defaultMethodAttributeAppenderFactory); defaultMethodAttributeAppenderFactory);
} }


@Override
public MethodAnnotationTarget<S> withDefaultValue(Object value, Class<?> type) {
TypeDescription typeDescription = new TypeDescription.ForLoadedType(nonNull(type));
if (!typeDescription.isAnnotationValue()) {
throw new IllegalArgumentException("Not an annotation value: " + type);
}
return withDefaultValue(AnnotationDescription.ForLoadedAnnotation.wrap(value, typeDescription));
}

@Override
public MethodAnnotationTarget<S> withDefaultValue(Object value) {
return new DefaultMethodAnnotationTarget(methodTokens,
methodMatcher,
new MethodRegistry.Handler.ForAnnotationValue(value),
MethodAttributeAppender.NoOp.INSTANCE);
}

@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public boolean equals(Object other) { public boolean equals(Object other) {
Expand Down Expand Up @@ -2531,8 +2548,7 @@ private DefaultExceptionDeclarableMethodInterception(MethodToken methodToken) {


@Override @Override
public MatchedMethodInterception<S> throwing(Class<?>... type) { public MatchedMethodInterception<S> throwing(Class<?>... type) {
return throwing( return throwing(new TypeList.ForLoadedType(nonNull(type)).toArray(new TypeDescription[type.length]));
new TypeList.ForLoadedType(nonNull(type)).toArray(new TypeDescription[type.length]));
} }


@Override @Override
Expand All @@ -2555,16 +2571,13 @@ public MethodAnnotationTarget<S> withoutCode() {
} }


@Override @Override
public MethodAnnotationTarget<S> withDefaultValue(Object value) { public MethodAnnotationTarget<S> withDefaultValue(Object value, Class<?> type) {
return withUnloadedDefaultValue(AnnotationDescription.ForLoadedAnnotation.wrap(value, methodToken.getReturnType())); return materialize(methodToken).withDefaultValue(value, type);
} }


@Override @Override
public MethodAnnotationTarget<S> withUnloadedDefaultValue(Object value) { public MethodAnnotationTarget<S> withDefaultValue(Object value) {
return new DefaultMethodAnnotationTarget(join(methodTokens, methodToken), return materialize(methodToken).withDefaultValue(value);
methodToken,
MethodRegistry.Handler.ForAnnotationValue.of(value, methodToken.getReturnType()),
MethodAttributeAppender.NoOp.INSTANCE);
} }


/** /**
Expand Down Expand Up @@ -2756,15 +2769,24 @@ private DefaultOptionalMatchedMethodInterception(TypeDescription[] interfaceType


@Override @Override
public MethodAnnotationTarget<S> intercept(Instrumentation instrumentation) { public MethodAnnotationTarget<S> intercept(Instrumentation instrumentation) {
return materialize().method(isDeclaredBy(anyOf((Object[]) interfaceType))) return materialize().method(isDeclaredBy(anyOf((Object[]) interfaceType))).intercept(nonNull(instrumentation));
.intercept(nonNull(instrumentation));
} }


@Override @Override
public MethodAnnotationTarget<S> withoutCode() { public MethodAnnotationTarget<S> withoutCode() {
return materialize().method(isDeclaredBy(anyOf((Object[]) interfaceType))).withoutCode(); return materialize().method(isDeclaredBy(anyOf((Object[]) interfaceType))).withoutCode();
} }


@Override
public MethodAnnotationTarget<S> withDefaultValue(Object value, Class<?> type) {
return materialize().method(isDeclaredBy(anyOf((Object[]) interfaceType))).withDefaultValue(value, type);
}

@Override
public MethodAnnotationTarget<S> withDefaultValue(Object value) {
return materialize().method(isDeclaredBy(anyOf((Object[]) interfaceType))).withDefaultValue(value);
}

@Override @Override
protected DynamicType.Builder<S> materialize() { protected DynamicType.Builder<S> materialize() {
return AbstractBase.this.materialize(classFileVersion, return AbstractBase.this.materialize(classFileVersion,
Expand Down
Expand Up @@ -142,26 +142,9 @@ public String toString() {


class ForAnnotationValue implements Handler, Compiled { class ForAnnotationValue implements Handler, Compiled {


public static Handler of(Object annotationValue, TypeDescription typeDescription) {
if (!typeDescription.isAnnotationValue()) {
throw new IllegalArgumentException("Not an annotation value type: " + typeDescription);
} else if (!typeDescription.isInstance(annotationValue)
|| (annotationValue.getClass() == Boolean.class && typeDescription.represents(boolean.class))
|| (annotationValue.getClass() == Byte.class && typeDescription.represents(byte.class))
|| (annotationValue.getClass() == Short.class && typeDescription.represents(short.class))
|| (annotationValue.getClass() == Character.class && typeDescription.represents(char.class))
|| (annotationValue.getClass() == Integer.class && typeDescription.represents(int.class))
|| (annotationValue.getClass() == Long.class && typeDescription.represents(long.class))
|| (annotationValue.getClass() == Float.class && typeDescription.represents(float.class))
|| (annotationValue.getClass() == Double.class && typeDescription.represents(double.class))) {
throw new IllegalArgumentException(annotationValue + " is no instance of " + typeDescription);
}
return new ForAnnotationValue(annotationValue);
}

private final Object annotationValue; private final Object annotationValue;


protected ForAnnotationValue(Object annotationValue) { public ForAnnotationValue(Object annotationValue) {
this.annotationValue = annotationValue; this.annotationValue = annotationValue;
} }


Expand Down

This file was deleted.

0 comments on commit f7b3047

Please sign in to comment.