Search in sources :

Example 1 with Implementation

use of net.bytebuddy.implementation.Implementation in project hibernate-orm by hibernate.

the class EnhancerImpl method doEnhance.

private DynamicType.Builder<?> doEnhance(DynamicType.Builder<?> builder, TypeDescription managedCtClass) {
    // can't effectively enhance interfaces
    if (managedCtClass.isInterface()) {
        log.debugf("Skipping enhancement of [%s]: it's an interface!", managedCtClass.getName());
        return null;
    }
    // skip already enhanced classes
    if (alreadyEnhanced(managedCtClass)) {
        log.debugf("Skipping enhancement of [%s]: already enhanced", managedCtClass.getName());
        return null;
    }
    PersistentAttributeTransformer transformer = PersistentAttributeTransformer.collectPersistentFields(managedCtClass, enhancementContext, classPool);
    if (enhancementContext.isEntityClass(managedCtClass)) {
        log.infof("Enhancing [%s] as Entity", managedCtClass.getName());
        builder = builder.implement(ManagedEntity.class).defineMethod(EnhancerConstants.ENTITY_INSTANCE_GETTER_NAME, Object.class, Visibility.PUBLIC).intercept(FixedValue.self());
        builder = addFieldWithGetterAndSetter(builder, EntityEntry.class, EnhancerConstants.ENTITY_ENTRY_FIELD_NAME, EnhancerConstants.ENTITY_ENTRY_GETTER_NAME, EnhancerConstants.ENTITY_ENTRY_SETTER_NAME);
        builder = addFieldWithGetterAndSetter(builder, ManagedEntity.class, EnhancerConstants.PREVIOUS_FIELD_NAME, EnhancerConstants.PREVIOUS_GETTER_NAME, EnhancerConstants.PREVIOUS_SETTER_NAME);
        builder = addFieldWithGetterAndSetter(builder, ManagedEntity.class, EnhancerConstants.NEXT_FIELD_NAME, EnhancerConstants.NEXT_GETTER_NAME, EnhancerConstants.NEXT_SETTER_NAME);
        builder = addInterceptorHandling(builder, managedCtClass);
        if (enhancementContext.doDirtyCheckingInline(managedCtClass)) {
            if (collectCollectionFields(managedCtClass).isEmpty()) {
                builder = builder.implement(SelfDirtinessTracker.class).defineField(EnhancerConstants.TRACKER_FIELD_NAME, DirtyTracker.class, FieldPersistence.TRANSIENT, Visibility.PRIVATE).annotateField(AnnotationDescription.Builder.ofType(Transient.class).build()).defineMethod(EnhancerConstants.TRACKER_CHANGER_NAME, void.class, Visibility.PUBLIC).withParameters(String.class).intercept(Advice.to(CodeTemplates.TrackChange.class).wrap(StubMethod.INSTANCE)).defineMethod(EnhancerConstants.TRACKER_GET_NAME, String[].class, Visibility.PUBLIC).intercept(Advice.to(CodeTemplates.GetDirtyAttributesWithoutCollections.class).wrap(StubMethod.INSTANCE)).defineMethod(EnhancerConstants.TRACKER_HAS_CHANGED_NAME, boolean.class, Visibility.PUBLIC).intercept(Advice.to(CodeTemplates.AreFieldsDirtyWithoutCollections.class).wrap(StubMethod.INSTANCE)).defineMethod(EnhancerConstants.TRACKER_CLEAR_NAME, void.class, Visibility.PUBLIC).intercept(Advice.to(CodeTemplates.ClearDirtyAttributesWithoutCollections.class).wrap(StubMethod.INSTANCE)).defineMethod(EnhancerConstants.TRACKER_SUSPEND_NAME, void.class, Visibility.PUBLIC).withParameters(boolean.class).intercept(Advice.to(CodeTemplates.SuspendDirtyTracking.class).wrap(StubMethod.INSTANCE)).defineMethod(EnhancerConstants.TRACKER_COLLECTION_GET_NAME, CollectionTracker.class, Visibility.PUBLIC).intercept(Advice.to(CodeTemplates.GetCollectionTrackerWithoutCollections.class).wrap(StubMethod.INSTANCE));
            } else {
                builder = builder.implement(ExtendedSelfDirtinessTracker.class).defineField(EnhancerConstants.TRACKER_FIELD_NAME, DirtyTracker.class, FieldPersistence.TRANSIENT, Visibility.PRIVATE).annotateField(AnnotationDescription.Builder.ofType(Transient.class).build()).defineField(EnhancerConstants.TRACKER_COLLECTION_NAME, CollectionTracker.class, FieldPersistence.TRANSIENT, Visibility.PRIVATE).annotateField(AnnotationDescription.Builder.ofType(Transient.class).build()).defineMethod(EnhancerConstants.TRACKER_CHANGER_NAME, void.class, Visibility.PUBLIC).withParameters(String.class).intercept(Advice.to(CodeTemplates.TrackChange.class).wrap(StubMethod.INSTANCE)).defineMethod(EnhancerConstants.TRACKER_GET_NAME, String[].class, Visibility.PUBLIC).intercept(Advice.to(CodeTemplates.GetDirtyAttributes.class).wrap(StubMethod.INSTANCE)).defineMethod(EnhancerConstants.TRACKER_HAS_CHANGED_NAME, boolean.class, Visibility.PUBLIC).intercept(Advice.to(CodeTemplates.AreFieldsDirty.class).wrap(StubMethod.INSTANCE)).defineMethod(EnhancerConstants.TRACKER_CLEAR_NAME, void.class, Visibility.PUBLIC).intercept(Advice.to(CodeTemplates.ClearDirtyAttributes.class).wrap(StubMethod.INSTANCE)).defineMethod(EnhancerConstants.TRACKER_SUSPEND_NAME, void.class, Visibility.PUBLIC).withParameters(boolean.class).intercept(Advice.to(CodeTemplates.SuspendDirtyTracking.class).wrap(StubMethod.INSTANCE)).defineMethod(EnhancerConstants.TRACKER_COLLECTION_GET_NAME, CollectionTracker.class, Visibility.PUBLIC).intercept(FieldAccessor.ofField(EnhancerConstants.TRACKER_COLLECTION_NAME));
                Implementation isDirty = StubMethod.INSTANCE, getDirtyNames = StubMethod.INSTANCE, clearDirtyNames = StubMethod.INSTANCE;
                for (FieldDescription collectionField : collectCollectionFields(managedCtClass)) {
                    if (collectionField.getType().asErasure().isAssignableTo(Map.class)) {
                        isDirty = Advice.withCustomMapping().bind(CodeTemplates.FieldName.class, collectionField.getName()).bind(CodeTemplates.FieldValue.class, collectionField).to(CodeTemplates.MapAreCollectionFieldsDirty.class).wrap(isDirty);
                        getDirtyNames = Advice.withCustomMapping().bind(CodeTemplates.FieldName.class, collectionField.getName()).bind(CodeTemplates.FieldValue.class, collectionField).to(CodeTemplates.MapGetCollectionFieldDirtyNames.class).wrap(getDirtyNames);
                        clearDirtyNames = Advice.withCustomMapping().bind(CodeTemplates.FieldName.class, collectionField.getName()).bind(CodeTemplates.FieldValue.class, collectionField).to(CodeTemplates.MapGetCollectionClearDirtyNames.class).wrap(clearDirtyNames);
                    } else {
                        isDirty = Advice.withCustomMapping().bind(CodeTemplates.FieldName.class, collectionField.getName()).bind(CodeTemplates.FieldValue.class, collectionField).to(CodeTemplates.CollectionAreCollectionFieldsDirty.class).wrap(isDirty);
                        getDirtyNames = Advice.withCustomMapping().bind(CodeTemplates.FieldName.class, collectionField.getName()).bind(CodeTemplates.FieldValue.class, collectionField).to(CodeTemplates.CollectionGetCollectionFieldDirtyNames.class).wrap(getDirtyNames);
                        clearDirtyNames = Advice.withCustomMapping().bind(CodeTemplates.FieldName.class, collectionField.getName()).bind(CodeTemplates.FieldValue.class, collectionField).to(CodeTemplates.CollectionGetCollectionClearDirtyNames.class).wrap(clearDirtyNames);
                    }
                }
                if (enhancementContext.hasLazyLoadableAttributes(managedCtClass)) {
                    clearDirtyNames = Advice.to(CodeTemplates.InitializeLazyAttributeLoadingInterceptor.class).wrap(clearDirtyNames);
                }
                builder = builder.defineMethod(EnhancerConstants.TRACKER_COLLECTION_CHANGED_NAME, boolean.class, Visibility.PUBLIC).intercept(isDirty).defineMethod(EnhancerConstants.TRACKER_COLLECTION_CHANGED_FIELD_NAME, void.class, Visibility.PUBLIC).withParameters(DirtyTracker.class).intercept(getDirtyNames).defineMethod(EnhancerConstants.TRACKER_COLLECTION_CLEAR_NAME, void.class, Visibility.PUBLIC).intercept(Advice.withCustomMapping().to(CodeTemplates.ClearDirtyCollectionNames.class).wrap(StubMethod.INSTANCE)).defineMethod(ExtendedSelfDirtinessTracker.REMOVE_DIRTY_FIELDS_NAME, void.class, Visibility.PUBLIC).withParameters(LazyAttributeLoadingInterceptor.class).intercept(clearDirtyNames);
            }
        }
        return transformer.applyTo(builder, false);
    } else if (enhancementContext.isCompositeClass(managedCtClass)) {
        log.infof("Enhancing [%s] as Composite", managedCtClass.getName());
        builder = builder.implement(ManagedComposite.class);
        builder = addInterceptorHandling(builder, managedCtClass);
        if (enhancementContext.doDirtyCheckingInline(managedCtClass)) {
            builder = builder.implement(CompositeTracker.class).defineField(EnhancerConstants.TRACKER_COMPOSITE_FIELD_NAME, CompositeOwnerTracker.class, FieldPersistence.TRANSIENT, Visibility.PRIVATE).annotateField(AnnotationDescription.Builder.ofType(Transient.class).build()).defineMethod(EnhancerConstants.TRACKER_COMPOSITE_SET_OWNER, void.class, Visibility.PUBLIC).withParameters(String.class, CompositeOwner.class).intercept(Advice.to(CodeTemplates.SetOwner.class).wrap(StubMethod.INSTANCE)).defineMethod(EnhancerConstants.TRACKER_COMPOSITE_CLEAR_OWNER, void.class, Visibility.PUBLIC).withParameters(String.class).intercept(Advice.to(CodeTemplates.ClearOwner.class).wrap(StubMethod.INSTANCE));
        }
        return transformer.applyTo(builder, false);
    } else if (enhancementContext.isMappedSuperclassClass(managedCtClass)) {
        log.infof("Enhancing [%s] as MappedSuperclass", managedCtClass.getName());
        builder = builder.implement(ManagedMappedSuperclass.class);
        return transformer.applyTo(builder, true);
    } else if (enhancementContext.doExtendedEnhancement(managedCtClass)) {
        log.infof("Extended enhancement of [%s]", managedCtClass.getName());
        return transformer.applyExtended(builder);
    } else {
        log.debugf("Skipping enhancement of [%s]: not entity or composite", managedCtClass.getName());
        return null;
    }
}
Also used : ManagedEntity(org.hibernate.engine.spi.ManagedEntity) Implementation(net.bytebuddy.implementation.Implementation) FieldDescription(net.bytebuddy.description.field.FieldDescription) ExtendedSelfDirtinessTracker(org.hibernate.engine.spi.ExtendedSelfDirtinessTracker) SelfDirtinessTracker(org.hibernate.engine.spi.SelfDirtinessTracker) ManagedMappedSuperclass(org.hibernate.engine.spi.ManagedMappedSuperclass) EntityEntry(org.hibernate.engine.spi.EntityEntry) CompositeOwnerTracker(org.hibernate.bytecode.enhance.internal.tracker.CompositeOwnerTracker) ExtendedSelfDirtinessTracker(org.hibernate.engine.spi.ExtendedSelfDirtinessTracker) Transient(javax.persistence.Transient) DirtyTracker(org.hibernate.bytecode.enhance.internal.tracker.DirtyTracker)

Example 2 with Implementation

use of net.bytebuddy.implementation.Implementation 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)

Example 3 with Implementation

use of net.bytebuddy.implementation.Implementation in project hibernate-orm by hibernate.

the class PersistentAttributeTransformer method fieldWriter.

private Implementation fieldWriter(FieldDescription enhancedField) {
    Implementation implementation;
    if (!enhancementContext.hasLazyLoadableAttributes(managedCtClass) || !enhancementContext.isLazyLoadable(enhancedField)) {
        if (enhancedField.getDeclaringType().asErasure().equals(managedCtClass)) {
            implementation = FieldAccessor.ofField(enhancedField.getName()).in(enhancedField.getDeclaringType().asErasure());
        } else {
            implementation = new Implementation.Simple(new FieldMethodWriter(managedCtClass, enhancedField));
        }
    } else {
        implementation = new Implementation.Simple(FieldWriterAppender.of(managedCtClass, enhancedField));
    }
    implementation = InlineDirtyCheckingHandler.wrap(managedCtClass, enhancementContext, enhancedField, implementation);
    return BiDirectionalAssociationHandler.wrap(managedCtClass, enhancementContext, enhancedField, implementation);
}
Also used : Implementation(net.bytebuddy.implementation.Implementation)

Aggregations

Implementation (net.bytebuddy.implementation.Implementation)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 Transient (javax.persistence.Transient)1 FieldDescription (net.bytebuddy.description.field.FieldDescription)1 Internal (org.apache.beam.sdk.annotations.Internal)1 FieldValueGetter (org.apache.beam.sdk.schemas.FieldValueGetter)1