Search in sources :

Example 1 with ByteCodeAppender

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.ByteCodeAppender in project byte-buddy by raphw.

the class InstrumentedTypeDefaultTest method testWithTypeInitializerSingle.

@Test
public void testWithTypeInitializerSingle() throws Exception {
    InstrumentedType instrumentedType = makePlainInstrumentedType();
    assertThat(instrumentedType.getDeclaredFields().size(), is(0));
    ByteCodeAppender byteCodeAppender = mock(ByteCodeAppender.class);
    instrumentedType = instrumentedType.withInitializer(byteCodeAppender);
    TypeInitializer typeInitializer = instrumentedType.getTypeInitializer();
    assertThat(typeInitializer.isDefined(), is(true));
    MethodDescription methodDescription = mock(MethodDescription.class);
    typeInitializer.apply(methodVisitor, implementationContext, methodDescription);
    verify(byteCodeAppender).apply(methodVisitor, implementationContext, methodDescription);
}
Also used : MethodDescription(net.bytebuddy.description.method.MethodDescription) LoadedTypeInitializer(net.bytebuddy.implementation.LoadedTypeInitializer) ByteCodeAppender(net.bytebuddy.implementation.bytecode.ByteCodeAppender) Test(org.junit.Test)

Example 2 with ByteCodeAppender

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.ByteCodeAppender in project byte-buddy by raphw.

the class InstrumentedTypeDefaultTest method testWithTypeInitializerDouble.

@Test
public void testWithTypeInitializerDouble() throws Exception {
    InstrumentedType instrumentedType = makePlainInstrumentedType();
    assertThat(instrumentedType.getDeclaredFields().size(), is(0));
    ByteCodeAppender first = mock(ByteCodeAppender.class), second = mock(ByteCodeAppender.class);
    MethodDescription methodDescription = mock(MethodDescription.class);
    when(first.apply(methodVisitor, implementationContext, methodDescription)).thenReturn(new ByteCodeAppender.Size(0, 0));
    when(second.apply(methodVisitor, implementationContext, methodDescription)).thenReturn(new ByteCodeAppender.Size(0, 0));
    instrumentedType = instrumentedType.withInitializer(first).withInitializer(second);
    TypeInitializer typeInitializer = instrumentedType.getTypeInitializer();
    assertThat(typeInitializer.isDefined(), is(true));
    typeInitializer.apply(methodVisitor, implementationContext, methodDescription);
    verify(first).apply(methodVisitor, implementationContext, methodDescription);
    verify(second).apply(methodVisitor, implementationContext, methodDescription);
}
Also used : MethodDescription(net.bytebuddy.description.method.MethodDescription) LoadedTypeInitializer(net.bytebuddy.implementation.LoadedTypeInitializer) ByteCodeAppender(net.bytebuddy.implementation.bytecode.ByteCodeAppender) Test(org.junit.Test)

Example 3 with ByteCodeAppender

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.ByteCodeAppender in project beam by apache.

the class ByteBuddyUtils method createCollectionTransformFunction.

// When processing a container (e.g. List<T>) we need to recursively process the element type.
// This function
// generates a subclass of Function that can be used to recursively transform each element of the
// container.
static Class createCollectionTransformFunction(Type fromType, Type toType, Function<StackManipulation, StackManipulation> convertElement) {
    // Generate a TypeDescription for the class we want to generate.
    TypeDescription.Generic functionGenericType = TypeDescription.Generic.Builder.parameterizedType(Function.class, Primitives.wrap((Class) fromType), Primitives.wrap((Class) toType)).build();
    DynamicType.Builder<Function> builder = (DynamicType.Builder<Function>) BYTE_BUDDY.with(new InjectPackageStrategy((Class) fromType)).subclass(functionGenericType).method(ElementMatchers.named("apply")).intercept(new Implementation() {

        @Override
        public ByteCodeAppender appender(Target target) {
            return (methodVisitor, implementationContext, instrumentedMethod) -> {
                // this + method parameters.
                int numLocals = 1 + instrumentedMethod.getParameters().size();
                StackManipulation readValue = MethodVariableAccess.REFERENCE.loadFrom(1);
                StackManipulation stackManipulation = new StackManipulation.Compound(convertElement.apply(readValue), MethodReturn.REFERENCE);
                StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext);
                return new ByteCodeAppender.Size(size.getMaximalSize(), numLocals);
            };
        }

        @Override
        public InstrumentedType prepare(InstrumentedType instrumentedType) {
            return instrumentedType;
        }
    });
    return builder.visit(new AsmVisitorWrapper.ForDeclaredMethods().writerFlags(ClassWriter.COMPUTE_FRAMES)).make().load(ReflectHelpers.findClassLoader(((Class) fromType).getClassLoader()), ClassLoadingStrategy.Default.INJECTION).getLoaded();
}
Also used : Arrays(java.util.Arrays) DateTimeZone(org.joda.time.DateTimeZone) ArrayAccess(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.collection.ArrayAccess) TypeCasting(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.assign.TypeCasting) AsmVisitorWrapper(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.asm.AsmVisitorWrapper) ArrayFactory(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.collection.ArrayFactory) ByteBuffer(java.nio.ByteBuffer) Opcodes(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.jar.asm.Opcodes) ByteCodeAppender(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.ByteCodeAppender) Label(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.jar.asm.Label) ClassUtils(org.apache.commons.lang3.ClassUtils) MethodVisitor(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.jar.asm.MethodVisitor) NullConstant(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.constant.NullConstant) Map(java.util.Map) Iterables(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Iterables) MethodReturn(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.member.MethodReturn) Method(java.lang.reflect.Method) Internal(org.apache.beam.sdk.annotations.Internal) ClassLoadingStrategy(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.loading.ClassLoadingStrategy) TypeCreation(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.TypeCreation) Collection(java.util.Collection) Set(java.util.Set) ReadableInstant(org.joda.time.ReadableInstant) Objects(java.util.Objects) List(java.util.List) Type(java.lang.reflect.Type) Modifier(java.lang.reflect.Modifier) ReflectHelpers(org.apache.beam.sdk.util.common.ReflectHelpers) BaseLocal(org.joda.time.base.BaseLocal) Context(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.Implementation.Context) Typing(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.assign.Assigner.Typing) Implementation(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.Implementation) SortedMap(java.util.SortedMap) ByteBuddy(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.ByteBuddy) TypeDescription(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.type.TypeDescription) Preconditions.checkNotNull(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkNotNull) TypeDescriptor(org.apache.beam.sdk.values.TypeDescriptor) RandomString(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.utility.RandomString) MethodVariableAccess(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.member.MethodVariableAccess) DynamicType(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.DynamicType) ArrayUtils(org.apache.commons.lang3.ArrayUtils) NamingStrategy(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.NamingStrategy) Constructor(java.lang.reflect.Constructor) FieldAccess(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.member.FieldAccess) ArrayList(java.util.ArrayList) TypeParameter(org.apache.beam.sdk.values.TypeParameter) InstrumentedType(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.scaffold.InstrumentedType) Collections2(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Collections2) FieldValueSetter(org.apache.beam.sdk.schemas.FieldValueSetter) Parameter(java.lang.reflect.Parameter) Maps(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Maps) FieldValueTypeInformation(org.apache.beam.sdk.schemas.FieldValueTypeInformation) ForLoadedType(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.type.TypeDescription.ForLoadedType) IntegerConstant(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.constant.IntegerConstant) Nullable(org.checkerframework.checker.nullness.qual.Nullable) Assigner(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.assign.Assigner) Duplication(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.Duplication) Compound(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.StackManipulation.Compound) ElementMatchers(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.matcher.ElementMatchers) Function(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Function) Lists(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Lists) ClassWriter(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.jar.asm.ClassWriter) StackManipulation(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.StackManipulation) MethodInvocation(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.member.MethodInvocation) ForLoadedConstructor(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.method.MethodDescription.ForLoadedConstructor) Instant(org.joda.time.Instant) ReadablePartial(org.joda.time.ReadablePartial) FieldValueGetter(org.apache.beam.sdk.schemas.FieldValueGetter) Collections(java.util.Collections) BaseNameResolver(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.NamingStrategy.SuffixingRandom.BaseNameResolver) ForLoadedMethod(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.method.MethodDescription.ForLoadedMethod) Primitives(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.primitives.Primitives) StackManipulation(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.StackManipulation) DynamicType(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.DynamicType) Compound(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.StackManipulation.Compound) Implementation(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.Implementation) Function(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Function) TypeDescription(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.type.TypeDescription) AsmVisitorWrapper(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.asm.AsmVisitorWrapper) ByteCodeAppender(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.ByteCodeAppender) InstrumentedType(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.scaffold.InstrumentedType)

Aggregations

MethodDescription (net.bytebuddy.description.method.MethodDescription)2 LoadedTypeInitializer (net.bytebuddy.implementation.LoadedTypeInitializer)2 ByteCodeAppender (net.bytebuddy.implementation.bytecode.ByteCodeAppender)2 Test (org.junit.Test)2 Constructor (java.lang.reflect.Constructor)1 Method (java.lang.reflect.Method)1 Modifier (java.lang.reflect.Modifier)1 Parameter (java.lang.reflect.Parameter)1 Type (java.lang.reflect.Type)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Set (java.util.Set)1 SortedMap (java.util.SortedMap)1 Internal (org.apache.beam.sdk.annotations.Internal)1