Skip to content

Commit

Permalink
Changed byte code constants for methods to only being able to represe…
Browse files Browse the repository at this point in the history
…nted methods in declared form.
  • Loading branch information
raphw committed Jul 19, 2015
1 parent 3674066 commit 85b99a4
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 18 deletions.
Expand Up @@ -170,8 +170,8 @@ protected ByteCodeAppender.Size apply(MethodVisitor methodVisitor,
.filter((named(fieldName))).getOnly()).getter(),
MethodVariableAccess.forType(TypeDescription.OBJECT).loadOffset(0),
cacheMethods
? MethodConstant.forMethod(instrumentedMethod).cached()
: MethodConstant.forMethod(instrumentedMethod),
? MethodConstant.forMethod(instrumentedMethod.asDeclared()).cached()
: MethodConstant.forMethod(instrumentedMethod.asDeclared()),
ArrayFactory.forType(TypeDescription.OBJECT).withValues(argumentValuesOf(instrumentedMethod)),
MethodInvocation.invoke(INVOCATION_HANDLER_TYPE.getDeclaredMethods().getOnly()),
assigner.assign(TypeDescription.OBJECT, instrumentedMethod.getReturnType().asRawType(), Assigner.DYNAMICALLY_TYPED),
Expand Down
Expand Up @@ -80,16 +80,16 @@ public MethodDelegationBinder.ParameterBinding<?> bind(AnnotationDescription.Loa
return new MethodDelegationBinder.ParameterBinding.Anonymous(ClassConstant.of(implementationTarget.getOriginType()));
} else if (parameterType.represents(Method.class)) {
return new MethodDelegationBinder.ParameterBinding.Anonymous(annotation.loadSilent().cache()
? MethodConstant.forMethod(source).cached()
: MethodConstant.forMethod(source));
? MethodConstant.forMethod(source.asDeclared()).cached()
: MethodConstant.forMethod(source.asDeclared()));
} else if (parameterType.represents(String.class)) {
return new MethodDelegationBinder.ParameterBinding.Anonymous(new TextConstant(source.toString()));
} else if (parameterType.represents(int.class)) {
return new MethodDelegationBinder.ParameterBinding.Anonymous(IntegerConstant.forValue(source.getModifiers()));
} else if (parameterType.equals(JavaType.METHOD_HANDLE.getTypeStub())) {
return new MethodDelegationBinder.ParameterBinding.Anonymous(MethodHandleConstant.of(source));
return new MethodDelegationBinder.ParameterBinding.Anonymous(MethodHandleConstant.of(source.asDeclared()));
} else if (parameterType.equals(JavaType.METHOD_TYPE.getTypeStub())) {
return new MethodDelegationBinder.ParameterBinding.Anonymous(MethodTypeConstant.of(source));
return new MethodDelegationBinder.ParameterBinding.Anonymous(MethodTypeConstant.of(source.asDeclared()));
} else {
throw new IllegalStateException("The " + target + " method's " + target.getIndex() +
" parameter is annotated with a Origin annotation with an argument not representing a Class," +
Expand Down
Expand Up @@ -28,15 +28,15 @@ public abstract class MethodConstant implements StackManipulation {
/**
* A description of the method to be loaded onto the stack.
*/
protected final MethodDescription methodDescription;
protected final MethodDescription.InDeclaredForm methodDescription;

/**
* Creates a new method constant.
*
* @param methodDescription The method description for which the {@link java.lang.reflect.Method} representation
* should be created.
*/
protected MethodConstant(MethodDescription methodDescription) {
protected MethodConstant(MethodDescription.InDeclaredForm methodDescription) {
this.methodDescription = methodDescription;
}

Expand All @@ -46,7 +46,7 @@ protected MethodConstant(MethodDescription methodDescription) {
* @param methodDescription The method to be loaded onto the stack.
* @return A stack manipulation that assigns a method constant for the given method description.
*/
public static CanCache forMethod(MethodDescription methodDescription) {
public static CanCache forMethod(MethodDescription.InDeclaredForm methodDescription) {
if (methodDescription.isTypeInitializer()) {
return CanCacheIllegal.INSTANCE;
} else if (methodDescription.isConstructor()) {
Expand Down Expand Up @@ -204,7 +204,7 @@ protected static class ForMethod extends MethodConstant implements CanCache {
*
* @param methodDescription The method to be loaded onto the stack.
*/
protected ForMethod(MethodDescription methodDescription) {
protected ForMethod(MethodDescription.InDeclaredForm methodDescription) {
super(methodDescription);
}

Expand Down Expand Up @@ -254,7 +254,7 @@ protected static class ForConstructor extends MethodConstant implements CanCache
*
* @param methodDescription The constructor to be loaded onto the stack.
*/
protected ForConstructor(MethodDescription methodDescription) {
protected ForConstructor(MethodDescription.InDeclaredForm methodDescription) {
super(methodDescription);
}

Expand Down
Expand Up @@ -42,7 +42,7 @@ private MethodHandleConstant(Handle handle) {
* @param methodDescription The method for which a method handle is to be put onto the operand stack.
* @return A stack manipulation that representedBy the loading of the handle.
*/
public static StackManipulation of(MethodDescription methodDescription) {
public static StackManipulation of(MethodDescription.InDeclaredForm methodDescription) {
return methodDescription.isTypeInitializer()
? Illegal.INSTANCE
: new MethodHandleConstant(new Handle(tagFor(methodDescription),
Expand Down
Expand Up @@ -39,7 +39,7 @@ protected MethodTypeConstant(Type methodType) {
* @param methodDescription The method of which the method type should be loaded onto the operand stack.
* @return A stack manipulation that loads the method type of the given method onto the operand stack.
*/
public static StackManipulation of(MethodDescription methodDescription) {
public static StackManipulation of(MethodDescription.InDeclaredForm methodDescription) {
return new MethodTypeConstant(Type.getMethodType(methodDescription.getDescriptor()));
}

Expand Down
@@ -1,5 +1,6 @@
package net.bytebuddy.implementation.bind.annotation;

import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.implementation.bind.MethodDelegationBinder;
import net.bytebuddy.test.utility.JavaVersionRule;
Expand Down Expand Up @@ -27,6 +28,9 @@ public class OriginBinderTest extends AbstractAnnotationBinderTest<Origin> {
@Mock
private TypeDescription targetType;

@Mock
private MethodDescription.InDeclaredForm methodDescription;

public OriginBinderTest() {
super(Origin.class);
}
Expand All @@ -37,6 +41,7 @@ public void setUp() throws Exception {
super.setUp();
when(target.getType()).thenReturn(targetType);
when(targetType.asRawType()).thenReturn(targetType);
when(source.asDeclared()).thenReturn(methodDescription);
}

@Override
Expand Down Expand Up @@ -88,7 +93,7 @@ public void testMethodHandleBinding() throws Exception {
when(target.getType()).thenReturn(targetType);
TypeDescription typeDescription = mock(TypeDescription.class);
when(typeDescription.asRawType()).thenReturn(typeDescription);
when(source.getDeclaringType()).thenReturn(typeDescription);
when(methodDescription.getDeclaringType()).thenReturn(typeDescription);
MethodDelegationBinder.ParameterBinding<?> parameterBinding = Origin.Binder.INSTANCE
.bind(annotationDescription, source, target, implementationTarget, assigner);
assertThat(parameterBinding.isValid(), is(true));
Expand All @@ -99,7 +104,7 @@ public void testMethodHandleBinding() throws Exception {
public void testMethodTypeBinding() throws Exception {
targetType = new TypeDescription.ForLoadedType(JavaType.METHOD_TYPE.load());
when(target.getType()).thenReturn(targetType);
when(source.getDescriptor()).thenReturn(FOO);
when(methodDescription.getDescriptor()).thenReturn(FOO);
MethodDelegationBinder.ParameterBinding<?> parameterBinding = Origin.Binder.INSTANCE
.bind(annotationDescription, source, target, implementationTarget, assigner);
assertThat(parameterBinding.isValid(), is(true));
Expand Down
Expand Up @@ -35,7 +35,7 @@ public class MethodConstantTest {
public TestRule mockitoRule = new MockitoRule(this);

@Mock
private MethodDescription methodDescription;
private MethodDescription.InDeclaredForm methodDescription;

@Mock
private TypeDescription declaringType, parameterType, fieldType;
Expand Down
Expand Up @@ -32,7 +32,7 @@ public class MethodHandleConstantTest {
public TestRule mockitoRule = new MockitoRule(this);

@Mock
private MethodDescription methodDescription;
private MethodDescription.InDeclaredForm methodDescription;

@Mock
private FieldDescription fieldDescription;
Expand Down
Expand Up @@ -28,7 +28,7 @@ public class MethodTypeConstantTest {
public TestRule mockitoRule = new MockitoRule(this);

@Mock
private MethodDescription methodDescription;
private MethodDescription.InDeclaredForm methodDescription;

@Mock
private MethodVisitor methodVisitor;
Expand Down

0 comments on commit 85b99a4

Please sign in to comment.