Java Code Examples for net.bytebuddy.implementation.bytecode.StackManipulation#Size
The following examples show how to use
net.bytebuddy.implementation.bytecode.StackManipulation#Size .
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: FieldConstantTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test public void testConstantCreationLegacy() throws Exception { when(classFileVersion.isAtLeast(ClassFileVersion.JAVA_V5)).thenReturn(false); when(declaringType.isVisibleTo(instrumentedType)).thenReturn(true); 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 2
Source File: CopierImplementation.java From unsafe with BSD 2-Clause "Simplified" License | 6 votes |
public Size apply(MethodVisitor methodVisitor, Implementation.Context implementationContext, MethodDescription instrumentedMethod) { checkMethodSignature(instrumentedMethod); try { StackManipulation stack = buildStack(); StackManipulation.Size finalStackSize = stack.apply(methodVisitor, implementationContext); return new Size(finalStackSize.getMaximalSize(), instrumentedMethod.getStackSize() + 2); // 2 stack slots for a single local variable } catch (NoSuchMethodException | NoSuchFieldException e) { throw new RuntimeException(e); } }
Example 3
Source File: SubclassImplementationTargetTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test public void testSuperTypeMethodIsInvokable() throws Exception { when(invokableMethod.isSpecializableFor(rawSuperClass)).thenReturn(true); Implementation.SpecialMethodInvocation specialMethodInvocation = makeImplementationTarget().invokeSuper(invokableToken); assertThat(specialMethodInvocation.isValid(), is(true)); assertThat(specialMethodInvocation.getMethodDescription(), is((MethodDescription) invokableMethod)); assertThat(specialMethodInvocation.getTypeDescription(), is(rawSuperClass)); MethodVisitor methodVisitor = mock(MethodVisitor.class); Implementation.Context implementationContext = mock(Implementation.Context.class); StackManipulation.Size size = specialMethodInvocation.apply(methodVisitor, implementationContext); verify(methodVisitor).visitMethodInsn(Opcodes.INVOKESPECIAL, BAR, FOO, QUX, false); verifyNoMoreInteractions(methodVisitor); verifyZeroInteractions(implementationContext); assertThat(size.getSizeImpact(), is(0)); assertThat(size.getMaximalSize(), is(0)); }
Example 4
Source File: MethodVariableAccessOtherTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testIncrement() throws Exception { StackManipulation stackManipulation = MethodVariableAccess.INTEGER.increment(4, 1); assertThat(stackManipulation.isValid(), is(true)); MethodVisitor methodVisitor = mock(MethodVisitor.class); Implementation.Context implementationContext = mock(Implementation.Context.class); StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext); assertThat(size.getSizeImpact(), is(0)); assertThat(size.getMaximalSize(), is(0)); verify(methodVisitor).visitIincInsn(4, 1); verifyNoMoreInteractions(methodVisitor); verifyZeroInteractions(implementationContext); }
Example 5
Source File: ReferenceTypeAwareAssignerTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testMutualAssignable() throws Exception { defineAssignability(true, true); StackManipulation stackManipulation = ReferenceTypeAwareAssigner.INSTANCE.assign(source, target, 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(methodVisitor); }
Example 6
Source File: MethodConstantTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testMethod() throws Exception { StackManipulation.Size size = MethodConstant.of(methodDescription).apply(methodVisitor, implementationContext); assertThat(size.getSizeImpact(), is(1)); assertThat(size.getMaximalSize(), is(6)); verify(methodVisitor).visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Class.class), "getDeclaredMethod", "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;", false); }
Example 7
Source File: MethodVariableAccessTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testLoading() throws Exception { StackManipulation stackManipulation = MethodVariableAccess.of(typeDefinition).loadFrom(4); assertThat(stackManipulation.isValid(), is(true)); StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext); assertThat(size.getSizeImpact(), is(this.size)); assertThat(size.getMaximalSize(), is(this.size)); verify(methodVisitor).visitVarInsn(readCode, 4); verifyNoMoreInteractions(methodVisitor); }
Example 8
Source File: SuperMethodCall.java From byte-buddy with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ public Size apply(MethodVisitor methodVisitor, Implementation.Context implementationContext, MethodDescription instrumentedMethod) { StackManipulation superMethodCall = implementationTarget .invokeDominant(instrumentedMethod.asSignatureToken()) .withCheckedCompatibilityTo(instrumentedMethod.asTypeToken()); if (!superMethodCall.isValid()) { throw new IllegalStateException("Cannot call super (or default) method for " + instrumentedMethod); } StackManipulation.Size size = new StackManipulation.Compound( MethodVariableAccess.allArgumentsOf(instrumentedMethod).prependThisReference(), superMethodCall, terminationHandler.of(instrumentedMethod) ).apply(methodVisitor, implementationContext); return new Size(size.getMaximalSize(), instrumentedMethod.getStackSize()); }
Example 9
Source File: TypeProxyCreationTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testForConstructorConstruction() 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.ForSuperMethodByConstructor(foo, implementationTarget, Collections.singletonList((TypeDescription) TypeDescription.ForLoadedType.of(Void.class)), true, 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(1)); assertThat(size.getMaximalSize(), is(3)); 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).visitInsn(Opcodes.ACONST_NULL); 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); }
Example 10
Source File: MethodVariableAccessOfMethodArgumentsTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testStaticMethod() throws Exception { when(methodDescription.isStatic()).thenReturn(true); StackManipulation stackManipulation = MethodVariableAccess.allArgumentsOf(methodDescription); 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 11
Source File: PrimitiveWideningDelegateTrivialTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testNoOpAssignment() throws Exception { StackManipulation stackManipulation = PrimitiveWideningDelegate.forPrimitive(sourceTypeDescription).widenTo(targetTypeDescription); assertThat(stackManipulation.isValid(), is(true)); StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext); assertThat(size.getSizeImpact(), is(0)); assertThat(size.getMaximalSize(), is(0)); verify(sourceTypeDescription, atLeast(1)).represents(sourceType); verify(targetTypeDescription, atLeast(1)).represents(sourceType); }
Example 12
Source File: HandleInvocationTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testInvocationDecreasingStack() throws Exception { JavaConstant.MethodType methodType = JavaConstant.MethodType.of(void.class, 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(0)); verify(methodVisitor).visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/invoke/MethodHandle", "invokeExact", "(Ljava/lang/Object;)V", false); verifyNoMoreInteractions(methodVisitor); verifyZeroInteractions(implementationContext); }
Example 13
Source File: DefaultMethodCall.java From byte-buddy with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ public Size apply(MethodVisitor methodVisitor, Context implementationContext, MethodDescription instrumentedMethod) { StackManipulation defaultMethodInvocation = locateDefault(instrumentedMethod); if (!defaultMethodInvocation.isValid()) { throw new IllegalStateException("Cannot invoke default method on " + instrumentedMethod); } StackManipulation.Size stackSize = new StackManipulation.Compound( MethodVariableAccess.allArgumentsOf(instrumentedMethod).prependThisReference(), defaultMethodInvocation, MethodReturn.of(instrumentedMethod.getReturnType()) ).apply(methodVisitor, implementationContext); return new Size(stackSize.getMaximalSize(), instrumentedMethod.getStackSize()); }
Example 14
Source File: MethodInvocationDynamicTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testDynamicConstructorBootstrap() throws Exception { when(methodDescription.isInvokeBootstrap()).thenReturn(true); when(methodDescription.isConstructor()).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_NEWINVOKESPECIAL, BAR, QUX, BAZ, false), argument); }
Example 15
Source File: IntegerConstantOpcodeTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@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 16
Source File: MethodVariableAccessOfMethodArgumentsTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testNonStaticMethod() throws Exception { StackManipulation stackManipulation = MethodVariableAccess.allArgumentsOf(methodDescription); 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).visitVarInsn(Opcodes.ALOAD, 2); verifyNoMoreInteractions(methodVisitor); }
Example 17
Source File: MethodConstantTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testConstructorPrivilegedCached() throws Exception { when(methodDescription.isConstructor()).thenReturn(true); when(implementationContext.cache(any(StackManipulation.class), any(TypeDescription.class))).thenReturn(fieldDescription); StackManipulation.Size size = MethodConstant.ofPrivileged(methodDescription).cached().apply(methodVisitor, implementationContext); assertThat(size.getSizeImpact(), is(1)); assertThat(size.getMaximalSize(), is(1)); verify(methodVisitor).visitFieldInsn(Opcodes.GETSTATIC, BAZ, FOO, QUX); verifyNoMoreInteractions(methodVisitor); verify(implementationContext).cache(MethodConstant.ofPrivileged(methodDescription), TypeDescription.ForLoadedType.of(Constructor.class)); verifyNoMoreInteractions(implementationContext); }
Example 18
Source File: FloatConstantTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testBiPush() throws Exception { StackManipulation floatConstant = FloatConstant.forValue(value); assertThat(floatConstant.isValid(), is(true)); StackManipulation.Size size = floatConstant.apply(methodVisitor, implementationContext); assertThat(size.getSizeImpact(), is(1)); assertThat(size.getMaximalSize(), is(1)); verify(methodVisitor).visitLdcInsn(value); verifyNoMoreInteractions(methodVisitor); verifyZeroInteractions(implementationContext); }
Example 19
Source File: TextConstantTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testTextValue() throws Exception { StackManipulation.Size size = new TextConstant(FOO).apply(methodVisitor, implementationContext); assertThat(size.getSizeImpact(), is(1)); assertThat(size.getMaximalSize(), is(1)); verify(methodVisitor).visitLdcInsn(FOO); verifyNoMoreInteractions(methodVisitor); verifyZeroInteractions(implementationContext); }
Example 20
Source File: ReferenceTypeAwareAssignerTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testTargetToSourceAssignableRuntimeType() throws Exception { defineAssignability(false, false); when(rawTarget.getInternalName()).thenReturn(FOO); StackManipulation stackManipulation = ReferenceTypeAwareAssigner.INSTANCE.assign(source, target, Assigner.Typing.DYNAMIC); assertThat(stackManipulation.isValid(), is(true)); StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext); assertThat(size.getSizeImpact(), is(0)); assertThat(size.getMaximalSize(), is(0)); verify(methodVisitor).visitTypeInsn(Opcodes.CHECKCAST, FOO); verifyNoMoreInteractions(methodVisitor); }