After I read the chapter 6 "closures" of the book Groovy and Grails Recipe, and I decided to use the power of closures of Groovy for resource (files) with other problem that I had, decompile in one step every class of jar library.
Well, the purpose of this class is call an external java decompiler (jad) from a Groovy class and execute the command into directory where the jar file was decompressed. And by using the power of closures executes recursively into this directory until find the classes.
Well, no more words, here the class
You can find more about "executing external processes" here and here
The Jad decompile executable file is located in this link
So, it is really useful the use of closures in Groovy. So you can see that I didn't use an static void main like java, here the point, I have created a groovy file named Decompiler.groovy, it contains the groovy class and the groovy script, so when the groovy file is compiled the Decompiler groovy class is created, and it contains the calling to the Executor class and the decompileClasses() method.
Well, I hope to follow posting about Groovy in the future.
Well, the purpose of this class is call an external java decompiler (jad) from a Groovy class and execute the command into directory where the jar file was decompressed. And by using the power of closures executes recursively into this directory until find the classes.
Well, no more words, here the class
package demo class Executor { // directory where the library(jar) was decompressed def path /** * Execute the decompilation of the classes into the directory defined in the path * @return */ def decompileClasses(){ def directory = new File(path) //make use of closures defined in the Object class directory.eachFileRecurse { def name = it.absolutePath //if the current resource hasn't a .class extension continues with next one if (name.indexOf(".class") < 0) return println "Processing ..." + it //getting the name of the class with the absolute path name = name.substring(0, name.indexOf( ".class")) + ".java" //defining the external command in order to be executed for every class file def command = "D:\\Jad.exe -o -r -d " + path +" -ff -nodos -v -s java " + it //execute the command def proc = command.execute() def outputCatcher = new StringBuffer() def errorCatcher = new StringBuffer() // Wait for the command to finish proc.consumeProcessOutput(outputCatcher, errorCatcher) proc.waitFor() // Obtain status and output // Obtain status and output println "return code: ${proc.exitValue()}" println 'out:\n' + outputCatcher println 'err:\n' + errorCatcher } } } def executor = new Executor(path:"D:\\myfolder\\my_jar") executor.decompileClasses()
You can find more about "executing external processes" here and here
The Jad decompile executable file is located in this link
So, it is really useful the use of closures in Groovy. So you can see that I didn't use an static void main like java, here the point, I have created a groovy file named Decompiler.groovy, it contains the groovy class and the groovy script, so when the groovy file is compiled the Decompiler groovy class is created, and it contains the calling to the Executor class and the decompileClasses() method.
Well, I hope to follow posting about Groovy in the future.
Comments