Skip to content

Commit

Permalink
Refactored handler.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Apr 11, 2016
1 parent 28a0bdd commit 3dbda7a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 19 deletions.
65 changes: 47 additions & 18 deletions byte-buddy-dep/src/main/java/net/bytebuddy/asm/Advice.java
Expand Up @@ -1123,6 +1123,8 @@ protected AdviceVisitor(MethodVisitor methodVisitor,

@Override
protected void onFirstCodeInstruction() {
methodEnter.prepare();
methodExit.prepare();
onAdviceStart();
methodEnter.apply(classReader);
onUserStart();
Expand Down Expand Up @@ -2659,7 +2661,6 @@ abstract class ForField implements OffsetMapping {
*
* @param name The name of the field.
* @param targetType The expected type that the field can be assigned to.
* @param readOnly
*/
protected ForField(String name, TypeDescription targetType, boolean readOnly) {
this.name = name;
Expand Down Expand Up @@ -3712,6 +3713,8 @@ interface ForMethodExit extends Resolved {

interface Bound {

void prepare();

void apply(ClassReader classReader);
}

Expand Down Expand Up @@ -3750,6 +3753,11 @@ public Resolved.ForMethodExit asMethodExitTo(List<? extends OffsetMapping.Factor
return this;
}

@Override
public void prepare() {
/* do nothing */
}

@Override
public void apply(ClassReader classReader) {
/* do nothing */
Expand Down Expand Up @@ -3840,12 +3848,16 @@ protected abstract static class Resolved implements Dispatcher.Resolved {
*/
protected final Map<Integer, OffsetMapping> offsetMappings;

protected final CodeTranslationVisitor.SuppressionHandler suppressionHandler;

/**
* Creates a new resolved version of a dispatcher.
*
* @param adviceMethod The represented advice method.
*/
protected Resolved(MethodDescription.InDefinedShape adviceMethod, List<OffsetMapping.Factory> factories) {
protected Resolved(MethodDescription.InDefinedShape adviceMethod,
List<OffsetMapping.Factory> factories,
TypeDescription throwableType) {
this.adviceMethod = adviceMethod;
offsetMappings = new HashMap<Integer, OffsetMapping>();
for (ParameterDescription.InDefinedShape parameterDescription : adviceMethod.getParameters()) {
Expand All @@ -3864,6 +3876,9 @@ protected Resolved(MethodDescription.InDefinedShape adviceMethod, List<OffsetMap
? new OffsetMapping.ForParameter(parameterDescription.getIndex(), READ_ONLY, parameterDescription.getType().asErasure())
: offsetMapping);
}
suppressionHandler = throwableType.represents(NoSuppression.class)
? CodeTranslationVisitor.SuppressionHandler.NoOp.INSTANCE
: new CodeTranslationVisitor.SuppressionHandler.Suppressing(throwableType);
}

@Override
Expand Down Expand Up @@ -3922,6 +3937,11 @@ protected CodeCopier(MethodDescription.InDefinedShape instrumentedMethod,
this.metaDataHandler = metaDataHandler;
}

@Override
public void prepare() {
suppressionHandler.onPrepare(methodVisitor);
}

@Override
public void apply(ClassReader classReader) {
classReader.accept(this, ClassReader.SKIP_DEBUG | metaDataHandler.getReaderHint());
Expand Down Expand Up @@ -3966,7 +3986,8 @@ protected ForMethodEnter(MethodDescription.InDefinedShape adviceMethod, List<? e
OffsetMapping.ForField.Factory.INSTANCE,
OffsetMapping.ForOrigin.Factory.INSTANCE,
OffsetMapping.ForIgnored.INSTANCE,
new OffsetMapping.Illegal(Thrown.class, Enter.class, Return.class, BoxedReturn.class)), userFactories));
new OffsetMapping.Illegal(Thrown.class, Enter.class, Return.class, BoxedReturn.class)), userFactories),
adviceMethod.getDeclaredAnnotations().ofType(OnMethodEnter.class).getValue(SUPPRESS, TypeDescription.class));
}

@Override
Expand All @@ -3987,7 +4008,7 @@ protected MethodVisitor apply(MethodVisitor methodVisitor,
instrumentedMethod,
adviceMethod,
offsetMappings,
adviceMethod.getDeclaredAnnotations().ofType(OnMethodEnter.class).getValue(SUPPRESS, TypeDescription.class));
suppressionHandler);
}

@Override
Expand Down Expand Up @@ -4044,7 +4065,8 @@ protected ForMethodExit(MethodDescription.InDefinedShape adviceMethod,
OffsetMapping.ForBoxedReturnValue.INSTANCE,
adviceMethod.getDeclaredAnnotations().ofType(OnMethodExit.class).loadSilent().onThrowable()
? OffsetMapping.ForThrowable.INSTANCE
: new OffsetMapping.Illegal(Thrown.class)), userFactories));
: new OffsetMapping.Illegal(Thrown.class)), userFactories),
adviceMethod.getDeclaredAnnotations().ofType(OnMethodExit.class).getValue(SUPPRESS, TypeDescription.class));
this.enterType = enterType;
}

Expand Down Expand Up @@ -4077,7 +4099,7 @@ protected MethodVisitor apply(MethodVisitor methodVisitor,
instrumentedMethod,
adviceMethod,
offsetMappings,
adviceMethod.getDeclaredAnnotations().ofType(OnMethodExit.class).getValue(SUPPRESS, TypeDescription.class),
suppressionHandler,
enterType.getStackSize().getSize() + getPadding().getSize());
}

Expand Down Expand Up @@ -4238,22 +4260,19 @@ protected abstract static class CodeTranslationVisitor extends ExceptionTableSen
* @param instrumentedMethod The instrumented method.
* @param adviceMethod The advice method.
* @param offsetMappings A mapping of offsets to resolved target offsets in the instrumented method.
* @param throwableType A throwable type to be suppressed or {@link NoSuppression} if no suppression should be applied.
*/
protected CodeTranslationVisitor(MethodVisitor methodVisitor,
MetaDataHandler.ForAdvice metaDataHandler,
MethodDescription.InDefinedShape instrumentedMethod,
MethodDescription.InDefinedShape adviceMethod,
Map<Integer, Resolved.OffsetMapping.Target> offsetMappings,
TypeDescription throwableType) {
SuppressionHandler suppressionHandler) {
super(Opcodes.ASM5, methodVisitor);
this.metaDataHandler = metaDataHandler;
this.instrumentedMethod = instrumentedMethod;
this.adviceMethod = adviceMethod;
this.offsetMappings = offsetMappings;
suppressionHandler = throwableType.represents(NoSuppression.class)
? SuppressionHandler.NoOp.INSTANCE
: new SuppressionHandler.Suppressing(throwableType);
this.suppressionHandler = suppressionHandler;
endOfMethod = new Label();
}

Expand Down Expand Up @@ -4356,6 +4375,8 @@ protected void onVisitIincInsn(int offset, int increment) {
*/
protected interface SuppressionHandler {

void onPrepare(MethodVisitor methodVisitor);

/**
* Invoked at the start of a method.
*
Expand All @@ -4373,6 +4394,7 @@ protected interface SuppressionHandler {
*/
void onEnd(MethodVisitor methodVisitor, MetaDataHandler.ForAdvice metaDataHandler, ReturnValueProducer returnValueProducer);


/**
* A non-operational suppression handler that does not suppress any method.
*/
Expand All @@ -4383,6 +4405,11 @@ enum NoOp implements SuppressionHandler {
*/
INSTANCE;

@Override
public void onPrepare(MethodVisitor methodVisitor) {
/* do nothing */
}

@Override
public void onStart(MethodVisitor methodVisitor, MetaDataHandler.ForAdvice metaDataHandler) {
/* do nothing */
Expand Down Expand Up @@ -4431,8 +4458,12 @@ protected Suppressing(TypeDescription throwableType) {
}

@Override
public void onStart(MethodVisitor methodVisitor, MetaDataHandler.ForAdvice metaDataHandler) {
public void onPrepare(MethodVisitor methodVisitor) {
methodVisitor.visitTryCatchBlock(startOfMethod, endOfMethod, endOfMethod, throwableType.getInternalName());
}

@Override
public void onStart(MethodVisitor methodVisitor, MetaDataHandler.ForAdvice metaDataHandler) {
methodVisitor.visitLabel(startOfMethod);
}

Expand Down Expand Up @@ -4483,15 +4514,14 @@ protected static class ForMethodEnter extends CodeTranslationVisitor {
* @param instrumentedMethod The instrumented method.
* @param adviceMethod The advice method.
* @param offsetMappings A mapping of offsets to resolved target offsets in the instrumented method.
* @param throwableType A throwable type to be suppressed or {@link NoSuppression} if no suppression should be applied.
*/
protected ForMethodEnter(MethodVisitor methodVisitor,
MetaDataHandler.ForAdvice metaDataHandler,
MethodDescription.InDefinedShape instrumentedMethod,
MethodDescription.InDefinedShape adviceMethod,
Map<Integer, Resolved.OffsetMapping.Target> offsetMappings,
TypeDescription throwableType) {
super(methodVisitor, metaDataHandler, instrumentedMethod, adviceMethod, offsetMappings, throwableType);
SuppressionHandler suppressionHandler) {
super(methodVisitor, metaDataHandler, instrumentedMethod, adviceMethod, offsetMappings, suppressionHandler);
}

@Override
Expand Down Expand Up @@ -4577,22 +4607,21 @@ protected static class ForMethodExit extends CodeTranslationVisitor {
* @param instrumentedMethod The instrumented method.
* @param adviceMethod The advice method.
* @param offsetMappings A mapping of offsets to resolved target offsets in the instrumented method.
* @param throwableType A throwable type to be suppressed or {@link NoSuppression} if no suppression should be applied.
* @param padding The padding after the instrumented method's arguments in the local variable array.
*/
protected ForMethodExit(MethodVisitor methodVisitor,
MetaDataHandler.ForAdvice metaDataHandler,
MethodDescription.InDefinedShape instrumentedMethod,
MethodDescription.InDefinedShape adviceMethod,
Map<Integer, Resolved.OffsetMapping.Target> offsetMappings,
TypeDescription throwableType,
SuppressionHandler suppressionHandler,
int padding) {
super(methodVisitor,
metaDataHandler,
instrumentedMethod,
adviceMethod,
offsetMappings,
throwableType);
suppressionHandler);
this.padding = padding;
}

Expand Down
Expand Up @@ -1100,7 +1100,6 @@ public void apply(MethodDescription.InDefinedShape mock) {
}
}).applyBasic();
ObjectPropertyAssertion.of(Advice.MetaDataHandler.Default.WithStackSizeComputation.ForAdvice.class).applyBasic();
ObjectPropertyAssertion.of(Advice.AdviceVisitor.CodeCopier.class).applyBasic();
ObjectPropertyAssertion.of(Advice.Dispatcher.Inactive.class).apply();
ObjectPropertyAssertion.of(Advice.Dispatcher.OffsetMapping.Context.ForMethodEntry.class).apply();
ObjectPropertyAssertion.of(Advice.Dispatcher.OffsetMapping.Context.ForMethodExit.class).apply();
Expand Down

0 comments on commit 3dbda7a

Please sign in to comment.