private CodeMethodInvokeExpression GenerateInvokeExpressionC2J(GMethod method, string uName, bool fieldSetter) { CodeExpression[] expressions = GetExpressionsC2J(method, uName, fieldSetter); string callName = GetCallNameC2J(method, fieldSetter); return(new CodeMethodInvokeExpression(envVariable, callName, expressions)); }
protected CodeMemberMethod CreateMethodSignature(GMethod method, bool skipSignature) { CodeMemberMethod tgtMethod; if (method.IsConstructor) { tgtMethod = new CodeConstructor(); } else { tgtMethod = new CodeMemberMethod(); tgtMethod.Name = method.JVMName; tgtMethod.ReturnType = method.ReturnType.JVMReference; } if (!config.SkipSignatures && !skipSignature) { if (method.IsConstructor) { Utils.AddAttribute(tgtMethod, "net.sf.jni4net.attributes.ClrConstructor", method.CLRSignature); } else { Utils.AddAttribute(tgtMethod, "net.sf.jni4net.attributes.ClrMethod", method.CLRSignature); } } tgtMethod.Attributes = method.Attributes; GenerateParameters(method, tgtMethod); return(tgtMethod); }
private void GenerateGetEnvC2J(GMethod method, CodeStatementCollection tgtStatements) { if (method.IsStatic || method.IsConstructor) { CodeStatement statement = new CodeVariableDeclarationStatement( new CodeTypeReference(typeof(JNIEnv), CodeTypeReferenceOptions.GlobalReference), envVariableName, new CodePropertyReferenceExpression(TypeReferenceEx(typeof(JNIEnv)), "ThreadEnv")); tgtStatements.Add(statement); } else { CodeStatement statement = new CodeVariableDeclarationStatement( new CodeTypeReference(typeof(JNIEnv), CodeTypeReferenceOptions.GlobalReference), envVariableName, new CodePropertyReferenceExpression (new CodeThisReferenceExpression (), "Env")); tgtStatements.Add(statement); } int framesize = (10 + method.Parameters.Count * 2); tgtStatements.Add(new CodeSnippetStatement(" using(new global::net.sf.jni4net.jni.LocalFrame(@__env, " + framesize + ")){")); }
private string GetCallNameC2J(GMethod method, bool fieldSetter) { var callName = new StringBuilder(); if (method.IsConstructor) { callName.Append("NewObject"); } else { if (method.ReturnType != null && method.ReturnType.IsPrimitive) { callName.Append(method.ReturnType.JVMResolved); callName[0] = Char.ToUpper(callName[0]); } else { callName.Append("Object"); } if (method.IsStatic) { callName.Insert(0, "Static"); } callName.Insert(0, method.IsField ? (fieldSetter ? "Set" : "Get") : "Call"); callName.Append(method.IsField ? "Field" : "Method"); if (method.ReturnType != null && !method.ReturnType.IsPrimitive) { callName.Append("Ptr"); } } return(callName.ToString()); }
private CodeStatement GenerateCallStatementC2J(GMethod method, CodeExpression invokeExpression, bool fieldSetter) { CodeStatement call; if (method.IsConstructor || method.IsVoid || fieldSetter) { call = new CodeExpressionStatement(invokeExpression); } else { if (method.ReturnType.IsPrimitive) { if (method.ReturnType.JVMSubst != null) { invokeExpression = new CodeCastExpression(method.ReturnType.CLRReference, invokeExpression); } call = new CodeMethodReturnStatement(invokeExpression); } else { CodeMethodInvokeExpression conversionExpression = CreateConversionExpressionJ2CParam(method.ReturnType, invokeExpression); call = new CodeMethodReturnStatement(conversionExpression); } } return(call); }
private List <CodeExpression> GenerateCallParamsJ2C(GMethod method) { var callParams = new List <CodeExpression>(); for (int p = 0; p < method.Parameters.Count; p++) { GType paramType = method.Parameters[p]; var invokeExpression = new CodeVariableReferenceExpression(method.ParameterNames[p]); if (paramType.IsOut) { var outExpression = new CodeSnippetExpression("out __out_" + method.ParameterNames[p]); callParams.Add(outExpression); } else if (paramType.IsRef) { var outExpression = new CodeSnippetExpression("ref __ref_" + method.ParameterNames[p]); callParams.Add(outExpression); } else if (paramType.IsPrimitive) { callParams.Add(invokeExpression); } else { CodeMethodInvokeExpression conversionExpression = CreateConversionExpressionJ2CParam(paramType, invokeExpression); callParams.Add(conversionExpression); } } return(callParams); }
private CodeExpression[] GetExpressionsC2J(GMethod method, string uName) { int offset = method.IsConstructor ? 3 : 2; var expressions = new CodeExpression[method.Parameters.Count + offset]; if (method.IsStatic || method.IsConstructor) { expressions[0] = new CodeFieldReferenceExpression(ProxyTypeEx, "staticClass"); } else { expressions[0] = new CodeThisReferenceExpression(); } expressions[1] = new CodeFieldReferenceExpression(ProxyTypeEx, uName); if (method.IsConstructor) { expressions[2] = new CodeThisReferenceExpression(); } for (int i = 0; i < method.Parameters.Count; i++) { GType parameter = method.Parameters[i]; string paramName = method.ParameterNames[i]; CodeExpression invokeExpression = new CodeVariableReferenceExpression(paramName); CodeMethodInvokeExpression conversionExpression = CreateConversionExpressionC2JParam(parameter, invokeExpression); expressions[i + offset] = conversionExpression; } return(expressions); }
protected void GenerateParameters(GMethod method, CodeMemberMethod tgtMethod) { for (int i = 0; i < method.Parameters.Count; i++) { var tgtParameter = new CodeParameterDeclarationExpression(); tgtParameter.Name = method.ParameterNames[i]; tgtParameter.Type = method.Parameters[i].CLRReference; tgtMethod.Parameters.Add(tgtParameter); } }
private void CreateMethodC2J(GMethod method, CodeTypeDeclaration tgtType, string uName, bool isProxy, bool fieldSetter) { CodeStatementCollection tgtStatements = CreateMethodSignature(tgtType, method, isProxy, fieldSetter); GenerateGetEnvC2J(method, tgtStatements); CodeMethodInvokeExpression invokeExpression = GenerateInvokeExpressionC2J(method, uName, fieldSetter); CodeStatement call = GenerateCallStatementC2J(method, invokeExpression, fieldSetter); tgtStatements.Add(call); GenerateEndFrameC2J(tgtStatements); }
public static void GAssemblyGroupCommonFinalizer(IGAssemblyGroupBasicConstructorResult gAssemblyGroupBasicConstructorResult) { //#region Lookup the Base GAssemblyUnit, GCompilationUnit, GNamespace, GClass, and primary GConstructor, GCompilationUnit gCompilationUnitDerived //var titularBaseClassName = $"{GAssemblyGroup.GName}Base"; //var titularAssemblyUnitLookupPrimaryConstructorResults = LookupPrimaryConstructorMethod(new List<IGAssemblyGroup>(){GAssemblyGroup},gClassName:titularBaseClassName) ; //#endregion //#region Lookup the Derived GAssemblyUnit, GCompilationUnit, GNamespace, and GClass //var titularClassName = $"{GAssemblyGroup.GName}"; //var titularAssemblyUnitLookupDerivedClassResults = LookupDerivedClass(new List<IGAssemblyGroup>(){GAssemblyGroup},gClassName:titularClassName) ; //#endregion #region Create Derived Constructors for all public Base Constructors // Create a constructor in the Titular class for every public constructor in the Titular Base class var baseConstructorsList = new List <IGMethod>(); baseConstructorsList.AddRange(gAssemblyGroupBasicConstructorResult.GClassBase.CombinedConstructors()); foreach (var bc in baseConstructorsList) { var gConstructor = new GMethod(new GMethodDeclaration(gAssemblyGroupBasicConstructorResult.GClassDerived.GName, isConstructor: true, gVisibility: "public", gArguments: bc.GDeclaration.GArguments, gBase: bc.GDeclaration.GArguments.ToBaseString())); gAssemblyGroupBasicConstructorResult.GClassDerived.GMethods.Add(gConstructor.Id, gConstructor); } #endregion #region Constructor Groups // ToDo handle method groups, will require a change to CombinedConstructors #endregion #region Condense GUsings in the Base and Derived GCompilationUnits of the Titular Assembly #endregion #region Condense GItemGroups in the GProjectUnit of the Titular Assembly #endregion #region Finalize the Statemachine //( // IEnumerable<GAssemblyUnit> gAssemblyUnits, // IEnumerable<GCompilationUnit> gCompilationUnits, // IEnumerable<GNamespace> gNamespacess, // IEnumerable<GClass> gClasss, // IEnumerable<GMethod> gMethods) lookupResultsTuple = LookupPrimaryConstructorMethod(); //MStateMachineFinalizer( GClassBase); //MStateMachineFinalizer(GTitularBaseCompilationUnit, gNamespace, GClassBase, gConstructorBase, gStateConfigurations); //MStateMachineFinalizer( GClassBase, gConstructorBase, gStateConfigurations); MStateMachineFinalizer(gAssemblyGroupBasicConstructorResult); #endregion #region Populate Interfaces for Titular Derived and Base Class PopulateInterface(gAssemblyGroupBasicConstructorResult.GClassDerived, gAssemblyGroupBasicConstructorResult.GTitularInterfaceDerivedInterface); PopulateInterface(gAssemblyGroupBasicConstructorResult.GClassBase, gAssemblyGroupBasicConstructorResult.GTitularInterfaceBaseInterface); #endregion #region populate the Interfaces CompilationUnits for additional classes found in the Titular Derived and Base CompilationUnits #endregion #region Condense GUsings in the Base and Derived GCompilationUnits of the Titular Interfaces Assembly #endregion #region Condense GItemGroups in the GProjectUnit of the Titular Interfaces Assembly #endregion }
private void GenerateMethodIdFieldC2J(GMethod method, CodeTypeDeclaration tgtType, string uName) { CodeMemberField fieldId; if (method.IsField) { fieldId = new CodeMemberField(TypeReference(typeof(FieldId)), uName); } else { fieldId = new CodeMemberField(TypeReference(typeof(MethodId)), uName); } fieldId.Attributes = MemberAttributes.Static | MemberAttributes.FamilyAndAssembly; tgtType.Members.Add(fieldId); }
/*******************************************************************************/ /*******************************************************************************/ //static IGMethod MCreateWriteAsyncMethodInConsoleSink(string gAccessModifier = "") { // var gMethodArgumentList = new List<IGArgument>() { // new GArgument("mesg","string"), // new GArgument("ct","CancellationTokenFromCaller?") // }; // var gMethodArguments = new Dictionary<IPhilote<IGArgument>, IGArgument>(); // foreach (var o in gMethodArgumentList) { gMethodArguments.Add(o.Philote, o); } // return new GMethod( // new GMethodDeclaration(gName: "WriteAsync", gType: "Task", // gVisibility: "public", gAccessModifier: gAccessModifier + " async", isConstructor: false, // gArguments: gMethodArguments), // gBody: new GBody(gStatements: // new List<string>() { //"StateMachine.Fire(Trigger.WriteAsyncStarted);", //"ct?.ThrowIfCancellationRequested();", //"await Console.WriteAsync(mesg);", //"StateMachine.Fire(Trigger.WriteAsyncFinished);", //"return Task.CompletedTask;" // }), // new GComment(new List<string>() { //"// Used to asynchronously write a string to the WriteAsync method of the Console instance" // })); //} static IList <IGMethod> MCreateWriteMethodInConsoleSink(string gAccessModifier = "") { var gMethodArgumentList = new List <IGArgument>() { new GArgument("mesg", "string"), new GArgument("ct", "CancellationTokenFromCaller?") }; var gMethodArguments = new Dictionary <IPhilote <IGArgument>, IGArgument>(); foreach (var o in gMethodArgumentList) { gMethodArguments.Add(o.Philote, o); } var publicWriteMethod = new GMethod( new GMethodDeclaration(gName: "Write", gType: "void", gVisibility: "public", gAccessModifier: gAccessModifier, isConstructor: false, gArguments: gMethodArguments), gBody: new GBody(gStatements: new List <string>() { "ct?.ThrowIfCancellationRequested();", "Mesg=mesg;", "StateMachine.Fire(Trigger.WriteStarted);", }), new GComment(new List <string>() { "// Used to write a string to the Console instance" })); gMethodArguments = new Dictionary <IPhilote <IGArgument>, IGArgument>(); var privateWriteMethod = new GMethod( new GMethodDeclaration(gName: "Write", gType: "void", gVisibility: "private", gAccessModifier: gAccessModifier, isConstructor: false, gArguments: gMethodArguments), gBody: new GBody(gStatements: new List <string>() { "Console.Write(Mesg);", "StateMachine.Fire(Trigger.WriteFinished);", }), new GComment(new List <string>() { "// (private) Used to write a string to the Console instance" })); return(new List <IGMethod>() { publicWriteMethod, privateWriteMethod }); }
public static IGMethod ConvertMethodToInterfaceMethod(IGMethod gMethod) { GMethod gInterfaceMethod = default; if (!gMethod.GDeclaration.IsConstructor && gMethod.GDeclaration.GVisibility == "public") { var gAccessModifier = gMethod.GDeclaration.GAccessModifier; var accessModifierRegex = new Regex("(?:override|async|virtual)"); gAccessModifier = accessModifierRegex.Replace(gAccessModifier, ""); gInterfaceMethod = new GMethod( new GMethodDeclaration(gMethod.GDeclaration.GName, gMethod.GDeclaration.GType, "", gAccessModifier, gMethod.GDeclaration.IsStatic, false, gMethod.GDeclaration.GArguments, isForInterface: true), gComment: gMethod.GComment, isForInterface: true); } return(gInterfaceMethod); }
private void GenerateMethodRegC2J(GMethod method, string uName) { var claxs = new CodeFieldReferenceExpression(CurrentTypeEx, "staticClass"); string getmethodidthrow = method.IsField ? method.IsStatic ? "GetStaticFieldID" : "GetFieldID" : method.IsStatic ? "GetStaticMethodID" : "GetMethodID"; var initBody = new CodeAssignStatement( new CodeFieldReferenceExpression(CurrentTypeEx, uName), new CodeMethodInvokeExpression( envVariable, getmethodidthrow, claxs, new CodePrimitiveExpression(method.JVMName), new CodePrimitiveExpression(method.JVMSignature))); InitStatements.Add(initBody); }
private void GenerateMethodJ2C(GMethod method, CodeTypeDeclaration tgtType, string uName) { var tgtMethod = new CodeMemberMethod(); tgtMethod.Statements.Add(new CodeCommentStatement(method.JVMSignature)); tgtMethod.Statements.Add(new CodeCommentStatement(method.CLRSignature)); tgtMethod.Name = uName; tgtMethod.Attributes = MemberAttributes.Static | MemberAttributes.Private; GenerateMethodParamsJ2C(tgtMethod, method); GenerateGetEnvJ2C(tgtMethod); GenerateMethodCallPrologJ2C(tgtMethod, method); GenerateCallJ2C(tgtMethod, method); GenerateMethodCallEpilogJ2C(tgtMethod, method); tgtType.Members.Add(tgtMethod); }
private void GenerateMethodRegistrationJ2C(GMethod method, string uName) { string callbackName = method.IsConstructor ? uName : method.JVMName; string callBackSignature = method.IsConstructor ? method.JVMSignature.Replace("(", "(Lnet/sf/jni4net/inj/IClrProxy;") : method.JVMSignature; var registation = new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("methods"), "Add", new CodeMethodInvokeExpression( new CodeMethodReferenceExpression( TypeReferenceEx(typeof(JNINativeMethod)), "Create"), new CodeVariableReferenceExpression(typeVariableName), new CodePrimitiveExpression(callbackName), new CodePrimitiveExpression(uName), new CodePrimitiveExpression(callBackSignature))); initMethod.Statements.Add(registation); }
public static IGMethod MCreateStartAsyncMethod(string gAccessModifier = "") { var gMethodArguments = new Dictionary <IPhilote <IGArgument>, IGArgument>(); foreach (var o in new List <IGArgument>() { new GArgument("genericHostsCancellationToken", "CancellationTokenFromCaller"), }) { gMethodArguments.Add(o.Philote, o); } var gMethodDeclaration = new GMethodDeclaration(gName: "StartAsync", gType: "Task", gVisibility: "public", gAccessModifier: gAccessModifier, isConstructor: false, gArguments: gMethodArguments); var gBody = new GBody(gStatements: new List <string>() { "#region Create linkedCancellationSource and linkedCancellationToken", " //// Combine the cancellation tokens,so that either can stop this HostedService", " //linkedCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(internalCancellationToken, externalCancellationToken);", " //GenericHostsCancellationToken = genericHostsCancellationToken;", "#endregion", "#region Register actions with the CancellationTokenFromCaller (s)", " //GenericHostsCancellationToken.Register(() => Logger.LogDebug(DebugLocalizer[\"{0} {1} GenericHostsCancellationToken has signalled stopping.\"], \"FileSystemToObjectGraphService\", \"StartAsync\"));", " //internalCancellationToken.Register(() => Logger.LogDebug(DebugLocalizer[\"{0} {1} internalCancellationToken has signalled stopping.\"], \"FileSystemToObjectGraphService\", \"internalCancellationToken\"));", " //linkedCancellationToken.Register(() => Logger.LogDebug(DebugLocalizer[\"{0} {1} GenericHostsCancellationToken has signalled stopping.\"], \"FileSystemToObjectGraphService\",\"GenericHostsCancellationToken\"));", "#endregion", "#region Register local event handlers with the IHostApplicationLifetime's events", " // Register the methods defined in this class with the three CancellationTokenFromCaller properties found on the IHostApplicationLifetime instance passed to this class in it's .ctor", " HostApplicationLifetime.ApplicationStarted.Register(OnStarted);", " HostApplicationLifetime.ApplicationStopping.Register(OnStopping);", " HostApplicationLifetime.ApplicationStopped.Register(OnStopped);", "#endregion", // "DataInitializationInStartAsyncReplacementPattern", "//return Task.CompletedTask;" }); GComment gComment = new GComment(new List <string>() { }); GMethod newgMethod = new GMethod(gMethodDeclaration, gBody, gComment); return(newgMethod); }
/*private void GenerateMethods(CodeTypeDeclaration tgtType) * { * foreach (GMethod method in type.Methods) * { * CreateMethodSignature(tgtType, method, false); * } * }*/ private CodeMemberMethod CreateConstructorHelper(GMethod constructor, string uName) { var tgtMethod = new CodeMemberMethod(); tgtMethod.Name = uName; tgtMethod.Attributes = MemberAttributes.Static | MemberAttributes.Private | MemberAttributes.New; Utils.AddAttribute(tgtMethod, "net.sf.jni4net.attributes.ClrMethod", constructor.CLRSignature); // inject thiz parameter var tgtParameter = new CodeParameterDeclarationExpression(); tgtParameter.Name = "thiz"; tgtParameter.Type = constructor.Type.CLRReference; tgtMethod.Parameters.Add(tgtParameter); GenerateParameters(constructor, tgtMethod); return(tgtMethod); }
private void CreateConstructorBody(GMethod constructor, CodeConstructor con, string uName) { if (!type.IsRootType) { con.BaseConstructorArgs.Add( new CodeCastExpression(TypeReference("net.sf.jni4net.inj.INJEnv"), new CodePrimitiveExpression(null))); con.BaseConstructorArgs.Add(new CodePrimitiveExpression(0)); } var parameters = new CodeExpression[constructor.Parameters.Count + 1]; for (int p = 0; p < constructor.Parameters.Count; p++) { parameters[p + 1] = new CodeVariableReferenceExpression(constructor.ParameterNames[p]); } parameters[0] = new CodeThisReferenceExpression(); con.Statements.Add( new CodeMethodInvokeExpression(CurrentTypeEx, uName, parameters)); }
/*******************************************************************************/ /*******************************************************************************/ static IGClass MCreateStateConfigurationClass(string gVisibility = "public") { //var gMethodArgumentList = new List<IGArgument>() { // new GArgument("requestorPhilote", "object"), // new GArgument("callback", "object"), // new GArgument("timerSignil", "object"), // new GArgument("ct", "CancellationTokenFromCaller?") //}; //var gMethodArguments = new Dictionary<IPhilote<IGArgument>, IGArgument>(); //foreach (var o in gMethodArgumentList) { // gMethodArguments.Add(o.Philote, o); //} var gClass = new GClass("StateConfiguration", gVisibility: gVisibility); var gProperty = new GProperty("State", "State", "{get;}", "public"); gClass.GPropertys.Add(gProperty.Philote, gProperty); gProperty = new GProperty("Trigger", "Trigger", "{get;}", "public"); gClass.GPropertys.Add(gProperty.Philote, gProperty); gProperty = new GProperty("NextState", "State", "{get;}", "public"); gClass.GPropertys.Add(gProperty.Philote, gProperty); var gMethodArguments = new Dictionary <IPhilote <IGArgument>, IGArgument>(); foreach (var o in new List <IGArgument>() { new GArgument("state", "State"), new GArgument("trigger", "Trigger"), new GArgument("nextState", "State"), }) { gMethodArguments.Add(o.Philote, o); } var gMethodDeclaration = new GMethodDeclaration(gName: "StateConfiguration", gVisibility: "public", isConstructor: true, gArguments: gMethodArguments); var gBody = new GBody( gStatements: new List <string>() { "State=state;", "Trigger=trigger;", "NextState=nextState;", }); var gMethod = new GMethod(gMethodDeclaration, gBody); gClass.GMethods.Add(gMethod.Philote, gMethod); return(gClass); }
private void GenerateMethodCallEpilogJ2C(CodeMemberMethod tgtMethod, GMethod method) { for (int p = 0; p < method.Parameters.Count; p++) { GType parameter = method.Parameters[p]; string name = method.ParameterNames[p]; if (parameter.IsOut) { var parExpression = new CodeVariableReferenceExpression(name); var outExpression = new CodeVariableReferenceExpression("__out_" + name); CodeMethodInvokeExpression invokeExpression = new CodeMethodInvokeExpression( new CodeMethodReferenceExpression( new CodeTypeReferenceExpression(typeof(Out)), "SetValue", new[] { parameter.CLRReference }), envVariable, parExpression, outExpression); tgtMethod.Statements.Add(invokeExpression); } else if (parameter.IsRef) { var parExpression = new CodeVariableReferenceExpression(name); var outExpression = new CodeVariableReferenceExpression("__ref_" + name); CodeMethodInvokeExpression invokeExpression = new CodeMethodInvokeExpression( new CodeMethodReferenceExpression( new CodeTypeReferenceExpression(typeof(Ref)), "SetValue", new[] { parameter.CLRReference }), envVariable, parExpression, outExpression); tgtMethod.Statements.Add(invokeExpression); } } tgtMethod.Statements.Add( new CodeSnippetStatement( " }catch (global::System.Exception __ex){@" + envVariableName + ".ThrowExisting(__ex);}" //} )); if (!method.IsConstructor && !method.IsVoid) { tgtMethod.Statements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression("__return"))); } }
private void GenerateMethodCallPrologJ2C(CodeMemberMethod tgtMethod, GMethod method) { if (!method.IsConstructor && !method.IsVoid) { if (method.ReturnType.IsPrimitive) { tgtMethod.Statements.Add(new CodeVariableDeclarationStatement(method.ReturnType.CLRReference, "__return", new CodeDefaultValueExpression(method.ReturnType.CLRReference))); } else { CodeTypeReference jnihandle = TypeReference(typeof(JniHandle)); tgtMethod.Statements.Add(new CodeVariableDeclarationStatement(jnihandle, "__return", new CodeDefaultValueExpression(jnihandle))); } } tgtMethod.Statements.Add( new CodeSnippetStatement( //using (new global::net.sf.jni4net.jni.LocalFrame(env)) { " try {")); for (int p = 0; p < method.Parameters.Count; p++) { GType parameter = method.Parameters[p]; string name = method.ParameterNames[p]; if (parameter.IsOut) { tgtMethod.Statements.Add(new CodeVariableDeclarationStatement(parameter.CLRReference, "__out_" + name)); } if (parameter.IsRef) { var parExpression = new CodeVariableReferenceExpression(name); CodeMethodInvokeExpression conversionExpression = new CodeMethodInvokeExpression( new CodeMethodReferenceExpression( new CodeTypeReferenceExpression(typeof(Ref)), "GetValue", new[] { parameter.CLRReference }), envVariable, parExpression); CodeVariableDeclarationStatement varExpression = new CodeVariableDeclarationStatement(parameter.CLRReference, "__ref_" + name, conversionExpression); tgtMethod.Statements.Add(varExpression); } } }
private void GenerateMethodParamsJ2C(CodeMemberMethod tgtMethod, GMethod method) { var enviParam = new CodeParameterDeclarationExpression( TypeReference(typeof(IntPtr)), envpVariableName); tgtMethod.Parameters.Add(enviParam); if (method.IsStatic || method.IsConstructor) { var classParam = new CodeParameterDeclarationExpression( TypeReference(typeof(JniLocalHandle)), classVariableName); tgtMethod.Parameters.Add(classParam); } if (!method.IsStatic || method.IsConstructor) { var classParam = new CodeParameterDeclarationExpression( TypeReference(typeof(JniLocalHandle)), objVariableName); tgtMethod.Parameters.Add(classParam); } for (int p = 0; p < method.Parameters.Count; p++) { GType paramType = method.Parameters[p]; CodeTypeReference parameter; if (!paramType.IsPrimitive || paramType.IsOut || paramType.IsRef) { parameter = TypeReference(typeof(JniLocalHandle)); } else { parameter = paramType.CLRReference; } string name = method.ParameterNames[p]; tgtMethod.Parameters.Add(new CodeParameterDeclarationExpression(parameter, name)); } }
protected void CreateMethodC2J(GMethod method, CodeTypeDeclaration tgtType, string uName, bool isProxy) { if (method.IsCLRMethod && !type.IsInterface && !type.IsDelegate) { return; } if (isProxy || !type.IsInterface) { GenerateMethodIdFieldC2J(method, tgtType, uName); GenerateMethodRegC2J(method, uName); } if (method.IsStatic && type.IsInterface && isProxy) { return; } CreateMethodC2J(method, tgtType, uName, isProxy, false); if (method.IsField && !method.IsFinal) { CreateMethodC2J(method, tgtType, uName, isProxy, true); } }
protected void CreateMethodC2J(GMethod method, CodeTypeDeclaration tgtType, string uName, bool isProxy) { if (method.IsCLRMethod && !type.IsInterface && !type.IsDelegate) { return; } if (isProxy || !type.IsInterface) { GenerateMethodIdFieldC2J(method, tgtType, uName); GenerateMethodRegC2J(method, uName); } if (method.IsStatic && type.IsInterface && isProxy) { return; } CodeStatementCollection tgtStatements = CreateMethodSignature(tgtType, method, isProxy); GenerateGetEnvC2J(method, tgtStatements); CodeMethodInvokeExpression invokeExpression = GenerateInvokeExpressionC2J(method, uName); CodeStatement call = GenerateCallStatementC2J(method, invokeExpression); tgtStatements.Add(call); GenerateEndFrameC2J(tgtStatements); }
// If HasInterfaces public static IGAssemblyGroupBasicConstructorResult MAssemblyGroupBasicConstructor(string gAssemblyGroupName = default, string subDirectoryForGeneratedFiles = default, string baseNamespaceName = default, bool hasInterfaces = true, IGPatternReplacement gPatternReplacement = default) { var _gPatternReplacement = gPatternReplacement == default ? new GPatternReplacement() : gPatternReplacement; #region Determine the names of the Titular Base and Derived CompilationUnits, Namespaces, Classes // everything to the right of the last "." character, returns original string if no "." var pos = gAssemblyGroupName.LastIndexOf(".") + 1; var gTitularCommonName = gAssemblyGroupName.Substring(pos, gAssemblyGroupName.Length - pos); var gTitularAssemblyUnitName = gAssemblyGroupName; var gNamespaceName = $"{baseNamespaceName}{gTitularCommonName}"; var gCompilationUnitCommonName = gTitularCommonName; var gTitularDerivedCompilationUnitName = gCompilationUnitCommonName; var gTitularBaseCompilationUnitName = gCompilationUnitCommonName + "Base"; var gClassDerivedName = gCompilationUnitCommonName; var gClassBaseName = gCompilationUnitCommonName + "Base"; #endregion // If HasInterfaces #region Determine the names TitularInterfaces Base and Derived CompilationUnits, Namespaces, Classes var gTitularInterfaceAssemblyUnitName = gAssemblyGroupName + ".Interfaces"; var gTitularInterfaceDerivedCompilationUnitName = "I" + gCompilationUnitCommonName; var gTitularInterfaceBaseCompilationUnitName = "I" + gCompilationUnitCommonName + "Base"; var gTitularInterfaceDerivedName = "I" + gCompilationUnitCommonName; var gTitularInterfaceBaseName = "I" + gCompilationUnitCommonName + "Base"; #endregion #region GReplacementPatternDictionary for gAssemblyGroup var gAssemblyGroupPatternReplacement = new GPatternReplacement(gName: "gAssemblyGroupPatternReplacement", gDictionary: new Dictionary <Regex, string>() { { new Regex("AssemblyGroupNameReplacementPattern"), gAssemblyGroupName }, }); // add the PatternReplacements specified as the gPatternReplacement argument foreach (var kvp in gPatternReplacement.GDictionary) { gAssemblyGroupPatternReplacement.GDictionary.Add(kvp.Key, kvp.Value); } #endregion #region Instantiate the GAssemblyGroup var gAssemblyGroup = new GAssemblyGroup(gName: gAssemblyGroupName, gPatternReplacement: gAssemblyGroupPatternReplacement); #endregion #region Titular AssemblyUnit #region GReplacementPatternDictionary for the Titular AssemblyUnit var gTitularAssemblyUnitPatternReplacement = new GPatternReplacement(gName: "gAssemblyUnitPatternReplacement", gDictionary: new Dictionary <Regex, string>() { { new Regex("AssemblyUnitNameReplacementPattern"), gTitularAssemblyUnitName } }); // add the AssemblyGroup PatternReplacements to the Titular AssemblyUnit PatternReplacements gTitularAssemblyUnitPatternReplacement.GDictionary.AddRange(gAssemblyGroupPatternReplacement.GDictionary); #endregion #region ProjectUnit for the Titular AssemblyUnit #region GPatternReplacement for the ProjectUnit var gTitularAssemblyUnitProjectUnitPatternReplacement = new GPatternReplacement(gName: "TitularAssemblyUnitProjectUnitPatternReplacement"); gTitularAssemblyUnitProjectUnitPatternReplacement.GDictionary.AddRange( gTitularAssemblyUnitPatternReplacement.GDictionary); #endregion var gTitularAssemblyUnitProjectUnit = new GProjectUnit(gName: gTitularAssemblyUnitName, gPatternReplacement: gTitularAssemblyUnitProjectUnitPatternReplacement); #endregion #region Instantiate the gTitularAssemblyUnit var gTitularAssemblyUnit = new GAssemblyUnit(gName: gTitularAssemblyUnitName, gRelativePath: gTitularAssemblyUnitName, gProjectUnit: gTitularAssemblyUnitProjectUnit, gPatternReplacement: gTitularAssemblyUnitPatternReplacement); #endregion #endregion gAssemblyGroup.GAssemblyUnits.Add(gTitularAssemblyUnit.Philote, gTitularAssemblyUnit); #region Titular Derived CompilationUnit #region Pattern Replacements for Titular Derived CompilationUnit var gTitularDerivedCompilationUnitPatternReplacement = new GPatternReplacement(gName: "GTitularDerivedCompilationUnitPatternReplacement", gDictionary: new Dictionary <Regex, string>() { { new Regex("CompilationUnitNameReplacementPattern"), gTitularDerivedCompilationUnitName }, //{new Regex("DataInitializationReplacementPattern"), tempdatainitialization}, //{new Regex("DataDisposalReplacementPattern"), "" // // "SubscriptionToConsoleReadLineAsyncAsObservableDisposeHandle.Dispose();" //}, }); // add the AssemblyUnit PatternReplacements to the Derived CompilationUnit PatternReplacements gTitularDerivedCompilationUnitPatternReplacement.GDictionary.AddRange(gTitularAssemblyUnitPatternReplacement .GDictionary); #endregion #region Instantiate the Titular Derived CompilationUnit var gTitularDerivedCompilationUnit = new GCompilationUnit(gTitularDerivedCompilationUnitName, gFileSuffix: ".cs", gRelativePath: subDirectoryForGeneratedFiles, gPatternReplacement: gTitularDerivedCompilationUnitPatternReplacement); #endregion gTitularAssemblyUnit.GCompilationUnits.Add(gTitularDerivedCompilationUnit.Philote, gTitularDerivedCompilationUnit); #region Instantiate the gNamespaceDerived var gNamespaceDerived = new GNamespace(gNamespaceName); #endregion gTitularDerivedCompilationUnit.GNamespaces.Add(gNamespaceDerived.Philote, gNamespaceDerived); #region Instantiate the gClassDerived // If hasInterfaces, the derived class Implements the Interface GClass gClassDerived; if (hasInterfaces) { gClassDerived = new GClass(gClassDerivedName, "public", gAccessModifier: "partial", gInheritance: gClassBaseName, gImplements: new List <string> { gTitularInterfaceDerivedName } //gDisposesOf: new List<string> { "CompilationUnitNameReplacementPatternDerivedData" } ); } else { gClassDerived = new GClass(gClassDerivedName, "public", gAccessModifier: "partial", gInheritance: gClassBaseName //Implements: new List<string> {gTitularInterfaceDerivedName} -- No Interfaces in this AssemblyGroup //gDisposesOf: new List<string> { "CompilationUnitNameReplacementPatternDerivedData" } ); } #endregion gNamespaceDerived.GClasss.Add(gClassDerived.Philote, gClassDerived); #endregion #region Titular Base CompilationUnit #region Pattern Replacements for Derived CompilationUnit var gTitularBaseCompilationUnitPatternReplacement = new GPatternReplacement(gName: "GTitularBaseCompilationUnitPatternReplacement", gDictionary: new Dictionary <Regex, string>() { { new Regex("CompilationUnitNameReplacementPattern"), gTitularBaseCompilationUnitName }, //{new Regex("DataInitializationReplacementPattern"), tempdatainitialization}, { // new Regex("DataDisposalReplacementPattern"), // //"SubscriptionToConsoleReadLineAsyncAsObservableDisposeHandle.Dispose();" // "" //}, }); // add the AssemblyUnit PatternReplacements to the Base CompilationUnit PatternReplacements foreach (var kvp in gTitularAssemblyUnitPatternReplacement.GDictionary) { gTitularBaseCompilationUnitPatternReplacement.GDictionary.Add(kvp.Key, kvp.Value); } #endregion #region Instantiate the Titular Base CompilationUnit var gTitularBaseCompilationUnit = new GCompilationUnit(gTitularBaseCompilationUnitName, gFileSuffix: ".cs", gRelativePath: subDirectoryForGeneratedFiles, gPatternReplacement: gTitularBaseCompilationUnitPatternReplacement); #endregion gTitularAssemblyUnit.GCompilationUnits.Add(gTitularBaseCompilationUnit.Philote, gTitularBaseCompilationUnit); #region Instantiate the gNamespaceBase var gNamespaceBase = new GNamespace(gNamespaceName); #endregion gTitularBaseCompilationUnit.GNamespaces.Add(gNamespaceBase.Philote, gNamespaceBase); #region Instantiate the gClassBase var gClassBase = new GClass(gClassBaseName, "public", gAccessModifier: "partial", //gInheritance: baseClass.GName // If HasInterfaces gImplements: new List <string> { gTitularInterfaceBaseName } //, "IDisposable" //gDisposesOf: new List<string> { "CompilationUnitNameReplacementPatternBaseData" } ); #endregion gNamespaceBase.GClasss.Add(gClassBase.Philote, gClassBase); #region Instantiate the gPrimaryConstructorBase (Primary Constructor for the Titular Base Class) var gPrimaryConstructorBase = new GMethod(new GMethodDeclaration(gClassBaseName, isConstructor: true, gVisibility: "public")); #endregion gClassBase.GMethods.Add(gPrimaryConstructorBase.Philote, gPrimaryConstructorBase); #endregion //#region Data Initialization (startup?) //// ToDo: encapsulate and refactor //string tempdatainitialization = @" // /* // #region configurationRoot for this HostedService // // Create the configurationBuilder for this HostedService. This creates an ordered chain of configuration providers. The first providers in the chain have the lowest priority, the last providers in the chain have a higher priority. // // The Environment has been configured by the GenericHost before this point is reached // // InitialStartupDirectory has been set by the GenericHost before this point is reached, and is where the GenericHost program or service was started // // LoadedFromDirectory has been configured by the GenericHost before this point is reached. It is the location where this assembly resides // // ToDo: Implement these two values into the GenericHost configurationRoot somehow, then remove from the constructor signature // // var loadedFromDirectory = hostConfiguration.GetValue<string>(\SomeStringConstantConfigrootKey\ \./\); //ToDo suport dynamic assembly loading form other Startup directories - Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); // // var initialStartupDirectory = hostConfiguration.GetValue<string>(\SomeStringConstantConfigrootKey\ \./\); // // Build the configurationRoot for this service // // var configurationBuilder = ConfigurationExtensions.StandardConfigurationBuilder(loadedFromDirectory, initialStartupDirectory, ConsoleMonitorDefaultConfiguration.Production, ConsoleMonitorStringConstants.SettingsFileName, ConsoleMonitorStringConstants.SettingsFileNameSuffix, StringConstants.CustomEnvironmentVariablePrefix, LoggerFactory, stringLocalizerFactory, hostEnvironment, hostConfiguration, linkedCancellationToken); // // ConfigurationRoot = configurationBuilder.Build(); // #endregion // // Embedded object as Data // //AssemblyUnitNameReplacementPatternBaseData = new AssemblyUnitNameReplacementPatternBaseData(); // */"; //#endregion /* ************************************************************************************ */ // If HasInterfaces #region Titular Interfaces AssemblyUnit #region GReplacementPatternDictionary for Titular Interfaces AssemblyUnit var gTitularInterfaceAssemblyUnitPatternReplacement = new GPatternReplacement( gDictionary: new Dictionary <Regex, string>() { { new Regex("AssemblyUnitNameReplacementPattern"), gTitularInterfaceAssemblyUnitName } }); // add the AssemblyGroup PatternReplacements to the Titular Interface AssemblyUnit PatternReplacements gTitularInterfaceAssemblyUnitPatternReplacement.GDictionary.AddRange(gAssemblyGroupPatternReplacement.GDictionary); #endregion #region ProjectUnit for the Titular Interface AssemblyUnit #region GPatternReplacement for the ProjectUnit var gTitularInterfaceAssemblyUnitProjectUnitPatternReplacement = new GPatternReplacement(gName: "TitularInterfaceAssemblyUnitProjectUnitPatternReplacement"); gTitularInterfaceAssemblyUnitProjectUnitPatternReplacement.GDictionary.AddRange( gTitularInterfaceAssemblyUnitPatternReplacement.GDictionary); #endregion var gTitularInterfaceAssemblyUnitProjectUnit = new GProjectUnit(gTitularInterfaceAssemblyUnitName, gPatternReplacement: gTitularInterfaceAssemblyUnitProjectUnitPatternReplacement); #endregion #region Instantiate the gTitularInterfaceAssemblyUnit var gTitularInterfaceAssemblyUnit = new GAssemblyUnit(gName: gTitularInterfaceAssemblyUnitName, gRelativePath: gTitularInterfaceAssemblyUnitName, gProjectUnit: gTitularInterfaceAssemblyUnitProjectUnit, gPatternReplacement: gTitularInterfaceAssemblyUnitPatternReplacement); #endregion gAssemblyGroup.GAssemblyUnits.Add(gTitularInterfaceAssemblyUnit.Philote, gTitularInterfaceAssemblyUnit); #region Titular Interface Derived CompilationUnit #region Pattern Replacements for Titular Interface Derived CompilationUnit var gInterfaceDerivedCompilationUnitPatternReplacement = new GPatternReplacement(gName: "gTitularInterfaceDerivedCompilationUnitPatternReplacement", gDictionary: new Dictionary <Regex, string>() { { new Regex("CompilationUnitNameReplacementPattern"), gTitularInterfaceDerivedCompilationUnitName } }); // add the interface AssemblyUnit PatternReplacements to the Interface Derived CompilationUnit PatternReplacements gInterfaceDerivedCompilationUnitPatternReplacement.GDictionary.AddRange(gTitularInterfaceAssemblyUnitPatternReplacement .GDictionary); #endregion #region instantiate the Titular Interface Derived CompilationUnit var gTitularInterfaceDerivedCompilationUnit = new GCompilationUnit(gTitularInterfaceDerivedCompilationUnitName, gFileSuffix: ".cs", gRelativePath: subDirectoryForGeneratedFiles, gPatternReplacement: gInterfaceDerivedCompilationUnitPatternReplacement); #endregion gTitularInterfaceAssemblyUnit.GCompilationUnits.Add(gTitularInterfaceDerivedCompilationUnit.Philote, gTitularInterfaceDerivedCompilationUnit); #region Namespace For Titular Interface Derived CompilationUnit var gTitularInterfaceDerivedNamespace = new GNamespace(gNamespaceName); #endregion gTitularInterfaceDerivedCompilationUnit.GNamespaces.Add(gTitularInterfaceDerivedNamespace.Philote, gTitularInterfaceDerivedNamespace); #region Create Titular Interface Derived Interface var gTitularInterfaceDerivedInterface = new GInterface(gTitularInterfaceDerivedName, "public"); #endregion gTitularInterfaceDerivedNamespace.GInterfaces.Add(gTitularInterfaceDerivedInterface.Philote, gTitularInterfaceDerivedInterface); #endregion #endregion #region Titular Interface Base CompilationUnit #region Pattern Replacements for Interface Base CompilationUnit var gInterfaceBaseCompilationUnitPatternReplacement = new GPatternReplacement( gDictionary: new Dictionary <Regex, string>() { { new Regex("CompilationUnitNameReplacementPattern"), "AssemblyUnitNameReplacementPattern" } }); gInterfaceBaseCompilationUnitPatternReplacement.GDictionary.AddRange(gTitularInterfaceAssemblyUnitPatternReplacement.GDictionary); #endregion #region instantiate the Titular Interface Base CompilationUnit var gTitularInterfaceBaseCompilationUnit = new GCompilationUnit(gTitularInterfaceBaseCompilationUnitName, gFileSuffix: ".cs", gRelativePath: subDirectoryForGeneratedFiles, gPatternReplacement: gInterfaceBaseCompilationUnitPatternReplacement); #endregion gTitularInterfaceAssemblyUnit.GCompilationUnits.Add(gTitularInterfaceBaseCompilationUnit.Philote, gTitularInterfaceBaseCompilationUnit); #region Namespace For Titular Interface Base CompilationUnit var gTitularInterfaceBaseNamespace = new GNamespace(gNamespaceName); #endregion gTitularInterfaceBaseCompilationUnit.GNamespaces.Add(gTitularInterfaceBaseNamespace.Philote, gTitularInterfaceBaseNamespace); #region Create Titular Interface Base Interface var gTitularInterfaceBaseInterface = new GInterface(gTitularInterfaceBaseName, "public"); #endregion gTitularInterfaceBaseNamespace.GInterfaces.Add(gTitularInterfaceBaseInterface.Philote, gTitularInterfaceBaseInterface); #endregion /* ************************************************************************************ */ #region Update the ProjectUnits for both AssemblyUnits #region PropertyGroups common to both AssemblyUnits new List <IGPropertyGroupInProjectUnit>() { PropertyGroupInProjectUnitForProjectUnitIsLibrary(), PropertyGroupInProjectUnitForPackableOnBuild(), PropertyGroupInProjectUnitForLifecycleStage(), PropertyGroupInProjectUnitForBuildConfigurations(), PropertyGroupInProjectUnitForVersionInfo() }.ForEach(gP => { gTitularAssemblyUnit.GProjectUnit.GPropertyGroupInProjectUnits.Add(gP.Philote, gP); gTitularInterfaceAssemblyUnit.GProjectUnit.GPropertyGroupInProjectUnits.Add(gP.Philote, gP); }); #endregion #region ItemGroups common to both AssemblyUnits new List <IGItemGroupInProjectUnit>() { //TBD }.ForEach(o => { gTitularAssemblyUnit.GProjectUnit.GItemGroupInProjectUnits.Add(o.Philote, o); gTitularInterfaceAssemblyUnit.GProjectUnit.GItemGroupInProjectUnits.Add(o.Philote, o); }); #endregion #region PropertyGroups only in Titular AssemblyUnit #endregion #region ItemGroups only in Titular AssemblyUnit #endregion #region Link the Titular Interfaces AssemblyUnit back to the ProjectUnit in the Titular AssemblyUnit var gItemGroupInProjectUint = new GItemGroupInProjectUnit( gName: "the Titular Interfaces AssemblyUnit", gDescription: "The Interfaces for the Classes specified in this assembly", gBody: new GBody(gStatements: new List <string>() { $"<PackageReference Include=\"{gTitularInterfaceAssemblyUnit.GName}\" />" })); gTitularAssemblyUnit.GProjectUnit.GItemGroupInProjectUnits.Add(gItemGroupInProjectUint.Philote, gItemGroupInProjectUint); #endregion #endregion GAssemblyGroupBasicConstructorResult mCreateAssemblyGroupResult = new GAssemblyGroupBasicConstructorResult() { SubDirectoryForGeneratedFiles = subDirectoryForGeneratedFiles, BaseNamespaceName = baseNamespaceName, GAssemblyGroupName = gAssemblyGroupName, GTitularAssemblyUnitName = gTitularAssemblyUnitName, GTitularBaseCompilationUnitName = gTitularBaseCompilationUnitName, GAssemblyGroup = gAssemblyGroup, gAssemblyGroupPatternReplacement = gAssemblyGroupPatternReplacement, GTitularAssemblyUnit = gTitularAssemblyUnit, GTitularAssemblyUnitPatternReplacement = gTitularAssemblyUnitPatternReplacement, GTitularDerivedCompilationUnit = gTitularDerivedCompilationUnit, GTitularDerivedCompilationUnitPatternReplacement = gTitularDerivedCompilationUnitPatternReplacement, GTitularBaseCompilationUnit = gTitularBaseCompilationUnit, GTitularBaseCompilationUnitPatternReplacement = gTitularBaseCompilationUnitPatternReplacement, GNamespaceDerived = gNamespaceDerived, GNamespaceBase = gNamespaceBase, GClassBase = gClassBase, GClassDerived = gClassDerived, GPrimaryConstructorBase = gPrimaryConstructorBase, // If HasInterfaces gTitularInterfaceAssemblyUnit = gTitularInterfaceAssemblyUnit, GTitularInterfaceDerivedCompilationUnit = gTitularInterfaceDerivedCompilationUnit, GTitularInterfaceBaseCompilationUnit = gTitularInterfaceBaseCompilationUnit, GTitularInterfaceDerivedInterface = gTitularInterfaceDerivedInterface, GTitularInterfaceBaseInterface = gTitularInterfaceBaseInterface }; return(mCreateAssemblyGroupResult); }
//public static void MStateMachineFinalizer( // ( // IEnumerable<GAssemblyUnit> gAssemblyUnits, // IEnumerable<GCompilationUnit> gCompilationUnits, // IEnumerable<GNamespace> gNamespacess, // IEnumerable<GClass> gClasss, // IEnumerable<GMethod> gMethods) lookupResults) { // MStateMachineFinalizer(lookupResults.gCompilationUnits.First(), lookupResults.gNamespacess.First(), // lookupResults.gClasss.First(), lookupResults.gMethods.First(), finalGStateConfigurations); //} //public static void MStateMachineFinalizer() { // MStateMachineFinalizer(mCreateAssemblyGroupResult.GTitularBaseCompilationUnit, // mCreateAssemblyGroupResult.GNamespaceBase, mCreateAssemblyGroupResult.GClassBase, // gStateConfiguration: mCreateAssemblyGroupResult.GPrimaryConstructorBase.GStateConfigurations); //} public static void MStateMachineFinalizer(IGAssemblyGroupBasicConstructorResult mCreateAssemblyGroupResult) { #region Accumulate the StateConfigurations var finalGStateConfiguration = new GStateConfiguration(); foreach (var gAU in mCreateAssemblyGroupResult.GAssemblyGroup.GAssemblyUnits) { // finalGStateConfigurations.Add(gAu.GStateConfiguration); foreach (var gCU in gAU.Value.GCompilationUnits) { // finalGStateConfigurations.Add(gCu.GStateConfiguration); foreach (var gNs in gCU.Value.GNamespaces) { // finalGStateConfigurations.Add(gNs.GStateConfiguration); foreach (var gCl in gNs.Value.GClasss) { // finalGStateConfigurations.Add(gCl.GStateConfiguration); foreach (var gMe in gCl.Value.CombinedMethods()) { finalGStateConfiguration.GDOTGraphStatements.AddRange(gMe.GStateConfiguration.GDOTGraphStatements); } } } } } #endregion if (finalGStateConfiguration.GDOTGraphStatements.Any()) { //var parsedDiGraph = ParseDiGraphToStateMachine(finalGStateConfiguration); ParseDiGraphToStateMachine(finalGStateConfiguration); #region StateMachine EnumerationGroups var gEnumerationGroup = new GEnumerationGroup(gName: "State and Trigger Enumerations for StateMachine"); #region State Enumeration #region State Enumeration members var gEnumerationMemberList = new List <IGEnumerationMember>(); var enumerationValue = 1; foreach (var name in finalGStateConfiguration.GStateNames) { gEnumerationMemberList.Add(LocalizableEnumerationMember(name, enumerationValue++)); } // gEnumerationMemberList = new List<IGEnumerationMember>() { // LocalizableEnumerationMember("WaitingForInitialization",1,"Power-On State - waiting until minimal initialization condition has been met","Waiting For Initialization"), // LocalizableEnumerationMember("InitiateContact",1,"Signal to the Console Monitor that we are a valid ConsoleSource","Initiate Contact"), //}; var gEnumerationMembers = new Dictionary <IPhilote <IGEnumerationMember>, IGEnumerationMember>(); foreach (var o in gEnumerationMemberList) { gEnumerationMembers.Add(o.Philote, o); } #endregion var gEnumeration = new GEnumeration(gName: "State", gVisibility: "public", gInheritance: "", gEnumerationMembers: gEnumerationMembers); #endregion gEnumerationGroup.GEnumerations.Add(gEnumeration.Philote, gEnumeration); #region Trigger Enumeration #region Trigger Enumeration members gEnumerationMemberList = new List <IGEnumerationMember>(); enumerationValue = 1; foreach (var name in finalGStateConfiguration.GTriggerNames) { gEnumerationMemberList.Add(LocalizableEnumerationMember(name, enumerationValue++)); } //gEnumerationMemberList = new List<IGEnumerationMember>() { // LocalizableEnumerationMember("InitializationCompleteReceived",1,"The minimal initialization conditions have been met","Initialization Complete Received"), //}; gEnumerationMembers = new Dictionary <IPhilote <IGEnumerationMember>, IGEnumerationMember >(); //{gEnumerationMemberList.ForEach(m=>m.Philote,m)}; foreach (var o in gEnumerationMemberList) { gEnumerationMembers.Add(o.Philote, o); } #endregion gEnumeration = new GEnumeration(gName: "Trigger", gVisibility: "public", gInheritance: "", gEnumerationMembers: gEnumerationMembers); gEnumerationGroup.GEnumerations.Add(gEnumeration.Philote, gEnumeration); #endregion mCreateAssemblyGroupResult.GNamespaceBase.AddEnumerationGroup(gEnumerationGroup); #endregion //#region StateMachine Transitions Static variable //// Add a StaticVariable to the class //List<string> gStatements = new List<string>() {"new List<StateConfiguration>(){"}; //foreach (var sc in parsedDiGraph.StateConfigurations) { // gStatements.Add(sc); //} //gStatements.Add("}"); //var gStaticVariable = new GStaticVariable("stateConfigurations", gType: "List<StateConfiguration>", // gBody: new GBody(gStatements)); //gClass.GStaticVariables.Add(gStaticVariable.Philote, gStaticVariable); //#endregion #region Create the detailed ConfigureStateMachine Method using the parsedDiGraph information // add a method to the class that configures the StateMachine according to the StateConfigurations parsed from the diGraph var gMethodGroup = new GMethodGroup(gName: "Detailed ConfigureStateMachine Method"); var gBody = new GBody(new List <string>()); var gMethod = new GMethod( new GMethodDeclaration(gName: "ConfigureStateMachine", gType: "void", gVisibility: "private", gAccessModifier: ""), gBody); //"// attribution :https://github.com/dhrobbins/ApprovaFlow/blob/master/ApprovaFlow/ApprovaFlow/ApprovaFlow/Workflow/WorkflowProcessor.cs", //"// attribution :https://github.com/frederiksen/Stateless-Designer/blob/master/src/StatelessDesigner.VS2017/StatelessCodeGenerator/StatelessCodeGenerator.cs", //"// Heavily modified to work in a code generator", //"", //gBody.GStatements.Add( // "#region Delegates for each state's Entry and Exit method calls, and GuardClauses method calls"); foreach (var stateName in finalGStateConfiguration.GStateNames) { gBody.GStatements.Add($"StateMachine.Configure(State.{stateName})"); var permittedStateTransitions = finalGStateConfiguration.GDiGraphStates.Where(x => x.state == stateName) .Select(x => (triggerName: x.trigger, nextStateName: x.nextstate)); foreach (var pST in permittedStateTransitions) { gBody.GStatements.Add($" .Permit(Trigger.{pST.triggerName},State.{pST.nextStateName})"); } gBody.GStatements.Add($";"); // $"public EntryExitDelegate On{stateName}Entry = null;", // $"public EntryExitDelegate On{stateName}Exit = null;", } ; //} //gBody.GStatements.Add("#endregion"); //gBody.GStatements.Add("#region Fluent"); //foreach (var stateConfiguration in parsedDiGraph.StateConfigurations) { // //gBody.GStatements. Add("StateMachine.Configure($State},"); // // if (true) // stateName == stateConfiguration) { // // gBody.GStatements.Add($"#endregion"); //} // gBody.GStatements.Add("#endregion"); //gBody.GStatements.AddRange( // "#region Delegates for each state's Entry and Exit method calls, and GuardClauses method calls", // "#endregion " // ); //"// Get a distinct list of states with a trigger from the stateConfigurations static variable", //"// State => Trigger => TargetState", //"var states = StateConfigurations.AsQueryable()", //".Select(x => x.State)", //".Distinct()", //".Select(x => x)", //".ToList();", //"// Get each trigger for each state", //"states.ForEach(state =>{", //"var triggers = StateConfigurations.AsQueryable()", //".Where(config => config.State == state)", //".Select(config => new { Trigger = config.Trigger, TargetState = config.NextState })", //".ToList();", //"triggers.ForEach(trig => {", //"StateMachine.Configure(state).Permit(trig.Trigger, trig.TargetState);", //" });", //"});", #endregion gMethodGroup.GMethods.Add(gMethod.Philote, gMethod); mCreateAssemblyGroupResult.GClassBase.GMethodGroups.Add(gMethodGroup.Philote, gMethodGroup); #region Add the statement that fires the InitializationCompleteReceived Trigger //var statementList = gClass.CombinedMethods().Where(x => x.GDeclaration.GName == "StartAsync").First().GBody // .GStatements; //statementList.Insert(statementList.Count - 1, "StateMachine.Fire(Trigger.InitializationCompleteReceived);"); #endregion } }
protected CodeStatementCollection CreateMethodSignature(CodeTypeDeclaration tgtType, GMethod method, bool isProxy) { bool add = true; CodeStatementCollection tgtStatements; CodeTypeMember tgtMember; CodeMemberMethod tgtMethod = null; CodeMemberPropertyEx tgtProperty; CodeMemberPropertyEx tgtEvent; if (method.IsConstructor) { var tgtConstructor = new CodeConstructor(); tgtMethod = tgtConstructor; tgtMember = tgtMethod; tgtStatements = tgtMethod.Statements; if (!type.IsRootType) { tgtConstructor.BaseConstructorArgs.Add( new CodeCastExpression(TypeReference("net.sf.jni4net.jni.JNIEnv"), new CodePrimitiveExpression(null))); } } else if (method.IsField) { var p = new CodeMemberProperty(); tgtMember = p; tgtStatements = p.GetStatements; p.Name = method.CLRName; p.Type = method.ReturnType.CLRReference; } else if (method.IsEvent) { tgtEvent = new CodeMemberPropertyEx(); tgtEvent.Getter = method.CLRPropertyAdd; tgtEvent.Setter = method.CLRPropertyRemove; tgtEvent.Name = method.CLRName; if (method.UseExplicitInterface) { tgtEvent.PrivateImplementationType = method.DeclaringType.CLRReference; } foreach (CodeTypeMember m in tgtType.Members) { var member = m as CodeMemberPropertyEx; if (member != null) { if (member.Getter == method || member.Setter == method) { tgtEvent = member; add = false; break; } } } int count = method.Parameters.Count - 1; tgtEvent.Type = method.Parameters[count].CLRReference; if (add) { for (int i = 0; i < count; i++) { var tgtParameter = new CodeParameterDeclarationExpression(); tgtParameter.Name = method.ParameterNames[i]; tgtParameter.Type = method.Parameters[i].CLRReference; tgtEvent.Parameters.Add(tgtParameter); } tgtEvent.Comments.Add(new CodeCommentStatement("__event__")); } tgtMember = tgtEvent; if (method.IsCLRPropertyAdd) { tgtEvent.GetStatements.Add(new CodeCommentStatement("__add__")); tgtStatements = tgtEvent.GetStatements; } else { tgtEvent.SetStatements.Add(new CodeCommentStatement("__remove__")); tgtStatements = tgtEvent.SetStatements; } } else if (method.IsProperty) { tgtProperty = new CodeMemberPropertyEx(); tgtProperty.Setter = method.CLRPropertySetter; tgtProperty.Getter = method.CLRPropertyGetter; tgtProperty.Name = method.CLRName; if (method.UseExplicitInterface) { tgtProperty.PrivateImplementationType = method.DeclaringType.CLRReference; } foreach (CodeTypeMember m in tgtType.Members) { var member = m as CodeMemberPropertyEx; if (member != null) { if (member.Getter == method || member.Setter == method) { tgtProperty = member; add = false; break; } } } int count = method.Parameters.Count; if (!method.IsCLRPropertyGetter) { count--; } if (add) { for (int i = 0; i < count; i++) { var tgtParameter = new CodeParameterDeclarationExpression(); tgtParameter.Name = method.ParameterNames[i]; tgtParameter.Type = method.Parameters[i].CLRReference; tgtProperty.Parameters.Add(tgtParameter); } } if (method.IsCLRPropertyGetter) { tgtProperty.Type = method.ReturnType.CLRReference; tgtStatements = tgtProperty.GetStatements; } else { tgtProperty.Type = method.Parameters[count].CLRReference; tgtStatements = tgtProperty.SetStatements; } tgtMember = tgtProperty; } else { tgtMethod = new CodeMemberMethod(); tgtMethod.Name = method.CLRName; tgtMethod.ReturnType = method.ReturnType.CLRReference; tgtMember = tgtMethod; tgtStatements = tgtMethod.Statements; if (method.UseExplicitInterface) { tgtMethod.PrivateImplementationType = method.DeclaringType.CLRReference; } } if (!config.SkipSignatures && !isProxy) { Utils.AddAttribute(tgtMember, "net.sf.jni4net.attributes.JavaMethodAttribute", method.JVMSignature); } tgtMember.Attributes = method.Attributes; if (isProxy) { tgtMember.Attributes |= MemberAttributes.Final; } if (tgtMethod != null) { GenerateParameters(method, tgtMethod); } if (add) { tgtType.Members.Add(tgtMember); } return(tgtStatements); }
private void GenerateCallJ2C(CodeMemberMethod tgtMethod, GMethod method) { List <CodeExpression> callParams = GenerateCallParamsJ2C(method); if (method.IsConstructor) { tgtMethod.ReturnType = new CodeTypeReference(typeof(void)); var call = new CodeObjectCreateExpression(RealType, callParams.ToArray()); tgtMethod.Statements.Add( new CodeVariableDeclarationStatement(RealType, realVariableName, call)); tgtMethod.Statements.Add( new CodeMethodInvokeExpression( new CodeMethodReferenceExpression(TypeReferenceEx(typeof(Convertor)), "InitProxy"), envVariable, objVariable, realVariable)); } else { CodeExpression targetObject; if (method.IsStatic) { targetObject = RealTypeEx; } else { CodeExpression expression = CreateConversionExpressionJ2C(type, objVariable); tgtMethod.Statements.Add( new CodeVariableDeclarationStatement(RealType, realVariableName, expression)); if (method.DeclaringType != type) { targetObject = new CodeCastExpression(method.DeclaringType.CLRReference, realVariable); } else { targetObject = realVariable; } } CodeExpression call; CodeExpression value = null; if (method.IsProperty) { if (method.IsVoid) { int last = callParams.Count - 1; value = callParams[last]; callParams.RemoveAt(last); } if (method.CLRProperty.GetIndexParameters().Length > 0) { call = new CodeIndexerExpression(targetObject, callParams.ToArray()); } else { call = new CodePropertyReferenceExpression(targetObject, method.CLRProperty.Name); } } else if (method.IsEvent) { int last = callParams.Count - 1; value = callParams[last]; if (method.IsCLRPropertyAdd) { call = new CodeEventReferenceExpression(targetObject, method.CLREvent.Name + "__event_add__"); } else { call = new CodeEventReferenceExpression(targetObject, method.CLREvent.Name + "__event_remove__"); } } else { call = new CodeMethodInvokeExpression(targetObject, method.CLRName, callParams.ToArray()); } CodeStatement callst; if (method.IsVoid) { if (method.IsProperty || method.IsEvent) { callst = new CodeAssignStatement(call, value); } else { callst = new CodeExpressionStatement(call); } } else { if (method.ReturnType.IsPrimitive) { if (method.ReturnType.JVMSubst != null) { call = new CodeCastExpression(method.ReturnType.CLRReference, call); } callst = new CodeAssignStatement(new CodeVariableReferenceExpression("__return"), call); tgtMethod.ReturnType = method.ReturnType.CLRReference; } else { CodeMethodInvokeExpression conversionExpression = CreateConversionExpressionC2J(method.ReturnType, call); callst = new CodeAssignStatement(new CodeVariableReferenceExpression("__return"), conversionExpression); tgtMethod.ReturnType = TypeReference(typeof(JniHandle)); } } tgtMethod.Statements.Add(callst); } }