public virtual void InvokeSuperOf(MethodInfo method) { IBytecodeBehaviorState state = BytecodeBehaviorState.State; MethodInfo superMethod = ReflectUtil.GetDeclaredMethod(false, state.CurrentType, NewType.GetType(method.ReturnType), method.Name, TypeUtil.GetClassesToTypes(method.GetParameters())); InvokeSuper(new MethodInstance(superMethod)); }
public static PropertyChangedEventHandler CreateParentChildEventHandler(Object entity) { //typeof(IPropertyChangeListener); MethodInfo method = ReflectUtil.GetDeclaredMethod(true, entity.GetType(), template_m_PropertyChanged.ReturnType.Type, template_m_PropertyChanged.Name, typeof(Object), typeof(PropertyChangedEventArgs)); return((PropertyChangedEventHandler)Delegate.CreateDelegate(typeof(PropertyChangedEventHandler), entity, template_m_PropertyChanged.Name)); }
public virtual void InvokeSuper(MethodInstance method) { NewType newType = BytecodeBehaviorState.State.NewType; if (newType.Equals(method.Owner)) { // Given method is not a super method. We look in the existing class hierarchy for a method with the same signature if (ConstructorInstance.CONSTRUCTOR_NAME.Equals(method.Name)) { ConstructorInfo c_method = ReflectUtil.GetDeclaredConstructor(true, BytecodeBehaviorState.State.CurrentType, method.Parameters); if (c_method == null) { throw new ArgumentException("Constructor has no super implementation: " + method); } method = new ConstructorInstance(c_method); } else { MethodInfo r_method = ReflectUtil.GetDeclaredMethod(true, BytecodeBehaviorState.State.CurrentType, method.ReturnType, method.Name, method.Parameters); if (r_method == null) { throw new ArgumentException("Method has no super implementation: " + method); } method = new MethodInstance(r_method); } } InvokeOnExactOwner(method); }
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)); }
public static MethodInstance FindByTemplate(bool tryOnly, NewType returnType, String methodName, params NewType[] parameters) { IBytecodeBehaviorState state = BytecodeBehaviorState.State; foreach (MethodInstance methodOnNewType in state.GetAlreadyImplementedMethodsOnNewType()) { if (!methodOnNewType.Name.Equals(methodName)) { continue; } NewType[] paramsOnNewType = methodOnNewType.Parameters; if (paramsOnNewType.Length != parameters.Length) { continue; } bool paramsEqual = true; for (int a = paramsOnNewType.Length; a-- > 0;) { if (!paramsOnNewType[a].Equals(parameters[a])) { paramsEqual = false; break; } } if (paramsEqual) { return(methodOnNewType); } if (returnType == null || methodOnNewType.ReturnType.Equals(returnType)) { return(methodOnNewType); } } Type currType = state.CurrentType; if (!currType.IsInterface) { while (currType != null && currType != typeof(Object)) { MethodInfo method = ReflectUtil.GetDeclaredMethod(true, currType, returnType, methodName, parameters); if (method != null) { if (method.Attributes.HasFlag(MethodAttributes.Abstract)) { // Method found but it is abstract. So it is not a callable instance break; } return(new MethodInstance(method)); } currType = currType.BaseType; } } if (tryOnly) { return(null); } throw new Exception("No method found on class hierarchy: " + methodName + ". Start type: " + state.NewType); }
public override void RefreshAccessors(Type realType) { base.RefreshAccessors(realType); Getter = ReflectUtil.GetDeclaredMethod(true, realType, PropertyType, "get_" + Name); Setter = ReflectUtil.GetDeclaredMethod(true, realType, null, "set_" + Name, PropertyType); IsWritable = this.Setter != null && !Setter.IsPrivate; IsReadable = this.Getter != null && !Getter.IsPrivate; RefreshDeclaringType(); }
protected void CheckEnhancedTypeConsistency(Type type) { IdentityHashSet <MethodInfo> allMethods = new IdentityHashSet <MethodInfo>(); foreach (Type interf in type.GetInterfaces()) { allMethods.AddAll(interf.GetMethods()); } Type currType = type; while (currType != typeof(Object) && currType != null) { allMethods.AddAll(currType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly)); currType = currType.BaseType; } if (allMethods.Count == 0) { throw new Exception("Type invalid (not a single method): " + type); } if (type.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly).Length == 0) { throw new Exception("Type invalid (not a single constructor): " + type); } if (!type.IsAbstract) { foreach (MethodInfo method in allMethods) { MethodInfo method2 = ReflectUtil.GetDeclaredMethod(true, type, method.ReturnType, method.Name, TypeUtil.GetParameterTypesToTypes(method.GetParameters())); if (method2 == null || method2.IsAbstract) { throw new Exception("Type is not abstract but has at least one abstract method: " + method); } } } Type[] interfaces = type.GetInterfaces(); foreach (Type interf in interfaces) { MethodInfo[] interfaceMethods = ReflectUtil.GetDeclaredMethods(interf); foreach (MethodInfo interfaceMethod in interfaceMethods) { try { if (type.GetMethod(interfaceMethod.Name, TypeUtil.GetParameterTypesToTypes(interfaceMethod.GetParameters())) == null) { throw new Exception("Type is not abstract but has at least one abstract method: " + interfaceMethod); } } catch (Exception e) { throw new Exception("Type is not abstract but has at least one abstract method: " + interfaceMethod, e); } } } }
public void PutAnnotations(ICustomAttributeProvider obj) { if (obj is MethodInfo) { MethodInfo m = (MethodInfo)obj; Type[] parameters = new Type[m.GetParameters().Length]; for (int a = m.GetParameters().Length; a-- > 0;) { parameters[a] = m.GetParameters()[a].ParameterType; } Type baseType = m.DeclaringType.BaseType; MethodInfo overriddenMethod = baseType != null?ReflectUtil.GetDeclaredMethod(true, baseType, m.ReturnType, m.Name, parameters) : null; if (overriddenMethod != null) { PutAnnotations(overriddenMethod); } } Object[] annotations = obj.GetCustomAttributes(true); foreach (Object anno in annotations) { Attribute annotation = (Attribute)anno; AttributeUsageAttribute attributeUsage = AnnotationUtil.GetAnnotation <AttributeUsageAttribute>(annotation.GetType(), true); Type type = annotation.GetType(); if (this.annotations == null) { this.annotations = new LinkedHashMap <Type, Attribute[]>(); } Attribute[] existingAttributes = this.annotations.Get(type); if (existingAttributes == null) { existingAttributes = new Attribute[0]; } Attribute[] newAttributes = new Attribute[existingAttributes.Length + 1]; Array.Copy(existingAttributes, newAttributes, existingAttributes.Length); newAttributes[existingAttributes.Length] = annotation; this.annotations.Put(type, newAttributes); } }
protected Type CreateGpType(AccessorClassLoader loader, Type proxyType, Type[] additionalProxyTypes, String className) { String classNameInternal = className.Replace('.', '/'); Type abstractType = typeof(GCProxy); List <Type> interfaceClasses = new List <Type>(); interfaceClasses.Add(proxyType); foreach (Type additionalProxyType in additionalProxyTypes) { interfaceClasses.Add(additionalProxyType); } TypeBuilder cw = loader.CreateNewType(TypeAttributes.Public, classNameInternal, abstractType, Type.EmptyTypes); { ConstructorInfo baseConstructor = abstractType.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[] { typeof(Object), typeof(IDisposable) }, null); if (baseConstructor == null) { throw new Exception("Constructor not found: " + abstractType.FullName); } ILGenerator mv = cw.DefineConstructor(MethodAttributes.Public, CallingConventions.HasThis, TypeUtil.GetParameterTypesToTypes(baseConstructor.GetParameters())).GetILGenerator(); mv.Emit(OpCodes.Ldarg_0); mv.Emit(OpCodes.Ldarg_1); mv.Emit(OpCodes.Ldarg_2); mv.Emit(OpCodes.Call, baseConstructor); mv.Emit(OpCodes.Ret); } { ConstructorInfo baseConstructor = abstractType.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[] { typeof(IDisposable) }, null); if (baseConstructor == null) { throw new Exception("Constructor not found: " + abstractType.FullName); } ILGenerator mv = cw.DefineConstructor(MethodAttributes.Public, CallingConventions.HasThis, TypeUtil.GetParameterTypesToTypes(baseConstructor.GetParameters())).GetILGenerator(); mv.Emit(OpCodes.Ldarg_0); mv.Emit(OpCodes.Ldarg_1); mv.Emit(OpCodes.Call, baseConstructor); mv.Emit(OpCodes.Ret); } MethodInfo targetMethod = ReflectUtil.GetDeclaredMethod(false, typeof(GCProxy), typeof(Object), "ResolveTarget"); CHashSet <MethodInfo> alreadyImplementedMethods = new CHashSet <MethodInfo>(); foreach (Type interfaceClass in interfaceClasses) { MethodInfo[] methods = interfaceClass.GetMethods(); foreach (MethodInfo method in methods) { if (GCProxy.disposeMethod.Equals(method)) { // will remain implemented by the GCProxy class continue; } if (!alreadyImplementedMethods.Add(method)) { continue; } MethodAttributes attributes = 0; Type[] paramTypes = TypeUtil.GetParameterTypesToTypes(method.GetParameters()); ILGenerator mv = cw.DefineMethod(method.Name, attributes, CallingConventions.HasThis, method.ReturnType, paramTypes).GetILGenerator(); mv.Emit(OpCodes.Ldarg_0); mv.Emit(OpCodes.Callvirt, targetMethod); mv.Emit(OpCodes.Castclass, method.DeclaringType); for (int a = 0, size = paramTypes.Length; a < size; a++) { mv.Emit(OpCodes.Ldarg, a + 1); } mv.Emit(OpCodes.Callvirt, method); mv.Emit(OpCodes.Ret); } } return(loader.GetType(classNameInternal, cw)); }
static AbstractRootCacheAwareInterceptor() { clearMethod = ReflectUtil.GetDeclaredMethod(false, typeof(IWritableCache), typeof(void), "Clear"); }
protected Type CreateType(AccessorClassLoader loader, String accessClassName, Type targetType, IPropertyInfo property) { if (Log.DebugEnabled) { Log.Debug("Creating accessor for " + targetType.FullName + "." + property.Name); } Type abstractAccessorType = typeof(AbstractAccessor); Type objType = typeof(Object); TypeBuilder cw = loader.CreateNewType(TypeAttributes.Public, accessClassName, abstractAccessorType, Type.EmptyTypes); { ConstructorInfo baseConstructor = abstractAccessorType.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[] { typeof(Type), typeof(String) }, null); ILGenerator mv = cw.DefineConstructor(MethodAttributes.Public, CallingConventions.HasThis, new Type[] { typeof(Type), typeof(String) }).GetILGenerator(); mv.Emit(OpCodes.Ldarg_0); mv.Emit(OpCodes.Ldarg_1); mv.Emit(OpCodes.Ldarg_2); mv.Emit(OpCodes.Call, baseConstructor); mv.Emit(OpCodes.Ret); } MethodInfo r_get = ReflectUtil.GetDeclaredMethod(true, targetType, property.PropertyType, "get_" + property.Name, new Type[0]); if (r_get == null) { r_get = ReflectUtil.GetDeclaredMethod(true, targetType, property.PropertyType, "Get" + property.Name, new Type[0]); } if (r_get == null) { r_get = ReflectUtil.GetDeclaredMethod(true, targetType, property.PropertyType, "Is" + property.Name, new Type[0]); } MethodInfo r_set = ReflectUtil.GetDeclaredMethod(true, targetType, property.PropertyType, "set_" + property.Name, new Type[] { null }); if (r_set == null) { r_set = ReflectUtil.GetDeclaredMethod(true, targetType, property.PropertyType, "Set" + property.Name, new Type[] { null }); } { ILGenerator mv = cw.DefineMethod("get_CanRead", MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.HideBySig, CallingConventions.HasThis, typeof(bool), Type.EmptyTypes).GetILGenerator(); mv.Emit(r_get != null && r_get.IsPublic ? OpCodes.Ldc_I4_1 : OpCodes.Ldc_I4_0); mv.Emit(OpCodes.Ret); } { ILGenerator mv = cw.DefineMethod("get_CanWrite", MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.HideBySig, CallingConventions.HasThis, typeof(bool), Type.EmptyTypes).GetILGenerator(); mv.Emit(r_set != null && r_set.IsPublic ? OpCodes.Ldc_I4_1 : OpCodes.Ldc_I4_0); mv.Emit(OpCodes.Ret); } { ILGenerator mv = cw.DefineMethod("GetValue", MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.HideBySig, CallingConventions.HasThis, typeof(Object), new Type[] { typeof(Object) }).GetILGenerator(); if (r_get == null) { mv.Emit(OpCodes.Ldstr, "Property not readable: " + targetType.FullName + "." + property.Name); mv.ThrowException(typeof(NotSupportedException)); } else { Type owner = r_get.DeclaringType; mv.Emit(OpCodes.Ldarg_1); mv.Emit(OpCodes.Castclass, owner); mv.Emit(OpCodes.Callvirt, r_get); if (r_get.ReturnType.IsValueType) { mv.Emit(OpCodes.Box, r_get.ReturnType); } } mv.Emit(OpCodes.Ret); } { MethodAttributes access = MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.HideBySig; access |= MethodAttributes.ReuseSlot; access &= ~MethodAttributes.VtableLayoutMask; ILGenerator mv = cw.DefineMethod("SetValue", access, CallingConventions.HasThis, typeof(void), new Type[] { typeof(Object), typeof(Object) }).GetILGenerator(); if (r_set == null) { mv.Emit(OpCodes.Ldstr, "Property not writable: " + targetType.FullName + "." + property.Name); mv.ThrowException(typeof(NotSupportedException)); } else { Type owner = r_get.DeclaringType; mv.Emit(OpCodes.Ldarg_1); mv.Emit(OpCodes.Castclass, owner); mv.Emit(OpCodes.Ldarg_2); Type paramType = r_set.GetParameters()[0].ParameterType; if (!objType.Equals(paramType)) { if (paramType.IsValueType) { mv.Emit(OpCodes.Unbox_Any, paramType); } else { mv.Emit(OpCodes.Castclass, paramType); } } mv.Emit(OpCodes.Callvirt, r_set); } mv.Emit(OpCodes.Ret); } return(loader.GetType(accessClassName, cw)); }
protected PropertyInstance ImplementPotentialPrimitive(PropertyInstance property, Member member) { if (member == null) { return(ImplementProperty(property, delegate(IMethodVisitor mg) { mg.PushNull(); mg.ReturnValue(); }, delegate(IMethodVisitor mg) { Label l_isNull = mg.NewLabel(); mg.LoadArg(0); mg.IfNull(l_isNull); mg.ThrowException(typeof(NotSupportedException), "Property is read-only"); mg.Mark(l_isNull); mg.ReturnValue(); })); } PropertyInstance p_conversionHelper = GetConversionHelperPI(this); MethodInstance m_convertValueToType = new MethodInstance(ReflectUtil.GetDeclaredMethod(false, typeof(IConversionHelper), typeof(Object), "ConvertValueToType", typeof(Type), typeof(Object))); Type type = member.RealType; FieldInstance field = ImplementField(new FieldInstance(FieldAttributes.Public, "f_" + property.Name, type)); return(ImplementProperty(property, delegate(IMethodVisitor mg) { if (field.Type.Type.IsPrimitive) { Label l_isNull = mg.NewLabel(); LocalVariableInfo loc_value = mg.NewLocal(field.Type); mg.GetThisField(field); mg.StoreLocal(loc_value); mg.LoadLocal(loc_value); mg.IfZCmp(field.Type, CompareOperator.EQ, l_isNull); mg.LoadLocal(loc_value); mg.Box(type); mg.ReturnValue(); mg.Mark(l_isNull); mg.PushNull(); } else if (field.Type.Type.IsValueType) { Type pType = field.Type.Type; mg.GetThisField(field); MethodInfo m_hasValue = pType.GetMethod("get_HasValue"); if (m_hasValue != null) { LocalVariableInfo loc_value = mg.NewLocal(pType); mg.StoreLocal(loc_value); mg.LoadLocal(loc_value); MethodInfo m_getValue = pType.GetMethod("get_Value"); LocalVariableInfo loc_realValue = mg.NewLocal(m_getValue.ReturnType); Label l_hasNoValue = mg.NewLabel(); mg.InvokeOnExactOwner(m_hasValue); mg.IfZCmp(CompareOperator.EQ, l_hasNoValue); mg.LoadLocal(loc_value); mg.InvokeOnExactOwner(m_getValue); mg.StoreLocal(loc_realValue); mg.LoadLocal(loc_realValue); mg.IfZCmp(CompareOperator.EQ, l_hasNoValue); mg.LoadLocal(loc_realValue); mg.ValueOf(m_getValue.ReturnType); mg.ReturnValue(); mg.Mark(l_hasNoValue); mg.PushNullOrZero(mg.Method.ReturnType); } else { mg.Box(pType); } } else { mg.GetThisField(field); } mg.ReturnValue(); }, delegate(IMethodVisitor mg) { mg.PutThisField(field, delegate(IMethodVisitor mg2) { Label l_isNull = mg2.NewLabel(); Label l_finish = mg2.NewLabel(); mg2.LoadArg(0); mg2.IfNull(l_isNull); mg.CallThisGetter(p_conversionHelper); mg.Push(type); mg.LoadArg(0); mg.InvokeVirtual(m_convertValueToType); mg2.Unbox(type); mg2.GoTo(l_finish); mg2.Mark(l_isNull); mg2.PushNullOrZero(field.Type); mg2.Mark(l_finish); }); mg.ReturnValue(); })); }
public MethodInstance(NewType owner, Type declaringTypeOfMethod, Type returnType, String methodName, params Type[] parameters) : this(owner != null ? owner : NewType.GetType(declaringTypeOfMethod), ReflectUtil.GetDeclaredMethod(false, declaringTypeOfMethod, returnType, methodName, parameters)) { // Intended blank }
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(); }