SHARE
TWEET

Untitled

a guest Aug 20th, 2019 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.lang.reflect.Method;
  2.  
  3. if (args == null || args.length == 0) {
  4.     println "select a suitable resource (Java file)"
  5.     return
  6. }
  7. // println "working on ${args[0]}"
  8.  
  9. fullFileName = args[0]
  10. if (!fullFileName.endsWith(".java")) {
  11.     println "selected resource is not a java file"
  12.     return
  13. }
  14.  
  15. fullFileName = fullFileName.replace("\\","/")
  16. pathFragmentToSplit = "src/main/java/"
  17. res = fullFileName.split(pathFragmentToSplit)
  18. // res.each { println it }
  19.  
  20. className = res[1].replace(".java","").replace("/",".");
  21.  
  22. // println className
  23.  
  24. java.lang.Class clazz = java.lang.Class.forName(className);
  25.  
  26. Method[] methods = clazz.getDeclaredMethods();
  27.  
  28. if (methods.length == 0) {
  29.     println "no methods found"
  30.     return
  31. }
  32.  
  33. println "\t public static class Builder {"
  34. println "\t\tprivate ${className} result = new ${className}();"
  35. println "\t\tpublic  ${className} build() { return result; }"
  36. println "\t\tpublic static Builder create() { return new Builder(); }"
  37.  
  38. methods.each {
  39.     setter = it.getName()
  40.     if (setter.startsWith('set')) {        
  41.         paramName = it.getParameters()[0].getName();
  42.         paramType = it.getParameters()[0].getType().getName();
  43.  
  44.         println "\t\tpublic Builder ${setter}(${paramType} ${paramName}) {\n\t\t\tresult.${setter}(${paramName});\n\t\t\treturn this;\n\t\t}"
  45.     }
  46. }
  47.  
  48. println "\t}"
  49.  
  50. /' You need a launch configuration in Eclipse or other IDE, that starts this script and gives the selected source file as argument
  51.    Example: C:\Program Files (x86)\Common Files\Oracle\Java\javapath\java.exe
  52.    Working Directory: ${workspace_loc:/my_server}
  53.    Arguments: -cp C:\development\bin\groovy-all-2.6.0-alpha-1.jar;C:\development\my_workspace\my_server\target\classes groovy.ui.GroovyMain C:\development\my_workspace\my_server\bin\GenBuilder.groovy ${selected_resource_loc}                
  54. '/
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top