コード例 #1
0
        public IMethodLevelBehavior <Attribute> CreateInterceptorModeBehavior(Type beanType)
        {
            MethodLevelHashMap <Attribute> methodToAnnotationMap = new MethodLevelHashMap <Attribute>();

            MethodInfo[] methods = ReflectUtil.GetMethods(beanType);
            foreach (MethodInfo method in methods)
            {
                Attribute annotation = LookForAnnotation(method);
                if (annotation != null)
                {
                    methodToAnnotationMap.Put(method, annotation);
                    continue;
                }
                Type[] parameters = TypeUtil.GetParameterTypesToTypes(method.GetParameters());
                foreach (Type currInterface in beanType.GetInterfaces())
                {
                    MethodInfo methodOnInterface = ReflectUtil.GetDeclaredMethod(true, currInterface, null, method.Name, parameters);
                    if (methodOnInterface == null)
                    {
                        continue;
                    }
                    annotation = LookForAnnotation(methodOnInterface);
                    if (annotation == null)
                    {
                        continue;
                    }
                    methodToAnnotationMap.Put(method, annotation);
                    break;
                }
            }
            return(new MethodLevelBehavior <Attribute>(LookForAnnotation(beanType), methodToAnnotationMap));
        }
コード例 #2
0
        public static IMethodLevelBehavior <T> Create <A>(Type beanType, AnnotationCache <A> annotationCache, Type behaviourType,
                                                          IBehaviorTypeExtractor <A, T> behaviourTypeExtractor, IBeanContextFactory beanContextFactory, IServiceContext beanContext) where A : Attribute
        {
            BehaviorKey key = new BehaviorKey(beanType, behaviourType);

            IMethodLevelBehavior <T> behavior = (IMethodLevelBehavior <T>)beanTypeToBehavior.Get(key);

            if (behavior != null)
            {
                if (behavior == noBehavior)
                {
                    return(null);
                }
                return(behavior);
            }
            A annotation = annotationCache.GetAnnotation(beanType);

            if (annotation == null)
            {
                beanTypeToBehavior.Put(key, noBehavior);
                return(null);
            }
            T defaultBehaviour = behaviourTypeExtractor.ExtractBehaviorType(annotation);
            MethodLevelHashMap <T> methodLevelBehaviour = null;

            MethodInfo[] methods = ReflectUtil.GetMethods(beanType);
            for (int a = methods.Length; a-- > 0;)
            {
                MethodInfo method             = methods[a];
                A          annotationOnMethod = annotationCache.GetAnnotation(method);
                if (annotationOnMethod != null)
                {
                    if (methodLevelBehaviour == null)
                    {
                        methodLevelBehaviour = new MethodLevelHashMap <T>();
                    }
                    T behaviourTypeOnMethod = behaviourTypeExtractor.ExtractBehaviorType(annotationOnMethod);
                    if (behaviourTypeOnMethod != null)
                    {
                        methodLevelBehaviour.Put(method, behaviourTypeOnMethod);
                    }
                }
            }
            if (methodLevelBehaviour == null)
            {
                methodLevelBehaviour = new MethodLevelHashMap <T>(0);
            }
            behavior = new MethodLevelBehavior <T>(defaultBehaviour, methodLevelBehaviour);
            beanTypeToBehavior.Put(key, behavior);
            return(behavior);
        }
コード例 #3
0
        protected Type CreateConstructorType(AccessorClassLoader loader, String constructorClassName, Type delegateType, Type targetType)
        {
            if (Log.DebugEnabled)
            {
                Log.Debug("Creating fast constructor handle for " + targetType.FullName);
            }
            Type delegateTypeHandle = delegateType;
            Type objType            = typeof(Object);
            Type superType;

            TypeBuilder cw;

            if (delegateType.IsInterface)
            {
                superType = objType;
                cw        = loader.CreateNewType(TypeAttributes.Public, constructorClassName, superType, new Type[] { delegateTypeHandle });
            }
            else
            {
                superType = delegateTypeHandle;
                cw        = loader.CreateNewType(TypeAttributes.Public, constructorClassName, superType, Type.EmptyTypes);
            }
            {
                ConstructorInfo baseConstructor = superType.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null);
                if (baseConstructor == null)
                {
                    throw new Exception("Constructor not found: " + superType.FullName);
                }
                ILGenerator mv = cw.DefineConstructor(MethodAttributes.Public, CallingConventions.HasThis, Type.EmptyTypes).GetILGenerator();
                mv.Emit(OpCodes.Ldarg_0);
                mv.Emit(OpCodes.Call, baseConstructor);
                mv.Emit(OpCodes.Ret);
            }
            MethodInfo[]      r_methods    = ReflectUtil.GetMethods(delegateType);
            ConstructorInfo[] constructors = targetType.GetConstructors();
            foreach (ConstructorInfo constructor in constructors)
            {
                ParameterInfo[] constructorParams = constructor.GetParameters();
                for (int a = r_methods.Length; a-- > 0;)
                {
                    MethodInfo r_method = r_methods[a];
                    if (r_method == null)
                    {
                        // already handled
                        continue;
                    }
                    if (!delegateType.IsInterface && !r_method.IsAbstract)
                    {
                        // only handle abstract methods
                        r_methods[a] = null;
                        continue;
                    }
                    ParameterInfo[] methodParams = r_method.GetParameters();
                    if (constructorParams.Length != methodParams.Length)
                    {
                        // no match
                        continue;
                    }
                    bool paramsEqual = true;
                    for (int b = constructorParams.Length; b-- > 0;)
                    {
                        if (!constructorParams[b].ParameterType.Equals(methodParams[b].ParameterType))
                        {
                            paramsEqual = false;
                            break;
                        }
                    }
                    if (!paramsEqual)
                    {
                        // no match
                        continue;
                    }
                    r_methods[a] = null;
                    ImplementConstructorOfConstructorType(r_method, constructor, cw);
                }
            }
            foreach (MethodInfo r_method in r_methods)
            {
                if (r_method != null)
                {
                    throw new ArgumentException("No matching constructor found on " + targetType.FullName + " to map on delegate method "
                                                + r_method.ToString());
                }
            }
            return(loader.GetType(constructorClassName, cw));
        }
コード例 #4
0
        public override void VisitEnd()
        {
            HashMap <String, List <MethodInfo> > nameToMethodsMap = new HashMap <String, List <MethodInfo> >();

            foreach (MethodInfo method in ReflectUtil.GetMethods(State.OriginalType))
            {
                List <MethodInfo> methodList = nameToMethodsMap.Get(method.Name);
                if (methodList == null)
                {
                    methodList = new List <MethodInfo>();
                    nameToMethodsMap.Put(method.Name, methodList);
                }
                methodList.Add(method);
            }
            foreach (IPropertyInfo propertyInfo in propertyInfos)
            {
                MethodInfo getter = ((MethodPropertyInfo)propertyInfo).Getter;
                MethodInfo setter = ((MethodPropertyInfo)propertyInfo).Setter;
                if (getter == null)
                {
                    // look for abstract definition of the getter
                    getter = ReflectUtil.GetDeclaredMethod(true, State.CurrentType, propertyInfo.PropertyType, "get_" + propertyInfo.Name);
                }
                if (setter == null)
                {
                    // look for abstract definition of the setter
                    setter = ReflectUtil.GetDeclaredMethod(true, State.CurrentType, typeof(void), "set_" + propertyInfo.Name,
                                                           propertyInfo.PropertyType);
                }
                MethodInstance m_getterTemplate = getter != null ? new MethodInstance(getter) : null;
                MethodInstance m_setterTemplate = setter != null ? new MethodInstance(setter) : null;
                MethodInstance m_getter         = MethodInstance.FindByTemplate(m_getterTemplate, true);
                MethodInstance m_setter         = MethodInstance.FindByTemplate(m_setterTemplate, true);

                if (m_getter != null && m_setter != null)
                {
                    // ensure both accessors are public
                    if (!m_getter.Access.HasFlag(MethodAttributes.Public))
                    {
                        IMethodVisitor mv = VisitMethod(m_getter.DeriveAccess(MethodAttributes.Public));
                        mv.LoadThis();
                        mv.LoadArgs();
                        mv.InvokeSuper(m_getter);
                        mv.ReturnValue();
                        mv.EndMethod();
                    }
                    if (!m_setter.Access.HasFlag(MethodAttributes.Public))
                    {
                        IMethodVisitor mv = VisitMethod(m_setter.DeriveAccess(MethodAttributes.Public));
                        mv.LoadThis();
                        mv.LoadArgs();
                        mv.InvokeSuper(m_getter);
                        mv.ReturnValue();
                        mv.EndMethod();
                    }
                    continue;
                }
                if (m_getter != null || m_setter != null)
                {
                    // at least one of the accessors is explicitly implemented
                    continue;
                }
                FieldInstance f_backingField = EnsureBackingField(propertyInfo);
                if (f_backingField == null)
                {
                    continue;
                }
                if (m_setterTemplate == null)
                {
                    m_setterTemplate = new MethodInstance(null, MethodAttributes.Public | MethodAttributes.SpecialName, m_setterTemplate != null ? m_setterTemplate.ReturnType : NewType.VOID_TYPE,
                                                          "set_" + propertyInfo.Name, f_backingField.Type);
                }
                // implement setter
                m_setterTemplate = ImplementSetter(m_setterTemplate, f_backingField);
                List <MethodInfo> allSettersWithSameName = nameToMethodsMap.Get(m_setterTemplate.Name);
                if (allSettersWithSameName != null)
                {
                    MethodInstance f_m_setterTemplate = m_setterTemplate;
                    foreach (MethodInfo setterWithSameName in allSettersWithSameName)
                    {
                        MethodInstance m_setterWithSameName = MethodInstance.FindByTemplate(setterWithSameName, true);
                        if (m_setterWithSameName != null)
                        {
                            // method is implemented, so nothing to do
                            continue;
                        }
                        IMethodVisitor mv = VisitMethod(new MethodInstance(setterWithSameName));
                        if (mv.Method.Parameters.Length != 1)
                        {
                            // this visitor handles only "true" setters with exactly one argument
                            continue;
                        }
                        mv.CallThisSetter(m_setterTemplate, delegate(IMethodVisitor mg)
                        {
                            mg.LoadArg(0);
                            mg.CheckCast(f_m_setterTemplate.Parameters[0].Type);
                        });
                        mv.ReturnVoidOrThis();
                        mv.EndMethod();
                    }
                }
                if (m_getterTemplate == null)
                {
                    m_getterTemplate = new MethodInstance(null, MethodAttributes.Public | MethodAttributes.SpecialName, f_backingField.Type, "get_" + propertyInfo.Name, null);
                }
                // implement getter
                m_getterTemplate = ImplementGetter(m_getterTemplate, f_backingField);
                List <MethodInfo> allGettersWithSameName = nameToMethodsMap.Get(m_getterTemplate.Name);
                if (allGettersWithSameName != null)
                {
                    foreach (MethodInfo getterWithSameName in allGettersWithSameName)
                    {
                        MethodInstance m_getterWithSameName = MethodInstance.FindByTemplate(getterWithSameName, true);
                        if (m_getterWithSameName != null)
                        {
                            // method is implemented, so nothing to do
                            continue;
                        }
                        IMethodVisitor mv = VisitMethod(new MethodInstance(getterWithSameName));
                        mv.CallThisGetter(m_getterTemplate);
                        mv.ReturnValue();
                        mv.EndMethod();
                    }
                }
            }
            base.VisitEnd();
        }