/ Published in: Groovy
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#! /usr/bin/env groovy import groovy.text.* import groovy.text.GStringTemplateEngine import java.lang.reflect.Field /** * @author Theo Briscoe * @modified by Arsalan S. * @modified by Dan Walker */ class GroovyFileReader { // static String IDL_PROJECT_SRC_EXCLUDED = "Stub,Helper,Holder,Operations,Interface" //Compile and run from ant //Evaluate command line arguments //Build list of java classes //foreach java class //load the class definitions //Use reflection to get list of fields //build the utility class //Place generated clsses in the correct directory //Identify enumerations //1. Load class definition & get all the public fields //2. Identify fields and store them in arrays //print " " // Again check for int and boolean arrays // if (field.getType().getName().contains("java.lang.String")){ // stringArrayFields.add(field) // } // Dividing the arrays in two categories // category 1: custom classes like com.aaa.* typeName = typeName.substring(startIndex,typeName.length()-1) // def a // Array of enum type enumFields.add(field) customFields.add(field) subClasses.add(typeName) } } actualClass = classType.getComponentType() stringArrayFields.add(field) } numericArrayFields.add(field) booleanArrayFields.add(field) } } // println "field name is "+ field.getType().getComponentType() stringFields.add(field) numericFields.add(field) booleanFields.add(field) //print " " println "\t--this is an enum field" enumFields.add(field) customFields.add(field) subClasses.add(field.getType().getName()) } } } } //Build strings stringFields.each { varsList += buildGeneralMerge(it.getName(),"String") + "\n\t\t" } numericFields.each { varsList += buildNumericMerge(it.getName()) + "\n\t\t" } numericArrayFields.each { // println str varsList += str } booleanFields.each { varsList += buildBooleanMerge(it.getName()) + "\n\t\t" } booleanArrayFields.each { varsList += buildGeneralMerge(it.getName(),"Boolean") + "\n\t\t" } enumFields.each { varsList += buildEnumMerge(it.getName()) + "\n\t\t" } customFields.each { customTypeName = it.getType().getName().substring(0,it.getType().getName().length()-1) customTypeName = it.getType().getName() } varsList += buildUserDefinedMerge(it.getName(),customTypeName) + "\n\t\t" } return fullMethod } } try { field.getName().equalsIgnoreCase("__value") } e.printStackTrace() // no need to define the result as it is already false } return result } return "updated.${field} = (newOne.${field} != false) ? newOne.${field} : original.${field};" } return "updated.${field} = original.${field};" } } return "updated.${field} = (newOne.${field} > 0) ? newOne.${field} : original.${field};" } Writable outputText = null // checking if the current custom class is in directList - a list of classes that require no merging // for (String value: directList){ // if (type.equ) // } templateText = "updated.${name} = original.${name};" templateText = "updated.${name} = merge${type}(original.${name},newOne.${name});" } } } } //def templateEngine = new SimpleTemplateEngine() // def stringMethods1 = buildStringMerge("type") // def stringMethods2 = buildStringMerge("address1") // def opFile = new File("C:/projects/GroovyClassGenerator/outputFiles/MergeUtil.java") // opFile.write(template.toString()) //println template.toString() } subClassesCopy = subClasses.clone() subClasses = [] // println "subClass Array: entry is "+str // println "\t addedClass value is "+ addedClass // do nothing as that custom class has already been taken care of // println "\t addedClass matched with subClass" classAdded = true } classAdded } finalStr+=getFields(str) importArray.add(str) } } return finalStr } /** * main method to generate the MergeUtil class using templates * @author Arsalan S. * */ openMethodList.each { importArray.add(it) } classes.each { finalStr += getFields(it) importArray.add(it) } // Adding the merge methods for custom sub classes if any of them is being used by main classes // Keep it recursive till no more customSubClasses left, in other words generate all subclasses merge methods finalStr = recursiveSubClassCall(finalStr) } // generating string for imports importArray.each { importString+=(IMPORTSTR + it + ";\n") } // --------------------------------------------------------- } }