net.bytebuddy.description.field.FieldList Java Examples

The following examples show how to use net.bytebuddy.description.field.FieldList. 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: 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 #2
Source File: FieldValueBinderTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testIllegalAssignmentStaticMethod() throws Exception {
    doReturn(void.class).when(annotation).declaringType();
    when(annotation.value()).thenReturn(FOO);
    when(instrumentedType.getDeclaredFields()).thenReturn(new FieldList.Explicit<FieldDescription.InDefinedShape>(fieldDescription));
    when(fieldDescription.getActualName()).thenReturn(FOO);
    when(fieldDescription.isVisibleTo(instrumentedType)).thenReturn(true);
    when(target.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    when(stackManipulation.isValid()).thenReturn(true);
    when(source.isStatic()).thenReturn(true);
    MethodDelegationBinder.ParameterBinding<?> binding = FieldValue.Binder.INSTANCE.bind(annotationDescription,
            source,
            target,
            implementationTarget,
            assigner,
            Assigner.Typing.STATIC);
    assertThat(binding.isValid(), is(false));
}
 
Example #3
Source File: MemberRemoval.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public ClassVisitor wrap(TypeDescription instrumentedType,
                         ClassVisitor classVisitor,
                         Implementation.Context implementationContext,
                         TypePool typePool,
                         FieldList<FieldDescription.InDefinedShape> fields,
                         MethodList<?> methods,
                         int writerFlags,
                         int readerFlags) {
    Map<String, FieldDescription.InDefinedShape> mappedFields = new HashMap<String, FieldDescription.InDefinedShape>();
    for (FieldDescription.InDefinedShape fieldDescription : fields) {
        mappedFields.put(fieldDescription.getInternalName() + fieldDescription.getDescriptor(), fieldDescription);
    }
    Map<String, MethodDescription> mappedMethods = new HashMap<String, MethodDescription>();
    for (MethodDescription methodDescription : CompoundList.<MethodDescription>of(methods, new MethodDescription.Latent.TypeInitializer(instrumentedType))) {
        mappedMethods.put(methodDescription.getInternalName() + methodDescription.getDescriptor(), methodDescription);
    }
    return new MemberRemovingClassVisitor(classVisitor, fieldMatcher, methodMatcher, mappedFields, mappedMethods);
}
 
Example #4
Source File: FieldValueBinderTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testLegalAssignment() throws Exception {
    doReturn(void.class).when(annotation).declaringType();
    when(annotation.value()).thenReturn(FOO);
    when(instrumentedType.getDeclaredFields()).thenReturn(new FieldList.Explicit<FieldDescription.InDefinedShape>(fieldDescription));
    when(fieldDescription.getActualName()).thenReturn(FOO);
    when(fieldDescription.isVisibleTo(instrumentedType)).thenReturn(true);
    when(target.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    when(stackManipulation.isValid()).thenReturn(true);
    MethodDelegationBinder.ParameterBinding<?> binding = FieldValue.Binder.INSTANCE.bind(annotationDescription,
            source,
            target,
            implementationTarget,
            assigner,
            Assigner.Typing.STATIC);
    assertThat(binding.isValid(), is(true));
}
 
Example #5
Source File: FieldValueBinderTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testIllegalAssignmentNonAssignable() throws Exception {
    doReturn(void.class).when(annotation).declaringType();
    when(annotation.value()).thenReturn(FOO);
    when(instrumentedType.getDeclaredFields()).thenReturn(new FieldList.Explicit<FieldDescription.InDefinedShape>(fieldDescription));
    when(fieldDescription.getActualName()).thenReturn(FOO);
    when(fieldDescription.isVisibleTo(instrumentedType)).thenReturn(true);
    when(target.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    when(stackManipulation.isValid()).thenReturn(false);
    MethodDelegationBinder.ParameterBinding<?> binding = FieldValue.Binder.INSTANCE.bind(annotationDescription,
            source,
            target,
            implementationTarget,
            assigner,
            Assigner.Typing.STATIC);
    assertThat(binding.isValid(), is(false));
}
 
Example #6
Source File: FieldValueBinderTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testLegalAssignmentStaticMethodStaticField() throws Exception {
    doReturn(void.class).when(annotation).declaringType();
    when(annotation.value()).thenReturn(FOO);
    when(instrumentedType.getDeclaredFields()).thenReturn(new FieldList.Explicit<FieldDescription.InDefinedShape>(fieldDescription));
    when(fieldDescription.getActualName()).thenReturn(FOO);
    when(fieldDescription.isVisibleTo(instrumentedType)).thenReturn(true);
    when(target.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    when(stackManipulation.isValid()).thenReturn(true);
    when(source.isStatic()).thenReturn(true);
    when(fieldDescription.isStatic()).thenReturn(true);
    MethodDelegationBinder.ParameterBinding<?> binding = FieldValue.Binder.INSTANCE.bind(annotationDescription,
            source,
            target,
            implementationTarget,
            assigner,
            Assigner.Typing.STATIC);
    assertThat(binding.isValid(), is(true));
}
 
Example #7
Source File: FieldValueBinderTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testIllegalAssignmentNonVisible() throws Exception {
    doReturn(void.class).when(annotation).declaringType();
    when(annotation.value()).thenReturn(FOO);
    when(instrumentedType.getDeclaredFields()).thenReturn((FieldList) new FieldList.Explicit<FieldDescription>(fieldDescription));
    when(fieldDescription.getActualName()).thenReturn(FOO);
    when(fieldDescription.isVisibleTo(instrumentedType)).thenReturn(false);
    when(target.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    when(stackManipulation.isValid()).thenReturn(true);
    MethodDelegationBinder.ParameterBinding<?> binding = FieldValue.Binder.INSTANCE.bind(annotationDescription,
            source,
            target,
            implementationTarget,
            assigner,
            Assigner.Typing.STATIC);
    assertThat(binding.isValid(), is(false));
}
 
Example #8
Source File: FieldValueBinderTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetterNameDiscovery() throws Exception {
    doReturn(void.class).when(annotation).declaringType();
    when(annotation.value()).thenReturn(FieldValue.Binder.Delegate.BEAN_PROPERTY);
    when(instrumentedType.getDeclaredFields()).thenReturn(new FieldList.Explicit<FieldDescription.InDefinedShape>(fieldDescription));
    when(fieldDescription.getActualName()).thenReturn(FOO);
    when(fieldDescription.isVisibleTo(instrumentedType)).thenReturn(true);
    when(target.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    when(stackManipulation.isValid()).thenReturn(true);
    when(source.getInternalName()).thenReturn("getFoo");
    when(source.getActualName()).thenReturn("getFoo");
    when(source.getReturnType()).thenReturn(TypeDescription.Generic.OBJECT);
    when(source.getParameters()).thenReturn(new ParameterList.Empty<ParameterDescription.InDefinedShape>());
    MethodDelegationBinder.ParameterBinding<?> binding = FieldValue.Binder.INSTANCE.bind(annotationDescription,
            source,
            target,
            implementationTarget,
            assigner,
            Assigner.Typing.STATIC);
    assertThat(binding.isValid(), is(true));
}
 
Example #9
Source File: FieldValueBinderTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetterNameDiscoveryBoolean() throws Exception {
    doReturn(void.class).when(annotation).declaringType();
    when(annotation.value()).thenReturn(FieldValue.Binder.Delegate.BEAN_PROPERTY);
    when(instrumentedType.getDeclaredFields()).thenReturn(new FieldList.Explicit<FieldDescription.InDefinedShape>(fieldDescription));
    when(fieldDescription.getActualName()).thenReturn(FOO);
    when(fieldDescription.isVisibleTo(instrumentedType)).thenReturn(true);
    when(target.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    when(stackManipulation.isValid()).thenReturn(true);
    when(source.getInternalName()).thenReturn("isFoo");
    when(source.getActualName()).thenReturn("isFoo");
    when(source.getReturnType()).thenReturn(TypeDescription.Generic.OfNonGenericType.ForLoadedType.of(boolean.class));
    when(source.getParameters()).thenReturn(new ParameterList.Empty<ParameterDescription.InDefinedShape>());
    MethodDelegationBinder.ParameterBinding<?> binding = FieldValue.Binder.INSTANCE.bind(annotationDescription,
            source,
            target,
            implementationTarget,
            assigner,
            Assigner.Typing.STATIC);
    assertThat(binding.isValid(), is(true));
}
 
Example #10
Source File: TransformerInvokerGenerator.java    From Diorite with MIT License 6 votes vote down vote up
@Override
        public ClassVisitor wrap(TypeDescription typeDescription, ClassVisitor cv, Context context, TypePool typePool,
                                 FieldList<FieldDescription.InDefinedShape> fieldList, MethodList<?> methodList, int i, int i1)
        {
//            public void visit(int version, int modifiers, String name, String signature, String superName, String[] interfaces) {
            cv.visit(ClassFileVersion.JAVA_V9.getMinorMajorVersion(), typeDescription.getModifiers(), typeDescription.getInternalName(), null,
                     typeDescription.getSuperClass().asErasure().getInternalName(), typeDescription.getInterfaces().asErasures().toInternalNames());
            TypeDescription clazz = this.clazz;
            String internalName = clazz.getInternalName();
            String descriptor = clazz.getDescriptor();
            MethodList<InDefinedShape> declaredMethods = clazz.getDeclaredMethods();
            int methodsSize = declaredMethods.size();
            String implName = GENERATED_PREFIX + "." + clazz.getName();
            String internalImplName = GENERATED_PREFIX.replace('.', '/') + "/" + internalName;
            String descriptorImplName = "L" + GENERATED_PREFIX.replace('.', '/') + "/" + internalName + ";";

            FieldVisitor fv;
            MethodVisitor mv;
            AnnotationVisitor av0;

            cv.visitEnd();
            return cv;
        }
 
Example #11
Source File: PropertyMutatorCollector.java    From jackson-modules-base with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
protected StackManipulation invocationOperation(AnnotatedMember annotatedMember,
        TypeDefinition beanClassDescription) {

    final String fieldName = annotatedMember.getName();
    final FieldList<FieldDescription> matchingFields =
            (FieldList<FieldDescription>) beanClassDescription.getDeclaredFields().filter(named(fieldName));

    if (matchingFields.size() == 1) { //method was declared on class
        return FieldAccess.forField(matchingFields.getOnly()).write();
    }
    if (matchingFields.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 field: " + fieldName);
    }
}
 
Example #12
Source File: PropertyAccessorCollector.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 fieldName = annotatedMember.getName();
    @SuppressWarnings("unchecked")
    final FieldList<FieldDescription> matchingFields =
            (FieldList<FieldDescription>) beanClassDescription.getDeclaredFields().filter(named(fieldName));

    if (matchingFields.size() == 1) { //method was declared on class
        return FieldAccess.forField(matchingFields.getOnly()).read();
    }
    if (matchingFields.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 field: " + fieldName);
    }
}
 
Example #13
Source File: FieldValueBinderTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testSetterNameDiscovery() throws Exception {
    doReturn(void.class).when(annotation).declaringType();
    when(annotation.value()).thenReturn(FieldValue.Binder.Delegate.BEAN_PROPERTY);
    when(instrumentedType.getDeclaredFields()).thenReturn(new FieldList.Explicit<FieldDescription.InDefinedShape>(fieldDescription));
    when(fieldDescription.getActualName()).thenReturn(FOO);
    when(fieldDescription.isVisibleTo(instrumentedType)).thenReturn(true);
    when(target.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    when(stackManipulation.isValid()).thenReturn(true);
    when(source.getInternalName()).thenReturn("setFoo");
    when(source.getActualName()).thenReturn("setFoo");
    when(source.getReturnType()).thenReturn(TypeDescription.Generic.VOID);
    when(source.getParameters()).thenReturn(new ParameterList.Explicit.ForTypes(source, TypeDescription.Generic.OBJECT));
    MethodDelegationBinder.ParameterBinding<?> binding = FieldValue.Binder.INSTANCE.bind(annotationDescription,
            source,
            target,
            implementationTarget,
            assigner,
            Assigner.Typing.STATIC);
    assertThat(binding.isValid(), is(true));
}
 
Example #14
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 #15
Source File: ModifierAdjustment.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public ModifierAdjustingClassVisitor wrap(TypeDescription instrumentedType,
                                          ClassVisitor classVisitor,
                                          Implementation.Context implementationContext,
                                          TypePool typePool,
                                          FieldList<FieldDescription.InDefinedShape> fields,
                                          MethodList<?> methods,
                                          int writerFlags,
                                          int readerFlags) {
    Map<String, FieldDescription.InDefinedShape> mappedFields = new HashMap<String, FieldDescription.InDefinedShape>();
    for (FieldDescription.InDefinedShape fieldDescription : fields) {
        mappedFields.put(fieldDescription.getInternalName() + fieldDescription.getDescriptor(), fieldDescription);
    }
    Map<String, MethodDescription> mappedMethods = new HashMap<String, MethodDescription>();
    for (MethodDescription methodDescription : CompoundList.<MethodDescription>of(methods, new MethodDescription.Latent.TypeInitializer(instrumentedType))) {
        mappedMethods.put(methodDescription.getInternalName() + methodDescription.getDescriptor(), methodDescription);
    }
    return new ModifierAdjustingClassVisitor(classVisitor,
            typeAdjustments,
            fieldAdjustments,
            methodAdjustments,
            instrumentedType,
            mappedFields,
            mappedMethods);
}
 
Example #16
Source File: AsmVisitorWrapper.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public ClassVisitor wrap(TypeDescription instrumentedType,
                         ClassVisitor classVisitor,
                         Implementation.Context implementationContext,
                         TypePool typePool,
                         FieldList<FieldDescription.InDefinedShape> fields,
                         MethodList<?> methods,
                         int writerFlags,
                         int readerFlags) {
    Map<String, MethodDescription> mapped = new HashMap<String, MethodDescription>();
    for (MethodDescription methodDescription : CompoundList.<MethodDescription>of(methods, new MethodDescription.Latent.TypeInitializer(instrumentedType))) {
        mapped.put(methodDescription.getInternalName() + methodDescription.getDescriptor(), methodDescription);
    }
    return new DispatchingVisitor(classVisitor,
            instrumentedType,
            implementationContext,
            typePool,
            mapped,
            writerFlags,
            readerFlags);
}
 
Example #17
Source File: AsmVisitorWrapper.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public ClassVisitor wrap(TypeDescription instrumentedType,
                         ClassVisitor classVisitor,
                         Implementation.Context implementationContext,
                         TypePool typePool,
                         FieldList<FieldDescription.InDefinedShape> fields,
                         MethodList<?> methods,
                         int writerFlags,
                         int readerFlags) {
    for (AsmVisitorWrapper asmVisitorWrapper : asmVisitorWrappers) {
        classVisitor = asmVisitorWrapper.wrap(instrumentedType,
                classVisitor,
                implementationContext,
                typePool,
                fields,
                methods,
                writerFlags,
                readerFlags);
    }
    return classVisitor;
}
 
Example #18
Source File: MemberSubstitution.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public FieldDescription resolve(TypeDescription targetType, ByteCodeElement target, TypeList.Generic parameters, TypeDescription.Generic result) {
    if (parameters.isEmpty()) {
        throw new IllegalStateException("Cannot substitute parameterless instruction with " + target);
    } else if (parameters.get(0).isPrimitive() || parameters.get(0).isArray()) {
        throw new IllegalStateException("Cannot access field on primitive or array type for " + target);
    }
    TypeDefinition current = parameters.get(0);
    do {
        FieldList<?> fields = current.getDeclaredFields().filter(not(isStatic()).<FieldDescription>and(isVisibleTo(instrumentedType)).and(matcher));
        if (fields.size() == 1) {
            return fields.getOnly();
        } else if (fields.size() > 1) {
            throw new IllegalStateException("Ambiguous field location of " + fields);
        }
        current = current.getSuperClass();
    } while (current != null);
    throw new IllegalStateException("Cannot locate field matching " + matcher + " on " + targetType);
}
 
Example #19
Source File: AsmVisitorWrapperForDeclaredFieldsTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnknown() throws Exception {
    assertThat(new AsmVisitorWrapper.ForDeclaredFields()
            .field(matcher, fieldVisitorWrapper)
            .wrap(instrumentedType,
                    classVisitor,
                    implementationContext,
                    typePool,
                    new FieldList.Explicit<FieldDescription.InDefinedShape>(foo, bar),
                    new MethodList.Empty<MethodDescription>(),
                    IRRELEVANT,
                    IRRELEVANT)
            .visitField(MODIFIERS, FOO + BAR, QUX, BAZ, QUX + BAZ), is(fieldVisitor));
    verifyZeroInteractions(matcher);
    verifyZeroInteractions(fieldVisitorWrapper);
}
 
Example #20
Source File: AsmVisitorWrapperForDeclaredFieldsTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testNotMatched() throws Exception {
    assertThat(new AsmVisitorWrapper.ForDeclaredFields()
            .field(matcher, fieldVisitorWrapper)
            .wrap(instrumentedType,
                    classVisitor,
                    implementationContext,
                    typePool,
                    new FieldList.Explicit<FieldDescription.InDefinedShape>(foo, bar),
                    new MethodList.Empty<MethodDescription>(),
                    IRRELEVANT,
                    IRRELEVANT)
            .visitField(MODIFIERS, BAR, QUX, BAZ, QUX + BAZ), is(fieldVisitor));
    verify(matcher).matches(bar);
    verifyNoMoreInteractions(matcher);
    verifyZeroInteractions(fieldVisitorWrapper);
}
 
Example #21
Source File: AsmVisitorWrapperForDeclaredFieldsTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testMatched() throws Exception {
    assertThat(new AsmVisitorWrapper.ForDeclaredFields()
            .field(matcher, fieldVisitorWrapper)
            .wrap(instrumentedType,
                    classVisitor,
                    implementationContext,
                    typePool,
                    new FieldList.Explicit<FieldDescription.InDefinedShape>(foo, bar),
                    new MethodList.Empty<MethodDescription>(),
                    IRRELEVANT,
                    IRRELEVANT)
            .visitField(MODIFIERS, FOO, QUX, BAZ, QUX + BAZ), is(wrappedVisitor));
    verify(matcher).matches(foo);
    verifyNoMoreInteractions(matcher);
    verify(fieldVisitorWrapper).wrap(instrumentedType, foo, fieldVisitor);
    verifyNoMoreInteractions(fieldVisitorWrapper);
}
 
Example #22
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();
    StackManipulation[] fieldLoading = new StackManipulation[fieldList.size()];
    int index = 0;
    for (FieldDescription fieldDescription : fieldList) {
        fieldLoading[index] = new StackManipulation.Compound(
                MethodVariableAccess.loadThis(),
                MethodVariableAccess.load(instrumentedMethod.getParameters().get(index)),
                FieldAccess.forField(fieldDescription).write()
        );
        index++;
    }
    StackManipulation.Size stackSize = new StackManipulation.Compound(
            MethodVariableAccess.loadThis(),
            MethodInvocation.invoke(ConstructorCall.INSTANCE.objectTypeDefaultConstructor),
            new StackManipulation.Compound(fieldLoading),
            MethodReturn.VOID
    ).apply(methodVisitor, implementationContext);
    return new Size(stackSize.getMaximalSize(), instrumentedMethod.getStackSize());
}
 
Example #23
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 #24
Source File: AsmVisitorWrapperForDeclaredMethodsTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnknownInvokable() throws Exception {
    assertThat(new AsmVisitorWrapper.ForDeclaredMethods()
            .invokable(matcher, methodVisitorWrapper)
            .wrap(instrumentedType,
                    classVisitor,
                    implementationContext,
                    typePool,
                    new FieldList.Empty<FieldDescription.InDefinedShape>(),
                    new MethodList.Explicit<MethodDescription>(foo, bar),
                    FLAGS,
                    FLAGS * 2)
            .visitMethod(MODIFIERS, FOO + BAR, QUX, BAZ, new String[]{QUX + BAZ}), is(methodVisitor));
    verifyZeroInteractions(matcher);
    verifyZeroInteractions(methodVisitorWrapper);
    verifyZeroInteractions(typePool);
}
 
Example #25
Source File: AsmVisitorWrapperForDeclaredMethodsTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testNonMatchedInvokable() throws Exception {
    assertThat(new AsmVisitorWrapper.ForDeclaredMethods()
            .invokable(matcher, methodVisitorWrapper)
            .wrap(instrumentedType,
                    classVisitor,
                    implementationContext,
                    typePool,
                    new FieldList.Empty<FieldDescription.InDefinedShape>(),
                    new MethodList.Explicit<MethodDescription>(foo, bar),
                    FLAGS,
                    FLAGS * 2)
            .visitMethod(MODIFIERS, BAR, BAZ, BAZ, new String[]{QUX + BAZ}), is(methodVisitor));
    verify(matcher).matches(bar);
    verifyNoMoreInteractions(matcher);
    verifyZeroInteractions(methodVisitorWrapper);
    verifyZeroInteractions(typePool);
}
 
Example #26
Source File: AsmVisitorWrapperForDeclaredMethodsTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testMatchedInvokable() throws Exception {
    assertThat(new AsmVisitorWrapper.ForDeclaredMethods()
            .invokable(matcher, methodVisitorWrapper)
            .wrap(instrumentedType,
                    classVisitor,
                    implementationContext,
                    typePool,
                    new FieldList.Empty<FieldDescription.InDefinedShape>(),
                    new MethodList.Explicit<MethodDescription>(foo, bar),
                    FLAGS,
                    FLAGS * 2)
            .visitMethod(MODIFIERS, FOO, QUX, BAZ, new String[]{QUX + BAZ}), is(wrappedVisitor));
    verify(matcher).matches(foo);
    verifyNoMoreInteractions(matcher);
    verify(methodVisitorWrapper).wrap(instrumentedType, foo, methodVisitor, implementationContext, typePool, FLAGS, FLAGS * 2);
    verifyNoMoreInteractions(methodVisitorWrapper);
    verifyZeroInteractions(typePool);
}
 
Example #27
Source File: TypeConstantAdjustmentTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testInstrumentationLegacyClassOtherType() throws Exception {
    ClassVisitor classVisitor = TypeConstantAdjustment.INSTANCE.wrap(mock(TypeDescription.class),
            this.classVisitor,
            mock(Implementation.Context.class),
            mock(TypePool.class),
            new FieldList.Empty<FieldDescription.InDefinedShape>(),
            new MethodList.Empty<MethodDescription>(),
            IGNORED,
            IGNORED);
    classVisitor.visit(ClassFileVersion.JAVA_V4.getMinorMajorVersion(), FOOBAR, FOO, BAR, QUX, new String[]{BAZ});
    MethodVisitor methodVisitor = classVisitor.visitMethod(FOOBAR, FOO, BAR, QUX, new String[]{BAZ});
    assertThat(methodVisitor, not(this.methodVisitor));
    methodVisitor.visitLdcInsn(FOO);
    verify(this.classVisitor).visit(ClassFileVersion.JAVA_V4.getMinorMajorVersion(), FOOBAR, FOO, BAR, QUX, new String[]{BAZ});
    verify(this.classVisitor).visitMethod(FOOBAR, FOO, BAR, QUX, new String[]{BAZ});
    verifyNoMoreInteractions(this.classVisitor);
    verify(this.methodVisitor).visitLdcInsn(FOO);
    verifyNoMoreInteractions(this.methodVisitor);
}
 
Example #28
Source File: TypeConstantAdjustmentTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testInstrumentationModernClassFile() throws Exception {
    ClassVisitor classVisitor = TypeConstantAdjustment.INSTANCE.wrap(mock(TypeDescription.class),
            this.classVisitor,
            mock(Implementation.Context.class),
            mock(TypePool.class),
            new FieldList.Empty<FieldDescription.InDefinedShape>(),
            new MethodList.Empty<MethodDescription>(),
            IGNORED,
            IGNORED);
    classVisitor.visit(ClassFileVersion.JAVA_V5.getMinorMajorVersion(), FOOBAR, FOO, BAR, QUX, new String[]{BAZ});
    assertThat(classVisitor.visitMethod(FOOBAR, FOO, BAR, QUX, new String[]{BAZ}), is(methodVisitor));
    verify(this.classVisitor).visit(ClassFileVersion.JAVA_V5.getMinorMajorVersion(), FOOBAR, FOO, BAR, QUX, new String[]{BAZ});
    verify(this.classVisitor).visitMethod(FOOBAR, FOO, BAR, QUX, new String[]{BAZ});
    verifyNoMoreInteractions(this.classVisitor);
    verifyZeroInteractions(methodVisitor);
}
 
Example #29
Source File: TypeConstantAdjustmentTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testInstrumentationLegacyClassFileObjectType() throws Exception {
    ClassVisitor classVisitor = TypeConstantAdjustment.INSTANCE.wrap(mock(TypeDescription.class),
            this.classVisitor,
            mock(Implementation.Context.class),
            mock(TypePool.class),
            new FieldList.Empty<FieldDescription.InDefinedShape>(),
            new MethodList.Empty<MethodDescription>(),
            IGNORED,
            IGNORED);
    classVisitor.visit(ClassFileVersion.JAVA_V4.getMinorMajorVersion(), FOOBAR, FOO, BAR, QUX, new String[]{BAZ});
    MethodVisitor methodVisitor = classVisitor.visitMethod(FOOBAR, FOO, BAR, QUX, new String[]{BAZ});
    assertThat(methodVisitor, not(this.methodVisitor));
    methodVisitor.visitLdcInsn(Type.getType(Object.class));
    verify(this.classVisitor).visit(ClassFileVersion.JAVA_V4.getMinorMajorVersion(), FOOBAR, FOO, BAR, QUX, new String[]{BAZ});
    verify(this.classVisitor).visitMethod(FOOBAR, FOO, BAR, QUX, new String[]{BAZ});
    verifyNoMoreInteractions(this.classVisitor);
    verify(this.methodVisitor).visitLdcInsn(Type.getType(Object.class).getClassName());
    verify(this.methodVisitor).visitMethodInsn(Opcodes.INVOKESTATIC,
            Type.getType(Class.class).getInternalName(),
            "forName",
            Type.getType(Class.class.getDeclaredMethod("forName", String.class)).getDescriptor(),
            false);
    verifyNoMoreInteractions(this.methodVisitor);
}
 
Example #30
Source File: TypeConstantAdjustmentTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testInstrumentationLegacyClassFileArrayType() throws Exception {
    ClassVisitor classVisitor = TypeConstantAdjustment.INSTANCE.wrap(mock(TypeDescription.class),
            this.classVisitor,
            mock(Implementation.Context.class),
            mock(TypePool.class),
            new FieldList.Empty<FieldDescription.InDefinedShape>(),
            new MethodList.Empty<MethodDescription>(),
            IGNORED,
            IGNORED);
    classVisitor.visit(ClassFileVersion.JAVA_V4.getMinorMajorVersion(), FOOBAR, FOO, BAR, QUX, new String[]{BAZ});
    MethodVisitor methodVisitor = classVisitor.visitMethod(FOOBAR, FOO, BAR, QUX, new String[]{BAZ});
    assertThat(methodVisitor, not(this.methodVisitor));
    methodVisitor.visitLdcInsn(Type.getType(Object[].class));
    verify(this.classVisitor).visit(ClassFileVersion.JAVA_V4.getMinorMajorVersion(), FOOBAR, FOO, BAR, QUX, new String[]{BAZ});
    verify(this.classVisitor).visitMethod(FOOBAR, FOO, BAR, QUX, new String[]{BAZ});
    verifyNoMoreInteractions(this.classVisitor);
    verify(this.methodVisitor).visitLdcInsn(Type.getType(Object[].class).getInternalName().replace('/', '.'));
    verify(this.methodVisitor).visitMethodInsn(Opcodes.INVOKESTATIC,
            Type.getType(Class.class).getInternalName(),
            "forName",
            Type.getType(Class.class.getDeclaredMethod("forName", String.class)).getDescriptor(),
            false);
    verifyNoMoreInteractions(this.methodVisitor);
}