/// <summary> /// Change Methode Names to be matching C# names /// </summary> /// <param name="inMethode"></param> /// <returns></returns> public override string MethodeName(MethodeContainer inMethode) { if (inMethode.ModifierList.Contains("private")) { return($"_{inMethode.Name}"); } return(inMethode.Name.PascalCase()); }
/// <summary> /// Add Methode to Class /// </summary> public static MethodeContainer AddMethode(this ClassContainer inClass, string inMethodeName, TypeContainer inReturnType, params FieldContainer[] inFieldContainer) { var tmpMethode = new MethodeContainer() { Name = inMethodeName, ReturnType = inReturnType, Parameter = inFieldContainer.ToList(), }; inClass.AddMethode(tmpMethode); return(tmpMethode); }
/// <summary> /// Find a Methode in the CLass, Matching the Template /// </summary> /// <param name="inMethodeTemplate"></param> /// <param name="inClass"></param> /// <returns></returns> public static MethodeContainer FindMatchingMethode(this MethodeContainer inMethodeTemplate, ClassContainer inClass, bool inCheckParams = true) { var tmpMethodeList = inClass.MethodeList .Where(inItem => inItem.Name == inMethodeTemplate.Name); if (inCheckParams) { //Match Methode Parameter lenght tmpMethodeList = tmpMethodeList.Where(inItem => inItem.Parameter.Count == inMethodeTemplate.Parameter.Count); //TODO Find Methode by Matching Params } return(tmpMethodeList.FirstOrDefault()); }
private void AddMethodeToString(StringBuilder inOutput, MethodeContainer inMethode, int inIndentDepth) { AddComment(inOutput, inMethode.Comment, inIndentDepth, true); var tmpMethodeString = $"{ReturnModifiersOrdered(inMethode.ModifierList)}{CreateStringFromType(inMethode.ReturnType)}{inMethode.Name}"; if (!inMethode.IsProperty) { tmpMethodeString += "(" + string.Join(", ", inMethode.Parameter.Select(inItem => $"{CreateStringFromType(inItem.Type)}{inItem.Name}{(inItem.DefaultValue != null ? " = " + inItem.DefaultValue : "")}")) + ")"; } inOutput.Append(CreateIndent(inIndentDepth) + tmpMethodeString); if (inMethode.ConstructorCall != null) { inOutput.AppendLine(""); inOutput.Append($"{CreateIndent(inIndentDepth + 1)}:{inMethode.ConstructorCall.Name}"); if (inMethode.IsProperty) { if (inMethode.Code != null) { throw new Exception("Properties with Code (get or set) is not Implemented yet"); } inOutput.Append("{get; set;}"); } else { inOutput.Append($"({string.Join(",", inMethode.ConstructorCall.Parameter.Select(inItem => AddCodeBlockToString(inItem, false)))})"); } } if (inMethode.Code == null) { if (inMethode.IsProperty) { inOutput.AppendLine("{get; set;}"); } inOutput.AppendLine(";"); } else { inOutput.AppendLine(""); inOutput.AppendLine(CreateIndent(inIndentDepth) + "{"); AddCodeBlockToString(inOutput, inMethode.Code, inIndentDepth + 1); inOutput.AppendLine(CreateIndent(inIndentDepth) + "}"); } }
/// <summary> /// Manage Code inside Methode /// </summary> /// <param name="inMethodeContainer"></param> /// <param name="inClass"></param> private void ManageCodeBlockOfMethode(MethodeContainer inMethodeContainer, ClassContainer inClass) { var tmpVariableList = new List <VariableDeclaration>(); tmpVariableList.AddRange(inMethodeContainer.Parameter); if (inMethodeContainer.Code != null) { for (var tmpI = 0; tmpI < inMethodeContainer.Code.CodeEntries.Count; tmpI++) { var tmpCodeBlock = inMethodeContainer.Code.CodeEntries[tmpI]; CodeEntryHandling(tmpCodeBlock, new FieldNameFinder(inClass) { VariableList = tmpVariableList }); } } }
/// <summary> /// Convert Methode to C# methode /// </summary> /// <param name="tmpMethode"></param> /// <param name="inClass"></param> private void ConvertMethode(MethodeContainer tmpMethode, ClassContainer inClass) { var tmpOldName = tmpMethode.Name; tmpMethode.Name = Converter.MethodeName(tmpMethode); tmpMethode.ModifierList = Converter.MapAndSortAttributes(tmpMethode.ModifierList); tmpMethode.Comment = Converter.Comment(tmpMethode.Comment, true); foreach (var tmpField in tmpMethode.Parameter) { ConvertField(tmpField); tmpField.Name = Converter.MethodeInParameter(tmpField); } //checking override, which changes from java to C# if the parent methode ist in a interface if (tmpMethode.ModifierList.Contains("override")) { var tmpParentClassQueue = new Queue <ClassContainer>(); tmpParentClassQueue.Enqueue(inClass); var tmpHandled = false; while (tmpParentClassQueue.Count > 0 && !tmpHandled) { var tmpClass = tmpParentClassQueue.Dequeue(); foreach (var tmpSubclass in tmpClass.InterfaceList) { var tmpSubClassContainer = _projectInfo.GetClassForType(tmpSubclass.Name, tmpClass.FullUsingList); if (tmpSubClassContainer != null) { tmpParentClassQueue.Enqueue(tmpSubClassContainer); //Check if Methode with same parameter Typers exist if (tmpSubClassContainer.MethodeList.Any(inItem => inItem.Name == tmpMethode.Name || inItem.Name == tmpOldName)) { if (tmpSubClassContainer.IsInterface()) { tmpMethode.ModifierList = tmpMethode.ModifierList.Where(inItem => inItem != "override").ToList(); tmpHandled = true; break; } } } } } } }
private static void HandlMethodeParameterContext(MethodeContainer tmpMethode, IFormalParameterContext tmpParam) { //Set parameters var tmpNewMethode = new FieldContainer { Name = tmpParam.variableDeclaratorId().IDENTIFIER().GetText(), Type = tmpParam.typeType().GetText(), }; if (tmpParam.variableModifier().Length > 0) { foreach (var tmpModifierContext in tmpParam.variableModifier()) { var tmpParamModifier = tmpModifierContext.GetText(); //F**k it: If it starts with @ it probably is an override. If Not -> Fix the C# Code:D if (tmpParamModifier.StartsWith("@")) { tmpParamModifier = tmpParamModifier.Substring(1).ToLower(); } tmpNewMethode.ModifierList.Add(tmpParamModifier); } } tmpMethode.Parameter.Add(tmpNewMethode); }
/// <summary> /// Change Methode Names to be matching C# names /// </summary> /// <param name="inMethode"></param> /// <returns></returns> public virtual string MethodeName(MethodeContainer inMethode) { return(inMethode.Name); }
public ProjectInformation CreateObjectInformation(List <string> inFileContents, IniParser.Model.IniData inConfiguration) { var tmpClassList = new List <ClassContainer>(); ProjectInformation tmpObjectInformation = new ProjectInformation(); if (LoadDefaultData) { tmpObjectInformation = ProjectInformationHelper.CreateSystemProjectInformation(ImportHelper.ImportClasses(JavaLangClassJson.JavaLang), ImportHelper.ImportAliasList(CompilerAliasHelper.SystemAliasJson), "java.lang"); } foreach (var tmpFile in inFileContents) { //tmpClassList.AddRange(JavaClassLoader.LoadFile(tmpFile)); tmpClassList.AddRange(JavaAntlrClassLoader.LoaderOptimization(tmpFile)); } tmpObjectInformation.FillClasses(tmpClassList); //Add Mapped Methodes to Class List (So we don't need String oä as a Class List var tmpAdditionalClasses = new List <ClassContainer>(); //Load all Classes, with Methodes we might need if (inConfiguration != null) { foreach (var tmpMap in inConfiguration["Methode"]) { var tmpLeftSplit = tmpMap.KeyName.Split('.'); var tmpNamespace = string.Join(".", tmpLeftSplit.SkipLast(2)); var tmpName = (TypeContainer)tmpLeftSplit.SkipLast(1).Last(); var tmpMethodeName = tmpLeftSplit.Last(); var tmpClass = tmpAdditionalClasses.FirstOrDefault(inItem => inItem.Namespace == tmpNamespace && inItem.Type == tmpName); if (tmpClass == null) { tmpClass = new ClassContainer { Type = tmpName, Namespace = tmpNamespace }; tmpAdditionalClasses.Add(tmpClass); } if (!tmpClass.MethodeList.Any(inItem => inItem.Name == tmpMethodeName)) { //TODO Check for Param Equality if (tmpClass.MethodeList.Count(inItem => inItem.Name == tmpMethodeName) > 1) { throw new NotImplementedException("Methode differenting with params not implemented"); } var tmpNewMethode = new MethodeContainer(); tmpNewMethode.Name = tmpMethodeName; tmpNewMethode.ModifierList = new List <string> { "public" }; tmpClass.MethodeList.Add(tmpNewMethode); } } } IResolveMethodeContentToIL tmpCodeHandler = new JavaMethodeCodeResolver(); foreach (var tmpClass in tmpClassList) { foreach (var tmpMethode in tmpClass.MethodeList) { tmpCodeHandler.Resolve(tmpMethode); } } foreach (var tmpClassouter in tmpClassList) { foreach (var tmpClass in tmpClassouter.InnerClasses) { foreach (var tmpMethode in tmpClass.MethodeList) { tmpCodeHandler.Resolve(tmpMethode); } } } //Fill them into the object Information tmpObjectInformation.FillClasses(tmpAdditionalClasses); return(tmpObjectInformation); }
public void Resolve(MethodeContainer inMethodeContainer) { if (inMethodeContainer.AntlrCode == null) { return; } var tmpCodeBlockStack = new Stack <CodeBlock>(); CodeBlock tmpCurrentCodeBlock = null; foreach (var tmpElement in inMethodeContainer.AntlrCode.GetChildren()) { var tmpType = tmpElement.GetType().Name; if (tmpElement is BlockContext) { var tmpBlock = tmpElement as BlockContext; foreach (var tmpBlockChild in tmpBlock.GetChildren()) { var tmpBlockType = tmpBlockChild.GetType().Name; if (tmpBlockChild is TerminalNodeImpl) { var tmpBlockText = tmpBlockChild.GetText(); if (tmpBlockText == "{") { tmpCurrentCodeBlock = new CodeBlock(); tmpCodeBlockStack.Push(tmpCurrentCodeBlock); if (tmpCodeBlockStack.Count == 1) { inMethodeContainer.Code = tmpCurrentCodeBlock; } } else if (tmpBlockText == "}") { tmpCurrentCodeBlock = tmpCodeBlockStack.Pop(); } else if (tmpBlockText.StartsWith("/")) { } else { throw new NotImplementedException("Not done yet"); } } else if (tmpBlockChild is BlockStatementContext) { HandloBlockStatementContent(tmpCurrentCodeBlock, tmpBlockChild as BlockStatementContext); } else { throw new NotImplementedException("Not done yet"); } } } else if (tmpElement is BlockStatementContext) { if (tmpCurrentCodeBlock == null) { tmpCurrentCodeBlock = new CodeBlock(); tmpCodeBlockStack.Push(tmpCurrentCodeBlock); inMethodeContainer.Code = tmpCurrentCodeBlock; } HandloBlockStatementContent(tmpCurrentCodeBlock, tmpElement as BlockStatementContext); } else if (tmpElement is TerminalNodeImpl) { } else { throw new NotImplementedException("Not done yet"); } } }
/// <summary> /// Add Methode Declaration to Class /// </summary> /// <param name="inClass"></param> /// <param name="tmpDeclaration"></param> /// <param name="tmpModifierList"></param> /// <returns></returns> private static MethodeContainer AddMethodeDeclaration(ClassContainer inClass, MethodDeclarationContext inMethodeContext, List <string> tmpModifierList) { var tmpMethode = new MethodeContainer { Name = inMethodeContext.IDENTIFIER().GetText(), AntlrCode = inMethodeContext.methodBody(), ReturnType = CreateTypeContainerFromType(inMethodeContext.typeTypeOrVoid()), }; var tmpParams = inMethodeContext.formalParameters()?.formalParameterList()?.formalParameter(); if (tmpParams != null) { foreach (var tmpParam in tmpParams) { var tmpNewMethode = new FieldContainer { Name = tmpParam.variableDeclaratorId().IDENTIFIER().GetText(), }; if (tmpParam.typeType().classOrInterfaceType() != null) { tmpNewMethode.Type = tmpParam.typeType().classOrInterfaceType().IDENTIFIER(0).GetText(); foreach (var tmpGeneric in tmpParam.typeType().classOrInterfaceType().typeArguments().SelectMany(inItem => inItem.typeArgument())) { var tmpType = new TypeContainer(tmpGeneric.GetChild(0).GetText()); if (tmpGeneric.EXTENDS() != null) { tmpType.Extends.Add(tmpGeneric.typeType().GetText()); } tmpNewMethode.Type.GenericTypes.Add(tmpType); } if (tmpParam.GetText().Contains("[")) { tmpNewMethode.Type.IsArray = true; } if (tmpParam.typeType().classOrInterfaceType().IDENTIFIER().Length > 1) { throw new NotImplementedException("Multi-Identifier needs to be handled"); } } else if (tmpParam.typeType().primitiveType() != null) { tmpNewMethode.Type = tmpParam.typeType().primitiveType().GetText(); } else { throw new NotImplementedException("Other Type Missing"); } if (tmpParam.variableModifier().Length > 0) { foreach (var tmpModifierContext in tmpParam.variableModifier()) { var tmpParamModifier = tmpModifierContext.GetText(); //F**k it: If it starts with @ it probably is an override. If Not -> Fix the C# Code:D if (tmpParamModifier.StartsWith("@")) { tmpParamModifier = tmpParamModifier.Substring(1).ToLower(); } tmpNewMethode.ModifierList.Add(tmpParamModifier); } } tmpMethode.Parameter.Add(tmpNewMethode); } } tmpMethode.ModifierList = tmpModifierList; inClass.AddMethode(tmpMethode); return(tmpMethode); }
internal static string ManageClassBodyContext(ClassContainer inClass, string tmpComment, ClassBodyContext tmpClassBoy) { var tmpChildList = tmpClassBoy.GetChildren().ToList(); for (var tmpI = 0; tmpI < tmpChildList.Count(); tmpI++) { var tmpItem = tmpChildList[tmpI]; var tmpType = tmpItem.GetType().Name; if (tmpItem is ClassBodyDeclarationContext) { var tmpClassBody = tmpItem as ClassBodyDeclarationContext; if (tmpClassBody.memberDeclaration() == null) { continue; } var tmpDeclaration = tmpClassBody.memberDeclaration(); if (tmpDeclaration == null) { continue; } var tmpModifierList = new List <string>(); //get modifiers together foreach (var tmpClassBodyContextModifier in tmpClassBody.modifier()) { var tmpModifierText = tmpClassBodyContextModifier.GetText(); //F**k it: If it starts with @ it probably is an override. If Not -> Fix the C# Code:D if (tmpModifierText.StartsWith("@")) { tmpModifierText = tmpModifierText.Substring(1).ToLower(); } tmpModifierList.Add(tmpModifierText); } if (tmpClassBody.children.Count > 1 && tmpClassBody.children[1].GetText() == "abstract" && !inClass.ModifierList.Contains("abstract")) { tmpModifierList.Add("abstract"); inClass.ModifierList.Add("abstract"); } if (tmpDeclaration.methodDeclaration() != null) { var tmpMethodeContainer = AddMethodeDeclaration(inClass, tmpDeclaration.methodDeclaration(), tmpModifierList); //In this case, we have a Generic methode Constructor if (tmpI > 0 && tmpChildList[tmpI - 1].GetText().EndsWith(">")) { var tmpDataList = new List <IParseTree>(); var tmpDepth = 0; var tmpNegativeI = 0; while (tmpDepth != 0 || tmpNegativeI == 0) { tmpNegativeI++; var tmpPreviiousCHild = tmpChildList[tmpI - tmpNegativeI]; foreach (var tmpChildOfChildOfPrechild in tmpPreviiousCHild.GetChildren().Reverse().SelectMany(inItem => inItem.GetChildren().Reverse())) { tmpDataList.Insert(0, tmpChildOfChildOfPrechild); if (tmpChildOfChildOfPrechild.GetText() == ">") { tmpDepth++; } else if (tmpChildOfChildOfPrechild.GetText() == "<") { tmpDepth--; } } } var tmpGenericListForMethode = GetGenericTypesFromGenericArgumentsChildren(inClass, tmpDataList); tmpMethodeContainer.GenericTypes.AddRange(tmpGenericListForMethode); } } else if (tmpDeclaration.fieldDeclaration() != null) { //Load field declaration into the container var tmpModifier = tmpDeclaration.fieldDeclaration(); var tmpFieldContainer = new FieldContainer { Name = tmpModifier.variableDeclarators().variableDeclarator(0).variableDeclaratorId().IDENTIFIER().GetText(), Type = tmpModifier.typeType().GetText(), Comment = tmpComment, }; tmpComment = ""; if (tmpModifier.variableDeclarators().variableDeclarator().Length > 0) { tmpFieldContainer.DefaultValue = CreateBlockFromMethodeInizialiser(tmpModifier.variableDeclarators().variableDeclarator()[0].variableInitializer(), inClass, tmpFieldContainer); } tmpFieldContainer.ModifierList = tmpModifierList; inClass.FieldList.Add(tmpFieldContainer); } else if (tmpDeclaration.constructorDeclaration() != null) { var tmpConstructor = tmpDeclaration.constructorDeclaration(); var tmpMethode = new MethodeContainer { Name = tmpConstructor.IDENTIFIER().GetText(), AntlrCode = tmpConstructor.block(), IsConstructor = true, Comment = tmpComment, }; tmpComment = ""; var tmpParams = tmpConstructor.formalParameters()?.formalParameterList()?.formalParameter(); if (tmpParams != null) { foreach (IFormalParameterContext tmpParam in tmpParams) { HandlMethodeParameterContext(tmpMethode, tmpParam); } if (tmpConstructor.formalParameters()?.formalParameterList()?.lastFormalParameter() != null) { HandlMethodeParameterContext(tmpMethode, tmpConstructor.formalParameters().formalParameterList().lastFormalParameter()); } } tmpMethode.ModifierList = tmpModifierList; inClass.AddMethode(tmpMethode); } else if (tmpDeclaration.genericMethodDeclaration() != null) { var tmpMethodeDeclaration = tmpDeclaration.genericMethodDeclaration(); var tmpMethodeContainer = AddMethodeDeclaration(inClass, tmpMethodeDeclaration.methodDeclaration(), tmpModifierList); var tmpGenericListForMethode = GetGenericTypesFromGenericArgumentsChildren(inClass, tmpMethodeDeclaration.typeParameters().GetChildren()); tmpMethodeContainer.GenericTypes.AddRange(tmpGenericListForMethode); } else { } } else if (tmpItem is TerminalNodeImpl) { if (tmpItem.GetText().StartsWith("/")) { tmpComment = tmpItem.GetText(); } else if (tmpItem.GetText() == "{") { } else if (tmpItem.GetText() == "}") { } else { } } else { } } return(tmpComment); }
/// <summary> /// Resolve an Interface Declaration /// </summary> /// <param name="tmpClass"></param> /// <param name="tmpComment"></param> /// <param name="tmpSubChild"></param> private static void ResolveInterfaceDeclaration(ClassContainer tmpClass, string tmpComment, IParseTree tmpSubChild) { var tmpBodyPart = (tmpSubChild as InterfaceBodyDeclarationContext).interfaceMemberDeclaration(); if (tmpBodyPart.interfaceMethodDeclaration() != null) { var tmpIntDec = tmpBodyPart.interfaceMethodDeclaration(); var tmpMethode = new MethodeContainer { Name = tmpIntDec.IDENTIFIER().GetText(), ReturnType = GetTypeContainer(tmpIntDec.typeTypeOrVoid().typeType()), Comment = tmpComment, AntlrCode = tmpIntDec.methodBody(), }; foreach (var tmpEntry in tmpIntDec.interfaceMethodModifier()) { if (tmpEntry.annotation() != null) { throw new NotImplementedException("Methode modifier annotation not Implemented"); } tmpMethode.ModifierList.Add(tmpEntry.GetText()); } var tmpParams = tmpIntDec.formalParameters()?.formalParameterList()?.formalParameter(); if (tmpParams != null) { foreach (var tmpParam in tmpParams) { var tmpModifier = tmpParam.variableModifier(); var tmpFieldContainer = new FieldContainer { Name = tmpParam.variableDeclaratorId().IDENTIFIER().GetText(), Type = GetTypeContainer(tmpParam.typeType()), }; foreach (var tmpInfo in tmpParam.variableModifier()) { tmpFieldContainer.ModifierList.Add(tmpInfo.GetText()); } tmpMethode.Parameter.Add(tmpFieldContainer); } } tmpClass.AddMethode(tmpMethode); } else if (tmpBodyPart.constDeclaration() != null) { var tmpConstDef = tmpBodyPart.constDeclaration(); var tmpDeclarator = tmpConstDef.constantDeclarator()[0]; var tmpFieldContainer = new FieldContainer { Name = tmpDeclarator.IDENTIFIER().GetText(), Type = GetTypeContainer(tmpConstDef.typeType()), Comment = tmpComment, }; var tmpInizialiser = tmpDeclarator.variableInitializer(); tmpFieldContainer.DefaultValue = CreateBlockFromMethodeInizialiser(tmpInizialiser, tmpClass, tmpFieldContainer); tmpClass.FieldList.Add(tmpFieldContainer); } else if (tmpBodyPart.classDeclaration() != null) { var tmpSubClass = new ClassContainer { UsingList = tmpClass.UsingList, Namespace = tmpClass.Namespace, Comment = tmpComment, }; tmpSubClass.Type = tmpBodyPart.classDeclaration().IDENTIFIER().GetText(); if (tmpBodyPart.classDeclaration().typeParameters() != null) { //Fill Type Parameters foreach (var tmpParam in tmpBodyPart.classDeclaration().typeParameters().typeParameter()) { var tmpInnerType = new TypeContainer(tmpParam.IDENTIFIER().GetText()); if (tmpParam.annotation().Length > 0 || tmpParam.EXTENDS() != null) { throw new NotImplementedException("Type Param Fill error"); } tmpSubClass.Type.GenericTypes.Add(tmpInnerType); } } FillClassContext(tmpSubClass, tmpBodyPart.classDeclaration()); tmpClass.InnerClasses.Add(tmpSubClass); } else { } }