コード例 #1
0
        protected ForkStateEntry[] GetForkStateEntries()
        {
            ForkStateEntry[] cachedForkStateEntries = this.cachedForkStateEntries;
            if (cachedForkStateEntries != null)
            {
                return(cachedForkStateEntries);
            }
            Lock writeLock = listeners.GetWriteLock();

            writeLock.Lock();
            try
            {
                // check again: concurrent thread might have been faster
                cachedForkStateEntries = this.cachedForkStateEntries;
                if (cachedForkStateEntries != null)
                {
                    return(cachedForkStateEntries);
                }
                IThreadLocalCleanupBean[] extensions       = listeners.GetExtensions();
                List <ForkStateEntry>     forkStateEntries = new List <ForkStateEntry>(extensions.Length);
                for (int a = 0, size = extensions.Length; a < size; a++)
                {
                    IThreadLocalCleanupBean extension = extensions[a];
                    FieldInfo[]             fields    = ReflectUtil.GetDeclaredFieldsInHierarchy(extension.GetType());
                    foreach (FieldInfo field in fields)
                    {
                        Forkable forkable = AnnotationUtil.GetAnnotation <Forkable>(field, false);
                        if (forkable == null)
                        {
                            continue;
                        }
                        Object valueTL = field.GetValue(extension);
                        if (valueTL == null)
                        {
                            continue;
                        }
                        Type           forkProcessorType = forkable.Processor;
                        IForkProcessor forkProcessor     = null;
                        if (forkProcessorType != null && !typeof(IForkProcessor).Equals(forkProcessorType))
                        {
                            WeakReference   beanContextOfExtensionR = extensionToContextMap.Get(extension);
                            IServiceContext beanContextOfExtension  = beanContextOfExtensionR != null ? (IServiceContext)beanContextOfExtensionR.Target : null;
                            if (beanContextOfExtension == null)
                            {
                                beanContextOfExtension = BeanContext;
                            }
                            forkProcessor = beanContextOfExtension.RegisterBean <IForkProcessor>(forkProcessorType).Finish();
                        }
                        forkStateEntries.Add(new ForkStateEntry(extension, field.Name, valueTL, forkable.Value, forkProcessor));
                    }
                }
                cachedForkStateEntries      = forkStateEntries.ToArray();
                this.cachedForkStateEntries = cachedForkStateEntries;
                return(cachedForkStateEntries);
            }
            finally
            {
                writeLock.Unlock();
            }
        }
コード例 #2
0
        protected PropertyInfoEntry GetPropertyEntry(Type type, SmartCopyMap <Type, PropertyInfoEntry> map, bool isOldIocMode, bool isIocMode)
        {
            ParamChecker.AssertParamNotNull(type, "type");
            PropertyInfoEntry propertyEntry = map.Get(type);

            if (propertyEntry != null)
            {
                return(propertyEntry);
            }
            Object writeLock = map.GetWriteLock();

            lock (writeLock)
            {
                propertyEntry = map.Get(type);
                if (propertyEntry != null)
                {
                    // Concurrent thread might have been faster
                    return(propertyEntry);
                }

                HashMap <String, HashMap <Type, HashMap <String, MethodInfo> > > sortedMethods = new HashMap <String, HashMap <Type, HashMap <String, MethodInfo> > >();
                MethodInfo[] methods = ReflectUtil.GetDeclaredMethodsInHierarchy(type);

                foreach (MethodInfo method in methods)
                {
                    if (method.DeclaringType.Equals(typeof(Object)))
                    {
                        continue;
                    }
                    if (method.IsStatic)
                    {
                        continue;
                    }
                    try
                    {
                        String propName = GetPropertyNameFor(method);
                        if (propName.Length == 0)
                        {
                            continue;
                        }
                        HashMap <Type, HashMap <String, MethodInfo> > sortedMethod = sortedMethods.Get(propName);
                        if (sortedMethod == null)
                        {
                            sortedMethod = HashMap <Type, HashMap <String, MethodInfo> > .Create(1);

                            sortedMethods.Put(propName, sortedMethod);
                        }

                        ParameterInfo[] parameterInfos = method.GetParameters();
                        Type            propertyType;
                        String          prefix;
                        if (parameterInfos.Length == 1)
                        {
                            propertyType = parameterInfos[0].ParameterType;
                            prefix       = "set";
                        }
                        else if (parameterInfos.Length == 0)
                        {
                            propertyType = method.ReturnType;
                            prefix       = "get";
                        }
                        else
                        {
                            throw new Exception("Method is not an accessor: " + method);
                        }

                        HashMap <String, MethodInfo> methodPerType = sortedMethod.Get(propertyType);
                        if (methodPerType == null)
                        {
                            methodPerType = HashMap <String, MethodInfo> .Create(2);

                            sortedMethod.Put(propertyType, methodPerType);
                        }

                        methodPerType.Put(prefix, method);
                    }
                    catch (Exception e)
                    {
                        throw RuntimeExceptionUtil.Mask(e, "Error occured while processing " + method);
                    }
                }

                HashMap <String, HashMap <String, MethodInfo> > filteredMethods = FilterOverriddenMethods(sortedMethods, type);

                HashMap <String, IPropertyInfo> propertyMap = new HashMap <String, IPropertyInfo>(0.5f);
                foreach (MapEntry <String, HashMap <String, MethodInfo> > propertyData in filteredMethods)
                {
                    String propertyName = propertyData.Key;

                    HashMap <String, MethodInfo> propertyMethods = propertyData.Value;
                    MethodInfo getter = propertyMethods.Get("get");
                    MethodInfo setter = propertyMethods.Get("set");

                    if (isIocMode)
                    {
                        if (setter == null ||
                            (!setter.IsPublic && !AnnotationUtil.IsAnnotationPresent <AutowiredAttribute>(setter, false) &&
                             !AnnotationUtil.IsAnnotationPresent <PropertyAttribute>(setter, false)))
                        {
                            continue;
                        }
                    }
                    MethodPropertyInfo propertyInfo = new MethodPropertyInfo(type, propertyName, getter, setter);
                    propertyMap.Put(propertyInfo.Name, propertyInfo);
                }

                Type[]      interfaces    = type.GetInterfaces();
                List <Type> typesToSearch = new List <Type>(interfaces);
                typesToSearch.Add(type);
                foreach (Type typeToSearch in typesToSearch)
                {
                    PropertyInfo[] properties = typeToSearch.GetProperties(BindingFlags.FlattenHierarchy | BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public);
                    foreach (PropertyInfo property in properties)
                    {
                        if (property.GetGetMethod() != null && property.GetGetMethod().GetParameters().Length != 0)
                        {
                            continue;
                        }
                        if (property.GetSetMethod() != null && property.GetSetMethod().GetParameters().Length != 1)
                        {
                            continue;
                        }
                        MethodInfo getter = null;
                        MethodInfo setter = null;

                        MethodPropertyInfo propertyInfo = (MethodPropertyInfo)propertyMap.Get(property.Name);
                        if (propertyInfo != null)
                        {
                            getter = propertyInfo.Getter;
                            setter = propertyInfo.Setter;
                        }
                        if (getter == null)
                        {
                            getter = property.GetGetMethod();
                        }
                        if (setter == null)
                        {
                            setter = property.GetSetMethod();
                        }
                        if (isIocMode && setter == null)
                        {
                            continue;
                        }
                        propertyInfo = new MethodPropertyInfo(type, property.Name, getter, setter);
                        propertyInfo.PutAnnotations(property);
                        propertyMap.Put(propertyInfo.Name, propertyInfo);
                    }
                }

                FieldInfo[] fields = ReflectUtil.GetDeclaredFieldsInHierarchy(type);
                foreach (FieldInfo field in fields)
                {
                    if (field.IsStatic)
                    {
                        continue;
                    }
                    if (isOldIocMode)
                    {
                        if (!AnnotationUtil.IsAnnotationPresent <AutowiredAttribute>(field, false) && !AnnotationUtil.IsAnnotationPresent <PropertyAttribute>(field, false))
                        {
                            continue;
                        }
                    }
                    String        propertyName     = GetPropertyNameFor(field);
                    IPropertyInfo existingProperty = propertyMap.Get(propertyName);
                    if (existingProperty != null && existingProperty.IsWritable)
                    {
                        // Ignore field injection if the already resolved (method-)property is writable
                        continue;
                    }
                    IPropertyInfo propertyInfo = new FieldPropertyInfo(type, propertyName, field);
                    propertyMap.Put(propertyInfo.Name, propertyInfo);
                }
                propertyEntry = new PropertyInfoEntry(propertyMap);
                map.Put(type, propertyEntry);
                return(propertyEntry);
            }
        }
コード例 #3
0
ファイル: MethodPropertyInfo.cs プロジェクト: vogelb/ambeth
        protected override void Init()
        {
            if (EntityType == null)
            {
                throw new ArgumentException("No class given");
            }
            if (Getter == null && Setter == null)
            {
                throw new ArgumentException("No property methods (class is '" + EntityType + "')");
            }
            else if (Getter != null)
            {
                Type declaringClass = Getter.DeclaringType;
                PropertyType = Getter.ReturnType;
                ElementType  = TypeInfoItemUtil.GetElementTypeUsingReflection(PropertyType, null);

                String      nameLower        = Name.ToLowerInvariant();
                FieldInfo[] fields           = ReflectUtil.GetDeclaredFieldsInHierarchy(declaringClass);
                FieldInfo   backingField     = null;
                FieldInfo   weakBackingField = null;
                String      backingFieldName = "<" + Name + ">k__BackingField";

                for (int a = fields.Length; a-- > 0;)
                {
                    FieldInfo field = fields[a];
                    if (field.IsStatic)
                    {
                        continue;
                    }
                    String fieldName = field.Name.ToLowerInvariant();
                    if (fieldName.Equals(nameLower) || field.Name.Equals(backingFieldName))
                    {
                        backingField = field;
                        break;
                    }
                    else if (fieldName.EndsWith(nameLower))
                    {
                        if (weakBackingField != null)
                        {
                            weakBackingField = null;
                            break;
                        }
                        weakBackingField = field;
                    }
                }
                if (backingField == null)
                {
                    backingField = weakBackingField;
                }
                BackingField = backingField;
                if (backingField != null)
                {
                    AddModifiers(backingField);
                    PutAnnotations(backingField);
                }
                PutAnnotations(Getter);
                if (Setter != null)
                {
                    if (Setter.GetParameters().Length != 1 || !PropertyType.IsAssignableFrom(Setter.GetParameters()[0].ParameterType))
                    {
                        throw new Exception("Misfitting property methods for property '" + Name + "' on class '" + EntityType.Name + "'");
                    }
                    PutAnnotations(Setter);
                }
            }
            else
            {
                Type        declaringClass = Setter.DeclaringType;
                FieldInfo[] fields         = ReflectUtil.GetDeclaredFieldsInHierarchy(declaringClass);
                for (int a = fields.Length; a-- > 0;)
                {
                    FieldInfo field = fields[a];
                    if (field.IsInitOnly || field.IsStatic)
                    {
                        continue;
                    }
                    String fieldName = field.Name;
                    if (fieldName.EndsWith(Name))
                    {
                        if (BackingField != null)
                        {
                            BackingField = null;
                            break;
                        }
                        BackingField = field;
                    }
                }
                if (BackingField != null)
                {
                    AddModifiers(BackingField);
                }
                PropertyType = Setter.GetParameters()[0].ParameterType;
                ElementType  = TypeInfoItemUtil.GetElementTypeUsingReflection(PropertyType, null);
                PutAnnotations(Setter);
            }
            //if (Getter != null && Modifier.isNative(Getter.getModifiers()))
            //{
            //	modifiers |= Modifier.NATIVE;
            //}
            //if (Setter != null && Modifier.isNative(Setter.getModifiers()))
            //{
            //	modifiers |= Modifier.NATIVE;
            //}
            if (Setter != null)
            {
                UpdateModifiers(false, (int)Modifier.FINAL);
            }
            bool?getterIsPublic = Getter != null ? (bool?)(Getter.IsPublic) : null;
            bool?setterIsPublic = Setter != null ? (bool?)(Setter.IsPublic) : null;

            if (getterIsPublic == true || setterIsPublic == true)
            {
                UpdateModifiers(false, (int)Modifier.PRIVATE);
                UpdateModifiers(false, (int)Modifier.PROTECTED);
                UpdateModifiers(true, (int)Modifier.PUBLIC);
            }
            else
            {
                bool?GetterIsProtected = Getter != null ? (bool?)(Getter.IsFamily || Getter.IsFamilyOrAssembly) : null;
                bool?SetterIsProtected = Setter != null ? (bool?)(Setter.IsFamily || Setter.IsFamilyOrAssembly) : null;
                if (GetterIsProtected == true || SetterIsProtected == true)
                {
                    UpdateModifiers(false, (int)Modifier.PRIVATE);
                    UpdateModifiers(false, (int)Modifier.PUBLIC);
                    UpdateModifiers(true, (int)Modifier.PROTECTED);
                }
                else
                {
                    UpdateModifiers(false, (int)Modifier.PROTECTED);
                    UpdateModifiers(false, (int)Modifier.PUBLIC);
                    UpdateModifiers(true, (int)Modifier.PRIVATE);
                }
            }
            RefreshDeclaringType();
            base.Init();
        }