net.bytebuddy.implementation.bytecode.StackManipulation Java Examples

The following examples show how to use net.bytebuddy.implementation.bytecode.StackManipulation. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FixedValue.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * Blueprint method that for applying the actual implementation.
 *
 * @param methodVisitor           The method visitor to which the implementation is applied to.
 * @param implementationContext   The implementation context for the given implementation.
 * @param instrumentedMethod      The instrumented method that is target of the implementation.
 * @param fixedValueType          A description of the type of the fixed value that is loaded by the
 *                                {@code valueLoadingInstruction}.
 * @param valueLoadingInstruction A stack manipulation that represents the loading of the fixed value onto the
 *                                operand stack.
 * @return A representation of the stack and variable array sized that are required for this implementation.
 */
protected ByteCodeAppender.Size apply(MethodVisitor methodVisitor,
                                      Context implementationContext,
                                      MethodDescription instrumentedMethod,
                                      TypeDescription.Generic fixedValueType,
                                      StackManipulation valueLoadingInstruction) {
    StackManipulation assignment = assigner.assign(fixedValueType, instrumentedMethod.getReturnType(), typing);
    if (!assignment.isValid()) {
        throw new IllegalArgumentException("Cannot return value of type " + fixedValueType + " for " + instrumentedMethod);
    }
    StackManipulation.Size stackSize = new StackManipulation.Compound(
            valueLoadingInstruction,
            assignment,
            MethodReturn.of(instrumentedMethod.getReturnType())
    ).apply(methodVisitor, implementationContext);
    return new ByteCodeAppender.Size(stackSize.getMaximalSize(), instrumentedMethod.getStackSize());
}
 
Example #2
Source File: PrimitiveUnboxingDelegateDirectTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testImplicitBoxing() throws Exception {
    TypeDescription.Generic referenceTypeDescription = mock(TypeDescription.Generic.class);
    when(referenceTypeDescription.asGenericType()).thenReturn(referenceTypeDescription);
    StackManipulation primitiveStackManipulation = PrimitiveUnboxingDelegate.forReferenceType(referenceTypeDescription)
            .assignUnboxedTo(primitiveTypeDescription, chainedAssigner, Assigner.Typing.DYNAMIC);
    assertThat(primitiveStackManipulation.isValid(), is(true));
    StackManipulation.Size size = primitiveStackManipulation.apply(methodVisitor, implementationContext);
    assertThat(size.getSizeImpact(), is(sizeChange));
    assertThat(size.getMaximalSize(), is(sizeChange));
    verify(methodVisitor).visitMethodInsn(Opcodes.INVOKEVIRTUAL,
            Type.getInternalName(wrapperType),
            unboxingMethodName,
            unboxingMethodDescriptor,
            false);
    verifyNoMoreInteractions(methodVisitor);
    verify(chainedAssigner).assign(referenceTypeDescription, TypeDescription.Generic.OfNonGenericType.ForLoadedType.of(wrapperType), Assigner.Typing.DYNAMIC);
    verifyNoMoreInteractions(chainedAssigner);
    verify(stackManipulation, atLeast(1)).isValid();
    verify(stackManipulation).apply(methodVisitor, implementationContext);
    verifyNoMoreInteractions(stackManipulation);
}
 
Example #3
Source File: SerializedConstant.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Size apply(MethodVisitor methodVisitor, Implementation.Context implementationContext) {
    try {
        return new StackManipulation.Compound(
                TypeCreation.of(TypeDescription.ForLoadedType.of(ObjectInputStream.class)),
                Duplication.SINGLE,
                TypeCreation.of(TypeDescription.ForLoadedType.of(ByteArrayInputStream.class)),
                Duplication.SINGLE,
                new TextConstant(serialization),
                new TextConstant(CHARSET),
                MethodInvocation.invoke(new MethodDescription.ForLoadedMethod(String.class.getMethod("getBytes", String.class))),
                MethodInvocation.invoke(new MethodDescription.ForLoadedConstructor(ByteArrayInputStream.class.getConstructor(byte[].class))),
                MethodInvocation.invoke(new MethodDescription.ForLoadedConstructor(ObjectInputStream.class.getConstructor(InputStream.class))),
                MethodInvocation.invoke(new MethodDescription.ForLoadedMethod(ObjectInputStream.class.getMethod("readObject")))
        ).apply(methodVisitor, implementationContext);
    } catch (NoSuchMethodException exception) {
        throw new IllegalStateException("Could not locate Java API method", exception);
    }
}
 
Example #4
Source File: RebaseImplementationTargetTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testRebasedMethodIsInvokable() throws Exception {
    when(invokableMethod.getDeclaringType()).thenReturn(instrumentedType);
    when(resolution.isRebased()).thenReturn(true);
    when(resolution.getResolvedMethod()).thenReturn(rebasedMethod);
    when(resolution.getPrependedParameters()).thenReturn(new TypeList.Empty());
    when(rebasedMethod.isSpecializableFor(instrumentedType)).thenReturn(true);
    Implementation.SpecialMethodInvocation specialMethodInvocation = makeImplementationTarget().invokeSuper(rebasedSignatureToken);
    assertThat(specialMethodInvocation.isValid(), is(true));
    assertThat(specialMethodInvocation.getMethodDescription(), is((MethodDescription) rebasedMethod));
    assertThat(specialMethodInvocation.getTypeDescription(), is(instrumentedType));
    MethodVisitor methodVisitor = mock(MethodVisitor.class);
    Implementation.Context implementationContext = mock(Implementation.Context.class);
    StackManipulation.Size size = specialMethodInvocation.apply(methodVisitor, implementationContext);
    verify(methodVisitor).visitMethodInsn(Opcodes.INVOKESPECIAL, BAZ, QUX, FOO, false);
    verifyNoMoreInteractions(methodVisitor);
    verifyZeroInteractions(implementationContext);
    assertThat(size.getSizeImpact(), is(0));
    assertThat(size.getMaximalSize(), is(0));
}
 
Example #5
Source File: MethodCallProxy.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Size apply(MethodVisitor methodVisitor,
                  Context implementationContext,
                  MethodDescription instrumentedMethod) {
    FieldList<?> fieldList = instrumentedType.getDeclaredFields();
    List<StackManipulation> fieldLoadings = new ArrayList<StackManipulation>(fieldList.size());
    for (FieldDescription fieldDescription : fieldList) {
        fieldLoadings.add(new StackManipulation.Compound(MethodVariableAccess.loadThis(), FieldAccess.forField(fieldDescription).read()));
    }
    StackManipulation.Size stackSize = new StackManipulation.Compound(
            new StackManipulation.Compound(fieldLoadings),
            MethodInvocation.invoke(accessorMethod),
            assigner.assign(accessorMethod.getReturnType(), instrumentedMethod.getReturnType(), Assigner.Typing.DYNAMIC),
            MethodReturn.of(instrumentedMethod.getReturnType())
    ).apply(methodVisitor, implementationContext);
    return new Size(stackSize.getMaximalSize(), instrumentedMethod.getStackSize());
}
 
Example #6
Source File: TypeProxyCreationTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testAccessorIsValid() throws Exception {
    TypeProxy typeProxy = new TypeProxy(mock(TypeDescription.class),
            mock(Implementation.Target.class),
            mock(TypeProxy.InvocationFactory.class),
            false,
            false);
    TypeProxy.MethodCall methodCall = typeProxy.new MethodCall(mock(MethodAccessorFactory.class));
    TypeDescription instrumentedType = mock(TypeDescription.class);
    FieldList<FieldDescription.InDefinedShape> fieldList = mock(FieldList.class);
    when(fieldList.filter(any(ElementMatcher.class))).thenReturn(fieldList);
    when(fieldList.getOnly()).thenReturn(mock(FieldDescription.InDefinedShape.class));
    when(instrumentedType.getDeclaredFields()).thenReturn(fieldList);
    TypeProxy.MethodCall.Appender appender = methodCall.new Appender(instrumentedType);
    Implementation.SpecialMethodInvocation specialMethodInvocation = mock(Implementation.SpecialMethodInvocation.class);
    when(specialMethodInvocation.isValid()).thenReturn(true);
    StackManipulation stackManipulation = appender.new AccessorMethodInvocation(mock(MethodDescription.class), specialMethodInvocation);
    assertThat(stackManipulation.isValid(), is(true));
    verify(specialMethodInvocation).isValid();
    verifyNoMoreInteractions(specialMethodInvocation);
}
 
Example #7
Source File: ClassConstantReferenceTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testClassConstantModernVisible() throws Exception {
    when(typeDescription.isVisibleTo(instrumentedType)).thenReturn(true);
    when(classFileVersion.isAtLeast(ClassFileVersion.JAVA_V5)).thenReturn(true);
    when(typeDescription.getDescriptor()).thenReturn(FOO);
    StackManipulation stackManipulation = ClassConstant.of(typeDescription);
    assertThat(stackManipulation.isValid(), is(true));
    StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext);
    assertThat(size.getSizeImpact(), is(1));
    assertThat(size.getMaximalSize(), is(1));
    verify(typeDescription).getDescriptor();
    verify(typeDescription).isVisibleTo(instrumentedType);
    verify(typeDescription).isPrimitive();
    verifyNoMoreInteractions(typeDescription);
    verify(methodVisitor).visitLdcInsn(Type.getType(FOO));
    verifyNoMoreInteractions(methodVisitor);
}
 
Example #8
Source File: FixedValue.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Size apply(MethodVisitor methodVisitor, Context implementationContext, MethodDescription instrumentedMethod) {
    if (instrumentedMethod.getParameters().size() <= index) {
        throw new IllegalStateException(instrumentedMethod + " does not define a parameter with index " + index);
    }
    ParameterDescription parameterDescription = instrumentedMethod.getParameters().get(index);
    StackManipulation stackManipulation = new StackManipulation.Compound(
            MethodVariableAccess.load(parameterDescription),
            assigner.assign(parameterDescription.getType(), instrumentedMethod.getReturnType(), typing),
            MethodReturn.of(instrumentedMethod.getReturnType())
    );
    if (!stackManipulation.isValid()) {
        throw new IllegalStateException("Cannot assign " + instrumentedMethod.getReturnType() + " to " + parameterDescription);
    }
    return new Size(stackManipulation.apply(methodVisitor, implementationContext).getMaximalSize(), instrumentedMethod.getStackSize());
}
 
Example #9
Source File: PropertyMutatorCollector.java    From jackson-modules-base with Apache License 2.0 6 votes vote down vote up
@Override
protected StackManipulation invocationOperation(AnnotatedMember annotatedMember,
        TypeDefinition beanClassDescription) {

    final String methodName = annotatedMember.getName();
    @SuppressWarnings("unchecked")
    final MethodList<MethodDescription> matchingMethods =
            (MethodList<MethodDescription>) beanClassDescription.getDeclaredMethods().filter(named(methodName));

    if (matchingMethods.size() == 1) { //method was declared on class
        return MethodInvocation.invoke(matchingMethods.getOnly());
    }
    if (matchingMethods.isEmpty()) { //method was not found on class, try super class
        return invocationOperation(annotatedMember, beanClassDescription.getSuperClass());
    }
    else { //should never happen
        throw new IllegalStateException("Could not find definition of method: " + methodName);
    }

}
 
Example #10
Source File: RebaseImplementationTargetTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testNonRebasedMethodIsInvokable() throws Exception {
    when(invokableMethod.getDeclaringType()).thenReturn(instrumentedType);
    when(invokableMethod.isSpecializableFor(instrumentedType)).thenReturn(true);
    when(resolution.isRebased()).thenReturn(false);
    when(resolution.getResolvedMethod()).thenReturn(invokableMethod);
    Implementation.SpecialMethodInvocation specialMethodInvocation = makeImplementationTarget().invokeSuper(rebasedSignatureToken);
    assertThat(specialMethodInvocation.isValid(), is(true));
    assertThat(specialMethodInvocation.getMethodDescription(), is((MethodDescription) invokableMethod));
    assertThat(specialMethodInvocation.getTypeDescription(), is(instrumentedType));
    MethodVisitor methodVisitor = mock(MethodVisitor.class);
    Implementation.Context implementationContext = mock(Implementation.Context.class);
    StackManipulation.Size size = specialMethodInvocation.apply(methodVisitor, implementationContext);
    verify(methodVisitor).visitMethodInsn(Opcodes.INVOKESPECIAL, BAZ, FOO, QUX, false);
    verifyNoMoreInteractions(methodVisitor);
    verifyZeroInteractions(implementationContext);
    assertThat(size.getSizeImpact(), is(0));
    assertThat(size.getMaximalSize(), is(0));
}
 
Example #11
Source File: DoubleConstantOpcodeTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testConstant() throws Exception {
    StackManipulation.Size size = DoubleConstant.forValue(value).apply(methodVisitor, implementationContext);
    assertThat(size.getSizeImpact(), is(2));
    assertThat(size.getMaximalSize(), is(2));
    verify(methodVisitor).visitInsn(opcode);
    verifyNoMoreInteractions(methodVisitor);
}
 
Example #12
Source File: MethodConstantTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testConstructor() throws Exception {
    when(methodDescription.isConstructor()).thenReturn(true);
    StackManipulation.Size size = MethodConstant.of(methodDescription).apply(methodVisitor, implementationContext);
    assertThat(size.getSizeImpact(), is(1));
    assertThat(size.getMaximalSize(), is(5));
    verify(methodVisitor).visitMethodInsn(Opcodes.INVOKEVIRTUAL,
            Type.getInternalName(Class.class),
            "getDeclaredConstructor",
            "([Ljava/lang/Class;)Ljava/lang/reflect/Constructor;",
            false);
}
 
Example #13
Source File: LongConstantTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testBiPush() throws Exception {
    StackManipulation longConstant = LongConstant.forValue(value);
    assertThat(longConstant.isValid(), is(true));
    StackManipulation.Size size = longConstant.apply(methodVisitor, implementationContext);
    assertThat(size.getSizeImpact(), is(2));
    assertThat(size.getMaximalSize(), is(2));
    verify(methodVisitor).visitLdcInsn(value);
    verifyNoMoreInteractions(methodVisitor);
    verifyZeroInteractions(implementationContext);
}
 
Example #14
Source File: MethodVariableAccessOfMethodArgumentsTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testStaticMethodWithPrepending() throws Exception {
    when(methodDescription.isStatic()).thenReturn(true);
    StackManipulation stackManipulation = MethodVariableAccess.allArgumentsOf(methodDescription).prependThisReference();
    assertThat(stackManipulation.isValid(), is(true));
    StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext);
    assertThat(size.getSizeImpact(), is(PARAMETER_STACK_SIZE));
    assertThat(size.getMaximalSize(), is(PARAMETER_STACK_SIZE));
    verify(methodVisitor).visitVarInsn(Opcodes.ALOAD, 0);
    verify(methodVisitor).visitVarInsn(Opcodes.ALOAD, 1);
    verifyNoMoreInteractions(methodVisitor);
}
 
Example #15
Source File: ConstructorCallStackManipulation.java    From jackson-modules-base with Apache License 2.0 5 votes vote down vote up
@Override
public StackManipulation.Size apply(MethodVisitor methodVisitor, Implementation.Context implementationContext) {
    final TypeDescription typeDescription = determineTypeDescription(implementationContext);

    final List<StackManipulation> stackManipulations = new ArrayList<>();
    stackManipulations.add(TypeCreation.of(typeDescription)); //new
    stackManipulations.add(Duplication.of(typeDescription)); //dup
    stackManipulations.addAll(constructorArgumentLoadingOperations); //load any needed variables
    stackManipulations.add(invoke(determineConstructor(typeDescription))); //invokespecial

    final StackManipulation.Compound delegate = new StackManipulation.Compound(stackManipulations);
    return delegate.apply(methodVisitor, implementationContext);
}
 
Example #16
Source File: VoidAwareAssignerVoidToNonVoidTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void testAssignNoDefaultValue() throws Exception {
    Assigner voidAwareAssigner = new VoidAwareAssigner(chainedAssigner);
    StackManipulation stackManipulation = voidAwareAssigner.assign(source, target, Assigner.Typing.STATIC);
    assertThat(stackManipulation.isValid(), is(false));
    stackManipulation.apply(methodVisitor, implementationContext);
}
 
Example #17
Source File: MethodConstantTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testConstructorPrivileged() throws Exception {
    when(methodDescription.isConstructor()).thenReturn(true);
    when(implementationContext.register(any(AuxiliaryType.class))).thenReturn(auxiliaryType);
    when(auxiliaryType.getDeclaredMethods()).thenReturn(new MethodList.Explicit<MethodDescription.InDefinedShape>(auxiliaryConstructor));
    StackManipulation.Size size = MethodConstant.ofPrivileged(methodDescription).apply(methodVisitor, implementationContext);
    assertThat(size.getSizeImpact(), is(4));
    assertThat(size.getMaximalSize(), is(7));
    verify(methodVisitor).visitMethodInsn(Opcodes.INVOKESPECIAL,
            QUX,
            BAR,
            FOO,
            false);
}
 
Example #18
Source File: MethodDelegationBinderTerminationHandlerTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testDropping() throws Exception {
    when(target.getReturnType()).thenReturn(genericSourceType);
    when(genericSourceType.getStackSize()).thenReturn(StackSize.SINGLE);
    StackManipulation stackManipulation = MethodDelegationBinder.TerminationHandler.Default.DROPPING.resolve(assigner, Assigner.Typing.STATIC, source, target);
    assertThat(stackManipulation, is((StackManipulation) Removal.SINGLE));
    verify(genericSourceType).getStackSize();
    verifyNoMoreInteractions(genericSourceType);
}
 
Example #19
Source File: InstanceCheckTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testInstanceCheck() {
    when(typeDescription.getInternalName()).thenReturn(FOO);
    StackManipulation stackManipulation = InstanceCheck.of(typeDescription);
    assertThat(stackManipulation.isValid(), is(true));
    StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext);
    assertThat(size.getSizeImpact(), is(0));
    assertThat(size.getMaximalSize(), is(0));
    verify(typeDescription).isPrimitive();
    verify(typeDescription).getInternalName();
    verifyNoMoreInteractions(typeDescription);
    verify(methodVisitor).visitTypeInsn(Opcodes.INSTANCEOF, FOO);
    verifyNoMoreInteractions(methodVisitor);
    verifyZeroInteractions(implementationContext);
}
 
Example #20
Source File: CreatorOptimizer.java    From jackson-modules-base with Apache License 2.0 5 votes vote down vote up
private StackManipulation creatorInvokerStackManipulation(Constructor<?> ctor, Method factory) {
    final StackManipulation invokeManipulation =
            null == ctor ?
                invoke(new ForLoadedMethod(factory)) :
                new ConstructorCallStackManipulation.KnownConstructorOfExistingType(ctor);
    return new StackManipulation.Compound(
            invokeManipulation,
            MethodReturn.REFERENCE
    );
}
 
Example #21
Source File: FieldAccessor.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a setter instrumentation for setting a constant value.
 *
 * @param fieldLocation      The field's location.
 * @param assigner           The assigner to use.
 * @param typing             Indicates if dynamic type castings should be attempted for incompatible assignments.
 * @param terminationHandler The termination handler to apply.
 * @param typeDescription    The value's type.
 * @param stackManipulation  A stack manipulation to load the constant value.
 */
protected OfConstantValue(FieldLocation fieldLocation,
                          Assigner assigner,
                          Assigner.Typing typing,
                          TerminationHandler terminationHandler,
                          TypeDescription.Generic typeDescription,
                          StackManipulation stackManipulation) {
    super(fieldLocation, assigner, typing, terminationHandler);
    this.typeDescription = typeDescription;
    this.stackManipulation = stackManipulation;
}
 
Example #22
Source File: SimpleExceptionHandler.java    From jackson-modules-base with Apache License 2.0 5 votes vote down vote up
public SimpleExceptionHandler(StackManipulation exceptionThrowingAppender,
                              StackManipulation exceptionHandlerAppender,
                              Class<? extends Exception> exceptionType,
                              int newLocalVariablesCount) {
    this.exceptionThrowingAppender = exceptionThrowingAppender;
    this.exceptionHandlerAppender = exceptionHandlerAppender;
    this.exceptionType = exceptionType;
    this.newLocalVariablesCount = newLocalVariablesCount;
}
 
Example #23
Source File: MemberSubstitution.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public StackManipulation resolve(TypeDescription targetType,
                                 ByteCodeElement target,
                                 TypeList.Generic parameters,
                                 TypeDescription.Generic result,
                                 int freeOffset) {
    MethodDescription methodDescription = methodResolver.resolve(targetType, target, parameters, result);
    if (!methodDescription.isAccessibleTo(instrumentedType)) {
        throw new IllegalStateException(instrumentedType + " cannot access " + methodDescription);
    }
    TypeList.Generic mapped = methodDescription.isStatic()
            ? methodDescription.getParameters().asTypeList()
            : new TypeList.Generic.Explicit(CompoundList.of(methodDescription.getDeclaringType(), methodDescription.getParameters().asTypeList()));
    if (!methodDescription.getReturnType().asErasure().isAssignableTo(result.asErasure())) {
        throw new IllegalStateException("Cannot assign return value of " + methodDescription + " to " + result);
    } else if (mapped.size() != parameters.size()) {
        throw new IllegalStateException("Cannot invoke " + methodDescription + " on " + parameters.size() + " parameters");
    }
    for (int index = 0; index < mapped.size(); index++) {
        if (!parameters.get(index).asErasure().isAssignableTo(mapped.get(index).asErasure())) {
            throw new IllegalStateException("Cannot invoke " + methodDescription + " on parameter " + index + " of type " + parameters.get(index));
        }
    }
    return methodDescription.isVirtual()
            ? MethodInvocation.invoke(methodDescription).virtual(mapped.get(THIS_REFERENCE).asErasure())
            : MethodInvocation.invoke(methodDescription);
}
 
Example #24
Source File: FieldAccessTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testPutter() throws Exception {
    FieldAccess.Defined setter = FieldAccess.forField(fieldDescription);
    assertThat(setter.write().isValid(), is(true));
    StackManipulation.Size size = setter.write().apply(methodVisitor, implementationContext);
    assertThat(size.getSizeImpact(), is(putterChange));
    assertThat(size.getMaximalSize(), is(putterMaximum));
    verify(methodVisitor).visitFieldInsn(putterOpcode, FOO, BAR, QUX);
    verifyNoMoreInteractions(methodVisitor);
}
 
Example #25
Source File: MethodConstantTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testMethodPrivileged() throws Exception {
    when(methodDescription.isMethod()).thenReturn(true);
    when(implementationContext.register(any(AuxiliaryType.class))).thenReturn(auxiliaryType);
    when(auxiliaryType.getDeclaredMethods()).thenReturn(new MethodList.Explicit<MethodDescription.InDefinedShape>(auxiliaryConstructor));
    StackManipulation.Size size = MethodConstant.ofPrivileged(methodDescription).apply(methodVisitor, implementationContext);
    assertThat(size.getSizeImpact(), is(5));
    assertThat(size.getMaximalSize(), is(8));
    verify(methodVisitor).visitMethodInsn(Opcodes.INVOKESPECIAL,
            QUX,
            BAR,
            FOO,
            false);
}
 
Example #26
Source File: IntegerConstantOpcodeTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testConstant() throws Exception {
    StackManipulation loading = IntegerConstant.forValue(value);
    if (value == 0 || value == 1) {
        assertThat(loading, is(IntegerConstant.forValue(value == 1)));
    }
    assertThat(loading.isValid(), is(true));
    StackManipulation.Size size = loading.apply(methodVisitor, implementationContext);
    assertThat(size.getSizeImpact(), is(1));
    assertThat(size.getMaximalSize(), is(1));
    verify(methodVisitor).visitInsn(opcode);
    verifyNoMoreInteractions(methodVisitor);
}
 
Example #27
Source File: MemberSubstitution.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public StackManipulation resolve(TypeDescription targetType,
                                 ByteCodeElement target,
                                 TypeList.Generic parameters,
                                 TypeDescription.Generic result,
                                 int freeOffset) {
    List<StackManipulation> stackManipulations = new ArrayList<StackManipulation>(parameters.size());
    for (int index = parameters.size() - 1; index >= 0; index--) {
        stackManipulations.add(Removal.of(parameters.get(index)));
    }
    return new StackManipulation.Compound(CompoundList.of(stackManipulations, DefaultValue.of(result.asErasure())));
}
 
Example #28
Source File: MethodInvocationTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
private void assertInvocation(StackManipulation stackManipulation,
                              int opcode,
                              String typeName,
                              boolean interfaceInvocation) {
    assertThat(stackManipulation.isValid(), is(true));
    StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext);
    assertThat(size.getSizeImpact(), is(expectedSize));
    assertThat(size.getMaximalSize(), is(Math.max(0, expectedSize)));
    verify(methodVisitor).visitMethodInsn(opcode, typeName, BAR, QUX, interfaceInvocation);
    verifyNoMoreInteractions(methodVisitor);
}
 
Example #29
Source File: FieldAccessOtherTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenericFieldAccessGetterEqualErasure() throws Exception {
    TypeDescription declaredErasure = mock(TypeDescription.class);
    when(genericType.asErasure()).thenReturn(declaredErasure);
    when(declaredType.asErasure()).thenReturn(declaredErasure);
    StackManipulation stackManipulation = FieldAccess.forField(genericField).read();
    assertThat(stackManipulation.isValid(), is(true));
    assertThat(stackManipulation, hasPrototype(FieldAccess.forField(fieldDescription).read()));
}
 
Example #30
Source File: PropertyAccessorCollector.java    From jackson-modules-base with Apache License 2.0 5 votes vote down vote up
@Override
protected StackManipulation usingIf() {

    return new UsingIfStackManipulation<>(
            LocalVarIndexCalculator.INSTANCE,
            props,
            SingleFieldStackManipulationSupplier.<T>of(beanClassDescription, methodReturn)
    );
}