// In the following three methods is possible to factorize the code // Check this!!! public bool method_call_3(Term objId, Term method_name, Term idResult) { Struct objIdTerm = (Struct)objId.getTerm(); Struct methodNameStruct = (Struct)method_name.getTerm(); string methodName = methodNameStruct.getName(); // check for accessing field or property Obj.Field <- set/get(X) // in that case: objId is '.'(Obj, Field) if (!objIdTerm.isAtom()) { if (objIdTerm is Var) { return(false); } Struct objIdStruct = (Struct)objIdTerm; if (objIdStruct.getName().Equals(".") && objIdStruct.getArity() == 2 && methodNameStruct.getArity() == 1) { if (methodName.Equals("set")) { return(oo_set(objIdStruct.getTerm(0), objIdStruct.getTerm(1), methodNameStruct.getTerm(0))); } else if (methodName.Equals("get")) { return(oo_get(objIdStruct.getTerm(0), objIdStruct.getTerm(1), methodNameStruct.getTerm(0))); } } } // Object must be instantiated if (objIdTerm is Var) { return(false); } // Retrieving object instance object obj = this.getRegisteredDynamicObject((Struct)objIdTerm); if (obj != null) { // Instance method Convention conv = FindConvention(obj.GetType()); if (conv != null) { // Convention found string revisedMethodName = conv.GetMemberName(methodName); Struct methodStruct = new Struct(revisedMethodName, getArrayFromMethod(methodNameStruct)); return(base.java_call_3(objId, methodStruct, idResult)); } else { // Convention NOT found return(base.java_call_3(objId, method_name, idResult)); } } else { // Object not found: could be a static call if (objId.isCompound()) { if (objIdTerm.getArity() == 1 && objIdTerm.getName().Equals("class")) { // Static method string className = RemoveApices(objIdTerm.getArg(0).ToString()); Convention conv = FindConventionByClassName(className); if (conv != null) { // Convention found string revisedClassName = conv.GetClassName(className); string revisedMethodName = conv.GetMemberName(methodName); Struct classNameStruct = new Struct(revisedClassName); Struct classStruct = new Struct("class", classNameStruct); Var classVar = new Var(); this.unify(classVar, classStruct); Struct staticMethod = new Struct(revisedMethodName, getArrayFromMethod(methodNameStruct)); Var staticMethodVar = new Var(); this.unify(staticMethodVar, staticMethod); return(base.java_call_3(classVar, staticMethodVar, idResult)); } else { // Convention NOT found return(base.java_call_3(objId, method_name, idResult)); } } } } return(false); }
private bool oo_get(Term objId, Term memberTerm, Term what) { what = what.getTerm(); // If memberTerm is not an atom and what is a not instantiated variable if (!memberTerm.isAtom()) { return(false); } string memberName = ((Struct)memberTerm).getName(); // Object must be instantiated if (objId is Var) { return(false); } // Retrieving object instance object obj = this.getRegisteredDynamicObject((Struct)objId); if (obj != null) { // Instance property or field Convention conv = FindConvention(obj.GetType()); if (conv != null) { // Convention found string revisedFieldName = conv.GetFieldName(memberName); string revisedPropertyMethod = conv.GetPropertyGetterMethod(memberName); FieldInfo field = obj.GetType().GetField(revisedFieldName); MethodInfo method = obj.GetType().GetMethod(revisedPropertyMethod); if (field != null && method == null) { // Field Struct filedStruct = new Struct(revisedFieldName); Struct objStruct = new Struct(".", objId, filedStruct); Var objVar = new Var(); this.unify(objVar, objStruct); Struct methodStruct = new Struct("get", what); Var methodVar = new Var(); this.unify(methodVar, methodStruct); return(base.java_call_3(objVar, methodVar, new Struct())); } else if (field == null && method != null) { // Property Struct methodStruct = new Struct(revisedPropertyMethod); return(base.java_call_3(objId, methodStruct, what)); } } else { // Convention NOT found FieldInfo field = obj.GetType().GetField(memberName); MethodInfo method = obj.GetType().GetMethod(memberName); if (field != null && method == null) { // Field Struct fieldStruct = new Struct(memberName); Struct objStruct = new Struct(".", objId, fieldStruct); Var objVar = new Var(); this.unify(objVar, objStruct); Struct methodStruct = new Struct("get", what); Var methodVar = new Var(); this.unify(methodVar, methodStruct); return(base.java_call_3(objVar, methodVar, new Struct())); } else if (field == null && method != null) { // Property Struct methodStruct = new Struct(memberName); return(base.java_call_3(objId, methodStruct, what)); } } } else { // Could be a static invocation if (objId.isCompound()) { Struct objIdStruct = (Struct)objId; if (objIdStruct.getArity() == 1 && objIdStruct.getName().Equals("class")) { // Static property or field string className = RemoveApices(objIdStruct.getArg(0).ToString()); Convention conv = FindConventionByClassName(className); if (conv != null) { // Convention found string revisedClassname = conv.GetClassName(className); Type classType = Type.GetType(revisedClassname); string revisedFieldName = conv.GetFieldName(memberName); string revisedPropertyMethod = conv.GetPropertyGetterMethod(memberName); FieldInfo field = classType.GetField(revisedFieldName); MethodInfo method = classType.GetMethod(revisedPropertyMethod); if (field != null && method == null) { // Field Struct fieldStruct = new Struct(revisedFieldName); Struct classNameStruct = new Struct(revisedClassname); Struct classStruct = new Struct("class", classNameStruct); Struct st = new Struct(".", classStruct, fieldStruct); Var classVar = new Var(); this.unify(classVar, st); Struct methodStruct = new Struct("get", what); Var methodVar = new Var(); this.unify(methodVar, methodStruct); return(base.java_call_3(classVar, methodVar, new Struct())); } else if (field == null && method != null) { // Property Struct classNameStruct = new Struct(revisedClassname); Struct classStruct = new Struct("class", classNameStruct); Var classVar = new Var(); this.unify(classVar, classStruct); Struct staticMethod = new Struct(revisedPropertyMethod); Var staticMethodVar = new Var(); this.unify(staticMethodVar, staticMethod); return(base.java_call_3(classVar, staticMethodVar, what)); } } else { // Convention NOT found Type classType = Type.GetType(className); FieldInfo field = classType.GetField(memberName); MethodInfo method = classType.GetMethod(memberName); if (field != null && method == null) { // Field Struct fieldStruct = new Struct(memberName); Struct classNameStruct = new Struct(className); Struct classStruct = new Struct("class", classNameStruct); Struct st = new Struct(".", classStruct, fieldStruct); Var classVar = new Var(); this.unify(classVar, st); Struct methodStruct = new Struct("get", what); Var methodVar = new Var(); this.unify(methodVar, methodStruct); return(base.java_call_3(classVar, methodVar, new Struct())); } else if (field == null && method != null) { // Property Struct classNameStruct = new Struct(className); Struct classStruct = new Struct("class", classNameStruct); Var classVar = new Var(); this.unify(classVar, classStruct); Struct staticMethod = new Struct(memberName); Var staticMethodVar = new Var(); this.unify(staticMethodVar, staticMethod); return(base.java_call_3(classVar, staticMethodVar, what)); } } } } } return(false); }
/// <summary> /// Creates of a .NET object from an assembly - not backtrackable case /// </summary> /// <param name="conventionName">Name of the convention to bind with the object</param> /// <param name="className">Name of the class with namespace informations</param> /// <param name="constructorName">Name of the constructor used to instantiate the class</param> /// <param name="argl">List of arguments for the constructor</param> /// <param name="id">Reference to the instantiated object (out)</param> /// <returns>True if the operation is successful</returns> public bool new_object_5(Term conventionName, Term className, Term constructorName, Term argl, Term id) { className = className.getTerm(); Struct arg = (Struct)argl.getTerm(); id = id.getTerm(); // Class name must be a simple atom if (!className.isAtom()) { return(false); } string clName = ((Struct)className).getName(); // Retrieving Convention name if present string convName = null; if (conventionName != null && (conventionName.getTerm() is Struct)) { convName = ((Struct)conventionName.getTerm()).getName(); } // Retrieving Constructor name if present string constrName = null; if (constructorName != null && (constructorName.getTerm() is Struct)) { constrName = ((Struct)constructorName.getTerm()).getName(); } Convention conv = FindConvention(convName); if (conv != null) { // Convention found Struct revisedClassNameStruct = null; string revisedClassName = conv.GetClassName(clName); revisedClassNameStruct = new Struct(revisedClassName); if (conv.IsArrayClass(revisedClassName)) { // Construction of an array string arrayClassName = revisedClassName.Substring(0, revisedClassName.Length - 2); arrayClassName = arrayClassName + "[]"; revisedClassNameStruct = new Struct(arrayClassName); } if (constrName != null && !constrName.Equals(String.Empty)) { // If it is specified the static method that build the object, I have to call it string revisedConstrName = conv.GetConstructorName(constrName); string constrClassName = conv.GetConstructorClass(revisedClassName); Struct constrNameStruct = new Struct(constrClassName); Struct constr = new Struct("class", constrNameStruct); Var constrClass = new Var(); this.unify(constrClass, constr); Struct constrMethod = new Struct(revisedConstrName, getArrayFromList(arg)); Var constrMethodName = new Var(); this.unify(constrMethodName, constrMethod); return(base.java_call_3(constrClass, constrMethodName, id)); } // Call the super class method after the modification of the names bool returnValue = base.java_object_3(revisedClassNameStruct, argl, id); // If everything went fine, the type has been loaded correctly by the JavaLibrary class loader // therefore we can bind the type to the convention if (returnValue) { Type classType = Type.GetType(revisedClassName); if (!IsBoundToConvention(classType)) { // The class is not link to the convention: link it! BindClassToConvention(classType, conv); } } return(returnValue); } else { // Convention NOT found // For arrays nothing to do: I guess that the user used [] if (constrName != null && !constrName.Equals(String.Empty)) { // If it is specified the static method that build the object, I have to call it // but this time without convention Struct constrNameStruct = new Struct(clName); Struct constr = new Struct("class", constrNameStruct); Var constrClass = new Var(); this.unify(constrClass, constr); Struct constrMethod = new Struct(constrName, getArrayFromList(arg)); Var constrMethodName = new Var(); this.unify(constrMethodName, constrMethod); return(base.java_call_3(constrClass, constrMethodName, id)); } } // No modifications return(base.java_object_3(className, argl, id)); }