Skip to content

Commit

Permalink
First step in migrating stack manipulation to usage of stack manipula…
Browse files Browse the repository at this point in the history
…tions.
  • Loading branch information
raphw committed Nov 15, 2016
1 parent cc3b2d2 commit da5a55f
Show file tree
Hide file tree
Showing 23 changed files with 581 additions and 1,775 deletions.
Expand Up @@ -4798,12 +4798,12 @@ protected Appender(List<FieldDescription.InDefinedShape> declaredFields) {
public Size apply(MethodVisitor methodVisitor, Context implementationContext, MethodDescription instrumentedMethod) {
List<StackManipulation> fieldAssignments = new ArrayList<StackManipulation>(declaredFields.size() * 3);
for (ParameterDescription parameterDescription : instrumentedMethod.getParameters()) {
fieldAssignments.add(MethodVariableAccess.REFERENCE.loadOffset(0));
fieldAssignments.add(MethodVariableAccess.of(parameterDescription.getType()).loadOffset(parameterDescription.getOffset()));
fieldAssignments.add(MethodVariableAccess.REFERENCE.loadFrom(0));
fieldAssignments.add(MethodVariableAccess.of(parameterDescription.getType()).loadFrom(parameterDescription.getOffset()));
fieldAssignments.add(FieldAccess.forField(declaredFields.get(parameterDescription.getIndex())).putter());
}
return new Size(new StackManipulation.Compound(
MethodVariableAccess.REFERENCE.loadOffset(0),
MethodVariableAccess.REFERENCE.loadFrom(0),
MethodInvocation.invoke(INSTANCE.objectConstructor),
new StackManipulation.Compound(fieldAssignments),
MethodReturn.VOID
Expand Down Expand Up @@ -5011,12 +5011,12 @@ protected Appender(MethodDescription targetMethod,
public Size apply(MethodVisitor methodVisitor, Context implementationContext, MethodDescription instrumentedMethod) {
List<StackManipulation> fieldAccess = new ArrayList<StackManipulation>(declaredFields.size() * 2);
for (FieldDescription.InDefinedShape fieldDescription : declaredFields) {
fieldAccess.add(MethodVariableAccess.REFERENCE.loadOffset(0));
fieldAccess.add(MethodVariableAccess.REFERENCE.loadFrom(0));
fieldAccess.add(FieldAccess.forField(fieldDescription).getter());
}
List<StackManipulation> parameterAccess = new ArrayList<StackManipulation>(instrumentedMethod.getParameters().size() * 2);
for (ParameterDescription parameterDescription : instrumentedMethod.getParameters()) {
parameterAccess.add(MethodVariableAccess.of(parameterDescription.getType()).loadOffset(parameterDescription.getOffset()));
parameterAccess.add(MethodVariableAccess.of(parameterDescription.getType()).loadFrom(parameterDescription.getOffset()));
parameterAccess.add(Assigner.DEFAULT.assign(parameterDescription.getType(),
specializedLambdaMethod.getParameterTypes().get(parameterDescription.getIndex()).asGenericType(),
Assigner.Typing.DYNAMIC));
Expand Down Expand Up @@ -5127,7 +5127,7 @@ public ByteCodeAppender appender(Target implementationTarget) {
}
List<StackManipulation> lambdaArguments = new ArrayList<StackManipulation>(implementationTarget.getInstrumentedType().getDeclaredFields().size());
for (FieldDescription.InDefinedShape fieldDescription : implementationTarget.getInstrumentedType().getDeclaredFields()) {
lambdaArguments.add(new StackManipulation.Compound(MethodVariableAccess.REFERENCE.loadOffset(0),
lambdaArguments.add(new StackManipulation.Compound(MethodVariableAccess.REFERENCE.loadFrom(0),
FieldAccess.forField(fieldDescription).getter(),
Assigner.DEFAULT.assign(fieldDescription.getType(), TypeDescription.Generic.OBJECT, Assigner.Typing.STATIC)));
}
Expand Down
2,010 changes: 308 additions & 1,702 deletions byte-buddy-dep/src/main/java/net/bytebuddy/asm/Advice.java

Large diffs are not rendered by default.

Expand Up @@ -125,7 +125,7 @@ protected StackManipulation setter(FieldDescription fieldDescription, ParameterD
}
return access(fieldDescription,
parameterDescription.getDeclaringMethod(),
new StackManipulation.Compound(MethodVariableAccess.of(fieldDescription.getType().asErasure()).loadOffset(parameterDescription.getOffset()),
new StackManipulation.Compound(MethodVariableAccess.of(fieldDescription.getType().asErasure()).loadFrom(parameterDescription.getOffset()),
assigner.assign(parameterDescription.getType(), fieldDescription.getType(), typing),
FieldAccess.forField(fieldDescription).putter()));
}
Expand All @@ -146,7 +146,7 @@ private StackManipulation access(FieldDescription fieldDescription, MethodDescri
}
return new StackManipulation.Compound(fieldDescription.isStatic()
? StackManipulation.Trivial.INSTANCE
: MethodVariableAccess.REFERENCE.loadOffset(0), fieldAccess);
: MethodVariableAccess.REFERENCE.loadFrom(0), fieldAccess);
}

@Override
Expand Down
Expand Up @@ -459,7 +459,7 @@ public Size apply(MethodVisitor methodVisitor, Context implementationContext, Me
throw new IllegalStateException("Cannot return 'this' from " + instrumentedMethod);
}
return new ByteCodeAppender.Simple(
MethodVariableAccess.REFERENCE.loadOffset(0),
MethodVariableAccess.REFERENCE.loadFrom(0),
MethodReturn.REFERENCE
).apply(methodVisitor, implementationContext, instrumentedMethod);
}
Expand Down Expand Up @@ -525,7 +525,7 @@ public Size apply(MethodVisitor methodVisitor, Context implementationContext, Me
}
ParameterDescription parameterDescription = instrumentedMethod.getParameters().get(index);
StackManipulation stackManipulation = new StackManipulation.Compound(
MethodVariableAccess.of(parameterDescription.getType()).loadOffset(parameterDescription.getOffset()),
MethodVariableAccess.of(parameterDescription.getType()).loadFrom(parameterDescription.getOffset()),
assigner.assign(parameterDescription.getType(), instrumentedMethod.getReturnType(), typing),
MethodReturn.of(instrumentedMethod.getReturnType())
);
Expand Down
Expand Up @@ -394,7 +394,7 @@ public Size apply(MethodVisitor methodVisitor, Context implementationContext, Me
StackManipulation.Size stackSize = new StackManipulation.Compound(
fieldDescription.isStatic()
? StackManipulation.Trivial.INSTANCE
: MethodVariableAccess.REFERENCE.loadOffset(0),
: MethodVariableAccess.REFERENCE.loadFrom(0),
FieldAccess.forField(fieldDescription).getter(),
MethodVariableAccess.allArgumentsOf(instrumentedMethod),
MethodInvocation.invoke(instrumentedMethod).virtual(fieldDescription.getType().asErasure()),
Expand Down
Expand Up @@ -1519,7 +1519,7 @@ public Size apply(MethodVisitor methodVisitor, Context implementationContext, Me
StackManipulation.Size stackSize = new StackManipulation.Compound(
fieldDescription.isStatic()
? StackManipulation.Trivial.INSTANCE
: MethodVariableAccess.REFERENCE.loadOffset(0),
: MethodVariableAccess.REFERENCE.loadFrom(0),
FieldAccess.forField(fieldDescription).getter(),
MethodReturn.of(fieldDescription.getType().asErasure())
).apply(methodVisitor, implementationContext);
Expand Down
Expand Up @@ -140,7 +140,7 @@ private List<StackManipulation> argumentValuesOf(MethodDescription instrumentedM
int currentIndex = 1;
for (TypeDescription.Generic parameterType : parameterTypes) {
instruction.add(new StackManipulation.Compound(
MethodVariableAccess.of(parameterType).loadOffset(currentIndex),
MethodVariableAccess.of(parameterType).loadFrom(currentIndex),
assigner.assign(parameterType, TypeDescription.Generic.OBJECT, Assigner.Typing.STATIC)));
currentIndex += parameterType.getStackSize().getSize();
}
Expand Down Expand Up @@ -177,7 +177,7 @@ protected ByteCodeAppender.Size apply(MethodVisitor methodVisitor,
StackManipulation.Size stackSize = new StackManipulation.Compound(
preparingManipulation,
FieldAccess.forField(fieldDescription).getter(),
MethodVariableAccess.REFERENCE.loadOffset(0),
MethodVariableAccess.REFERENCE.loadFrom(0),
cacheMethods
? MethodConstant.forMethod(instrumentedMethod.asDefined()).cached()
: MethodConstant.forMethod(instrumentedMethod.asDefined()),
Expand Down Expand Up @@ -465,7 +465,7 @@ public Size apply(MethodVisitor methodVisitor, Context implementationContext, Me
instrumentedMethod,
fieldDescription.isStatic()
? StackManipulation.Trivial.INSTANCE
: MethodVariableAccess.REFERENCE.loadOffset(0),
: MethodVariableAccess.REFERENCE.loadFrom(0),
fieldDescription);
}

Expand Down
Expand Up @@ -1511,7 +1511,7 @@ public Resolved resolve(TypeDescription instrumentedType, MethodDescription inst
} else if (!instrumentedType.isAssignableTo(typeDescription)) {
throw new IllegalStateException(instrumentedType + " is not assignable to " + instrumentedType);
}
return new Resolved.Simple(MethodVariableAccess.REFERENCE.loadOffset(0), typeDescription);
return new Resolved.Simple(MethodVariableAccess.REFERENCE.loadFrom(0), typeDescription);
}

@Override
Expand Down Expand Up @@ -1661,7 +1661,7 @@ public Resolved resolve(TypeDescription instrumentedType, MethodDescription inst
}
return doResolve(new StackManipulation.Compound(resolution.getField().isStatic()
? StackManipulation.Trivial.INSTANCE
: MethodVariableAccess.REFERENCE.loadOffset(0), FieldAccess.forField(resolution.getField()).getter()),
: MethodVariableAccess.REFERENCE.loadFrom(0), FieldAccess.forField(resolution.getField()).getter()),
resolution.getField().getType(),
assigner,
typing);
Expand Down Expand Up @@ -1788,7 +1788,7 @@ public Resolved resolve(TypeDescription instrumentedType, MethodDescription inst
if (index >= parameters.size()) {
throw new IllegalStateException("No parameter " + index + " for " + instrumentedMethod);
}
return doResolve(MethodVariableAccess.of(parameters.get(index).getType().asErasure()).loadOffset(parameters.get(index).getOffset()),
return doResolve(MethodVariableAccess.of(parameters.get(index).getType().asErasure()).loadFrom(parameters.get(index).getOffset()),
parameters.get(index).getType(),
assigner,
typing);
Expand Down
Expand Up @@ -745,7 +745,7 @@ public StackManipulation resolve(MethodDescription invokedMethod, MethodDescript
return new StackManipulation.Compound(
invokedMethod.isStatic()
? StackManipulation.Trivial.INSTANCE
: MethodVariableAccess.REFERENCE.loadOffset(0),
: MethodVariableAccess.REFERENCE.loadFrom(0),
invokedMethod.isConstructor()
? Duplication.SINGLE
: StackManipulation.Trivial.INSTANCE
Expand Down Expand Up @@ -898,7 +898,7 @@ public StackManipulation resolve(MethodDescription invokedMethod, MethodDescript
return new StackManipulation.Compound(
invokedMethod.isStatic()
? StackManipulation.Trivial.INSTANCE
: MethodVariableAccess.REFERENCE.loadOffset(0),
: MethodVariableAccess.REFERENCE.loadFrom(0),
FieldAccess.forField(instrumentedType.getDeclaredFields().filter(named(fieldName)).getOnly()).getter());
}

Expand Down Expand Up @@ -966,7 +966,7 @@ public StackManipulation resolve(MethodDescription invokedMethod,
if (!stackManipulation.isValid()) {
throw new IllegalStateException("Cannot invoke " + invokedMethod + " on " + parameterDescription.getType());
}
return new StackManipulation.Compound(MethodVariableAccess.of(parameterDescription.getType()).loadOffset(parameterDescription.getOffset()), stackManipulation);
return new StackManipulation.Compound(MethodVariableAccess.of(parameterDescription.getType()).loadFrom(parameterDescription.getOffset()), stackManipulation);
}

@Override
Expand Down Expand Up @@ -1088,7 +1088,7 @@ protected ForThisReference(TypeDescription instrumentedType) {
@Override
public StackManipulation resolve(ParameterDescription target, Assigner assigner, Assigner.Typing typing) {
StackManipulation stackManipulation = new StackManipulation.Compound(
MethodVariableAccess.REFERENCE.loadOffset(0),
MethodVariableAccess.REFERENCE.loadFrom(0),
assigner.assign(instrumentedType.asGenericType(), target.getType(), typing));
if (!stackManipulation.isValid()) {
throw new IllegalStateException("Cannot assign " + instrumentedType + " to " + target);
Expand Down Expand Up @@ -1253,7 +1253,7 @@ protected ForMethodParameter(int index, MethodDescription instrumentedMethod) {
public StackManipulation resolve(ParameterDescription target, Assigner assigner, Assigner.Typing typing) {
ParameterDescription parameterDescription = instrumentedMethod.getParameters().get(index);
StackManipulation stackManipulation = new StackManipulation.Compound(
MethodVariableAccess.of(parameterDescription.getType().asErasure()).loadOffset(parameterDescription.getOffset()),
MethodVariableAccess.of(parameterDescription.getType().asErasure()).loadFrom(parameterDescription.getOffset()),
assigner.assign(parameterDescription.getType(), target.getType(), typing));
if (!stackManipulation.isValid()) {
throw new IllegalStateException("Cannot assign " + parameterDescription + " to " + target + " for " + instrumentedMethod);
Expand Down Expand Up @@ -1560,7 +1560,7 @@ public StackManipulation resolve(ParameterDescription target, Assigner assigner,
StackManipulation stackManipulation = new StackManipulation.Compound(
fieldDescription.isStatic()
? StackManipulation.Trivial.INSTANCE
: MethodVariableAccess.REFERENCE.loadOffset(0),
: MethodVariableAccess.REFERENCE.loadFrom(0),
FieldAccess.forField(fieldDescription).getter(),
assigner.assign(fieldDescription.getType(), target.getType(), typing)
);
Expand Down
Expand Up @@ -975,7 +975,7 @@ public Resolution resolve(TypeDescription instrumentedType) {
return new Resolution(methodGraphCompiler.compile(resolution.getField().getType(), instrumentedType).listNodes().asMethodList().filter(matcher),
new StackManipulation.Compound(resolution.getField().isStatic()
? StackManipulation.Trivial.INSTANCE
: MethodVariableAccess.REFERENCE.loadOffset(0), FieldAccess.forField(resolution.getField()).getter()),
: MethodVariableAccess.REFERENCE.loadFrom(0), FieldAccess.forField(resolution.getField()).getter()),
new MethodDelegationBinder.MethodInvoker.Virtual(resolution.getField().getType().asErasure()),
resolution.getField().isStatic());
}
Expand Down
Expand Up @@ -297,15 +297,15 @@ private Appender(TypeDescription instrumentedType) {

@Override
public Size apply(MethodVisitor methodVisitor, Context implementationContext, MethodDescription instrumentedMethod) {
StackManipulation thisReference = MethodVariableAccess.REFERENCE.loadOffset(0);
StackManipulation thisReference = MethodVariableAccess.REFERENCE.loadFrom(0);
FieldList<?> fieldList = instrumentedType.getDeclaredFields();
StackManipulation[] fieldLoading = new StackManipulation[fieldList.size()];
int index = 0;
for (FieldDescription fieldDescription : fieldList) {
fieldLoading[index] = new StackManipulation.Compound(
thisReference,
MethodVariableAccess.of(fieldDescription.getType().asErasure())
.loadOffset(instrumentedMethod.getParameters().get(index).getOffset()),
.loadFrom(instrumentedMethod.getParameters().get(index).getOffset()),
FieldAccess.forField(fieldDescription).putter()
);
index++;
Expand Down Expand Up @@ -416,7 +416,7 @@ private Appender(TypeDescription instrumentedType) {
public Size apply(MethodVisitor methodVisitor,
Context implementationContext,
MethodDescription instrumentedMethod) {
StackManipulation thisReference = MethodVariableAccess.of(instrumentedType).loadOffset(0);
StackManipulation thisReference = MethodVariableAccess.of(instrumentedType).loadFrom(0);
FieldList<?> fieldList = instrumentedType.getDeclaredFields();
StackManipulation[] fieldLoading = new StackManipulation[fieldList.size()];
int index = 0;
Expand Down

0 comments on commit da5a55f

Please sign in to comment.