Java Code Examples for net.bytebuddy.implementation.bytecode.StackManipulation#apply()
The following examples show how to use
net.bytebuddy.implementation.bytecode.StackManipulation#apply() .
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: PrimitiveBoxingDelegateTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test public void testBoxing() throws Exception { StackManipulation boxingStackManipulation = PrimitiveBoxingDelegate.forPrimitive(primitiveTypeDescription) .assignBoxedTo(targetType, chainedAssigner, Assigner.Typing.STATIC); assertThat(boxingStackManipulation.isValid(), is(true)); StackManipulation.Size size = boxingStackManipulation.apply(methodVisitor, implementationContext); assertThat(size.getSizeImpact(), is(sizeChange)); assertThat(size.getMaximalSize(), is(0)); verify(primitiveTypeDescription).represents(primitiveType); verify(primitiveTypeDescription, atLeast(1)).represents(any(Class.class)); verifyNoMoreInteractions(primitiveTypeDescription); verify(chainedAssigner).assign(referenceTypeDescription.asGenericType(), targetType, Assigner.Typing.STATIC); verifyNoMoreInteractions(chainedAssigner); verify(methodVisitor).visitMethodInsn(Opcodes.INVOKESTATIC, referenceTypeDescription.getInternalName(), VALUE_OF, boxingMethodDescriptor, false); verifyNoMoreInteractions(methodVisitor); verify(stackManipulation, atLeast(1)).isValid(); verify(stackManipulation).apply(methodVisitor, implementationContext); verifyNoMoreInteractions(stackManipulation); }
Example 2
Source File: AbstractArrayFactoryTest.java From byte-buddy with Apache License 2.0 | 6 votes |
protected void testCreationUsing(Class<?> componentType, int storageOpcode) throws Exception { defineComponentType(componentType); CollectionFactory arrayFactory = ArrayFactory.forType(this.componentType); StackManipulation arrayStackManipulation = arrayFactory.withValues(Collections.singletonList(stackManipulation)); assertThat(arrayStackManipulation.isValid(), is(true)); verify(stackManipulation, atLeast(1)).isValid(); StackManipulation.Size size = arrayStackManipulation.apply(methodVisitor, implementationContext); assertThat(size.getSizeImpact(), is(1)); assertThat(size.getMaximalSize(), is(3 + StackSize.of(componentType).toIncreasingSize().getSizeImpact())); verify(methodVisitor).visitInsn(Opcodes.ICONST_1); verifyArrayCreation(methodVisitor); verify(methodVisitor).visitInsn(Opcodes.DUP); verify(methodVisitor).visitInsn(Opcodes.ICONST_0); verify(stackManipulation).apply(methodVisitor, implementationContext); verify(methodVisitor).visitInsn(storageOpcode); verifyNoMoreInteractions(methodVisitor); verifyNoMoreInteractions(stackManipulation); }
Example 3
Source File: FieldConstantTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test public void testConstantCreationModernInvisible() throws Exception { when(classFileVersion.isAtLeast(ClassFileVersion.JAVA_V5)).thenReturn(true); when(declaringType.isVisibleTo(instrumentedType)).thenReturn(false); StackManipulation stackManipulation = new FieldConstant(fieldDescription); assertThat(stackManipulation.isValid(), is(true)); StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext); assertThat(size.getSizeImpact(), is(1)); assertThat(size.getMaximalSize(), is(2)); verify(methodVisitor).visitLdcInsn(BAZ); verify(methodVisitor).visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(Class.class), "forName", Type.getMethodDescriptor(Type.getType(Class.class), Type.getType(String.class)), false); verify(methodVisitor).visitLdcInsn(BAR); verify(methodVisitor).visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Class", "getDeclaredField", "(Ljava/lang/String;)Ljava/lang/reflect/Field;", false); verifyNoMoreInteractions(methodVisitor); }
Example 4
Source File: PrimitiveUnboxingDelegateWideningTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test public void testTrivialBoxing() throws Exception { StackManipulation stackManipulation = PrimitiveUnboxingDelegate.forReferenceType(referenceTypeDescription) .assignUnboxedTo(primitiveTypeDescription, chainedAssigner, Assigner.Typing.STATIC); assertThat(stackManipulation.isValid(), is(true)); StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext); assertThat(size.getSizeImpact(), is(sizeChange)); assertThat(size.getMaximalSize(), is(interimMaximum)); verify(methodVisitor).visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(referenceType), unboxingMethodName, unboxingMethodDescriptor, false); verify(methodVisitor).visitInsn(wideningOpcode); verifyNoMoreInteractions(methodVisitor); }
Example 5
Source File: VoidAwareAssignerVoidToNonVoidTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testAssignDefaultValue() throws Exception { Assigner voidAwareAssigner = new VoidAwareAssigner(chainedAssigner); StackManipulation stackManipulation = voidAwareAssigner.assign(source, target, Assigner.Typing.DYNAMIC); assertThat(stackManipulation.isValid(), is(true)); StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext); assertThat(size.getSizeImpact(), is(StackSize.of(targetType).getSize())); assertThat(size.getMaximalSize(), is(StackSize.of(targetType).getSize())); verify(methodVisitor).visitInsn(opcode); verifyNoMoreInteractions(methodVisitor); }
Example 6
Source File: TypeProxyCreationTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testForReflectionFactoryConstruction() throws Exception { when(implementationTarget.getInstrumentedType()).thenReturn(foo); when(invocationFactory.invoke(eq(implementationTarget), eq(foo), any(MethodDescription.class))) .thenReturn(specialMethodInvocation); when(specialMethodInvocation.isValid()).thenReturn(true); when(specialMethodInvocation.apply(any(MethodVisitor.class), any(Implementation.Context.class))) .thenReturn(new StackManipulation.Size(0, 0)); when(methodAccessorFactory.registerAccessorFor(specialMethodInvocation, MethodAccessorFactory.AccessType.DEFAULT)).thenReturn(proxyMethod); StackManipulation stackManipulation = new TypeProxy.ForSuperMethodByReflectionFactory(foo, implementationTarget, true, false); MethodVisitor methodVisitor = mock(MethodVisitor.class); Implementation.Context implementationContext = mock(Implementation.Context.class); when(implementationContext.register(any(AuxiliaryType.class))) .thenReturn(TypeDescription.ForLoadedType.of(FooProxyMake.class)); assertThat(stackManipulation.isValid(), is(true)); StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext); assertThat(size.getSizeImpact(), is(1)); assertThat(size.getMaximalSize(), is(3)); verify(implementationContext).register(any(AuxiliaryType.class)); verifyNoMoreInteractions(implementationContext); verify(methodVisitor).visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(FooProxyMake.class), TypeProxy.REFLECTION_METHOD, Type.getMethodDescriptor(FooProxyMake.class.getDeclaredMethod("make")), false); verify(methodVisitor).visitInsn(Opcodes.DUP); verify(methodVisitor).visitFieldInsn(Opcodes.PUTFIELD, Type.getInternalName(FooProxyMake.class), TypeProxy.INSTANCE_FIELD, Type.getDescriptor(Void.class)); verify(methodVisitor).visitVarInsn(Opcodes.ALOAD, 0); verifyNoMoreInteractions(methodVisitor); }
Example 7
Source File: AssignerEqualTypesOnlyTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testAssignmentGenericEqual() throws Exception { StackManipulation stackManipulation = Assigner.EqualTypesOnly.GENERIC.assign(first, first, Assigner.Typing.of(dynamicallyTyped)); assertThat(stackManipulation.isValid(), is(true)); StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext); assertThat(size.getSizeImpact(), is(0)); assertThat(size.getMaximalSize(), is(0)); verifyZeroInteractions(first); }
Example 8
Source File: ClassConstantPrimitiveTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testClassConstant() throws Exception { StackManipulation stackManipulation = ClassConstant.of(primitiveType); assertThat(stackManipulation.isValid(), is(true)); StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext); assertThat(size.getSizeImpact(), is(1)); assertThat(size.getMaximalSize(), is(1)); verify(methodVisitor).visitFieldInsn(Opcodes.GETSTATIC, wrapperType.getInternalName(), "TYPE", "Ljava/lang/Class;"); verifyNoMoreInteractions(methodVisitor); verifyZeroInteractions(implementationContext); }
Example 9
Source File: MethodInvocationHandleTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testExactHandleConstructor() throws Exception { when(methodDescription.isConstructor()).thenReturn(true); StackManipulation stackManipulation = MethodInvocation.invoke(methodDescription).onHandle(MethodInvocation.HandleType.EXACT); assertThat(stackManipulation.isValid(), is(true)); StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext); assertThat(size.getSizeImpact(), is(-1)); assertThat(size.getMaximalSize(), is(0)); verify(methodVisitor).visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/invoke/MethodHandle", "invokeExact", BAZ, false); }
Example 10
Source File: JavaConstantValueTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testMethodHandle() throws Exception { when(javaConstant.asConstantPoolValue()).thenReturn(FOO); StackManipulation stackManipulation = new JavaConstantValue(javaConstant); StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext); assertThat(size.getSizeImpact(), is(1)); assertThat(size.getMaximalSize(), is(1)); verify(javaConstant).asConstantPoolValue(); verifyNoMoreInteractions(javaConstant); verify(methodVisitor).visitLdcInsn(FOO); verifyNoMoreInteractions(methodVisitor); verifyZeroInteractions(implementationContext); }
Example 11
Source File: MethodVariableAccessTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testStoring() throws Exception { StackManipulation stackManipulation = MethodVariableAccess.of(typeDefinition).storeAt(4); assertThat(stackManipulation.isValid(), is(true)); StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext); assertThat(size.getSizeImpact(), is(-this.size)); assertThat(size.getMaximalSize(), is(0)); verify(methodVisitor).visitVarInsn(writeCode, 4); verifyNoMoreInteractions(methodVisitor); }
Example 12
Source File: GenericTypeAwareAssignerTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testDynamicTypingRawAssignability() throws Exception { TypeDescription.Generic source = TypeDefinition.Sort.describe(Sample.class.getDeclaredField("parameterizedWildcardStringLowerBound").getGenericType()); TypeDescription.Generic target = TypeDefinition.Sort.describe(Sample.class.getDeclaredField("parameterizedWildcardStringUpperBound").getGenericType()); StackManipulation assignment = GenericTypeAwareAssigner.INSTANCE.assign(source, target, Assigner.Typing.DYNAMIC); assertThat(assignment.isValid(), is(true)); MethodVisitor methodVisitor = mock(MethodVisitor.class); Implementation.Context implementationContext = mock(Implementation.Context.class); StackManipulation.Size size = assignment.apply(methodVisitor, implementationContext); assertThat(size.getSizeImpact(), is(0)); assertThat(size.getMaximalSize(), is(0)); verifyZeroInteractions(methodVisitor); verifyZeroInteractions(implementationContext); }
Example 13
Source File: VoidAwareAssignerTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testAssignVoidToVoid() throws Exception { when(sourceTypeDescription.represents(void.class)).thenReturn(true); when(targetTypeDescription.represents(void.class)).thenReturn(true); Assigner voidAwareAssigner = new VoidAwareAssigner(chainedAssigner); StackManipulation stackManipulation = voidAwareAssigner.assign(sourceTypeDescription, targetTypeDescription, Assigner.Typing.STATIC); assertThat(stackManipulation.isValid(), is(true)); StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext); assertThat(size.getSizeImpact(), is(0)); assertThat(size.getMaximalSize(), is(0)); verifyZeroInteractions(chainedAssigner); }
Example 14
Source File: MethodInvocationDynamicTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testDynamicStaticBootstrap() throws Exception { when(methodDescription.isInvokeBootstrap()).thenReturn(true); when(methodDescription.isStatic()).thenReturn(true); StackManipulation stackManipulation = MethodInvocation.invoke(methodDescription) .dynamic(FOO, returnType, Arrays.asList(firstType, secondType), Collections.singletonList(argument)); assertThat(stackManipulation.isValid(), is(true)); StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext); assertThat(size.getSizeImpact(), is(0)); assertThat(size.getMaximalSize(), is(0)); verify(methodVisitor).visitInvokeDynamicInsn(FOO, "(" + FOO + BAR + ")" + QUX, new Handle(Opcodes.H_INVOKESTATIC, BAR, QUX, BAZ, false), argument); }
Example 15
Source File: MethodVariableAccessOfMethodArgumentsTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testNonStaticMethodWithPrepending() throws Exception { 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 + 1)); assertThat(size.getMaximalSize(), is(PARAMETER_STACK_SIZE + 1)); verify(methodVisitor).visitVarInsn(Opcodes.ALOAD, 0); verify(methodVisitor).visitVarInsn(Opcodes.ALOAD, 1); verify(methodVisitor).visitVarInsn(Opcodes.ALOAD, 2); verifyNoMoreInteractions(methodVisitor); }
Example 16
Source File: VoidAwareAssignerNonVoidToVoidTest.java From byte-buddy with Apache License 2.0 | 5 votes |
private void testAssignDefaultValue(boolean dynamicallyTyped) throws Exception { Assigner voidAwareAssigner = new VoidAwareAssigner(chainedAssigner); StackManipulation stackManipulation = voidAwareAssigner.assign(source, target, Assigner.Typing.of(dynamicallyTyped)); assertThat(stackManipulation.isValid(), is(true)); StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext); assertThat(size.getSizeImpact(), is(-1 * StackSize.of(sourceType).getSize())); assertThat(size.getMaximalSize(), is(0)); verify(methodVisitor).visitInsn(opcode); verifyNoMoreInteractions(methodVisitor); }
Example 17
Source File: MethodInvocationHandleTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testExactHandleNonStatic() throws Exception { StackManipulation stackManipulation = MethodInvocation.invoke(methodDescription).onHandle(MethodInvocation.HandleType.EXACT); assertThat(stackManipulation.isValid(), is(true)); StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext); assertThat(size.getSizeImpact(), is(-1)); assertThat(size.getMaximalSize(), is(0)); verify(methodVisitor).visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/invoke/MethodHandle", "invokeExact", "(" + BAR + BAZ.substring(1), false); }
Example 18
Source File: HandleInvocationTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testInvocationIncreasingStack() throws Exception { JavaConstant.MethodType methodType = JavaConstant.MethodType.of(Object.class); StackManipulation stackManipulation = new HandleInvocation(methodType); assertThat(stackManipulation.isValid(), is(true)); StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext); assertThat(size.getSizeImpact(), is(1)); assertThat(size.getMaximalSize(), is(1)); verify(methodVisitor).visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/invoke/MethodHandle", "invokeExact", "()Ljava/lang/Object;", false); verifyNoMoreInteractions(methodVisitor); verifyZeroInteractions(implementationContext); }
Example 19
Source File: MethodVariableAccessOfMethodArgumentsTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testBridgeMethodWithCasting() throws Exception { when(secondRawParameterType.asErasure()).thenReturn(secondRawParameterType); when(bridgeMethod.getParameters()).thenReturn(new ParameterList.Explicit.ForTypes(bridgeMethod, secondParameterType, secondParameterType)); StackManipulation stackManipulation = MethodVariableAccess.allArgumentsOf(methodDescription).asBridgeOf(bridgeMethod); 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, 1); verify(methodVisitor).visitTypeInsn(Opcodes.CHECKCAST, FOO); verify(methodVisitor).visitVarInsn(Opcodes.ALOAD, 2); verifyNoMoreInteractions(methodVisitor); }
Example 20
Source File: TypeProxyCreationTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testForDefaultMethodConstruction() throws Exception { when(implementationTarget.getInstrumentedType()).thenReturn(foo); when(invocationFactory.invoke(eq(implementationTarget), eq(foo), any(MethodDescription.class))) .thenReturn(specialMethodInvocation); when(specialMethodInvocation.isValid()).thenReturn(true); when(specialMethodInvocation.apply(any(MethodVisitor.class), any(Implementation.Context.class))) .thenReturn(new StackManipulation.Size(0, 0)); when(methodAccessorFactory.registerAccessorFor(specialMethodInvocation, MethodAccessorFactory.AccessType.DEFAULT)).thenReturn(proxyMethod); StackManipulation stackManipulation = new TypeProxy.ForDefaultMethod(foo, implementationTarget, false); MethodVisitor methodVisitor = mock(MethodVisitor.class); Implementation.Context implementationContext = mock(Implementation.Context.class); when(implementationContext.register(any(AuxiliaryType.class))).thenReturn(foo); assertThat(stackManipulation.isValid(), is(true)); StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext); assertThat(size.getSizeImpact(), is(0)); assertThat(size.getMaximalSize(), is(2)); verify(implementationContext).register(any(AuxiliaryType.class)); verifyNoMoreInteractions(implementationContext); verify(methodVisitor).visitTypeInsn(Opcodes.NEW, Type.getInternalName(Foo.class)); verify(methodVisitor, times(2)).visitInsn(Opcodes.DUP); verify(methodVisitor).visitMethodInsn(Opcodes.INVOKESPECIAL, foo.getInternalName(), MethodDescription.CONSTRUCTOR_INTERNAL_NAME, foo.getDeclaredMethods().filter(isConstructor()).getOnly().getDescriptor(), false); verify(methodVisitor).visitFieldInsn(Opcodes.PUTFIELD, foo.getInternalName(), TypeProxy.INSTANCE_FIELD, Type.getDescriptor(Void.class)); verify(methodVisitor).visitVarInsn(Opcodes.ALOAD, 0); verifyNoMoreInteractions(methodVisitor); }