Skip to content

Commit

Permalink
Use new frame generation abstraction and add more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Aug 20, 2022
1 parent f83e348 commit 98b6fc9
Show file tree
Hide file tree
Showing 6 changed files with 400 additions and 231 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import net.bytebuddy.description.modifier.FieldManifestation;
import net.bytebuddy.description.modifier.Ownership;
import net.bytebuddy.description.modifier.Visibility;
import net.bytebuddy.description.type.TypeDefinition;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.dynamic.DynamicType;
Expand All @@ -40,6 +41,7 @@
import java.security.Permission;
import java.security.PrivilegedAction;
import java.security.PrivilegedExceptionAction;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -273,51 +275,22 @@ public Size apply(MethodVisitor methodVisitor, Implementation.Context implementa
methodVisitor.visitLabel(end);
methodVisitor.visitJumpInsn(Opcodes.GOTO, complete);
methodVisitor.visitLabel(classNotFound);
switch (implementationContext.getFrameGeneration()) {
case GENERATE:
methodVisitor.visitFrame(Opcodes.F_SAME1, EMPTY.length, EMPTY, 1, new Object[]{Type.getInternalName(ClassNotFoundException.class)});
break;
case EXPAND:
methodVisitor.visitFrame(Opcodes.F_NEW, EMPTY.length, EMPTY, 1, new Object[]{Type.getInternalName(ClassNotFoundException.class)});
break;
case DISABLED:
break;
default:
throw new IllegalStateException();
}
implementationContext.getFrameGeneration().same1(methodVisitor,
TypeDescription.ForLoadedType.of(ClassNotFoundException.class),
Collections.<TypeDefinition>emptyList());
methodVisitor.visitInsn(Opcodes.POP);
methodVisitor.visitInsn(Opcodes.ICONST_0);
methodVisitor.visitFieldInsn(Opcodes.PUTSTATIC, instrumentedType.getInternalName(), name, Type.getDescriptor(boolean.class));
methodVisitor.visitJumpInsn(Opcodes.GOTO, complete);
methodVisitor.visitLabel(securityException);
switch (implementationContext.getFrameGeneration()) {
case GENERATE:
methodVisitor.visitFrame(Opcodes.F_SAME1, EMPTY.length, EMPTY, 1, new Object[]{Type.getInternalName(SecurityException.class)});
break;
case EXPAND:
methodVisitor.visitFrame(Opcodes.F_NEW, EMPTY.length, EMPTY, 1, new Object[]{Type.getInternalName(SecurityException.class)});
break;
case DISABLED:
break;
default:
throw new IllegalStateException();
}
implementationContext.getFrameGeneration().same1(methodVisitor,
TypeDescription.ForLoadedType.of(SecurityException.class),
Collections.<TypeDefinition>emptyList());
methodVisitor.visitInsn(Opcodes.POP);
methodVisitor.visitInsn(Opcodes.ICONST_1);
methodVisitor.visitFieldInsn(Opcodes.PUTSTATIC, instrumentedType.getInternalName(), name, Type.getDescriptor(boolean.class));
methodVisitor.visitLabel(complete);
switch (implementationContext.getFrameGeneration()) {
case GENERATE:
methodVisitor.visitFrame(Opcodes.F_SAME, EMPTY.length, EMPTY, EMPTY.length, EMPTY);
break;
case EXPAND:
methodVisitor.visitFrame(Opcodes.F_NEW, EMPTY.length, EMPTY, EMPTY.length, EMPTY);
break;
case DISABLED:
break;
default:
throw new IllegalStateException();
}
implementationContext.getFrameGeneration().same(methodVisitor, Collections.<TypeDefinition>emptyList());
return new Size(Math.max(3, size), 0);
}

Expand Down Expand Up @@ -513,22 +486,7 @@ public void visitCode() {
false);
mv.visitInsn(Type.getType(token.getReturnType().getDescriptor()).getOpcode(Opcodes.IRETURN));
mv.visitLabel(label);
switch (frameGeneration) {
case GENERATE:
mv.visitFrame(Opcodes.F_SAME, EMPTY.length, EMPTY, EMPTY.length, EMPTY);
break;
case EXPAND:
Object[] localVariable = new Object[token.getParameterTypes().size()];
for (int index = 0; index < localVariable.length; index++) {
localVariable[index] = token.getParameterTypes().get(index).getInternalName();
}
mv.visitFrame(Opcodes.F_NEW, localVariable.length, localVariable, EMPTY.length, EMPTY);
break;
case DISABLED:
break;
default:
throw new IllegalStateException();
}
frameGeneration.same(mv, token.getParameterTypes());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;

import java.util.*;

Expand Down Expand Up @@ -401,16 +400,6 @@ public int getRequiredVariablePadding() {
@HashCodeAndEqualsPlugin.Enhance
class UsingJump implements NullValueGuard {

/**
* An empty array.
*/
private static final Object[] EMPTY = new Object[0];

/**
* An array containing a single reference value.
*/
private static final Object[] REFERENCE = new Object[]{Type.getInternalName(Object.class)};

/**
* The instrumented method.
*/
Expand Down Expand Up @@ -498,59 +487,18 @@ protected class AfterInstruction extends StackManipulation.AbstractBase {
public Size apply(MethodVisitor methodVisitor, Context implementationContext) {
methodVisitor.visitJumpInsn(Opcodes.GOTO, endOfBlock);
methodVisitor.visitLabel(secondValueNull);
switch (implementationContext.getFrameGeneration()) {
case GENERATE:
methodVisitor.visitFrame(Opcodes.F_SAME1, EMPTY.length, EMPTY, REFERENCE.length, REFERENCE);
break;
case EXPAND:
methodVisitor.visitFrame(Opcodes.F_NEW,
2,
new Object[]{implementationContext.getInstrumentedType().getInternalName(), Type.getInternalName(Object.class)},
REFERENCE.length,
REFERENCE);
break;
case DISABLED:
break;
default:
throw new IllegalStateException();
}
implementationContext.getFrameGeneration().same1(methodVisitor,
TypeDescription.OBJECT,
Arrays.asList(implementationContext.getInstrumentedType(), TypeDescription.OBJECT));
methodVisitor.visitJumpInsn(Opcodes.IFNULL, endOfBlock);
methodVisitor.visitLabel(firstValueNull);
switch (implementationContext.getFrameGeneration()) {
case GENERATE:
methodVisitor.visitFrame(Opcodes.F_SAME, EMPTY.length, EMPTY, EMPTY.length, EMPTY);
break;
case EXPAND:
methodVisitor.visitFrame(Opcodes.F_NEW,
2,
new Object[]{implementationContext.getInstrumentedType().getInternalName(), Type.getInternalName(Object.class)},
EMPTY.length,
EMPTY);
break;
case DISABLED:
break;
default:
throw new IllegalStateException();
}
implementationContext.getFrameGeneration().same(methodVisitor,
Arrays.asList(implementationContext.getInstrumentedType(), TypeDescription.OBJECT));
methodVisitor.visitInsn(Opcodes.ICONST_0);
methodVisitor.visitInsn(Opcodes.IRETURN);
methodVisitor.visitLabel(endOfBlock);
switch (implementationContext.getFrameGeneration()) {
case GENERATE:
methodVisitor.visitFrame(Opcodes.F_SAME, EMPTY.length, EMPTY, EMPTY.length, EMPTY);
break;
case EXPAND:
methodVisitor.visitFrame(Opcodes.F_NEW,
2,
new Object[]{implementationContext.getInstrumentedType().getInternalName(), Type.getInternalName(Object.class)},
EMPTY.length,
EMPTY);
break;
case DISABLED:
break;
default:
throw new IllegalStateException();
}
implementationContext.getFrameGeneration().same(methodVisitor,
Arrays.asList(implementationContext.getInstrumentedType(), TypeDescription.OBJECT));
return Size.ZERO;
}
}
Expand Down Expand Up @@ -949,22 +897,8 @@ public Size apply(MethodVisitor methodVisitor, Context implementationContext) {
methodVisitor.visitInsn(value);
methodVisitor.visitInsn(Opcodes.IRETURN);
methodVisitor.visitLabel(label);
switch (implementationContext.getFrameGeneration()) {
case GENERATE:
methodVisitor.visitFrame(Opcodes.F_SAME, EMPTY.length, EMPTY, EMPTY.length, EMPTY);
break;
case EXPAND:
methodVisitor.visitFrame(Opcodes.F_NEW,
2,
new Object[]{implementationContext.getInstrumentedType().getInternalName(), Type.getInternalName(Object.class)},
EMPTY.length,
EMPTY);
break;
case DISABLED:
break;
default:
throw new IllegalStateException();
}
implementationContext.getFrameGeneration().same(methodVisitor,
Arrays.asList(implementationContext.getInstrumentedType(), TypeDescription.OBJECT));
return new Size(-1, 1);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.objectweb.asm.Opcodes;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static net.bytebuddy.matcher.ElementMatchers.*;
Expand Down Expand Up @@ -382,16 +383,6 @@ public int getRequiredVariablePadding() {
@HashCodeAndEqualsPlugin.Enhance
class UsingJump implements NullValueGuard {

/**
* An empty array.
*/
private static final Object[] EMPTY = new Object[0];

/**
* An array that only contains an integer stack map frame.
*/
private static final Object[] INTEGER = new Object[]{Opcodes.INTEGER};

/**
* The instrumented method.
*/
Expand Down Expand Up @@ -462,22 +453,9 @@ protected class AfterInstruction extends StackManipulation.AbstractBase {
*/
public Size apply(MethodVisitor methodVisitor, Context implementationContext) {
methodVisitor.visitLabel(label);
switch (implementationContext.getFrameGeneration()) {
case GENERATE:
methodVisitor.visitFrame(Opcodes.F_SAME1, EMPTY.length, EMPTY, INTEGER.length, INTEGER);
break;
case EXPAND:
methodVisitor.visitFrame(Opcodes.F_NEW,
1,
new Object[]{implementationContext.getInstrumentedType().getInternalName()},
INTEGER.length,
INTEGER);
break;
case DISABLED:
break;
default:
throw new IllegalStateException();
}
implementationContext.getFrameGeneration().same1(methodVisitor,
TypeDescription.ForLoadedType.of(int.class),
Arrays.asList(implementationContext.getInstrumentedType(), TypeDescription.OBJECT));
return Size.ZERO;
}
}
Expand Down
Loading

0 comments on commit 98b6fc9

Please sign in to comment.