/** * public boolean hasPendingChanges() { return isToBeUpdated() || isToBeCreated() || isToBeDeleted(); } */ protected void ImplementHasPendingChanges(PropertyInstance p_hasPendingChanges, PropertyInstance p_ToBeUpdated, PropertyInstance p_ToBeCreated, PropertyInstance p_ToBeDeleted) { IMethodVisitor mg = VisitMethod(p_hasPendingChanges.Getter); p_hasPendingChanges = PropertyInstance.FindByTemplate(p_hasPendingChanges, false); p_hasPendingChanges.AddAnnotation(c_ignoreToBeUpdated); p_hasPendingChanges.AddAnnotation(c_fireThisOPC, p_ToBeCreated.Name); p_hasPendingChanges.AddAnnotation(c_fireThisOPC, p_ToBeUpdated.Name); p_hasPendingChanges.AddAnnotation(c_fireThisOPC, p_ToBeDeleted.Name); Label trueLabel = mg.NewLabel(); mg.LoadThis(); mg.InvokeVirtual(p_ToBeUpdated.Getter); mg.IfZCmp(CompareOperator.NE, trueLabel); mg.LoadThis(); mg.InvokeVirtual(p_ToBeCreated.Getter); mg.IfZCmp(CompareOperator.NE, trueLabel); mg.LoadThis(); mg.InvokeVirtual(p_ToBeDeleted.Getter); mg.IfZCmp(CompareOperator.NE, trueLabel); mg.Push(false); mg.ReturnValue(); mg.Mark(trueLabel); mg.Push(true); mg.ReturnValue(); mg.EndMethod(); }
protected void ImplementToStringMethod(PropertyInstance p_entityEqualsTemplate) { { MethodInstance methodTemplate = new MethodInstance(null, typeof(Object), typeof(String), "ToString"); MethodInstance method = MethodInstance.FindByTemplate(methodTemplate, true); if (NewType.GetType(typeof(Object)).Equals(method.Owner) || method.Access.HasFlag(MethodAttributes.Abstract)) { IMethodVisitor mg = VisitMethod(methodTemplate); mg.CallThisGetter(p_entityEqualsTemplate); mg.LoadThis(); mg.LoadThis(); mg.InvokeVirtual(entityEquals_toString_Obj); mg.ReturnValue(); mg.EndMethod(); } } { MethodInstance methodTemplate = new MethodInstance(null, typeof(IPrintable), typeof(void), "ToString", typeof(StringBuilder)); MethodInstance method = MethodInstance.FindByTemplate(methodTemplate, true); if (method == null || method.Access.HasFlag(MethodAttributes.Abstract)) { IMethodVisitor mg = VisitMethod(methodTemplate); mg.CallThisGetter(p_entityEqualsTemplate); mg.LoadThis(); mg.LoadArgs(); mg.InvokeVirtual(entityEquals_toString_Printable); mg.ReturnValue(); mg.EndMethod(); } } }
public virtual void LoadThis() { if (mv != null) { mv.LoadThis(); } }
protected void ImplementCascadeLoadMode() { FieldInstance f_cascadeLoadMode = ImplementField(new FieldInstance(FieldAttributes.Private, "__cascadeLoadMode", typeof(CascadeLoadMode))); ImplementGetter(template_m_getCascadeLoadMode, f_cascadeLoadMode); ImplementSetter(template_m_setCascadeLoadMode, f_cascadeLoadMode); ConstructorInfo[] constructors = State.CurrentType.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly); for (int a = constructors.Length; a-- > 0;) { ConstructorInstance ci = new ConstructorInstance(constructors[a]); ci = new ConstructorInstance(MethodAttributes.Public, ci.Parameters); IMethodVisitor mv = VisitMethod(ci); mv.LoadThis(); mv.LoadArgs(); mv.InvokeSuperOfCurrentMethod(); mv.PutThisField(f_cascadeLoadMode, delegate(IMethodVisitor mg) { mg.PushEnum(CascadeLoadMode.DEFAULT); }); mv.ReturnValue(); mv.EndMethod(); } }
protected MethodInstance ImplementUsePropertyChangeSupport(PropertyInstance p_propertyChangeTemplate, FieldInstance f_propertyChangeSupport) { MethodInstance m_getPropertyChangeSupport = MethodInstance.FindByTemplate(template_m_usePropertyChangeSupport, true); if (m_getPropertyChangeSupport == null) { // create field that holds propertyChangeSupport f_propertyChangeSupport = ImplementField(f_propertyChangeSupport); IMethodVisitor mg = VisitMethod(template_m_usePropertyChangeSupport); HideFromDebug(mg.Method); Label l_pcsValid = mg.NewLabel(); mg.GetThisField(f_propertyChangeSupport); mg.Dup(); mg.IfNonNull(l_pcsValid); mg.Pop(); // remove 2nd null instance from stack caused by previous dup mg.PutThisField(f_propertyChangeSupport, delegate(IMethodVisitor mg2) { mg.CallThisGetter(p_propertyChangeTemplate); mg.LoadThis(); mg.InvokeVirtual(m_newPropertyChangeSupport); }); mg.GetThisField(f_propertyChangeSupport); mg.Mark(l_pcsValid); mg.ReturnValue(); // return instance already on the stack by both branches mg.EndMethod(); m_getPropertyChangeSupport = mg.Method; } return(m_getPropertyChangeSupport); }
protected void ImplementDefaultConstructor() { IMethodVisitor mv = VisitMethod(ConstructorInstance.defaultConstructor); mv.LoadThis(); mv.InvokeSuperOfCurrentMethod(); mv.ReturnValue(); mv.EndMethod(); }
protected void ImplementPropertyChanged(PropertyInstance p_propertyChangeTemplate) { MethodInstance m_propertyChanged_super = MethodInstance.FindByTemplate(template_m_PropertyChanged, true); IMethodVisitor mv = VisitMethod(template_m_PropertyChanged); if (m_propertyChanged_super != null) { mv.LoadThis(); mv.LoadArgs(); mv.InvokeSuperOfCurrentMethod(); } mv.CallThisGetter(p_propertyChangeTemplate); mv.LoadThis(); mv.LoadArgs(); // call PCT.HandlePropertyChange(this, sender, arg) mv.InvokeVirtual(m_handlePropertyChange); mv.ReturnValue(); mv.EndMethod(); }
protected static void ImplementDefaultDelegatingMethod(IMethodVisitor mg, PropertyInstance p_compositeIdTemplate, PropertyInstance p_idMembers, MethodInstance delegatedMethod) { mg.CallThisGetter(p_compositeIdTemplate); mg.CallThisGetter(p_idMembers); mg.LoadThis(); mg.LoadArgs(); mg.InvokeVirtual(delegatedMethod); mg.ReturnValue(); mg.EndMethod(); }
protected void ImplementSetUninitialized(PropertyInstance p_valueHolderContainerTemplate, PropertyInstance p_valueHolderContainerEntry) { { IMethodVisitor mv = VisitMethod(m_template_setUninitialized_Member); mv.CallThisGetter(p_valueHolderContainerEntry); mv.LoadThis(); mv.LoadArgs(); mv.InvokeVirtual(m_vhce_setUninitialized_Member); mv.ReturnValue(); mv.EndMethod(); } }
protected void ImplementConstructor(MethodInstance template_m_setPrimitivePropertyPrivilege, MethodInstance template_m_setRelationPropertyPrivilege) { IBytecodeBehaviorState state = State; ConstructorInfo constructor = state.CurrentType.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[] { typeof(bool), typeof(bool), typeof(bool), typeof(bool), typeof(bool), typeof(IPropertyPrivilege[]), typeof(IPropertyPrivilege[]) }, null); ConstructorInstance c_method = new ConstructorInstance(constructor); IMethodVisitor mg = VisitMethod(c_method); mg.LoadThis(); mg.LoadArgs(); mg.InvokeConstructor(c_method); Type propertyPrivilegeType = typeof(IPropertyPrivilege); for (int primitiveIndex = 0, size = metaData.PrimitiveMembers.Length; primitiveIndex < size; primitiveIndex++) { mg.LoadThis(); mg.Push(primitiveIndex); mg.LoadArg(5); mg.Push(primitiveIndex); mg.ArrayLoad(propertyPrivilegeType); mg.InvokeVirtual(template_m_setPrimitivePropertyPrivilege); } for (int relationIndex = 0, size = metaData.RelationMembers.Length; relationIndex < size; relationIndex++) { mg.LoadThis(); mg.Push(relationIndex); mg.LoadArg(6); mg.Push(relationIndex); mg.ArrayLoad(propertyPrivilegeType); mg.InvokeVirtual(template_m_setRelationPropertyPrivilege); } mg.ReturnValue(); mg.EndMethod(); }
protected void ImplementConstructor(PropertyInstance p_embeddedMemberTemplate) { HashSet <Member> alreadyHandledFirstMembers = new HashSet <Member>(); List <Script> scripts = new List <Script>(); { Script script = HandleMember(p_embeddedMemberTemplate, metaData.IdMember, alreadyHandledFirstMembers); if (script != null) { scripts.Add(script); } } foreach (Member member in metaData.PrimitiveMembers) { Script script = HandleMember(p_embeddedMemberTemplate, member, alreadyHandledFirstMembers); if (script != null) { scripts.Add(script); } } foreach (RelationMember member in metaData.RelationMembers) { Script script = HandleMember(p_embeddedMemberTemplate, member, alreadyHandledFirstMembers); if (script != null) { scripts.Add(script); } } if (scripts.Count == 0) { return; } OverrideConstructors(delegate(IClassVisitor cv, ConstructorInstance superConstructor) { IMethodVisitor mv = cv.VisitMethod(superConstructor); mv.LoadThis(); mv.LoadArgs(); mv.InvokeSuperOfCurrentMethod(); foreach (Script script in scripts) { script(mv); } mv.ReturnValue(); mv.EndMethod(); }); }
protected void ImplementHashCodeMethod(PropertyInstance p_entityEqualsTemplate) { MethodInstance methodTemplate = new MethodInstance(null, typeof(Object), typeof(int), "GetHashCode"); MethodInstance method = MethodInstance.FindByTemplate(methodTemplate, true); if (NewType.GetType(typeof(Object)).Equals(method.Owner) || method.Access.HasFlag(MethodAttributes.Abstract)) { IMethodVisitor mg = VisitMethod(methodTemplate); mg.CallThisGetter(p_entityEqualsTemplate); mg.LoadThis(); mg.LoadArgs(); mg.InvokeVirtual(entityEquals_HashCode); mg.ReturnValue(); mg.EndMethod(); } }
protected void ImplementHashCode() { PropertyInstance p_objRefTemplate = GetObjRefTemplatePI(this); MethodInstance method = MethodInstance.FindByTemplate(template_m_hashCode, true); if (method == null || method.Access.HasFlag(MethodAttributes.Abstract)) { IMethodVisitor mg = VisitMethod(template_m_hashCode); mg.CallThisGetter(p_objRefTemplate); mg.LoadThis(); mg.LoadArgs(); mg.InvokeVirtual(m_objRef_hashCode); mg.ReturnValue(); mg.EndMethod(); } }
public virtual IMethodVisitor StartOverrideWithSuperCall(MethodInstance superMethod) { IBytecodeBehaviorState state = State; NewType superType = NewType.GetType(state.CurrentType); if (!superType.Equals(superMethod.Owner)) { throw new ArgumentException("Not a method of " + state.CurrentType + ": " + superMethod); } IMethodVisitor mg = VisitMethod(superMethod); mg.LoadThis(); mg.LoadArgs(); mg.InvokeSuper(superMethod); return(mg); }
protected void ImplementSelfGetter(PropertyInstance p_valueHolderContainerTemplate) { NewType owner = State.NewType; MethodInstance m_getSelf = new MethodInstance(owner, typeof(IValueHolderContainer), typeof(IObjRelation), "Get__Self", typeof(int)); { // public IObjRelation getSelf(String memberName) // { // return ValueHolderContainerTemplate.GetSelf(this, relationIndex); // } IMethodVisitor mv = VisitMethod(m_getSelf); mv.CallThisGetter(p_valueHolderContainerTemplate); // this mv.LoadThis(); // relationIndex mv.LoadArgs(); mv.InvokeVirtual(m_template_getSelf); mv.ReturnValue(); mv.EndMethod(); } }
protected void ImplementIdVersionConstructor(PropertyInstance p_id, PropertyInstance p_version) { ConstructorInfo superConstructor = State.CurrentType.GetConstructor(Type.EmptyTypes); ConstructorInstance ci_super = new ConstructorInstance(superConstructor); IMethodVisitor mv = VisitMethod(new ConstructorInstance(MethodAttributes.Public, typeof(Object), typeof(Object))); mv.LoadThis(); mv.InvokeConstructor(ci_super); mv.CallThisSetter(p_id, delegate(IMethodVisitor mg) { mg.LoadArg(0); }); mv.CallThisSetter(p_version, delegate(IMethodVisitor mg) { mg.LoadArg(1); }); mv.ReturnValue(); mv.EndMethod(); }
protected void ImplementRelationSetter(String propertyName, MethodInstance m_set_template, FieldInstance f_initialized, FieldInstance f_objRefs) { // public void setPropertyName(String propertyName) // { // PropertyName$initialized = true; // PropertyName$objRefs = null; // super.setPropertyName(propertyName); // } MethodInstance m_set; { IMethodVisitor mg = base.VisitMethod(m_set_template); m_set = mg.Method; mg.PutThisField(f_initialized, delegate(IMethodVisitor mv2) { mg.PushEnum(ValueHolderState.INIT); }); mg.PutThisField(f_objRefs, delegate(IMethodVisitor mv2) { mv2.PushNull(); }); mg.LoadThis(); mg.LoadArgs(); mg.InvokeSuperOfCurrentMethod(); mg.ReturnVoidOrThis(); mg.EndMethod(); } // public void setPropertyName$NoInit(String propertyName) // { // super.setPropertyName(propertyName); // } { String noInitSetMethodName = ValueHolderIEC.GetSetterNameOfRelationPropertyWithNoInit(propertyName); IMethodVisitor mv = base.VisitMethod(m_set.Access, noInitSetMethodName, m_set.ReturnType, m_set.Parameters); mv.LoadThis(); mv.LoadArgs(); mv.InvokeSuper(m_set); mv.ReturnVoidOrThis(); mv.EndMethod(); } }
protected void ImplementEmbeddedConstructor(FieldInstance f_parentObject, ConstructorInstance superConstructor) { if (superConstructor.Parameters.Length > 0 && superConstructor.Parameters[0].Equals(f_parentObject.Type)) { // super constructor already enhanced return; } bool baseIsEnhanced = false;//EntityEnhancer.IsEnhancedType(vs.CurrentType); NewType[] parameters = superConstructor.Parameters; NewType[] types; if (baseIsEnhanced) { // Only Pass-through constructors necessary. So signature remains the same types = null;//TypeUtil.GetClassesToTypes(..GetTypes(parameters); } else { types = new NewType[parameters.Length + 1]; for (int a = parameters.Length + 1; a-- > 1;) { types[a] = parameters[a - 1]; } types[0] = f_parentObject.Type; } IMethodVisitor mv = VisitMethod(new ConstructorInstance(MethodAttributes.Public, types)); mv.LoadThis(); for (int a = 1, size = types.Length; a < size; a++) { mv.LoadArg(a); // Load constructor argument one by one, starting with the 2nd constructor argument } mv.InvokeConstructor(superConstructor); mv.PutThisField(f_parentObject, delegate(IMethodVisitor mv2) { mv2.LoadArg(0); }); mv.ReturnValue(); mv.EndMethod(); }
public override void VisitEnd() { MethodInstance m_get__Id = MethodInstance.FindByTemplate(GetIdMethodCreator.template_m_entityEquals_getId, true); if (m_get__Id != null) { base.VisitEnd(); return; } MethodInstance m_getEntityMetaData = EntityMetaDataHolderVisitor.GetImplementedGetEntityMetaData(this, metaData); IMethodVisitor mg = VisitMethod(GetIdMethodCreator.template_m_entityEquals_getId); mg.CallThisGetter(m_getEntityMetaData); mg.InvokeInterface(new MethodInstance(null, typeof(IEntityMetaData), typeof(PrimitiveMember), "get_IdMember")); mg.LoadThis(); mg.Push(false); mg.InvokeVirtual(EntityMetaDataMemberVisitor.template_m_getValueWithFlag); mg.ReturnValue(); mg.EndMethod(); base.VisitEnd(); }
/** * public boolean isToBeCreated() { return get__Id() == null; } * * @param owner */ protected PropertyInstance ImplementToBeCreated(PropertyInstance p_toBeCreated) { IMethodVisitor mg = VisitMethod(p_toBeCreated.Getter); p_toBeCreated = PropertyInstance.FindByTemplate(p_toBeCreated, false); Member idMember = metaData.IdMember; if (idMember is CompositeIdMember) { List <String> names = new List <String>(); foreach (Member itemMember in ((CompositeIdMember)idMember).Members) { names.Add(itemMember.Name); } p_toBeCreated.AddAnnotation(c_fireThisOPC, new Object[] { names.ToArray() }); } else { p_toBeCreated.AddAnnotation(c_fireThisOPC, idMember.Name); } Label trueLabel = mg.NewLabel(); mg.LoadThis(); mg.InvokeVirtual(GetIdMethodCreator.GetGetId()); mg.IfNull(trueLabel); mg.Push(false); mg.ReturnValue(); mg.Mark(trueLabel); mg.Push(true); mg.ReturnValue(); mg.EndMethod(); return(p_toBeCreated); }
protected void ImplementPropertyChangeConfigurable() { String fieldName = "__propertyChangeActive"; if (State.GetAlreadyImplementedField(fieldName) != null) { if (properties == null) { throw new Exception("It seems that this visitor has been executing twice"); } return; } else if (properties != null) { // do not apply in this case return; } FieldInstance f_propertyChangeActive = ImplementField(new FieldInstance(FieldAttributes.Private, fieldName, typeof(bool))); ConstructorInfo[] constructors = State.CurrentType.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly); for (int a = constructors.Length; a-- > 0;) { ConstructorInstance ci = new ConstructorInstance(constructors[a]); IMethodVisitor mv = VisitMethod(ci); mv.LoadThis(); mv.LoadArgs(); mv.InvokeSuperOfCurrentMethod(); mv.PutThisField(f_propertyChangeActive, delegate(IMethodVisitor mg) { mg.Push(true); }); mv.ReturnValue(); mv.EndMethod(); } ImplementGetter(template_m_isPropertyChangeActive, f_propertyChangeActive); ImplementSetter(template_m_setPropertyChangeActive, f_propertyChangeActive); }
protected void ImplementToString() { PropertyInstance p_objRefTemplate = GetObjRefTemplatePI(this); MethodInstance methodSb; { methodSb = MethodInstance.FindByTemplate(template_m_toStringSb, true); if (methodSb == null || methodSb.Access.HasFlag(MethodAttributes.Abstract)) { IMethodVisitor mg = VisitMethod(template_m_toStringSb); mg.CallThisGetter(p_objRefTemplate); mg.LoadThis(); mg.LoadArgs(); mg.InvokeVirtual(m_objRef_toStringSb); mg.ReturnValue(); mg.EndMethod(); methodSb = mg.Method; } } { MethodInstance method = MethodInstance.FindByTemplate(template_m_toString, true); if (method == null || NewType.GetType(typeof(Object)).Equals(method.Owner) || methodSb.Access.HasFlag(MethodAttributes.Abstract)) { IMethodVisitor mg = VisitMethod(template_m_toString); LocalVariableInfo loc_sb = mg.NewLocal(typeof(StringBuilder)); mg.LoadThis(); mg.NewInstance(c_stringBuilder, null); mg.StoreLocal(loc_sb); mg.LoadLocal(loc_sb); mg.InvokeVirtual(methodSb); mg.LoadLocal(loc_sb); mg.InvokeVirtual(new MethodInstance(null, typeof(StringBuilder), typeof(String), "ToString")); mg.ReturnValue(); mg.EndMethod(); } } }
/** * {@inheritDoc} */ public override void VisitEnd() { if (!HasValidConstructor()) { IBytecodeBehaviorState state = State; ConstructorInfo[] constructors = state.CurrentType.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly); foreach (ConstructorInfo constructor in constructors) { MethodAttributes access = constructor.Attributes; access &= ~MethodAttributes.Family; access &= ~MethodAttributes.Private; access |= MethodAttributes.Public; ConstructorInstance c_method = new ConstructorInstance(access, TypeUtil.GetClassesToTypes(constructor.GetParameters())); IMethodVisitor mg = VisitMethod(c_method); mg.LoadThis(); mg.LoadArgs(); mg.InvokeConstructor(new ConstructorInstance(constructor)); mg.ReturnValue(); mg.EndMethod(); } if (constructors.Length == 0) { // Implement "first" default constructor ConstructorInstance c_method = new ConstructorInstance(typeof(Object).GetConstructor(null)); IMethodVisitor ga = VisitMethod(c_method); ga.LoadThis(); ga.InvokeConstructor(c_method); ga.ReturnValue(); ga.EndMethod(); } } base.VisitEnd(); }
public override void VisitEnd() { CompositeIdEnhancementHint context = State.GetContext <CompositeIdEnhancementHint>(); Member[] idMembers = context.IdMembers; CompositeIdMixin compositeIdTemplate = State.BeanContext.GetService <CompositeIdMixin>(); PropertyInstance p_compositeIdTemplate = ImplementAssignedReadonlyProperty("CompositeIdTemplate", compositeIdTemplate); Type[] constructorTypes = new Type[idMembers.Length]; FieldInstance[] fields = new FieldInstance[idMembers.Length]; // order does matter here (to maintain field order for debugging purpose on later objects) for (int a = 0, size = idMembers.Length; a < size; a++) { Member member = idMembers[a]; String fieldName = CompositeIdMember.FilterEmbeddedFieldName(member.Name); constructorTypes[a] = member.RealType; fields[a] = new FieldInstance(FieldAttributes.Public | FieldAttributes.InitOnly, fieldName, constructorTypes[a]); ImplementField(fields[a], delegate(IFieldVisitor fv) { fv.VisitAnnotation(typeof(PropertyAttribute).GetConstructor(Type.EmptyTypes)); }); } { IMethodVisitor mg = VisitMethod(new ConstructorInstance(MethodAttributes.Public, constructorTypes)); mg.LoadThis(); mg.InvokeOnExactOwner(new ConstructorInstance(typeof(Object).GetConstructor(null))); // order does matter here for (int index = 0, size = fields.Length; index < size; index++) { mg.PutThisField(fields[index], delegate(IMethodVisitor mg2) { mg2.LoadArg(index); } ); } mg.ReturnValue(); mg.EndMethod(); } PropertyInstance p_idMembers = ImplementAssignedReadonlyProperty("IdMembers", new CompositeIdValueResolveDelegate(fields)); { // Implement boolean Object.equals(Object) IMethodVisitor mg = VisitMethod(new MethodInstance(null, typeof(Object), typeof(bool), "Equals", typeof(Object))); // public static boolean CompositeIdCreator.equalsCompositeId(FieldAccess fa, int[] fieldIndexOfMembers, Object left, Object right) ImplementDefaultDelegatingMethod(mg, p_compositeIdTemplate, p_idMembers, m_equalsCompositeId); } { // Implement int Object.hashCode() IMethodVisitor mg = VisitMethod(new MethodInstance(null, typeof(Object), typeof(int), "GetHashCode")); // public static int CompositeIdCreator.hashCodeCompositeId(FieldAccess fa, int[] fieldIndexOfMembers, Object compositeId) ImplementDefaultDelegatingMethod(mg, p_compositeIdTemplate, p_idMembers, m_hashCodeCompositeId); } { // Implement String Object.toString() IMethodVisitor mg = VisitMethod(new MethodInstance(null, typeof(Object), typeof(String), "ToString")); // public static int CompositeIdCreator.toStringCompositeId(FieldAccess fa, int[] fieldIndexOfMembers, Object compositeId) ImplementDefaultDelegatingMethod(mg, p_compositeIdTemplate, p_idMembers, m_toStringCompositeId); } { // Implement void IPrintable.toString(StringBuilder) IMethodVisitor mg = VisitMethod(new MethodInstance(null, typeof(IPrintable), typeof(void), "ToString", typeof(StringBuilder))); // public static int CompositeIdCreator.toStringCompositeId(FieldAccess fa, int[] fieldIndexOfMembers, Object compositeId) ImplementDefaultDelegatingMethod(mg, p_compositeIdTemplate, p_idMembers, m_toStringSbCompositeId); } base.VisitEnd(); }
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(); }
protected void ImplementPropertyChangeOnProperty(PropertyInstance propertyInfo, PropertyInstance p_propertyChangeTemplate, MethodInstance m_firePropertyChange, FieldInstance f_propertyChangeSupport) { // add property change detection and notification if (propertyInfo.Getter == null || propertyInfo.Setter == null) { return; } if (InitializeEmbeddedMemberVisitor.IsEmbeddedMember(metaData, propertyInfo.Name)) { return; } PropertyInstance p_getterMethodHandle = ImplementAssignedReadonlyProperty(propertyInfo.Name + "$MethodHandle", new MethodHandleValueResolver(PropertyInfoProvider, propertyInfo.Name)); Type propertyType = propertyInfo.PropertyType.Type; MethodInstance m_hasPropertyChanged = GetApplicableHasPropertyChangedOverload(propertyType); // check value type of last parameter bool isBoxingNeededForHasPropertyChanged = IsBoxingNeededForHasPropertyChangedOverload(m_hasPropertyChanged, propertyType); IMethodVisitor mg = VisitMethod(propertyInfo.Setter); Label l_finish = mg.NewLabel(); Label l_noOldValue = mg.NewLabel(); Label l_noChangeCheck = mg.NewLabel(); LocalVariableInfo loc_oldValue; if (isBoxingNeededForHasPropertyChanged) { loc_oldValue = mg.NewLocal(typeof(Object)); } else { loc_oldValue = mg.NewLocal(propertyType); } LocalVariableInfo loc_valueChanged = mg.NewLocal <bool>(); MethodInstance m_getSuper = EnhancerUtil.GetSuperGetter(propertyInfo); bool relationProperty = m_getSuper.Name.EndsWith(ValueHolderIEC.GetNoInitSuffix()); // initialize flag with false mg.Push(false); mg.StoreLocal(loc_valueChanged); // initialize oldValue with null mg.PushNullOrZero(loc_oldValue.LocalType); mg.StoreLocal(loc_oldValue); if (relationProperty) { // check if a setter call to an UNINITIALIZED relation occured with value null // if it the case there would be no PCE because oldValue & newValue are both null // but we need a PCE in this special case Label l_noSpecialHandling = mg.NewLabel(); FieldInstance f_state = State.GetAlreadyImplementedField(ValueHolderIEC.GetInitializedFieldName(propertyInfo.Name)); mg.GetThisField(f_state); mg.PushEnum(ValueHolderState.INIT); mg.IfCmp(typeof(ValueHolderState), CompareOperator.EQ, l_noSpecialHandling); mg.Push(true); mg.StoreLocal(loc_valueChanged); mg.Mark(l_noSpecialHandling); } // check if value should be checked to decide for a PCE mg.LoadLocal(loc_valueChanged); mg.IfZCmp(CompareOperator.NE, l_noOldValue); // get old field value calling super property getter mg.LoadThis(); mg.InvokeOnExactOwner(m_getSuper); if (isBoxingNeededForHasPropertyChanged) { mg.Box(propertyType); } mg.StoreLocal(loc_oldValue); mg.Mark(l_noOldValue); // set new field value calling super property setter mg.LoadThis(); mg.LoadArg(0); mg.InvokeOnExactOwner(EnhancerUtil.GetSuperSetter(propertyInfo)); mg.PopIfReturnValue(EnhancerUtil.GetSuperSetter(propertyInfo)); // check if value should be checked to decide for a PCE mg.LoadLocal(loc_valueChanged); mg.IfZCmp(CompareOperator.NE, l_noChangeCheck); LocalVariableInfo loc_newValue = null; if (isBoxingNeededForHasPropertyChanged) { loc_newValue = mg.NewLocal(typeof(Object)); // loc_1 Object newValue // Object loc_1 = (Object)value; mg.LoadArg(0); mg.Box(propertyType); mg.StoreLocal(loc_newValue); } mg.CallThisGetter(p_propertyChangeTemplate); // call HasPropertyChanged (static) mg.LoadThis(); // "this" as Object obj mg.Push(propertyInfo.Name); // String propertyName mg.LoadLocal(loc_oldValue); if (loc_newValue != null) { mg.LoadLocal(loc_newValue); } else { mg.LoadArg(0); } mg.InvokeVirtual(m_hasPropertyChanged); //// if (!result) //// { return; } mg.IfZCmp(CompareOperator.EQ, l_finish); mg.Mark(l_noChangeCheck); // call firePropertyChange on this mg.LoadThis(); // propertyChangeSupport mg.GetThisField(f_propertyChangeSupport); // property mg.CallThisGetter(p_getterMethodHandle); // oldValue mg.LoadLocal(loc_oldValue); if (!isBoxingNeededForHasPropertyChanged && propertyType.IsValueType) { // old value has not already been boxed but it is now necessary mg.ValueOf(propertyType); } // newValue if (loc_newValue != null) { mg.LoadLocal(loc_newValue); } else { mg.LoadArg(0); if (propertyType.IsValueType) { mg.ValueOf(propertyType); } } // firePropertyChange(propertyChangeSupport, property, oldValue, newValue) mg.InvokeVirtual(m_firePropertyChange); // return mg.Mark(l_finish); mg.ReturnVoidOrThis(); mg.EndMethod(); }
protected PropertyInstance ImplementNotifyPropertyChangedSource(PropertyInstance p_propertyChangeTemplate, FieldInstance f_propertyChangeSupport) { MethodInstance m_onPropertyChanged_Values = MethodInstance.FindByTemplate(template_m_onPropertyChanged_Values, true); if (m_onPropertyChanged_Values == null) { IMethodVisitor mv = VisitMethod(template_m_onPropertyChanged_Values); mv.CallThisGetter(p_propertyChangeTemplate); mv.LoadThis(); mv.GetThisField(f_propertyChangeSupport); // getMethodHandle(sender, propertyName) mv.CallThisGetter(p_propertyChangeTemplate); mv.LoadThis(); mv.LoadArg(0); mv.InvokeVirtual(m_getMethodHandle); mv.LoadArg(1); mv.LoadArg(2); // firePropertyChange(sender, propertyChangeSupport, property, oldValue, newValue) mv.InvokeVirtual(m_firePropertyChange); mv.PopIfReturnValue(m_firePropertyChange); mv.ReturnVoidOrThis(); mv.EndMethod(); m_onPropertyChanged_Values = mv.Method; } MethodInstance m_onPropertyChanged = MethodInstance.FindByTemplate(template_m_onPropertyChanged, true); if (m_onPropertyChanged == null) { IMethodVisitor mv = VisitMethod(template_m_onPropertyChanged); mv.LoadThis(); mv.LoadArg(0); mv.PushNull(); mv.PushNull(); mv.InvokeVirtual(m_onPropertyChanged_Values); mv.PopIfReturnValue(m_onPropertyChanged_Values); mv.ReturnVoidOrThis(); mv.EndMethod(); m_onPropertyChanged = mv.Method; } PropertyInstance p_pceHandlers = PropertyInstance.FindByTemplate(p_propertyChangeSupport, true); if (p_pceHandlers == null) { HideFromDebug(ImplementGetter(p_propertyChangeSupport.Getter, f_propertyChangeSupport)); p_pceHandlers = PropertyInstance.FindByTemplate(p_propertyChangeSupport, false); } if (EmbeddedEnhancementHint.HasMemberPath(State.Context)) { PropertyInstance p_parentEntity = EmbeddedTypeVisitor.GetParentObjectProperty(this); if (MethodInstance.FindByTemplate(p_parentChildEventHandler.Getter, true) == null) { IMethodVisitor mv = VisitMethod(p_parentChildEventHandler.Getter); mv.CallThisGetter(p_parentEntity); mv.InvokeInterface(p_parentChildEventHandler.Getter); mv.ReturnValue(); mv.EndMethod(); HideFromDebug(mv.Method); } if (MethodInstance.FindByTemplate(p_collectionEventHandler.Getter, true) == null) { IMethodVisitor mv = VisitMethod(p_collectionEventHandler.Getter); mv.CallThisGetter(p_parentEntity); mv.InvokeInterface(p_collectionEventHandler.Getter); mv.ReturnValue(); mv.EndMethod(); HideFromDebug(mv.Method); } } else { if (MethodInstance.FindByTemplate(p_parentChildEventHandler.Getter, true) == null) { HideFromDebug(ImplementLazyInitProperty(p_parentChildEventHandler, delegate(IMethodVisitor mv) { MethodInstance method = new MethodInstance(null, typeof(NotifyPropertyChangedClassVisitor), typeof(PropertyChangedEventHandler), "CreateParentChildEventHandler", typeof(Object)); mv.LoadThis(); mv.InvokeStatic(method); })); } if (MethodInstance.FindByTemplate(p_collectionEventHandler.Getter, true) == null) { HideFromDebug(ImplementLazyInitProperty(p_collectionEventHandler, delegate(IMethodVisitor mv) { MethodInstance method = new MethodInstance(null, typeof(NotifyPropertyChangedClassVisitor), typeof(NotifyCollectionChangedEventHandler), "CreateCollectionEventHandler", typeof(Object)); mv.LoadThis(); mv.InvokeStatic(method); })); } } //MethodAttributes ma = MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.HideBySig; //{ // ConstructorInfo pceaCI = typeof(PropertyChangedEventArgs).GetConstructor(new Type[] { typeof(String) }); // MethodBuilder mb = VisitorUtil.DefineMethod(vs, onPropertyChangedMI_string, ma); // ILGenerator gen = mb.GetILGenerator(); // gen.Emit(OpCodes.Ldarg_0); // gen.Emit(OpCodes.Ldarg_1); // gen.Emit(OpCodes.Newobj, pceaCI); // gen.Emit(OpCodes.Call, onPropertyChangedMI_pceArg); // gen.Emit(OpCodes.Ret); //} //{ // MethodBuilder mb = VisitorUtil.DefineMethod(vs, onPropertyChangedMI_pceArg, ma); // ILGenerator gen = mb.GetILGenerator(); // gen.Emit(OpCodes.Ldarg_0); // gen.Emit(OpCodes.Call, pctPI.GetGetMethod()); // gen.Emit(OpCodes.Ldarg_0); // gen.Emit(OpCodes.Ldarg_1); // gen.Emit(OpCodes.Call, FirePropertyChangedMI); // gen.Emit(OpCodes.Ret); //} // List<PropertyChangedEventHandler> PropertyChangedEventHandlers { get; } //void OnPropertyChanged(String propertyName); //void OnPropertyChanged(PropertyChangedEventArgs args); return(p_pceHandlers); }
protected void ImplementConstructors() { if (metaData.RelationMembers.Length == 0) { return; } RelationMember[] relationMembers = metaData.RelationMembers; List <FieldInstance[]> fieldsList = new List <FieldInstance[]>(); for (int a = relationMembers.Length; a-- > 0;) { RelationMember relationMember = relationMembers[a]; relationMember = (RelationMember)GetApplicableMember(relationMember); if (relationMember == null) { // member is handled in another type continue; } String propertyName = relationMember.Name; String fieldName = ValueHolderIEC.GetObjRefsFieldName(propertyName); FieldInstance field = State.GetAlreadyImplementedField(fieldName); String fieldName2 = ValueHolderIEC.GetInitializedFieldName(propertyName); FieldInstance field2 = State.GetAlreadyImplementedField(fieldName2); fieldsList.Add(new FieldInstance[] { field, field2 }); } if (fieldsList.Count == 0) { return; } PropertyInstance p_emptyRelations = ImplementAssignedReadonlyProperty("EmptyRelations", ObjRef.EMPTY_ARRAY); OverrideConstructors(delegate(IClassVisitor cv, ConstructorInstance superConstructor) { IMethodVisitor mv = cv.VisitMethod(superConstructor); mv.LoadThis(); mv.LoadArgs(); mv.InvokeSuperOfCurrentMethod(); LocalVariableInfo loc_emptyRelations = mv.NewLocal <IObjRef[]>(); LocalVariableInfo loc_lazyState = mv.NewLocal <ValueHolderState>(); mv.CallThisGetter(p_emptyRelations); mv.StoreLocal(loc_emptyRelations); mv.PushEnum(ValueHolderState.LAZY); mv.StoreLocal(loc_lazyState); foreach (FieldInstance[] fields in fieldsList) { mv.PutThisField(fields[0], delegate(IMethodVisitor mv2) { mv2.LoadLocal(loc_emptyRelations); }); mv.PutThisField(fields[1], delegate(IMethodVisitor mv2) { mv2.LoadLocal(loc_lazyState); }); } mv.ReturnValue(); mv.EndMethod(); }); }
protected void ImplementRelationGetter(String propertyName, MethodInstance m_getMethod_template, MethodInstance m_setMethod, int relationIndex, PropertyInstance p_valueHolderContainerTemplate, PropertyInstance p_targetCache, PropertyInstance p_relationMembers, FieldInstance f_initialized, FieldInstance f_objRefs) { // public String getPropertyName() // { // if (!PropertyName$initialized) // { // setPropertyName(RelationsGetterVisitor.valueHolderContainer_getValue(this, $relationMembers, get__IndexOfPropertyName(), $targetCache, $beanContext, // propertyName$objRefs)); // } // return super.getPropertyName(); // } Script script_getVHC; if (EmbeddedEnhancementHint.HasMemberPath(State.Context)) { PropertyInstance p_rootEntity = EmbeddedTypeVisitor.GetRootEntityProperty(this); script_getVHC = delegate(IMethodVisitor mv) { mv.CallThisGetter(p_rootEntity); }; } else { script_getVHC = delegate(IMethodVisitor mv) { // this mv.LoadThis(); }; } MethodInstance m_getMethod; { PropertyInstance p_cacheModification = SetCacheModificationMethodCreator.GetCacheModificationPI(this); MethodInstance m_getMethod_scoped = new MethodInstance(State.NewType, MethodAttributes.HideBySig | MethodAttributes.Private | MethodAttributes.Final, NewType.VOID_TYPE, propertyName + "$DoInitialize"); { IMethodVisitor mg = VisitMethod(m_getMethod_scoped); // this => for this.setPropertyName(...) mg.LoadThis(); // call template.getValue(..) mg.CallThisGetter(p_valueHolderContainerTemplate); // getVhc() script_getVHC.Invoke(mg); // $relationMembers mg.CallThisGetter(p_relationMembers); // get__IndexOfPropertyName() mg.Push(relationIndex); // $targetCache mg.CallThisGetter(p_targetCache); // propertyName$objRefs mg.GetThisField(f_objRefs); mg.InvokeVirtual(m_template_getValue); mg.CheckCast(m_setMethod.Parameters[0].Type); mg.InvokeVirtual(m_setMethod); mg.ReturnValue(); mg.EndMethod(); } { IMethodVisitor mg = base.VisitMethod(m_getMethod_template); m_getMethod = mg.Method; HideFromDebug(m_getMethod); Label l_initialized = mg.NewLabel(); mg.GetThisField(f_initialized); mg.PushEnum(ValueHolderState.INIT); mg.IfCmp(typeof(ValueHolderState), CompareOperator.EQ, l_initialized); SetCacheModificationMethodCreator.CacheModificationInternalUpdate(p_cacheModification, mg, delegate(IMethodVisitor mv2) { mv2.LoadThis(); mv2.InvokeOnExactOwner(m_getMethod_scoped); }); mg.Mark(l_initialized); mg.LoadThis(); mg.InvokeSuperOfCurrentMethod(); mg.ReturnValue(); mg.EndMethod(); } } // public String getPropertyName$NoInit() // { // return super.getPropertyName(); // } { MethodInstance m_getNoInit = m_getMethod_template.DeriveName(ValueHolderIEC.GetGetterNameOfRelationPropertyWithNoInit(propertyName)); IMethodVisitor mg = base.VisitMethod(m_getNoInit); PropertyInstance p_getNoInit = PropertyInstance.FindByTemplate(propertyName + ValueHolderIEC.GetNoInitSuffix(), m_getNoInit.ReturnType, false); p_getNoInit.AddAnnotation(c_fireThisOPC, propertyName); p_getNoInit.AddAnnotation(c_fireTargetOPC, propertyName); mg.LoadThis(); mg.InvokeSuper(m_getMethod); mg.ReturnValue(); mg.EndMethod(); } }