Skip to content

Commit

Permalink
Minor API improvement when binding fixed values.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Jul 25, 2016
1 parent a02a706 commit 13512f0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
15 changes: 14 additions & 1 deletion byte-buddy-dep/src/main/java/net/bytebuddy/asm/Advice.java
Expand Up @@ -7837,6 +7837,19 @@ protected WithCustomMapping(Map<Class<? extends Annotation>, DynamicValue<?>> dy
this.dynamicValues = dynamicValues; this.dynamicValues = dynamicValues;
} }


/**
* Binds the supplied annotation to the supplied fixed value.
*
* @param type The type of the annotation being bound.
* @param value The type reference to bind to this annotation.
* @param <T> The annotation type.
* @return A new builder for an advice that considers the supplied annotation type during binding.
* @see DynamicValue.ForFixedValue
*/
public <T extends Annotation> WithCustomMapping bind(Class<? extends T> type, TypeDescription value) {
return bind(type, new DynamicValue.ForFixedValue(value));
}

/** /**
* Binds the supplied annotation to the supplied fixed value. * Binds the supplied annotation to the supplied fixed value.
* *
Expand All @@ -7846,7 +7859,7 @@ protected WithCustomMapping(Map<Class<? extends Annotation>, DynamicValue<?>> dy
* @return A new builder for an advice that considers the supplied annotation type during binding. * @return A new builder for an advice that considers the supplied annotation type during binding.
* @see DynamicValue.ForFixedValue * @see DynamicValue.ForFixedValue
*/ */
public <T extends Annotation> WithCustomMapping bind(Class<? extends T> type, Object value) { public <T extends Annotation> WithCustomMapping bind(Class<? extends T> type, Serializable value) {
return bind(type, new DynamicValue.ForFixedValue(value)); return bind(type, new DynamicValue.ForFixedValue(value));
} }


Expand Down
Expand Up @@ -17,6 +17,7 @@
import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes; import org.objectweb.asm.Opcodes;


import java.io.Serializable;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.util.Arrays; import java.util.Arrays;
Expand All @@ -33,11 +34,11 @@ public class AdviceInconsistentStackSizeTest {


private final Class<?> type; private final Class<?> type;


private final Object original, replaced; private final Serializable original, replaced;


private final int opcode; private final int opcode;


public AdviceInconsistentStackSizeTest(Class<?> type, Object original, Object replaced, int opcode) { public AdviceInconsistentStackSizeTest(Class<?> type, Serializable original, Serializable replaced, int opcode) {
this.type = type; this.type = type;
this.original = original; this.original = original;
this.replaced = replaced; this.replaced = replaced;
Expand Down Expand Up @@ -75,7 +76,7 @@ public void testInconsistentStackSize() throws Exception {
.make() .make()
.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER) .load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER)
.getLoaded(); .getLoaded();
assertThat(adviced.getDeclaredMethod(FOO).invoke(adviced.newInstance()), is(replaced)); assertThat(adviced.getDeclaredMethod(FOO).invoke(adviced.newInstance()), is((Object) replaced));
} }


@Test @Test
Expand Down
Expand Up @@ -14,6 +14,7 @@
import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.MethodVisitor;


import java.io.IOException; import java.io.IOException;
import java.io.Serializable;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
Expand Down Expand Up @@ -1547,12 +1548,12 @@ public Object resolve(MethodDescription.InDefinedShape instrumentedMethod,
boolean initialized) { boolean initialized) {
return null; return null;
} }
}).bind(Custom.class, null); }).bind(Custom.class, (Serializable) null);
} }


@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testNotAnnotationType() throws Exception { public void testNotAnnotationType() throws Exception {
Advice.withCustomMapping().bind(Annotation.class, null); Advice.withCustomMapping().bind(Annotation.class, (Serializable) null);
} }


@Test(expected = IllegalStateException.class) @Test(expected = IllegalStateException.class)
Expand Down
Expand Up @@ -13,6 +13,7 @@
import org.objectweb.asm.Type; import org.objectweb.asm.Type;


import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.Serializable;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
Expand Down Expand Up @@ -70,9 +71,9 @@ public static Collection<Object[]> data() {


private final Class<?> type; private final Class<?> type;


private final Object value; private final Serializable value;


public AdviceTypeTest(Class<?> advice, Class<?> type, Object value) { public AdviceTypeTest(Class<?> advice, Class<?> type, Serializable value) {
this.advice = advice; this.advice = advice;
this.type = type; this.type = type;
this.value = value; this.value = value;
Expand All @@ -90,7 +91,7 @@ public void testAdvice() throws Exception {
.make() .make()
.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER) .load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER)
.getLoaded(); .getLoaded();
assertThat(type.getDeclaredMethod(FOO, this.type, this.type).invoke(type.newInstance(), value, value), is(value)); assertThat(type.getDeclaredMethod(FOO, this.type, this.type).invoke(type.newInstance(), value, value), is((Object) value));
assertThat(type.getDeclaredField(ENTER).get(null), is((Object) 1)); assertThat(type.getDeclaredField(ENTER).get(null), is((Object) 1));
assertThat(type.getDeclaredField(EXIT).get(null), is((Object) 1)); assertThat(type.getDeclaredField(EXIT).get(null), is((Object) 1));
} }
Expand All @@ -109,7 +110,7 @@ public void testAdviceWithException() throws Exception {
.getLoaded(); .getLoaded();
type.getDeclaredField(exception).set(null, true); type.getDeclaredField(exception).set(null, true);
try { try {
assertThat(type.getDeclaredMethod(BAR, this.type, this.type).invoke(type.newInstance(), value, value), is(value)); assertThat(type.getDeclaredMethod(BAR, this.type, this.type).invoke(type.newInstance(), value, value), is((Object) value));
fail(); fail();
} catch (InvocationTargetException exception) { } catch (InvocationTargetException exception) {
assertThat(exception.getCause(), instanceOf(RuntimeException.class)); assertThat(exception.getCause(), instanceOf(RuntimeException.class));
Expand Down

0 comments on commit 13512f0

Please sign in to comment.