Skip to content

Commit

Permalink
Added test for field access.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Apr 9, 2016
1 parent 9fafdb2 commit 6839a84
Show file tree
Hide file tree
Showing 5 changed files with 293 additions and 98 deletions.
99 changes: 50 additions & 49 deletions byte-buddy-dep/src/main/java/net/bytebuddy/asm/Advice.java
Expand Up @@ -947,7 +947,7 @@ protected ForAdvice(MethodDescription.InDefinedShape methodDescription,


@Override @Override
public void recordMaxima(int stackSize, int localVariableLength) { public void recordMaxima(int stackSize, int localVariableLength) {
WithStackSizeComputation.this.stackSize = Math.max(WithStackSizeComputation.this.stackSize, stackSize); WithStackSizeComputation.this.stackSize = Math.max(WithStackSizeComputation.this.stackSize, stackSize) + padding;
WithStackSizeComputation.this.localVariableLength = Math.max(WithStackSizeComputation.this.localVariableLength, localVariableLength WithStackSizeComputation.this.localVariableLength = Math.max(WithStackSizeComputation.this.localVariableLength, localVariableLength
- methodDescription.getStackSize() - methodDescription.getStackSize()
+ instrumentedMethod.getStackSize() + instrumentedMethod.getStackSize()
Expand Down Expand Up @@ -1695,7 +1695,7 @@ public int getPadding() {


@Override @Override
public String toString() { public String toString() {
return "Advice.Dispatcher.Active.Resolved.OffsetMapping.Context.ForMethodEntry." + name(); return "Advice.Dispatcher.OffsetMapping.Context.ForMethodEntry." + name();
} }
} }


Expand Down Expand Up @@ -1764,7 +1764,7 @@ public int getPadding() {


@Override @Override
public String toString() { public String toString() {
return "Advice.Dispatcher.Active.Resolved.OffsetMapping.Context.ForMethodExit." + name(); return "Advice.Dispatcher.OffsetMapping.Context.ForMethodExit." + name();
} }
} }
} }
Expand Down Expand Up @@ -1842,7 +1842,7 @@ public int resolveIncrement(MethodVisitor methodVisitor, int increment) {


@Override @Override
public String toString() { public String toString() {
return "Advice.Dispatcher.Active.Resolved.OffsetMapping.Target.ForDefaultValue." + name(); return "Advice.Dispatcher.OffsetMapping.Target.ForDefaultValue." + name();
} }
} }


Expand Down Expand Up @@ -1892,7 +1892,7 @@ public int hashCode() {


@Override @Override
public String toString() { public String toString() {
return "Advice.Dispatcher.Active.Resolved.OffsetMapping.Target.ForParameter{" + return "Advice.Dispatcher.OffsetMapping.Target.ForParameter{" +
"offset=" + offset + "offset=" + offset +
'}'; '}';
} }
Expand Down Expand Up @@ -1959,7 +1959,7 @@ public int hashCode() {


@Override @Override
public String toString() { public String toString() {
return "Advice.Dispatcher.Active.Resolved.OffsetMapping.Target.ForReadOnlyParameter{" + return "Advice.Dispatcher.OffsetMapping.Target.ForReadOnlyParameter{" +
"offset=" + offset + "offset=" + offset +
'}'; '}';
} }
Expand Down Expand Up @@ -2036,7 +2036,7 @@ public int hashCode() {


@Override @Override
public String toString() { public String toString() {
return "Advice.Dispatcher.Active.Resolved.OffsetMapping.Target.ForReadOnlyField{" + return "Advice.Dispatcher.OffsetMapping.Target.ForReadOnlyField{" +
"fieldDescription=" + fieldDescription + "fieldDescription=" + fieldDescription +
'}'; '}';
} }
Expand Down Expand Up @@ -2141,7 +2141,7 @@ public int hashCode() {


@Override @Override
public String toString() { public String toString() {
return "Advice.Dispatcher.Active.Resolved.OffsetMapping.Target.ForReadOnlyField{" + return "Advice.Dispatcher.OffsetMapping.Target.ForReadOnlyField{" +
"fieldDescription=" + fieldDescription + "fieldDescription=" + fieldDescription +
'}'; '}';
} }
Expand Down Expand Up @@ -2208,7 +2208,7 @@ public int hashCode() {


@Override @Override
public String toString() { public String toString() {
return "Advice.Dispatcher.Active.Resolved.OffsetMapping.Target.ForConstantPoolValue{" + return "Advice.Dispatcher.OffsetMapping.Target.ForConstantPoolValue{" +
"value=" + value + "value=" + value +
'}'; '}';
} }
Expand Down Expand Up @@ -2522,7 +2522,7 @@ public int hashCode() {


@Override @Override
public String toString() { public String toString() {
return "Advice.Dispatcher.Active.Resolved.OffsetMapping.ForParameter{" + return "Advice.Dispatcher.OffsetMapping.ForParameter{" +
"index=" + index + "index=" + index +
", readOnly=" + readOnly + ", readOnly=" + readOnly +
", targetType=" + targetType + ", targetType=" + targetType +
Expand All @@ -2549,7 +2549,7 @@ public OffsetMapping make(ParameterDescription.InDefinedShape parameterDescripti


@Override @Override
public String toString() { public String toString() {
return "Advice.Dispatcher.Active.Resolved.OffsetMapping.ForParameter.Factory." + name(); return "Advice.Dispatcher.OffsetMapping.ForParameter.Factory." + name();
} }
} }
} }
Expand Down Expand Up @@ -2619,7 +2619,7 @@ public int hashCode() {


@Override @Override
public String toString() { public String toString() {
return "Advice.Dispatcher.Active.Resolved.OffsetMapping.ForThisReference{" + return "Advice.Dispatcher.OffsetMapping.ForThisReference{" +
"readOnly=" + readOnly + "readOnly=" + readOnly +
", targetType=" + targetType + ", targetType=" + targetType +
'}'; '}';
Expand All @@ -2645,7 +2645,7 @@ public OffsetMapping make(ParameterDescription.InDefinedShape parameterDescripti


@Override @Override
public String toString() { public String toString() {
return "Advice.Dispatcher.Active.Resolved.OffsetMapping.ForThisReference.Factory." + name(); return "Advice.Dispatcher.OffsetMapping.ForThisReference.Factory." + name();
} }
} }
} }
Expand Down Expand Up @@ -2680,21 +2680,6 @@ protected ForField(String name, TypeDescription targetType, boolean readOnly) {
this.readOnly = readOnly; this.readOnly = readOnly;
} }


@Override
public boolean equals(Object object) {
if (this == object) return true;
if (object == null || getClass() != object.getClass()) return false;
ForField forField = (ForField) object;
return name.equals(forField.name) && targetType.equals(forField.targetType);
}

@Override
public int hashCode() {
int result = name.hashCode();
result = 31 * result + targetType.hashCode();
return result;
}

@Override @Override
public Target resolve(MethodDescription.InDefinedShape instrumentedMethod, Context context) { public Target resolve(MethodDescription.InDefinedShape instrumentedMethod, Context context) {
FieldLocator.Resolution resolution = fieldLocator(instrumentedMethod.getDeclaringType()).locate(name); FieldLocator.Resolution resolution = fieldLocator(instrumentedMethod.getDeclaringType()).locate(name);
Expand All @@ -2714,6 +2699,22 @@ public Target resolve(MethodDescription.InDefinedShape instrumentedMethod, Conte
: new Target.ForField(resolution.getField()); : new Target.ForField(resolution.getField());
} }


@Override
public boolean equals(Object object) {
if (this == object) return true;
if (object == null || getClass() != object.getClass()) return false;
ForField forField = (ForField) object;
return name.equals(forField.name) && targetType.equals(forField.targetType) && readOnly == forField.readOnly;
}

@Override
public int hashCode() {
int result = name.hashCode();
result = 31 * result + targetType.hashCode();
result = 31 * result + (readOnly ? 1 : 0);
return result;
}

/** /**
* Returns a field locator for this instance. * Returns a field locator for this instance.
* *
Expand Down Expand Up @@ -2744,7 +2745,7 @@ protected FieldLocator fieldLocator(TypeDescription instrumentedType) {


@Override @Override
public String toString() { public String toString() {
return "Advice.Dispatcher.Active.Resolved.OffsetMapping.ForField.WithImplicitType{" + return "Advice.Dispatcher.OffsetMapping.ForField.WithImplicitType{" +
"name=" + name + "name=" + name +
", targetType=" + targetType + ", targetType=" + targetType +
'}'; '}';
Expand Down Expand Up @@ -2799,7 +2800,7 @@ public int hashCode() {


@Override @Override
public String toString() { public String toString() {
return "Advice.Dispatcher.Active.Resolved.OffsetMapping.ForField.WithExplicitType{" + return "Advice.Dispatcher.OffsetMapping.ForField.WithExplicitType{" +
"name=" + name + "name=" + name +
", targetType=" + targetType + ", targetType=" + targetType +
", explicitType=" + explicitType + ", explicitType=" + explicitType +
Expand Down Expand Up @@ -2856,7 +2857,7 @@ public OffsetMapping make(ParameterDescription.InDefinedShape parameterDescripti


@Override @Override
public String toString() { public String toString() {
return "Advice.Dispatcher.Active.Resolved.OffsetMapping.ForField.Factory." + name(); return "Advice.Dispatcher.OffsetMapping.ForField.Factory." + name();
} }
} }
} }
Expand Down Expand Up @@ -2970,7 +2971,7 @@ public int hashCode() {


@Override @Override
public String toString() { public String toString() {
return "Advice.Dispatcher.Active.Resolved.OffsetMapping.ForOrigin{" + return "Advice.Dispatcher.OffsetMapping.ForOrigin{" +
"renderers=" + renderers + "renderers=" + renderers +
'}'; '}';
} }
Expand Down Expand Up @@ -3005,7 +3006,7 @@ public String apply(MethodDescription.InDefinedShape instrumentedMethod) {


@Override @Override
public String toString() { public String toString() {
return "Advice.Dispatcher.Active.Resolved.OffsetMapping.ForOrigin.Renderer.ForMethodName." + name(); return "Advice.Dispatcher.OffsetMapping.ForOrigin.Renderer.ForMethodName." + name();
} }
} }


Expand All @@ -3026,7 +3027,7 @@ public String apply(MethodDescription.InDefinedShape instrumentedMethod) {


@Override @Override
public String toString() { public String toString() {
return "Advice.Dispatcher.Active.Resolved.OffsetMapping.ForOrigin.Renderer.ForTypeName." + name(); return "Advice.Dispatcher.OffsetMapping.ForOrigin.Renderer.ForTypeName." + name();
} }
} }


Expand All @@ -3047,7 +3048,7 @@ public String apply(MethodDescription.InDefinedShape instrumentedMethod) {


@Override @Override
public String toString() { public String toString() {
return "Advice.Dispatcher.Active.Resolved.OffsetMapping.ForOrigin.Renderer.ForDescriptor." + name(); return "Advice.Dispatcher.OffsetMapping.ForOrigin.Renderer.ForDescriptor." + name();
} }
} }


Expand All @@ -3068,7 +3069,7 @@ public String apply(MethodDescription.InDefinedShape instrumentedMethod) {


@Override @Override
public String toString() { public String toString() {
return "Advice.Dispatcher.Active.Resolved.OffsetMapping.ForOrigin.Renderer.ForStringRepresentation." + name(); return "Advice.Dispatcher.OffsetMapping.ForOrigin.Renderer.ForStringRepresentation." + name();
} }
} }


Expand Down Expand Up @@ -3111,7 +3112,7 @@ public int hashCode() {


@Override @Override
public String toString() { public String toString() {
return "Advice.Dispatcher.Active.Resolved.OffsetMapping.ForOrigin.Renderer.ForConstantValue{" + return "Advice.Dispatcher.OffsetMapping.ForOrigin.Renderer.ForConstantValue{" +
"value='" + value + '\'' + "value='" + value + '\'' +
'}'; '}';
} }
Expand Down Expand Up @@ -3143,7 +3144,7 @@ public OffsetMapping make(ParameterDescription.InDefinedShape parameterDescripti


@Override @Override
public String toString() { public String toString() {
return "Advice.Dispatcher.Active.Resolved.OffsetMapping.ForOrigin.Factory." + name(); return "Advice.Dispatcher.OffsetMapping.ForOrigin.Factory." + name();
} }
} }
} }
Expand Down Expand Up @@ -3172,7 +3173,7 @@ public OffsetMapping make(ParameterDescription.InDefinedShape parameterDescripti


@Override @Override
public String toString() { public String toString() {
return "Advice.Dispatcher.Active.Resolved.OffsetMapping.ForIgnored." + name(); return "Advice.Dispatcher.OffsetMapping.ForIgnored." + name();
} }
} }


Expand Down Expand Up @@ -3226,7 +3227,7 @@ public Target resolve(MethodDescription.InDefinedShape instrumentedMethod, Conte


@Override @Override
public String toString() { public String toString() {
return "Advice.Dispatcher.Active.Resolved.OffsetMapping.ForEnterValue." + name(); return "Advice.Dispatcher.OffsetMapping.ForEnterValue." + name();
} }


/** /**
Expand Down Expand Up @@ -3277,7 +3278,7 @@ public int hashCode() {


@Override @Override
public String toString() { public String toString() {
return "Advice.Dispatcher.Active.Resolved.OffsetMapping.ForEnterValue.Factory{" + return "Advice.Dispatcher.OffsetMapping.ForEnterValue.Factory{" +
"enterType=" + enterType + "enterType=" + enterType +
'}'; '}';
} }
Expand Down Expand Up @@ -3337,7 +3338,7 @@ public int hashCode() {


@Override @Override
public String toString() { public String toString() {
return "Advice.Dispatcher.Active.Resolved.OffsetMapping.ForReturnValue{" + return "Advice.Dispatcher.OffsetMapping.ForReturnValue{" +
"readOnly=" + readOnly + "readOnly=" + readOnly +
", targetType=" + targetType + ", targetType=" + targetType +
'}'; '}';
Expand All @@ -3363,7 +3364,7 @@ public OffsetMapping make(ParameterDescription.InDefinedShape parameterDescripti


@Override @Override
public String toString() { public String toString() {
return "Advice.Dispatcher.Active.Resolved.OffsetMapping.ForReturnValue.Factory." + name(); return "Advice.Dispatcher.OffsetMapping.ForReturnValue.Factory." + name();
} }
} }
} }
Expand Down Expand Up @@ -3468,7 +3469,7 @@ public Target resolve(MethodDescription.InDefinedShape instrumentedMethod, Conte


@Override @Override
public String toString() { public String toString() {
return "Advice.Dispatcher.Active.Resolved.OffsetMapping.ForThrowable." + name(); return "Advice.Dispatcher.OffsetMapping.ForThrowable." + name();
} }
} }


Expand Down Expand Up @@ -3628,7 +3629,7 @@ public int hashCode() {


@Override @Override
public String toString() { public String toString() {
return "Advice.Dispatcher.Active.Resolved.OffsetMapping.Illegal{" + return "Advice.Dispatcher.OffsetMapping.Illegal{" +
"annotations=" + annotations + "annotations=" + annotations +
'}'; '}';
} }
Expand Down Expand Up @@ -4020,15 +4021,15 @@ protected MethodVisitor apply(MethodVisitor methodVisitor,
adviceMethod, adviceMethod,
offsetMappings, offsetMappings,
adviceMethod.getDeclaredAnnotations().ofType(OnMethodExit.class).getValue(SUPPRESS, TypeDescription.class), adviceMethod.getDeclaredAnnotations().ofType(OnMethodExit.class).getValue(SUPPRESS, TypeDescription.class),
enterType.getStackSize().getSize() + getAdditionalPadding().getSize()); enterType.getStackSize().getSize() + getPadding().getSize());
} }


/** /**
* Returns the additional padding this exit advice implies. * Returns the additional padding this exit advice implies.
* *
* @return The additional padding this exit advice implies. * @return The additional padding this exit advice implies.
*/ */
protected abstract StackSize getAdditionalPadding(); protected abstract StackSize getPadding();


@Override @Override
public boolean equals(Object other) { public boolean equals(Object other) {
Expand Down Expand Up @@ -4063,7 +4064,7 @@ protected WithExceptionHandler(MethodDescription.InDefinedShape adviceMethod,
} }


@Override @Override
protected StackSize getAdditionalPadding() { protected StackSize getPadding() {
return StackSize.SINGLE; return StackSize.SINGLE;
} }


Expand Down Expand Up @@ -4099,7 +4100,7 @@ protected WithoutExceptionHandler(MethodDescription.InDefinedShape adviceMethod,
} }


@Override @Override
protected StackSize getAdditionalPadding() { protected StackSize getPadding() {
return StackSize.SINGLE; return StackSize.SINGLE;
} }


Expand Down
Expand Up @@ -18,10 +18,13 @@ public class AdviceAnnotationTest {
public static Collection<Object[]> data() { public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{ return Arrays.asList(new Object[][]{
{Advice.Argument.class, ElementType.PARAMETER}, {Advice.Argument.class, ElementType.PARAMETER},
{Advice.BoxedArguments.class, ElementType.PARAMETER},
{Advice.BoxedReturn.class, ElementType.PARAMETER},
{Advice.FieldValue.class, ElementType.PARAMETER}, {Advice.FieldValue.class, ElementType.PARAMETER},
{Advice.Enter.class, ElementType.PARAMETER}, {Advice.Enter.class, ElementType.PARAMETER},
{Advice.Ignored.class, ElementType.PARAMETER}, {Advice.Ignored.class, ElementType.PARAMETER},
{Advice.Origin.class, ElementType.PARAMETER}, {Advice.Origin.class, ElementType.PARAMETER},
{Advice.OriginType.class, ElementType.PARAMETER},
{Advice.Return.class, ElementType.PARAMETER}, {Advice.Return.class, ElementType.PARAMETER},
{Advice.This.class, ElementType.PARAMETER}, {Advice.This.class, ElementType.PARAMETER},
{Advice.Thrown.class, ElementType.PARAMETER}, {Advice.Thrown.class, ElementType.PARAMETER},
Expand Down
Expand Up @@ -11,7 +11,7 @@
import static net.bytebuddy.matcher.ElementMatchers.named; import static net.bytebuddy.matcher.ElementMatchers.named;


@RunWith(Parameterized.class) @RunWith(Parameterized.class)
public class AdviceWriteTest { public class AdviceParameterWriteTest {


private static final String FOO = "foo", BAR = "bar", ENTER = "enter", EXIT = "exit", exception = "exception"; private static final String FOO = "foo", BAR = "bar", ENTER = "enter", EXIT = "exit", exception = "exception";


Expand All @@ -36,7 +36,7 @@ public static Collection<Object[]> data() {


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


public AdviceWriteTest(Class<?> type) { public AdviceParameterWriteTest(Class<?> type) {
this.type = type; this.type = type;
} }


Expand Down

0 comments on commit 6839a84

Please sign in to comment.