Exemple #1
0
 protected Type GetEnhancedTypeIntern(Type entityType, IEnhancementHint enhancementHint)
 {
     lock (writeLock)
     {
         WeakReference existingBaseTypeR = DictionaryExtension.ValueOrDefault(extendedTypeToType, entityType);
         if (existingBaseTypeR != null)
         {
             Type existingBaseType = (Type)existingBaseTypeR.Target;
             if (existingBaseType != null)
             {
                 // there is already an enhancement of the given baseType. Now we check if the existing enhancement is made with the same enhancementHint
                 ValueType valueType2 = DictionaryExtension.ValueOrDefault(typeToExtendedType, existingBaseType);
                 if (valueType2 != null && valueType2.ContainsKey(enhancementHint))
                 {
                     // do nothing: the given entity is already the result of the enhancement of the existingBaseType with the given enhancementHint
                     // it is not possible to enhance the same two times
                     return(entityType);
                 }
             }
         }
         ValueType valueType = DictionaryExtension.ValueOrDefault(typeToExtendedType, entityType);
         if (valueType == null)
         {
             return(null);
         }
         WeakReference extendedTypeR = valueType.Get(enhancementHint);
         if (extendedTypeR == null)
         {
             return(null);
         }
         return((Type)extendedTypeR.Target);
     }
 }
 public BytecodeBehaviorState(Type currentType, NewType newType, Type originalType, IServiceContext beanContext, IEnhancementHint context)
 {
     this.currentType  = currentType;
     this.newType      = newType;
     this.originalType = originalType;
     this.beanContext  = beanContext;
     this.context      = context;
 }
        public static String GetMemberPath(IEnhancementHint hint)
        {
            if (hint == null)
            {
                return(null);
            }
            EmbeddedEnhancementHint unwrap = hint.Unwrap <EmbeddedEnhancementHint>();

            if (unwrap == null)
            {
                return(null);
            }
            return(unwrap.MemberPath);
        }
        public static Type GetRootEntityType(IEnhancementHint hint)
        {
            if (hint == null)
            {
                return(null);
            }
            EmbeddedEnhancementHint unwrap = hint.Unwrap <EmbeddedEnhancementHint>();

            if (unwrap == null)
            {
                return(null);
            }
            return(unwrap.RootEntityType);
        }
        public static Type GetParentObjectType(IEnhancementHint hint)
        {
            if (hint == null)
            {
                return(null);
            }
            EmbeddedEnhancementHint unwrap = hint.Unwrap <EmbeddedEnhancementHint>();

            if (unwrap == null)
            {
                return(null);
            }
            return(unwrap.ParentObjectType);
        }
Exemple #6
0
        public static Type GetEntityType(IEnhancementHint hint)
        {
            EntityEnhancementHint entityEnhancementHint = hint.Unwrap <EntityEnhancementHint>();

            if (entityEnhancementHint != null)
            {
                return(BytecodeBehaviorState.State.OriginalType);
            }
            EmbeddedEnhancementHint embeddedEnhancementHint = hint.Unwrap <EmbeddedEnhancementHint>();

            if (embeddedEnhancementHint != null)
            {
                return(embeddedEnhancementHint.RootEntityType);
            }
            return(null);
        }
Exemple #7
0
        public Type GetEnhancedType(Type typeToEnhance, IEnhancementHint hint)
        {
            ITargetNameEnhancementHint targetNameHint = hint.Unwrap <ITargetNameEnhancementHint>();

            if (targetNameHint == null && hint is ITargetNameEnhancementHint)
            {
                targetNameHint = (ITargetNameEnhancementHint)hint;
            }
            String newTypeNamePrefix = typeToEnhance.FullName;

            if (targetNameHint != null)
            {
                newTypeNamePrefix = targetNameHint.GetTargetName(typeToEnhance);
            }
            return(GetEnhancedType(typeToEnhance, newTypeNamePrefix, hint));
        }
        public static T SetState <T>(Type originalType, Type currentType, NewType newType, IServiceContext beanContext,
                                     IEnhancementHint context, IResultingBackgroundWorkerDelegate <T> runnable)
        {
            IBytecodeBehaviorState oldState = stateTL.Value;

            stateTL.Value = new BytecodeBehaviorState(currentType, newType, originalType, beanContext, context);
            try
            {
                return(runnable.Invoke());
            }
            finally
            {
                if (oldState != null)
                {
                    stateTL.Value = oldState;
                }
                else
                {
                    stateTL.Value = null;
                }
            }
        }
 public static bool HasMemberPath(IEnhancementHint hint)
 {
     return(GetMemberPath(hint) != null);
 }
Exemple #10
0
 public virtual Type GetTypeToExtendFrom(Type originalType, Type currentType, IEnhancementHint hint)
 {
     return(null);
 }
Exemple #11
0
        protected Type EnhanceTypeIntern(Type originalType, String newTypeNamePrefix, IList <IBytecodeBehavior> pendingBehaviors,
                                         IEnhancementHint hint)
        {
            if (pendingBehaviors.Count == 0)
            {
                return(originalType);
            }
            WeakReference originalTypeR = typeToExtendedType.GetWeakReferenceEntry(originalType);

            if (originalTypeR == null)
            {
                throw new Exception("Must never happen");
            }
            String lastTypeHandleName = newTypeNamePrefix;

            newTypeNamePrefix = newTypeNamePrefix.Replace('.', '/');
            StringBuilder sw = new StringBuilder();

            try
            {
                Type currentType = originalType;
                if (currentType.IsInterface)
                {
                    currentType = typeof(Object);
                }
                for (int a = 0, size = pendingBehaviors.Count; a < size; a++)
                {
                    Type newCurrentType = pendingBehaviors[a].GetTypeToExtendFrom(originalType, currentType, hint);
                    if (newCurrentType != null)
                    {
                        currentType = newCurrentType;
                    }
                }
                int iterationCount = 0;

                List <BytecodeBehaviorState> pendingStatesToPostProcess = new List <BytecodeBehaviorState>();
                Type currentContent = currentType; //BytecodeClassLoader.ReadTypeAsBinary(currentType);
                while (pendingBehaviors.Count > 0)
                {
                    iterationCount++;

                    NewType newTypeHandle = NewType.GetObjectType(newTypeNamePrefix + "$A" + iterationCount);
                    lastTypeHandleName = newTypeHandle.ClassName;

                    IBytecodeBehavior[] currentPendingBehaviors = ListUtil.ToArray(pendingBehaviors);
                    pendingBehaviors.Clear();

                    if (currentPendingBehaviors.Length > 0 && Log.DebugEnabled)
                    {
                        Log.Debug("Applying behaviors on " + newTypeHandle.ClassName + ": " + Arrays.ToString(currentPendingBehaviors));
                    }
                    BytecodeEnhancer This            = this;
                    Type             fCurrentContent = currentContent;

                    BytecodeBehaviorState acquiredState = null;
                    Type newContent = BytecodeBehaviorState.SetState(originalType, currentType, newTypeHandle, BeanContext, hint,
                                                                     delegate()
                    {
                        acquiredState = (BytecodeBehaviorState)BytecodeBehaviorState.State;
                        return(This.ExecutePendingBehaviors(fCurrentContent, sw, currentPendingBehaviors, pendingBehaviors));
                    });
                    if (newContent == null)
                    {
                        if (pendingBehaviors.Count > 0)
                        {
                            // "fix" the iterationCount to have a consistent class name hierarchy
                            iterationCount--;
                            continue;
                        }
                        return(currentType);
                    }
                    Type newType = newContent;// BytecodeClassLoader.LoadClass(newTypeHandle.InternalName, newContent);
                    extendedTypeToType.Add(newType, originalTypeR);
                    pendingStatesToPostProcess.Add(acquiredState);
                    currentContent = newContent;
                    currentType    = newType;
                }
                for (int a = 0, size = pendingStatesToPostProcess.Count; a < size; a++)
                {
                    pendingStatesToPostProcess[a].PostProcessCreatedType(currentType);
                }
                return(currentType);
            }
            catch (Exception e)
            {
                BytecodeClassLoader.Save();
                String classByteCode = sw.ToString();
                if (classByteCode.Length > 0)
                {
                    if (TraceDir != null)
                    {
                        LogBytecodeOutput(lastTypeHandleName, classByteCode);
                    }
                    else
                    {
                        throw RuntimeExceptionUtil.Mask(e, "Bytecode:\n" + classByteCode);
                    }
                }
                throw;
            }
        }
Exemple #12
0
        public Type GetEnhancedType(Type typeToEnhance, String newTypeNamePrefix, IEnhancementHint hint)
        {
            Type extendedType = GetEnhancedTypeIntern(typeToEnhance, hint);

            if (extendedType != null)
            {
                return(extendedType);
            }
            lock (writeLock)
            {
                // Concurrent thread may have been faster
                extendedType = GetEnhancedTypeIntern(typeToEnhance, hint);
                if (extendedType != null)
                {
                    return(extendedType);
                }
                if (Log.InfoEnabled)
                {
                    Log.Info("Enhancing " + typeToEnhance + " with hint: " + hint);
                }
                ValueType valueType = DictionaryExtension.ValueOrDefault(typeToExtendedType, typeToEnhance);
                if (valueType == null)
                {
                    valueType = new ValueType();
                    typeToExtendedType.Add(typeToEnhance, valueType);
                }
                else
                {
                    valueType.AddChangeCount();
                    newTypeNamePrefix += "_O" + valueType.ChangeCount;
                }

                List <IBytecodeBehavior> pendingBehaviors = new List <IBytecodeBehavior>();

                IBytecodeBehavior[] extensions = bytecodeBehaviorExtensions.GetExtensions();
                pendingBehaviors.AddRange(extensions);

                Type enhancedType;
                if (pendingBehaviors.Count > 0)
                {
                    enhancedType = EnhanceTypeIntern(typeToEnhance, newTypeNamePrefix, pendingBehaviors, hint);
                }
                else
                {
                    enhancedType = typeToEnhance;
                }
                WeakReference entityTypeR = typeToExtendedType.GetWeakReferenceEntry(typeToEnhance);
                if (entityTypeR == null)
                {
                    throw new Exception("Must never happen");
                }
                hardRefToTypes.Add(enhancedType);
                hardRefToTypes.Add(typeToEnhance);

                if (TraceDir != null)
                {
                    LogBytecodeOutput(enhancedType.FullName, BytecodeClassLoader.ToPrintableBytecode(enhancedType));
                }
                else if (Log.DebugEnabled)
                {
                    // note that this intentionally will only be logged to the console if the traceDir is NOT specified already
                    Log.Debug(BytecodeClassLoader.ToPrintableBytecode(enhancedType));
                }
                try
                {
                    CheckEnhancedTypeConsistency(enhancedType);
                }
                catch (Exception e)
                {
                    if (Log.ErrorEnabled)
                    {
                        Log.Error(BytecodeClassLoader.ToPrintableBytecode(enhancedType), e);
                    }
                    BytecodeClassLoader.Save();
                    throw;
                }
                WeakReference enhancedEntityTypeR = new WeakReference(enhancedType);
                valueType.Put(hint, enhancedEntityTypeR);
                extendedTypeToType.Add(enhancedType, entityTypeR);
                if (Log.InfoEnabled)
                {
                    Log.Info("Enhancement finished successfully with type: " + enhancedType);
                }
                return(enhancedType);
            }
        }
 protected bool IsActive(IEnhancementHint hint, Type originalType)
 {
     return(hint != null && ImplementAbstractObjectFactory.IsRegistered(originalType));
 }
 public override Type GetTypeToExtendFrom(Type originalType, Type currentType, IEnhancementHint hint)
 {
     if (!IsActive(GetContext(hint), originalType))
     {
         return(base.GetTypeToExtendFrom(originalType, currentType, hint));
     }
     return(ImplementAbstractObjectFactory.GetBaseType(originalType));
 }
 protected IEnhancementHint GetContext(IEnhancementHint hint)
 {
     return(hint.Unwrap <ImplementAbstractObjectEnhancementHint>());
 }
Exemple #16
0
 public abstract Type GetTypeToExtendFrom(Type originalType, Type currentType, IEnhancementHint hint);