Skip to content

Instantly share code, notes, and snippets.

@yujiorama
Last active April 22, 2021 07:16
Show Gist options
  • Save yujiorama/5681dbabd84a64ffb28431175f663586 to your computer and use it in GitHub Desktop.
Save yujiorama/5681dbabd84a64ffb28431175f663586 to your computer and use it in GitHub Desktop.
oneliner
//
// usage:
// groovy LineShrink.groovy <target.java>
// java -jar ~/.m2/repository/org/codehaus/groovy/groovy/2.5.8/groovy-2.5.8.jar LineShrink.groovy <target.java>
//
def input = new File(args[0])
def methodDefinitionStartPattern = /^\s{4}(public|protected|private)?\s{1,2}[a-zA-Z]{1}[^\s\(\)]+\s*[a-zA-Z]{1}[^\s\(\)]+\(.*$/
def methodDefinitionEndPattern = /^\s{4}\}$/
def inMethodDefinition = false
def lineCommentPattern = /^\s*\/\/.*$/
def stack = []
input.eachLine { line ->
if (line.trim().empty) {
return
}
if (inMethodDefinition) {
if (line ==~ lineCommentPattern) {
return
}
stack << line
def m = line =~ methodDefinitionEndPattern
if (m) {
println(stack.join(""))
stack = []
inMethodDefinition = false
}
return
}
def m = line =~ methodDefinitionStartPattern
if (m) {
inMethodDefinition = true
stack << line
return
}
println(line)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment