/// <param name="fileName"> </param> /// <param name="loc"> </param> /// <param name="superClass"> </param> public ClassGenerator(string fileName, int loc, ClassGenerator superClass) : base() { this.fileName = fileName; //Atleast one variable per class. Added in response to Ishtiaque's comment. int maxNoOfVars = ConfigurationXMLParser.getPropertyAsInt("maxNoOfClassFields"); int minNoOfFields = ConfigurationXMLParser.getPropertyAsInt("minNoOfClassFields"); // to avoid Illegal argument exception if (maxNoOfVars != 0) { this.numOfVars = (new Random()).Next(maxNoOfVars); } else { this.numOfVars = 0; } if (numOfVars < minNoOfFields) { numOfVars = minNoOfFields; } this.numberOfMethods = 0; this.loc = loc; this.percent = ConfigurationXMLParser.getPropertyAsInt("probability"); this.maxNestedIfs = ConfigurationXMLParser.getPropertyAsInt("maxNestedIfs"); this.maxAllowedMethodCalls = ConfigurationXMLParser.getPropertyAsInt("maxAllowedMethodCalls"); this.superClass = superClass; }
private static void establishClassInterfaceRelationships(List <InterfaceGenerator> interfaceList, List <ClassGenerator> list) { int numberOfInterfaces = interfaceList.Count; int maxInterfacesToImplement = ConfigurationXMLParser.getPropertyAsInt("maxInterfacesToImplement"); if (numberOfInterfaces == 0) { Console.WriteLine("No interfaces generated!"); return; } foreach (ClassGenerator generator in list) { List <InterfaceGenerator> generatorInterfaceList = generator.InterfaceList; Random random = new Random(); //A class can implement 0 or more interfaces. int numberOfInterfacesToImplement = random.Next(maxInterfacesToImplement); for (int j = 0; j < numberOfInterfacesToImplement; j++) { InterfaceGenerator interfaceGenerator = interfaceList[random.Next(interfaceList.Count)]; if (generatorInterfaceList.Contains(interfaceGenerator)) { continue; } generatorInterfaceList.Add(interfaceGenerator); } } }
public InterfaceGenerator(string name, List <ClassGenerator> classList) { this.name = name; //we need atleast one method per interface. not a marker interface. int maxNumOfMethods = ConfigurationXMLParser.getPropertyAsInt("maxNoOfMethodsPerInterface"); int numOfMethods; if (maxNumOfMethods < 1) { numOfMethods = 1; } else { numOfMethods = (new Random()).Next(maxNumOfMethods); } if (numOfMethods == 0) { numOfMethods = 1; } this.numOfMethods = numOfMethods; for (int i = 0; i < numOfMethods; i++) { //use this method only to extract the signature and discard them Method method = Method.generateMethodForInterface(ProgGenUtil.maxNoOfParameters, classList, name + "Method" + i); methodSignatures.Add(method.MethodSignature); } }
//read config information needed for code generation. static ProgGenUtil() { allowedTypes = AllowedTypesAsList; primitivesMap["char"] = Type.Primitives.CHAR; primitivesMap["byte"] = Type.Primitives.BYTE; primitivesMap["short"] = Type.Primitives.SHORT; primitivesMap["int"] = Type.Primitives.INT; primitivesMap["long"] = Type.Primitives.LONG; primitivesMap["float"] = Type.Primitives.FLOAT; primitivesMap["double"] = Type.Primitives.DOUBLE; primitivesMap["String"] = Type.Primitives.STRING; primitivesMap["Object"] = Type.Primitives.OBJECT; primitivesMap["Other"] = Type.Primitives.OTHER; readInjectContents(); maximumArraySize = ConfigurationXMLParser.getPropertyAsInt("maximumArraySize"); if (ConfigurationXMLParser.getProperty("useQueries").Equals("true", StringComparison.InvariantCultureIgnoreCase)) { useQueries = true; } //if (ConfigurationXMLParser.getProperty("useQueries").equalsIgnoreCase("true")) //{ // useQueries = true; //} string callType = ConfigurationXMLParser.getProperty("callType"); if (callType.Equals("MCO2_2", StringComparison.CurrentCultureIgnoreCase)) { methodCallType = CallType.crossClassWithoutRecursionLimit; } else if (callType.Equals("MCO2_1", StringComparison.CurrentCultureIgnoreCase)) { methodCallType = CallType.crossClassWithRecursionLimit; } else if (callType.Equals("MCO1_1", StringComparison.CurrentCultureIgnoreCase)) { methodCallType = CallType.localWithRecursionLimit; } else { //Original: methodCallType = CallType.localWithoutRecursionLimit; //Veena : Changed it to methodCallType = CallType.localWithRecursionLimit; } }
public static void Main(string[] args) { /* For ant script: specify 'config.xml' file and output directory */ if (args.Length == 1) { pathToDir = args[0] + System.IO.Path.PathSeparator; } /* List of generated class objects: ClassGenerators */ List <ClassGenerator> list = new List <ClassGenerator>(); // List<InterfaceGenerator> interfaceList = new List<InterfaceGenerator>(); int numberOfClasses = 0; int maxInheritanceDepth = 0; int noOfInheritanceChains = 0; int noOfInterfaces = 0; int maxInterfacesToImplement = 0; /* Set of generated classes, it's updated in ClassGenerator.generate() */ HashSet <string> generatedClasses = new HashSet <string>(); HashSet <string> preGeneratedClasses = new HashSet <string>(); try { string className = ConfigurationXMLParser.getProperty("classNamePrefix"); int totalLoc = ConfigurationXMLParser.getPropertyAsInt("totalLOC"); numberOfClasses = ConfigurationXMLParser.getPropertyAsInt("noOfClasses"); maxInheritanceDepth = ConfigurationXMLParser.getPropertyAsInt("maxInheritanceDepth"); // e.g. 3 noOfInheritanceChains = ConfigurationXMLParser.getPropertyAsInt("noOfInheritanceChains"); // 2 => "A-B-C" ; "E-F-G" noOfInterfaces = ConfigurationXMLParser.getPropertyAsInt("noOfInterfaces"); maxInterfacesToImplement = ConfigurationXMLParser.getPropertyAsInt("maxInterfacesToImplement"); if (numberOfClasses < (maxInheritanceDepth * noOfInheritanceChains)) { Console.WriteLine("Insufficent number of classes. Should be atleast: " + maxInheritanceDepth * noOfInheritanceChains); Environment.Exit(1); } HashSet <string> classList = new HashSet <string>(); for (int i = 0; i < numberOfClasses; i++) { classList.Add(className + i); } //E.g., {[2,5,6], [0,1,4]} List <List <int> > inheritanceHierarchies = new List <List <int> >(); //inheritanceHierarchies = ProgGenUtil.getInheritanceList(noOfInheritanceChains, maxInheritanceDepth, numberOfClasses); for (int i = 0; i < numberOfClasses; i++) { //Ishtiaque: All classes have equal number of variables, methods, etc. Should we change it? // classes are like A1, A2, etc where A=<UserDefinedName> //Bala: All such cases are handled in the ClassGenerator. It generates arbitrary number of // fields, methods. Only constraint is it should override all the methods of the interfaces // it implements. list.Add(new ClassGenerator(className + i, totalLoc / numberOfClasses, null) ); } string path = @"C:\Users\VeenaBalasubramanya\Desktop\Adv_Se\rugrat\TestPrograms\com\accenture\lab\carfast\test"; //Directory directory = Directory.CreateDirectory(path); DirectoryInfo directory = Directory.CreateDirectory(path); //System.IO.FileStream fs = System.IO.File.Create(pathString); if (!directory.Exists) { //Console.WriteLine(directory.mkdirs()); directory = System.IO.Directory.CreateDirectory(path); Console.WriteLine(directory); } /* * for (int i = 0; i < noOfInterfaces; i++) * { * InterfaceGenerator generator = new InterfaceGenerator(className + "Interface" + i, list); * interfaceList.Add(generator); * writeToFile(generator); * } * * //establishClassRelationships(inheritanceHierarchies, list); * * //establishClassInterfaceRelationships(interfaceList, list); */ } catch (System.FormatException e) { Console.WriteLine("Please enter integer values for arguments that expect integers!!!"); Console.WriteLine(e.ToString()); Console.Write(e.StackTrace); Environment.Exit(1); } //do pre-generation for classes //pre-generation determines the class members variables and method signatures foreach (ClassGenerator generator in list) { generator.preGenerateForMethodSignature(list, preGeneratedClasses); } foreach (ClassGenerator generator in list) { //Ishtiaque: How can 'generatedClasses' contain any of the ClassGenerator objects from the list? //Bala: classes are generated semi-recursively. The classes will invoke class generation on the //super class. The current class will be generated only after all its ancestor classes are generated. //We do not want to regenerate the ancestor classses and make stale the information used by its sub-classes //based on the earlier version. if (!generatedClasses.Contains(generator.FileName)) { //call generate to construct the class contents generator.generate(list, generatedClasses, preGeneratedClasses); } writeToFile(generator); } //generate DBUtil only if useQueries is TRUE //if (ProgGenUtil.useQueries) //{ // DBUtilGenerator dbUtilGenerator = new DBUtilGenerator(); // writeToFile(dbUtilGenerator); //} /* writing SingleEntry class */ //SingleEntryGenerator singleEntryGen = new SingleEntryGenerator(list); //String className = ConfigurationXMLParser.getProperty("classNamePrefix")+"Start"; //write(className, singleEntryGen.toString()); TreeOfSingleEntryGenerator treeSingleEntryGen = new TreeOfSingleEntryGenerator(list, pathToDir); treeSingleEntryGen.generateTreeOfSingleEntryClass(); //write the reachability matrix if (ConfigurationXMLParser.getProperty("doReachabilityMatrix").Equals("no")) { return; } List <Method> methodListAll = new List <Method>(); foreach (ClassGenerator generator in list) { methodListAll.AddRange(generator.MethodList); } StringBuilder builder = new StringBuilder(); builder.Append("Name, "); foreach (Method method in methodListAll) { builder.Append(method.AssociatedClass.FileName + "." + method.Name); builder.Append(", "); } builder.Append("\n"); foreach (Method method in methodListAll) { builder.Append(method.AssociatedClass.FileName + "." + method.Name); builder.Append(", "); foreach (Method calledMethod in methodListAll) { if (method.CalledMethodsWithClassName.Contains(calledMethod.AssociatedClass.FileName + "." + calledMethod.Name)) { builder.Append("1, "); } else { builder.Append("0, "); } } builder.Append("\n"); } writeReachabilityMatrix(builder.ToString()); }