net.bytebuddy.implementation.bytecode.StackSize Java Examples

The following examples show how to use net.bytebuddy.implementation.bytecode.StackSize. 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: MethodRebaseResolverResolutionForRebasedConstructorTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Before
@SuppressWarnings("unchecked")
public void setUp() throws Exception {
    when(typeDescription.asErasure()).thenReturn(rawTypeDescription);
    when(methodDescription.isConstructor()).thenReturn(true);
    when(methodDescription.getDeclaringType()).thenReturn(rawTypeDescription);
    when(methodDescription.getReturnType()).thenReturn(returnType);
    when(methodDescription.getParameters()).thenReturn(new ParameterList.Explicit.ForTypes(methodDescription, parameterType));
    when(placeholderType.getStackSize()).thenReturn(StackSize.ZERO);
    when(placeholderType.asErasure()).thenReturn(rawPlaceholderType);
    when(placeholderType.asGenericType()).thenReturn(placeholderType);
    when(rawPlaceholderType.asGenericType()).thenReturn(placeholderType);
    when(parameterType.asGenericType()).thenReturn(parameterType);
    when(parameterType.getStackSize()).thenReturn(StackSize.ZERO);
    when(rawParameterType.getStackSize()).thenReturn(StackSize.ZERO);
    when(parameterType.asErasure()).thenReturn(rawParameterType);
    when(parameterType.accept(any(TypeDescription.Generic.Visitor.class))).thenReturn(parameterType);
    when(rawParameterType.asGenericType()).thenReturn(parameterType);
    when(methodDescription.getInternalName()).thenReturn(FOO);
    when(methodDescription.getDescriptor()).thenReturn(QUX);
    when(rawTypeDescription.getInternalName()).thenReturn(BAR);
    when(rawPlaceholderType.getDescriptor()).thenReturn(BAZ);
    when(otherPlaceHolderType.getDescriptor()).thenReturn(FOO);
    when(returnType.asErasure()).thenReturn(rawReturnType);
}
 
Example #2
Source File: ArrayFactory.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Size apply(MethodVisitor methodVisitor, Implementation.Context implementationContext) {
    Size size = IntegerConstant.forValue(stackManipulations.size()).apply(methodVisitor, implementationContext);
    // The array's construction does not alter the stack's size.
    size = size.aggregate(arrayCreator.apply(methodVisitor, implementationContext));
    int index = 0;
    for (StackManipulation stackManipulation : stackManipulations) {
        methodVisitor.visitInsn(Opcodes.DUP);
        size = size.aggregate(StackSize.SINGLE.toIncreasingSize());
        size = size.aggregate(IntegerConstant.forValue(index++).apply(methodVisitor, implementationContext));
        size = size.aggregate(stackManipulation.apply(methodVisitor, implementationContext));
        methodVisitor.visitInsn(arrayCreator.getStorageOpcode());
        size = size.aggregate(sizeDecrease);
    }
    return size;
}
 
Example #3
Source File: MethodInvocation.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Size apply(MethodVisitor methodVisitor, Implementation.Context implementationContext) {
    StringBuilder stringBuilder = new StringBuilder("(");
    for (TypeDescription parameterType : parameterTypes) {
        stringBuilder.append(parameterType.getDescriptor());
    }
    String methodDescriptor = stringBuilder.append(')').append(returnType.getDescriptor()).toString();
    methodVisitor.visitInvokeDynamicInsn(methodName,
            methodDescriptor,
            new Handle(handle == legacyHandle || implementationContext.getClassFileVersion().isAtLeast(ClassFileVersion.JAVA_V11)
                    ? handle
                    : legacyHandle,
                    bootstrapMethod.getDeclaringType().getInternalName(),
                    bootstrapMethod.getInternalName(),
                    bootstrapMethod.getDescriptor(),
                    bootstrapMethod.getDeclaringType().isInterface()),
            arguments.toArray(new Object[0]));
    int stackSize = returnType.getStackSize().getSize() - StackSize.of(parameterTypes);
    return new Size(stackSize, Math.max(stackSize, 0));
}
 
Example #4
Source File: FieldConstantTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    when(declaringType.getInternalName()).thenReturn(FOO);
    when(fieldDescription.getInternalName()).thenReturn(BAR);
    when(fieldDescription.getDeclaringType()).thenReturn(declaringType);
    when(declaringType.getDescriptor()).thenReturn("L" + QUX + ";");
    when(implementationContext.cache(new FieldConstant(fieldDescription), TypeDescription.ForLoadedType.of(Field.class)))
            .thenReturn(cacheField);
    when(cacheField.getDeclaringType()).thenReturn(cacheDeclaringType);
    when(cacheField.isStatic()).thenReturn(true);
    when(declaringType.getName()).thenReturn(BAZ);
    when(cacheDeclaringType.getInternalName()).thenReturn(BAZ);
    when(cacheField.getName()).thenReturn(FOO + BAR);
    when(cacheField.getType()).thenReturn(genericCacheFieldType);
    when(genericCacheFieldType.asErasure()).thenReturn(cacheFieldType);
    when(genericCacheFieldType.getStackSize()).thenReturn(StackSize.SINGLE);
    when(cacheField.getInternalName()).thenReturn(FOO + BAR);
    when(cacheField.getDescriptor()).thenReturn(QUX + BAZ);
    when(implementationContext.getClassFileVersion()).thenReturn(classFileVersion);
    when(implementationContext.getInstrumentedType()).thenReturn(instrumentedType);
}
 
Example #5
Source File: StackAwareMethodVisitorTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testDrainFreeList() throws Exception {
    StackAwareMethodVisitor methodVisitor = new StackAwareMethodVisitor(this.methodVisitor, methodDescription);
    methodVisitor.visitLdcInsn(1);
    methodVisitor.visitVarInsn(Opcodes.ISTORE, 41);
    methodVisitor.visitLdcInsn(1);
    methodVisitor.visitLdcInsn(1);
    assertThat(methodVisitor.drainStack(Opcodes.ISTORE, Opcodes.ILOAD, StackSize.SINGLE), is(43));
    InOrder inOrder = inOrder(this.methodVisitor);
    inOrder.verify(this.methodVisitor).visitLdcInsn(1);
    inOrder.verify(this.methodVisitor).visitVarInsn(Opcodes.ISTORE, 41);
    inOrder.verify(this.methodVisitor, times(2)).visitLdcInsn(1);
    inOrder.verify(this.methodVisitor).visitVarInsn(Opcodes.ISTORE, 42);
    inOrder.verify(this.methodVisitor).visitInsn(Opcodes.POP);
    inOrder.verify(this.methodVisitor).visitVarInsn(Opcodes.ILOAD, 42);
    verifyNoMoreInteractions(this.methodVisitor);
}
 
Example #6
Source File: AbstractTypeDescriptionGenericTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testNestedStaticTypeVariableType() throws Exception {
    TypeDescription.Generic typeDescription = describeType(NestedStaticTypeVariableType.class.getDeclaredField(FOO));
    // The toString implementation for parameterized types was changed within the Java 8 version range.
    assertThat(typeDescription.getTypeName(), CoreMatchers.anyOf(
            is(NestedStaticTypeVariableType.class.getDeclaredField(FOO).getGenericType().toString()),
            is("net.bytebuddy.description.type.AbstractTypeDescriptionGenericTest$NestedStaticTypeVariableType$Placeholder<java.lang.String>"),
            is("net.bytebuddy.description.type.AbstractTypeDescriptionGenericTest$NestedStaticTypeVariableType" +
                    ".net.bytebuddy.description.type.AbstractTypeDescriptionGenericTest$NestedStaticTypeVariableType$Placeholder<java.lang.String>")));
    assertThat(typeDescription.getSort(), is(TypeDefinition.Sort.PARAMETERIZED));
    assertThat(typeDescription.getStackSize(), is(StackSize.SINGLE));
    assertThat(typeDescription.getTypeArguments().size(), is(1));
    assertThat(typeDescription.getTypeArguments().getOnly(), is((TypeDefinition) TypeDescription.ForLoadedType.of(String.class)));
    Type ownerType = ((ParameterizedType) NestedStaticTypeVariableType.class.getDeclaredField(FOO).getGenericType()).getOwnerType();
    assertThat(typeDescription.getOwnerType(), is(TypeDefinition.Sort.describe(ownerType)));
    assertThat(typeDescription.getOwnerType().getSort(), is(TypeDefinition.Sort.NON_GENERIC));
}
 
Example #7
Source File: AbstractTypeDescriptionGenericTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testNestedSpecifiedTypeVariableType() throws Exception {
    TypeDescription.Generic typeDescription = describeType(NestedSpecifiedTypeVariableType.class.getDeclaredField(FOO));
    // The toString implementation for parameterized types was changed within the Java 8 version range.
    assertThat(typeDescription.getTypeName(), CoreMatchers.anyOf(
            is(NestedSpecifiedTypeVariableType.class.getDeclaredField(FOO).getGenericType().toString()),
            is("net.bytebuddy.description.type.AbstractTypeDescriptionGenericTest$NestedSpecifiedTypeVariableType<java.lang.String>$Placeholder"),
            is("net.bytebuddy.description.type.AbstractTypeDescriptionGenericTest" +
                    ".net.bytebuddy.description.type.AbstractTypeDescriptionGenericTest$NestedSpecifiedTypeVariableType<java.lang.String>.Placeholder")));
    assertThat(typeDescription.getSort(), is(TypeDefinition.Sort.PARAMETERIZED));
    assertThat(typeDescription.getStackSize(), is(StackSize.SINGLE));
    assertThat(typeDescription.getTypeArguments().size(), is(0));
    Type ownerType = ((ParameterizedType) NestedSpecifiedTypeVariableType.class.getDeclaredField(FOO).getGenericType()).getOwnerType();
    assertThat(typeDescription.getOwnerType(), is(TypeDefinition.Sort.describe(ownerType)));
    assertThat(typeDescription.getOwnerType().getSort(), is(TypeDefinition.Sort.PARAMETERIZED));
    assertThat(typeDescription.getOwnerType().getTypeArguments().size(), is(1));
    assertThat(typeDescription.getOwnerType().getTypeArguments().getOnly().getSort(), is(TypeDefinition.Sort.NON_GENERIC));
    assertThat(typeDescription.getOwnerType().getTypeArguments().getOnly(), is((TypeDefinition) TypeDescription.ForLoadedType.of(String.class)));
}
 
Example #8
Source File: MethodInvocationHandleTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    when(methodDescription.asDefined()).thenReturn(methodDescription);
    when(methodDescription.getReturnType()).thenReturn(returnType);
    when(methodDescription.getDeclaringType()).thenReturn(declaringType);
    when(returnType.getStackSize()).thenReturn(StackSize.ZERO);
    when(firstType.getStackSize()).thenReturn(StackSize.ZERO);
    when(firstType.getDescriptor()).thenReturn(FOO);
    when(secondType.getDescriptor()).thenReturn(BAR);
    when(secondType.getStackSize()).thenReturn(StackSize.ZERO);
    when(returnType.getStackSize()).thenReturn(StackSize.ZERO);
    when(methodDescription.getInternalName()).thenReturn(QUX);
    when(methodDescription.getDescriptor()).thenReturn(BAZ);
    when(declaringType.getDescriptor()).thenReturn(BAR);
    when(methodDescription.getParameters()).thenReturn(new ParameterList.Explicit.ForTypes(methodDescription, firstType, secondType));
}
 
Example #9
Source File: MethodRebaseResolverResolutionForRebasedMethodTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Before
@SuppressWarnings("unchecked")
public void setUp() throws Exception {
    when(methodDescription.getDeclaringType()).thenReturn(typeDescription);
    when(methodDescription.getReturnType()).thenReturn(genericReturnType);
    when(methodDescription.getInternalName()).thenReturn(FOO);
    when(methodDescription.getDescriptor()).thenReturn(BAZ);
    when(typeDescription.getInternalName()).thenReturn(BAR);
    when(typeDescription.getDescriptor()).thenReturn(BAR);
    when(instrumentedType.isInterface()).thenReturn(interfaceType);
    when(methodNameTransformer.transform(methodDescription)).thenReturn(QUX);
    when(otherMethodNameTransformer.transform(methodDescription)).thenReturn(FOO + BAR);
    when(parameterType.getStackSize()).thenReturn(StackSize.ZERO);
    when(methodDescription.getParameters()).thenReturn(new ParameterList.Explicit.ForTypes(methodDescription, genericParameterType));
    when(genericReturnType.asErasure()).thenReturn(returnType);
    when(genericReturnType.asRawType()).thenReturn(genericReturnType);
    when(genericReturnType.accept(any(TypeDescription.Generic.Visitor.class))).thenReturn(genericReturnType);
    when(genericParameterType.asErasure()).thenReturn(parameterType);
    when(genericParameterType.asGenericType()).thenReturn(genericParameterType);
    when(parameterType.asGenericType()).thenReturn(genericParameterType);
    when(genericParameterType.asRawType()).thenReturn(genericParameterType);
    when(genericParameterType.accept(any(TypeDescription.Generic.Visitor.class))).thenReturn(genericParameterType);
}
 
Example #10
Source File: AbstractTypeDescriptionTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testStackSize() throws Exception {
    assertThat(describe(void.class).getStackSize(), is(StackSize.ZERO));
    assertThat(describe(boolean.class).getStackSize(), is(StackSize.SINGLE));
    assertThat(describe(byte.class).getStackSize(), is(StackSize.SINGLE));
    assertThat(describe(short.class).getStackSize(), is(StackSize.SINGLE));
    assertThat(describe(char.class).getStackSize(), is(StackSize.SINGLE));
    assertThat(describe(int.class).getStackSize(), is(StackSize.SINGLE));
    assertThat(describe(long.class).getStackSize(), is(StackSize.DOUBLE));
    assertThat(describe(float.class).getStackSize(), is(StackSize.SINGLE));
    assertThat(describe(double.class).getStackSize(), is(StackSize.DOUBLE));
    assertThat(describe(Object.class).getStackSize(), is(StackSize.SINGLE));
    assertThat(describe(SampleClass.class).getStackSize(), is(StackSize.SINGLE));
    assertThat(describe(Object[].class).getStackSize(), is(StackSize.SINGLE));
    assertThat(describe(long[].class).getStackSize(), is(StackSize.SINGLE));
}
 
Example #11
Source File: MethodVariableAccessOfMethodArgumentsTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Before
@SuppressWarnings("unchecked")
public void setUp() throws Exception {
    when(methodDescription.getDeclaringType()).thenReturn(declaringType);
    when(declaringType.getStackSize()).thenReturn(StackSize.SINGLE);
    when(firstParameterType.getStackSize()).thenReturn(StackSize.SINGLE);
    when(firstParameterType.asErasure()).thenReturn(firstRawParameterType);
    when(firstParameterType.asGenericType()).thenReturn(firstParameterType);
    when(secondParameterType.asErasure()).thenReturn(secondRawParameterType);
    when(secondParameterType.getStackSize()).thenReturn(StackSize.SINGLE);
    when(secondParameterType.asGenericType()).thenReturn(secondParameterType);
    when(methodDescription.getParameters()).thenReturn(new ParameterList.Explicit.ForTypes(methodDescription, firstParameterType, secondParameterType));
    when(bridgeMethod.getDeclaringType()).thenReturn(declaringType);
    when(secondRawParameterType.getInternalName()).thenReturn(FOO);
    when(firstParameterType.accept(any(TypeDescription.Generic.Visitor.class))).thenReturn(firstParameterType);
    when(secondParameterType.accept(any(TypeDescription.Generic.Visitor.class))).thenReturn(secondParameterType);
}
 
Example #12
Source File: AbstractArrayFactoryTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
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 #13
Source File: AbstractTypeDescriptionGenericTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testNestedTypeVariableType() throws Exception {
    TypeDescription.Generic typeDescription = describeType(NestedTypeVariableType.class.getDeclaredField(FOO));
    // The toString implementation for parameterized types was changed within the Java 8 version range.
    assertThat(typeDescription.getTypeName(), CoreMatchers.anyOf(
            is(NestedTypeVariableType.class.getDeclaredField(FOO).getGenericType().toString()),
            is("net.bytebuddy.description.type.AbstractTypeDescriptionGenericTest$NestedTypeVariableType<T>$Placeholder"),
            is("net.bytebuddy.description.type.AbstractTypeDescriptionGenericTest" +
                    ".net.bytebuddy.description.type.AbstractTypeDescriptionGenericTest$NestedTypeVariableType<T>.Placeholder")));
    assertThat(typeDescription.getSort(), is(TypeDefinition.Sort.PARAMETERIZED));
    assertThat(typeDescription.getStackSize(), is(StackSize.SINGLE));
    assertThat(typeDescription.getTypeArguments().size(), is(0));
    Type ownerType = ((ParameterizedType) NestedTypeVariableType.class.getDeclaredField(FOO).getGenericType()).getOwnerType();
    assertThat(typeDescription.getOwnerType(), is(TypeDefinition.Sort.describe(ownerType)));
    assertThat(typeDescription.getOwnerType().getSort(), is(TypeDefinition.Sort.PARAMETERIZED));
    assertThat(typeDescription.getOwnerType().getTypeArguments().size(), is(1));
    assertThat(typeDescription.getOwnerType().getTypeArguments().getOnly().getSort(), is(TypeDefinition.Sort.VARIABLE));
    assertThat(typeDescription.getOwnerType().getTypeArguments().getOnly().getSymbol(), is(T));
}
 
Example #14
Source File: MethodBindingBuilderTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Before
@SuppressWarnings("unchecked")
public void setUp() throws Exception {
    when(methodDescription.getParameters()).thenReturn((ParameterList) methodParameterList);
    when(methodDescription.isStatic()).thenReturn(false);
    TypeDescription declaringType = mock(TypeDescription.class);
    when(methodDescription.getDeclaringType()).thenReturn(declaringType);
    when(declaringType.getInternalName()).thenReturn(FOO);
    when(declaringType.isInterface()).thenReturn(false);
    when(methodDescription.getInternalName()).thenReturn(BAR);
    when(methodDescription.getDescriptor()).thenReturn(BAZ);
    when(methodDescription.getStackSize()).thenReturn(0);
    when(methodDescription.getReturnType()).thenReturn(returnType);
    when(returnType.getStackSize()).thenReturn(StackSize.ZERO);
    when(legalStackManipulation.isValid()).thenReturn(true);
    when(illegalStackManipulation.isValid()).thenReturn(false);
}
 
Example #15
Source File: AllArgumentsBinderTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
private void testLegalStrictBinding(Assigner.Typing typing) throws Exception {
    when(annotation.value()).thenReturn(AllArguments.Assignment.STRICT);
    when(stackManipulation.isValid()).thenReturn(true);
    when(source.getParameters()).thenReturn(new ParameterList.Explicit.ForTypes(source, firstSourceType, secondSourceType));
    when(source.isStatic()).thenReturn(false);
    when(targetType.isArray()).thenReturn(true);
    when(targetType.getComponentType()).thenReturn(componentType);
    when(componentType.getStackSize()).thenReturn(StackSize.SINGLE);
    when(target.getType()).thenReturn(targetType);
    MethodDelegationBinder.ParameterBinding<?> parameterBinding = AllArguments.Binder.INSTANCE
            .bind(annotationDescription, source, target, implementationTarget, assigner, typing);
    assertThat(parameterBinding.isValid(), is(true));
    verify(source, atLeast(1)).getParameters();
    verify(source, atLeast(1)).isStatic();
    verify(target, atLeast(1)).getType();
    verify(target, never()).getDeclaredAnnotations();
    verify(assigner).assign(firstSourceType, componentType, typing);
    verify(assigner).assign(secondSourceType, componentType, typing);
    verifyNoMoreInteractions(assigner);
}
 
Example #16
Source File: StackAwareMethodVisitor.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * Drains all supplied elements of the operand stack.
 *
 * @param stackSizes The stack sizes of the elements to drain.
 */
private void doDrain(List<StackSize> stackSizes) {
    ListIterator<StackSize> iterator = stackSizes.listIterator(stackSizes.size());
    while (iterator.hasPrevious()) {
        StackSize current = iterator.previous();
        switch (current) {
            case SINGLE:
                super.visitInsn(Opcodes.POP);
                break;
            case DOUBLE:
                super.visitInsn(Opcodes.POP2);
                break;
            default:
                throw new IllegalStateException("Unexpected stack size: " + current);
        }
    }
}
 
Example #17
Source File: StackAwareMethodVisitor.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * Drains the stack to only contain the top value. For this, the value on top of the stack is temporarily stored
 * in the local variable array until all values on the stack are popped off. Subsequently, the top value is pushed
 * back onto the operand stack.
 *
 * @param store The opcode used for storing the top value.
 * @param load  The opcode used for loading the top value.
 * @param size  The size of the value on top of the operand stack.
 * @return The minimal size of the local variable array that is required to perform the operation.
 */
public int drainStack(int store, int load, StackSize size) {
    int difference = current.get(current.size() - 1).getSize() - size.getSize();
    if (current.size() == 1 && difference == 0) {
        return 0;
    } else {
        super.visitVarInsn(store, freeIndex);
        if (difference == 1) {
            super.visitInsn(Opcodes.POP);
        } else if (difference != 0) {
            throw new IllegalStateException("Unexpected remainder on the operand stack: " + difference);
        }
        doDrain(current.subList(0, current.size() - 1));
        super.visitVarInsn(load, freeIndex);
        return freeIndex + size.getSize();
    }
}
 
Example #18
Source File: AllArgumentsBinderTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testIllegalBinding() throws Exception {
    when(target.getIndex()).thenReturn(1);
    when(annotation.value()).thenReturn(AllArguments.Assignment.STRICT);
    when(stackManipulation.isValid()).thenReturn(false);
    when(source.getParameters()).thenReturn(new ParameterList.Explicit.ForTypes(source, firstSourceType, secondSourceType));
    when(source.isStatic()).thenReturn(false);
    when(targetType.isArray()).thenReturn(true);
    when(targetType.getComponentType()).thenReturn(componentType);
    when(componentType.getStackSize()).thenReturn(StackSize.SINGLE);
    when(target.getType()).thenReturn(targetType);
    when(target.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    MethodDelegationBinder.ParameterBinding<?> parameterBinding = AllArguments.Binder.INSTANCE
            .bind(annotationDescription, source, target, implementationTarget, assigner, Assigner.Typing.STATIC);
    assertThat(parameterBinding.isValid(), is(false));
    verify(source, atLeast(1)).getParameters();
    verify(source, atLeast(1)).isStatic();
    verify(target, atLeast(1)).getType();
    verify(target, never()).getDeclaredAnnotations();
    verify(assigner).assign(firstSourceType, componentType, Assigner.Typing.STATIC);
    verifyNoMoreInteractions(assigner);
}
 
Example #19
Source File: AbstractTypeDescriptionGenericTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testGenericArrayType() throws Exception {
    TypeDescription.Generic typeDescription = describeType(SimpleGenericArrayType.class.getDeclaredField(FOO));
    assertThat(typeDescription.getSort(), is(TypeDefinition.Sort.GENERIC_ARRAY));
    assertThat(typeDescription.getStackSize(), is(StackSize.SINGLE));
    assertThat(typeDescription.getDeclaredFields().size(), is(0));
    assertThat(typeDescription.getDeclaredMethods().size(), is(0));
    assertThat(typeDescription.getSuperClass(), is(TypeDescription.Generic.OBJECT));
    assertThat(typeDescription.getInterfaces(), is(TypeDescription.ARRAY_INTERFACES));
    assertThat(typeDescription.getActualName(), is(SimpleGenericArrayType.class.getDeclaredField(FOO).getGenericType().toString()));
    assertThat(typeDescription.getTypeName(), is(SimpleGenericArrayType.class.getDeclaredField(FOO).getGenericType().toString()));
    assertThat(typeDescription.toString(), is(SimpleGenericArrayType.class.getDeclaredField(FOO).getGenericType().toString()));
    assertThat(typeDescription.hashCode(),
            is(TypeDefinition.Sort.describe(SimpleGenericArrayType.class.getDeclaredField(FOO).getGenericType()).hashCode()));
    assertThat(typeDescription, is(TypeDefinition.Sort.describe(SimpleGenericArrayType.class.getDeclaredField(FOO).getGenericType())));
    assertThat(typeDescription, CoreMatchers.not(TypeDefinition.Sort.describe(SimpleGenericArrayType.class.getDeclaredField(FOO).getType())));
    assertThat(typeDescription, CoreMatchers.not(new Object()));
    assertThat(typeDescription.equals(null), is(false));
    assertThat(typeDescription.getComponentType().getSort(), is(TypeDefinition.Sort.PARAMETERIZED));
    assertThat(typeDescription.getComponentType().getTypeArguments().size(), is(1));
    assertThat(typeDescription.getComponentType().getTypeArguments().getOnly().getSort(), is(TypeDefinition.Sort.NON_GENERIC));
    assertThat(typeDescription.getComponentType().getTypeArguments().getOnly().asErasure().represents(String.class), is(true));
    assertThat(typeDescription.getTypeName(), is(SimpleGenericArrayType.class.getDeclaredField(FOO).getGenericType().toString()));
}
 
Example #20
Source File: IfEqual.java    From curiostack with MIT License 6 votes vote down vote up
@Override
public Size apply(MethodVisitor methodVisitor, Context implementationContext) {
  final int opcode;
  Size size = new Size(-StackSize.of(variableType).getSize() * 2, 0);
  if (variableType == int.class || variableType == boolean.class) {
    opcode = Opcodes.IF_ICMPEQ;
  } else if (variableType == long.class) {
    methodVisitor.visitInsn(Opcodes.LCMP);
    opcode = Opcodes.IFEQ;
  } else if (variableType == float.class) {
    methodVisitor.visitInsn(Opcodes.FCMPG);
    opcode = Opcodes.IFEQ;
  } else if (variableType == double.class) {
    methodVisitor.visitInsn(Opcodes.DCMPG);
    opcode = Opcodes.IFEQ;
  } else {
    // Reference type comparison assumes the result of Object.equals is already on the stack.
    opcode = Opcodes.IFNE;
    // There is only a boolean on the stack, so we only consume one item, unlike the others that
    // consume both.
    size = new Size(-1, 0);
  }
  methodVisitor.visitJumpInsn(opcode, destination);
  return size;
}
 
Example #21
Source File: FieldProxyBinderTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Before
@Override
public void setUp() throws Exception {
    super.setUp();
    when(getterMethod.getDeclaringType()).thenReturn(getterType);
    when(setterMethod.getDeclaringType()).thenReturn(setterType);
    when(instrumentedType.getDeclaredFields()).thenReturn(new FieldList.Explicit<FieldDescription.InDefinedShape>(fieldDescription));
    when(fieldDescription.getType()).thenReturn(genericFieldType);
    when(genericFieldType.getSort()).thenReturn(TypeDefinition.Sort.NON_GENERIC);
    when(genericFieldType.getStackSize()).thenReturn(StackSize.ZERO);
    when(genericFieldType.asErasure()).thenReturn(fieldType);
    when(fieldType.getSort()).thenReturn(TypeDefinition.Sort.NON_GENERIC);
    when(fieldType.asErasure()).thenReturn(fieldType);
    when(fieldType.getInternalName()).thenReturn(FOO);
    when(genericSetterType.asErasure()).thenReturn(setterType);
    when(genericGetterType.asErasure()).thenReturn(getterType);
}
 
Example #22
Source File: AbstractTypeDescriptionGenericTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testGenericArrayOfGenericComponentType() throws Exception {
    TypeDescription.Generic typeDescription = describeType(GenericArrayOfGenericComponentType.class.getDeclaredField(FOO));
    assertThat(typeDescription.getSort(), is(TypeDefinition.Sort.GENERIC_ARRAY));
    assertThat(typeDescription.getStackSize(), is(StackSize.SINGLE));
    assertThat(typeDescription.getDeclaredFields().size(), is(0));
    assertThat(typeDescription.getDeclaredMethods().size(), is(0));
    assertThat(typeDescription.getOwnerType(), nullValue(TypeDescription.Generic.class));
    assertThat(typeDescription.getSuperClass(), is(TypeDescription.Generic.OBJECT));
    assertThat(typeDescription.getInterfaces(), is(TypeDescription.ARRAY_INTERFACES));
    assertThat(typeDescription.getActualName(), is(GenericArrayOfGenericComponentType.class.getDeclaredField(FOO).getGenericType().toString()));
    assertThat(typeDescription.getTypeName(), is(GenericArrayOfGenericComponentType.class.getDeclaredField(FOO).getGenericType().toString()));
    assertThat(typeDescription.toString(), is(GenericArrayOfGenericComponentType.class.getDeclaredField(FOO).getGenericType().toString()));
    assertThat(typeDescription.hashCode(),
            is(TypeDefinition.Sort.describe(GenericArrayOfGenericComponentType.class.getDeclaredField(FOO).getGenericType()).hashCode()));
    assertThat(typeDescription, is(TypeDefinition.Sort.describe(GenericArrayOfGenericComponentType.class.getDeclaredField(FOO).getGenericType())));
    assertThat(typeDescription, CoreMatchers.not(TypeDefinition.Sort.describe(GenericArrayOfGenericComponentType.class.getDeclaredField(FOO).getType())));
    assertThat(typeDescription, CoreMatchers.not(new Object()));
    assertThat(typeDescription.equals(null), is(false));
    assertThat(typeDescription.getComponentType().getSort(), is(TypeDefinition.Sort.PARAMETERIZED));
    assertThat(typeDescription.getComponentType().getTypeArguments().size(), is(1));
    assertThat(typeDescription.getComponentType().getTypeArguments().getOnly().getSort(), is(TypeDefinition.Sort.VARIABLE));
    assertThat(typeDescription.getComponentType().getTypeArguments().getOnly().asErasure().represents(String.class), is(true));
    assertThat(typeDescription.getTypeName(), is(GenericArrayOfGenericComponentType.class.getDeclaredField(FOO).getGenericType().toString()));
}
 
Example #23
Source File: AbstractTypeDescriptionGenericTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testTypeVariableType() throws Exception {
    TypeDescription.Generic typeDescription = describeType(SimpleTypeVariableType.class.getDeclaredField(FOO));
    assertThat(typeDescription.getSort(), is(TypeDefinition.Sort.VARIABLE));
    assertThat(typeDescription.getActualName(), is(SimpleTypeVariableType.class.getDeclaredField(FOO).getGenericType().toString()));
    assertThat(typeDescription.getTypeName(), is(SimpleTypeVariableType.class.getDeclaredField(FOO).getGenericType().toString()));
    assertThat(typeDescription.toString(), is(SimpleTypeVariableType.class.getDeclaredField(FOO).getGenericType().toString()));
    assertThat(typeDescription.hashCode(),
            is(TypeDefinition.Sort.describe(SimpleTypeVariableType.class.getDeclaredField(FOO).getGenericType()).hashCode()));
    assertThat(typeDescription, is(TypeDefinition.Sort.describe(SimpleTypeVariableType.class.getDeclaredField(FOO).getGenericType())));
    assertThat(typeDescription,
            CoreMatchers.not(TypeDefinition.Sort.describe(SimpleTypeVariableType.class.getDeclaredField(FOO).getType())));
    assertThat(typeDescription, CoreMatchers.not(new Object()));
    assertThat(typeDescription.equals(null), is(false));
    assertThat(typeDescription.getSymbol(), is(T));
    assertThat(typeDescription.getUpperBounds().size(), is(1));
    assertThat(typeDescription.getUpperBounds().getOnly(), is(TypeDescription.Generic.OBJECT));
    assertThat(typeDescription.getUpperBounds().getOnly().getStackSize(), is(StackSize.SINGLE));
    assertThat(typeDescription.getTypeName(), is(SimpleTypeVariableType.class.getDeclaredField(FOO).getGenericType().toString()));
    MatcherAssert.assertThat(typeDescription.getTypeVariableSource(), is((TypeVariableSource) TypeDescription.ForLoadedType.of(SimpleTypeVariableType.class)));
    assertThat(typeDescription.getTypeVariableSource().getTypeVariables().size(), is(1));
    assertThat(typeDescription.getTypeVariableSource().getTypeVariables().getOnly(), is(typeDescription));
}
 
Example #24
Source File: AllArgumentsBinderTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Before
@Override
@SuppressWarnings("unchecked")
public void setUp() throws Exception {
    super.setUp();
    when(firstSourceType.getStackSize()).thenReturn(StackSize.SINGLE);
    when(secondSourceType.getStackSize()).thenReturn(StackSize.SINGLE);
    when(componentType.asErasure()).thenReturn(rawComponentType);
    when(targetType.getComponentType()).thenReturn(componentType);
    when(targetType.asErasure()).thenReturn(rawTargetType);
    when(firstSourceType.asGenericType()).thenReturn(firstSourceType);
    when(firstSourceType.accept(any(TypeDescription.Generic.Visitor.class))).thenReturn(firstSourceType);
    when(secondSourceType.asGenericType()).thenReturn(secondSourceType);
    when(secondSourceType.accept(any(TypeDescription.Generic.Visitor.class))).thenReturn(secondSourceType);
}
 
Example #25
Source File: VoidAwareAssignerNonVoidToVoidTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
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 #26
Source File: AbstractTypeDescriptionGenericTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testInterfaceOnlyMultipleUpperBoundTypeVariableType() throws Exception {
    TypeDescription.Generic typeDescription = describeType(InterfaceOnlyMultipleUpperBoundTypeVariableType.class.getDeclaredField(FOO));
    assertThat(typeDescription.getSort(), is(TypeDefinition.Sort.VARIABLE));
    assertThat(typeDescription.getSymbol(), is(T));
    assertThat(typeDescription.getStackSize(), is(StackSize.SINGLE));
    assertThat(typeDescription.getUpperBounds().size(), is(2));
    assertThat(typeDescription.getUpperBounds().get(0), is((TypeDefinition) TypeDescription.ForLoadedType.of(Foo.class)));
    assertThat(typeDescription.getUpperBounds().get(1), is((TypeDefinition) TypeDescription.ForLoadedType.of(Bar.class)));
    assertThat(typeDescription.getTypeName(), is(InterfaceOnlyMultipleUpperBoundTypeVariableType.class.getDeclaredField(FOO).getGenericType().toString()));
    assertThat(typeDescription.getTypeVariableSource(), is((TypeVariableSource) TypeDescription.ForLoadedType.of(InterfaceOnlyMultipleUpperBoundTypeVariableType.class)));
    assertThat(typeDescription.getTypeVariableSource().getTypeVariables().size(), is(1));
    assertThat(typeDescription.getTypeVariableSource().getTypeVariables().getOnly(), is(typeDescription));
}
 
Example #27
Source File: MethodConstantTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Before
@SuppressWarnings("unchecked")
public void setUp() throws Exception {
    when(declaringType.asErasure()).thenReturn(declaringType);
    when(methodDescription.getDeclaringType()).thenReturn(declaringType);
    when(methodDescription.getInternalName()).thenReturn(FOO);
    when(methodDescription.getParameters()).thenReturn((ParameterList) parameterList);
    when(parameterList.asTypeList()).thenReturn(typeList);
    when(declaringType.getDescriptor()).thenReturn(BAR);
    when(typeList.asErasures()).thenReturn(rawTypeList);
    when(rawTypeList.iterator()).thenReturn(Collections.singletonList(parameterType).iterator());
    when(parameterType.getDescriptor()).thenReturn(QUX);
    when(fieldDescription.getType()).thenReturn(genericFieldType);
    when(fieldDescription.isStatic()).thenReturn(true);
    when(genericFieldType.asErasure()).thenReturn(fieldType);
    when(genericFieldType.getStackSize()).thenReturn(StackSize.SINGLE);
    when(fieldDescription.getDeclaringType()).thenReturn(declaringType);
    when(declaringType.getInternalName()).thenReturn(BAZ);
    when(fieldDescription.getInternalName()).thenReturn(FOO);
    when(fieldDescription.getDescriptor()).thenReturn(QUX);
    when(fieldDescription.asDefined()).thenReturn(fieldDescription);
    when(implementationContext.getClassFileVersion()).thenReturn(classFileVersion);
    when(implementationContext.getInstrumentedType()).thenReturn(instrumentedType);
    when(auxiliaryConstructor.isConstructor()).thenReturn(true);
    when(auxiliaryConstructor.getDeclaringType()).thenReturn(auxiliaryType);
    when(auxiliaryConstructor.getReturnType()).thenReturn(TypeDescription.Generic.VOID);
    when(auxiliaryConstructor.getDescriptor()).thenReturn(FOO);
    when(auxiliaryConstructor.getInternalName()).thenReturn(BAR);
    when(auxiliaryType.getInternalName()).thenReturn(QUX);
}
 
Example #28
Source File: SuperMethodCallOtherTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
@SuppressWarnings("unchecked")
public void testNoSuper() throws Exception {
    when(typeDescription.getSuperClass()).thenReturn(superClass);
    when(methodDescription.getParameters()).thenReturn((ParameterList) new ParameterList.Empty<ParameterDescription>());
    when(methodDescription.getReturnType()).thenReturn(genericReturnType);
    when(methodDescription.getDeclaringType()).thenReturn(declaringType);
    when(declaringType.getStackSize()).thenReturn(StackSize.SINGLE);
    when(returnType.getStackSize()).thenReturn(StackSize.SINGLE);
    when(rawSuperClass.getDeclaredMethods()).thenReturn(superClassMethods);
    when(superClassMethods.filter(any(ElementMatcher.class))).thenReturn(superClassMethods);
    when(implementationTarget.invokeDominant(token)).thenReturn(Implementation.SpecialMethodInvocation.Illegal.INSTANCE);
    SuperMethodCall.INSTANCE.appender(implementationTarget).apply(methodVisitor, implementationContext, methodDescription);
}
 
Example #29
Source File: SuperMethodCallOtherTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
@SuppressWarnings("unchecked")
public void testStaticMethod() throws Exception {
    when(typeDescription.getSuperClass()).thenReturn(superClass);
    when(methodDescription.isStatic()).thenReturn(true);
    when(methodDescription.getParameters()).thenReturn((ParameterList) new ParameterList.Empty<ParameterDescription>());
    when(methodDescription.getReturnType()).thenReturn(genericReturnType);
    when(returnType.getStackSize()).thenReturn(StackSize.SINGLE);
    when(rawSuperClass.getDeclaredMethods()).thenReturn(superClassMethods);
    when(superClassMethods.filter(any(ElementMatcher.class))).thenReturn(superClassMethods);
    when(implementationTarget.invokeDominant(token)).thenReturn(Implementation.SpecialMethodInvocation.Illegal.INSTANCE);
    SuperMethodCall.INSTANCE.appender(implementationTarget).apply(methodVisitor, implementationContext, methodDescription);
}
 
Example #30
Source File: AbstractTypeDescriptionGenericTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testShadowedTypeVariableType() throws Exception {
    TypeDescription.Generic typeDescription = describeReturnType(ShadowingTypeVariableType.class.getDeclaredMethod(FOO));
    assertThat(typeDescription.getSort(), is(TypeDefinition.Sort.VARIABLE));
    assertThat(typeDescription.getSymbol(), is(T));
    assertThat(typeDescription.getStackSize(), is(StackSize.SINGLE));
    assertThat(typeDescription.getUpperBounds().size(), is(1));
    assertThat(typeDescription.getUpperBounds().getOnly(), is(TypeDescription.Generic.OBJECT));
    assertThat(typeDescription.getTypeName(), is(ShadowingTypeVariableType.class.getDeclaredMethod(FOO).getGenericReturnType().toString()));
    assertThat(typeDescription.getTypeVariableSource(), is((TypeVariableSource) new MethodDescription.ForLoadedMethod(ShadowingTypeVariableType.class.getDeclaredMethod(FOO))));
    assertThat(typeDescription.getTypeVariableSource().getTypeVariables().size(), is(1));
    assertThat(typeDescription.getTypeVariableSource().getTypeVariables().getOnly(), is(typeDescription));
}