Skip to content

Commit

Permalink
Clean up.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Jun 1, 2020
1 parent 51cf839 commit bb85163
Showing 1 changed file with 30 additions and 158 deletions.
188 changes: 30 additions & 158 deletions byte-buddy-dep/src/main/java/net/bytebuddy/asm/Advice.java
Original file line number Diff line number Diff line change
Expand Up @@ -8049,7 +8049,7 @@ protected MethodVisitor doApply(MethodVisitor methodVisitor,
argumentHandler,
OffsetMapping.Sort.ENTER));
}
return new CodeTranslationVisitor.ForMethodEnter(methodVisitor,
return new CodeTranslationVisitor(methodVisitor,
implementationContext,
argumentHandler,
methodSizeHandler,
Expand All @@ -8061,7 +8061,8 @@ protected MethodVisitor doApply(MethodVisitor methodVisitor,
offsetMappings,
suppressionHandler,
relocationHandler,
postProcessor);
postProcessor,
false);
}

/**
Expand Down Expand Up @@ -8293,7 +8294,7 @@ private MethodVisitor doApply(MethodVisitor methodVisitor,
argumentHandler,
OffsetMapping.Sort.EXIT));
}
return new CodeTranslationVisitor.ForMethodExit(methodVisitor,
return new CodeTranslationVisitor(methodVisitor,
implementationContext,
argumentHandler,
methodSizeHandler,
Expand All @@ -8305,7 +8306,8 @@ private MethodVisitor doApply(MethodVisitor methodVisitor,
offsetMappings,
suppressionHandler,
relocationHandler,
postProcessor);
postProcessor,
true);
}

/**
Expand Down Expand Up @@ -8430,7 +8432,7 @@ public TypeDescription getThrowable() {
/**
* A visitor for translating an advice method's byte code for inlining into the instrumented method.
*/
protected abstract static class CodeTranslationVisitor extends MethodVisitor {
protected static class CodeTranslationVisitor extends MethodVisitor {

/**
* Indicates an empty operand stack.
Expand Down Expand Up @@ -8502,6 +8504,11 @@ protected abstract static class CodeTranslationVisitor extends MethodVisitor {
*/
private final PostProcessor postProcessor;

/**
* {@code true} if this visitor is for exit advice.
*/
private final boolean exit;

/**
* A label indicating the end of the advice byte code.
*/
Expand All @@ -8523,6 +8530,7 @@ protected abstract static class CodeTranslationVisitor extends MethodVisitor {
* @param suppressionHandler A bound suppression handler that is used for suppressing exceptions of this advice method.
* @param relocationHandler A bound relocation handler that is responsible for considering a non-standard control flow.
* @param postProcessor The post processor to apply.
* @param exit {@code true} if this visitor is for exit advice.
*/
protected CodeTranslationVisitor(MethodVisitor methodVisitor,
Context implementationContext,
Expand All @@ -8536,7 +8544,8 @@ protected CodeTranslationVisitor(MethodVisitor methodVisitor,
Map<Integer, OffsetMapping.Target> offsetMappings,
SuppressionHandler.Bound suppressionHandler,
RelocationHandler.Bound relocationHandler,
PostProcessor postProcessor) {
PostProcessor postProcessor,
boolean exit) {
super(OpenedClassReader.ASM_API, new StackAwareMethodVisitor(methodVisitor, instrumentedMethod));
this.methodVisitor = methodVisitor;
this.implementationContext = implementationContext;
Expand All @@ -8551,6 +8560,7 @@ protected CodeTranslationVisitor(MethodVisitor methodVisitor,
this.suppressionHandler = suppressionHandler;
this.relocationHandler = relocationHandler;
this.postProcessor = postProcessor;
this.exit = exit;
endOfMethod = new Label();
}

Expand Down Expand Up @@ -8692,21 +8702,21 @@ public void visitEnd() {
|| adviceMethod.getReturnType().represents(char.class)
|| adviceMethod.getReturnType().represents(int.class)) {
stackMapFrameHandler.injectReturnFrame(methodVisitor);
methodVisitor.visitVarInsn(Opcodes.ISTORE, getReturnValueOffset());
methodVisitor.visitVarInsn(Opcodes.ISTORE, exit ? argumentHandler.exit() : argumentHandler.enter());
} else if (adviceMethod.getReturnType().represents(long.class)) {
stackMapFrameHandler.injectReturnFrame(methodVisitor);
methodVisitor.visitVarInsn(Opcodes.LSTORE, getReturnValueOffset());
methodVisitor.visitVarInsn(Opcodes.LSTORE, exit ? argumentHandler.exit() : argumentHandler.enter());
} else if (adviceMethod.getReturnType().represents(float.class)) {
stackMapFrameHandler.injectReturnFrame(methodVisitor);
methodVisitor.visitVarInsn(Opcodes.FSTORE, getReturnValueOffset());
methodVisitor.visitVarInsn(Opcodes.FSTORE, exit ? argumentHandler.exit() : argumentHandler.enter());
} else if (adviceMethod.getReturnType().represents(double.class)) {
stackMapFrameHandler.injectReturnFrame(methodVisitor);
methodVisitor.visitVarInsn(Opcodes.DSTORE, getReturnValueOffset());
methodVisitor.visitVarInsn(Opcodes.DSTORE, exit ? argumentHandler.exit() : argumentHandler.enter());
} else if (!adviceMethod.getReturnType().represents(void.class)) {
stackMapFrameHandler.injectReturnFrame(methodVisitor);
methodVisitor.visitVarInsn(Opcodes.ASTORE, getReturnValueOffset());
methodVisitor.visitVarInsn(Opcodes.ASTORE, exit ? argumentHandler.exit() : argumentHandler.enter());
}
methodSizeHandler.requireStackSize(relocationHandler.apply(methodVisitor, getReturnValueOffset()));
methodSizeHandler.requireStackSize(relocationHandler.apply(methodVisitor, exit ? argumentHandler.exit() : argumentHandler.enter()));
stackMapFrameHandler.injectCompletionFrame(methodVisitor);
methodSizeHandler.recordMaxima(postProcessor
.resolve(instrumentedType, instrumentedMethod, assigner, argumentHandler)
Expand All @@ -8717,126 +8727,6 @@ public void visitEnd() {
public void visitMaxs(int stackSize, int localVariableLength) {
methodSizeHandler.recordMaxima(stackSize, localVariableLength);
}

/**
* Resolves the offset of the advice method's local variable. The returned value is only valid if
* this advice method does not return {@code void}.
*
* @return The offset of the represented advice method.
*/
protected abstract int getReturnValueOffset();

/**
* A code translation visitor that retains the return value of the represented advice method.
*/
protected static class ForMethodEnter extends CodeTranslationVisitor {

/**
* Creates a code translation visitor for translating exit advice.
*
* @param methodVisitor A method visitor for writing the instrumented method's byte code.
* @param implementationContext The implementation context to use.
* @param argumentHandler A handler for accessing values on the local variable array.
* @param methodSizeHandler A handler for computing the method size requirements.
* @param stackMapFrameHandler A handler for translating and injecting stack map frames.
* @param instrumentedType The instrumented type.
* @param instrumentedMethod The instrumented method.
* @param assigner The assigner to use.
* @param adviceMethod The advice method.
* @param offsetMappings A mapping of offsets to resolved target offsets in the instrumented method.
* @param suppressionHandler A bound suppression handler that is used for suppressing exceptions of this advice method.
* @param relocationHandler A bound relocation handler that is responsible for considering a non-standard control flow.
* @param postProcessor The post processor to apply.
*/
protected ForMethodEnter(MethodVisitor methodVisitor,
Context implementationContext,
ArgumentHandler.ForAdvice argumentHandler,
MethodSizeHandler.ForAdvice methodSizeHandler,
StackMapFrameHandler.ForAdvice stackMapFrameHandler,
TypeDescription instrumentedType,
MethodDescription instrumentedMethod,
Assigner assigner,
MethodDescription.InDefinedShape adviceMethod,
Map<Integer, OffsetMapping.Target> offsetMappings,
SuppressionHandler.Bound suppressionHandler,
RelocationHandler.Bound relocationHandler,
PostProcessor postProcessor) {
super(methodVisitor,
implementationContext,
argumentHandler,
methodSizeHandler,
stackMapFrameHandler,
instrumentedType,
instrumentedMethod,
assigner,
adviceMethod,
offsetMappings,
suppressionHandler,
relocationHandler,
postProcessor);
}

@Override
protected int getReturnValueOffset() {
return argumentHandler.enter();
}
}

/**
* A code translation visitor that discards the return value of the represented advice method.
*/
protected static class ForMethodExit extends CodeTranslationVisitor {

/**
* Creates a code translation visitor for translating exit advice.
*
* @param methodVisitor A method visitor for writing the instrumented method's byte code.
* @param implementationContext The implementation context to use.
* @param argumentHandler A handler for accessing values on the local variable array.
* @param methodSizeHandler A handler for computing the method size requirements.
* @param stackMapFrameHandler A handler for translating and injecting stack map frames.
* @param instrumentedType The instrumented type.
* @param instrumentedMethod The instrumented method.
* @param assigner The assigner to use.
* @param adviceMethod The advice method.
* @param offsetMappings A mapping of offsets to resolved target offsets in the instrumented method.
* @param suppressionHandler A bound suppression handler that is used for suppressing exceptions of this advice method.
* @param relocationHandler A bound relocation handler that is responsible for considering a non-standard control flow.
* @param postProcessor The post processor to apply.
*/
protected ForMethodExit(MethodVisitor methodVisitor,
Implementation.Context implementationContext,
ArgumentHandler.ForAdvice argumentHandler,
MethodSizeHandler.ForAdvice methodSizeHandler,
StackMapFrameHandler.ForAdvice stackMapFrameHandler,
TypeDescription instrumentedType,
MethodDescription instrumentedMethod,
Assigner assigner,
MethodDescription.InDefinedShape adviceMethod,
Map<Integer, OffsetMapping.Target> offsetMappings,
SuppressionHandler.Bound suppressionHandler,
RelocationHandler.Bound relocationHandler,
PostProcessor postProcessor) {
super(methodVisitor,
implementationContext,
argumentHandler,
methodSizeHandler,
stackMapFrameHandler,
instrumentedType,
instrumentedMethod,
assigner,
adviceMethod,
offsetMappings,
suppressionHandler,
relocationHandler,
postProcessor);
}

@Override
protected int getReturnValueOffset() {
return argumentHandler.exit();
}
}
}
}

Expand Down Expand Up @@ -9168,32 +9058,24 @@ public void apply() {
|| adviceMethod.getReturnType().represents(short.class)
|| adviceMethod.getReturnType().represents(char.class)
|| adviceMethod.getReturnType().represents(int.class)) {
methodVisitor.visitVarInsn(Opcodes.ISTORE, getReturnValueOffset());
methodVisitor.visitVarInsn(Opcodes.ISTORE, isExitAdvice() ? argumentHandler.exit() : argumentHandler.enter());
} else if (adviceMethod.getReturnType().represents(long.class)) {
methodVisitor.visitVarInsn(Opcodes.LSTORE, getReturnValueOffset());
methodVisitor.visitVarInsn(Opcodes.LSTORE, isExitAdvice() ? argumentHandler.exit() : argumentHandler.enter());
} else if (adviceMethod.getReturnType().represents(float.class)) {
methodVisitor.visitVarInsn(Opcodes.FSTORE, getReturnValueOffset());
methodVisitor.visitVarInsn(Opcodes.FSTORE, isExitAdvice() ? argumentHandler.exit() : argumentHandler.enter());
} else if (adviceMethod.getReturnType().represents(double.class)) {
methodVisitor.visitVarInsn(Opcodes.DSTORE, getReturnValueOffset());
methodVisitor.visitVarInsn(Opcodes.DSTORE, isExitAdvice() ? argumentHandler.exit() : argumentHandler.enter());
} else if (!adviceMethod.getReturnType().represents(void.class)) {
methodVisitor.visitVarInsn(Opcodes.ASTORE, getReturnValueOffset());
methodVisitor.visitVarInsn(Opcodes.ASTORE, isExitAdvice() ? argumentHandler.exit() : argumentHandler.enter());
}
methodSizeHandler.requireStackSize(relocationHandler.apply(methodVisitor, getReturnValueOffset()));
methodSizeHandler.requireStackSize(relocationHandler.apply(methodVisitor, isExitAdvice() ? argumentHandler.exit() : argumentHandler.enter()));
stackMapFrameHandler.injectCompletionFrame(methodVisitor);
methodSizeHandler.recordMaxima(Math.max(maximumStackSize, adviceMethod.getReturnType().getStackSize().getSize()), EMPTY);
methodSizeHandler.recordMaxima(postProcessor
.resolve(instrumentedType, instrumentedMethod, assigner, argumentHandler)
.apply(methodVisitor, implementationContext).getMaximalSize(), EMPTY);
}

/**
* Resolves the offset of the advice method's local variable. The returned value is only valid if
* this advice method does not return {@code void}.
*
* @return The offset of the represented advice method.
*/
protected abstract int getReturnValueOffset();

/**
* Returns {@code true} if this writer represents exit advice.
*
Expand Down Expand Up @@ -9261,14 +9143,9 @@ public void initialize() {
/* do nothing */
}

@Override
protected int getReturnValueOffset() {
return argumentHandler.enter();
}

@Override
protected boolean isExitAdvice() {
return true;
return false;
}
}

Expand Down Expand Up @@ -9352,14 +9229,9 @@ public void initialize() {
methodSizeHandler.requireStackSize(adviceMethod.getReturnType().getStackSize().getSize());
}

@Override
protected int getReturnValueOffset() {
return argumentHandler.exit();
}

@Override
protected boolean isExitAdvice() {
return false;
return true;
}
}
}
Expand Down

0 comments on commit bb85163

Please sign in to comment.