Skip to content

Commit a088f42

Browse files
authored
Merge pull request BruceEckel#13 from quidryan/javap
Javap
2 parents 9961c7b + bfb9929 commit a088f42

1 file changed

Lines changed: 79 additions & 62 deletions

File tree

build.gradle

Lines changed: 79 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import org.gradle.internal.jvm.Jvm
12
import org.apache.tools.ant.util.TeeOutputStream
2-
boolean debug = false
3+
boolean debug = false
34

45
class Tags {
56
Boolean hasMainMethod = false
@@ -16,13 +17,12 @@ class Tags {
1617
String javap = null
1718
String runFirst = null
1819
String outputLine = null
19-
private List<String> lines
2020
private String block
21-
def Tags(List<String> lines) {
22-
this.lines = lines
23-
block = lines.join('')
21+
def Tags(File file) {
22+
block = file.text
2423
hasMainMethod = block.contains('main(String[] args)')
25-
fileRoot = lines[0].split("/")[-1] - ".java"
24+
def firstLine = block.substring(0, block.indexOf("\n"))
25+
fileRoot = firstLine.split("/")[-1] - ".java"
2626
mainClass = fileRoot
2727
javaCmd = extract('java')
2828
if(javaCmd) {
@@ -42,27 +42,30 @@ class Tags {
4242
ignoreOutput = hasTag('IgnoreOutput')
4343
javap = extract('javap') // Includes only arguments to command
4444
runFirst = extract('RunFirst:')
45-
lines.each {
46-
if(it =~ /\\/* Output:/) {
47-
outputLine = it.trim()
48-
}
49-
}
45+
outputLine = extractOutputLine()
5046
}
5147
private def hasTag(String marker) {
5248
return block.contains("// {" + marker + "}")
5349
}
50+
51+
def extractOutputLine() {
52+
if (!block.contains("/* Output:")) {
53+
return null
54+
} else {
55+
return "/* Output:"
56+
}
57+
}
58+
5459
private def extract(String marker) {
55-
if(!block.contains("// {" + marker))
60+
// Assume some whitespace is after marker
61+
if(!block.contains("// {${marker} "))
5662
return null
57-
def re = "\\/\\/ \\{" + marker + /[^}]+}/
58-
def tagline = block =~ re
59-
if(tagline.getCount()) {
60-
def rtrim = tagline[0].reverse().dropWhile{ it != '}'}.reverse()[0..-2]
61-
def ltrim = rtrim - ("// {" + marker)
62-
ltrim = ltrim.replaceAll("//", " ")
63-
return ltrim.trim()
63+
def matcher = (block =~ /\/\/ \{${marker}\s+([^}]+)/)
64+
if (matcher) {
65+
def matched = matcher[0][1].trim()
66+
return matched.replaceAll("\n?//", "")
6467
} else {
65-
println "Searching for: " + re
68+
println "Searching for: " + matcher
6669
println block
6770
System.exit(1)
6871
}
@@ -81,11 +84,10 @@ class Tags {
8184
}
8285
public String toString() {
8386
String result = ""
84-
for(ln in lines)
87+
block.eachLine{ ln ->
8588
if(ln.startsWith("//") || ln.startsWith("package "))
8689
result += ln + "\n"
87-
else
88-
break
90+
}
8991
"""
9092
hasMainMethod
9193
compileTimeError
@@ -128,57 +130,72 @@ subprojects {
128130
}
129131
}
130132

131-
List tasks = []
133+
List createdTasks = []
132134

133-
projectDir.eachFileRecurse {
134-
if (it.name.endsWith('.java')) {
135+
projectDir.eachFileRecurse { file ->
136+
if (file.name.endsWith('.java')) {
135137

136-
Tags tags = new Tags(it.readLines())
138+
Tags tags = new Tags(file)
137139
if(debug && tags.hasTags()) println tags
138-
if(tags.javap) return // TEMPORARILY IGNORE JAVAP FILES UNTIL PROBLEM FIXED
139-
// Add tasks for java sources with main methods
140-
if ((tags.hasMainMethod || tags.javaCmd) && (!tags.compileTimeError)) {
141140

142-
if (!tags.validateByHand) {
143-
// Only add tasks that we know we can run successfully to the task list
144-
tasks.add(tags.fileRoot)
141+
// Exclude java sources that will not compile
142+
if (tags.compileTimeError) {
143+
sourceSets.main.java.excludes.add(file.name)
144+
} else {
145+
JavaExec javaTask = null
146+
// Add tasks for java sources with main methods
147+
if (tags.hasMainMethod || tags.javaCmd) {
148+
149+
javaTask = tasks.create(name: tags.fileRoot, type: JavaExec, dependsOn: tags.runFirst) {
150+
main = tags.mainClass
151+
classpath = sourceSets.main.runtimeClasspath
152+
args = tags.args
153+
jvmArgs = tags.jVMArgs
154+
}
155+
} else if (tags.javap) {
156+
// Create task for running javap
157+
javaTask = tasks.create(name: "${tags.fileRoot}", type: JavaExec, dependsOn: tags.runFirst) {
158+
main = "com.sun.tools.javap.Main"
159+
classpath = sourceSets.main.runtimeClasspath + files(Jvm.current().toolsJar)
160+
// Assuming javap represents all the args and there's no need to jVMArgs
161+
args tags.javap.split()
162+
}
145163
}
146164

147-
String basePath = it.absolutePath.replaceAll('\\.java', '')
148-
File outFile = new File(basePath + '.out')
149-
File errFile = new File(basePath + '.err')
150-
151-
task "$tags.fileRoot"(type: JavaExec, dependsOn: tags.runFirst) {
152-
main = tags.mainClass
153-
classpath = sourceSets.main.runtimeClasspath
154-
args = tags.args
155-
jvmArgs = tags.jVMArgs
156-
ignoreExitValue = tags.validateByHand || tags.throwsException
157-
doFirst {
158-
if(outFile.exists())
159-
outFile.delete()
160-
if(tags.outputLine)
161-
outFile << tags.outputLine + "\n"
162-
163-
standardOutput = new TeeOutputStream(new FileOutputStream(outFile, true), System.out)
164-
errorOutput = new TeeOutputStream(new FileOutputStream(errFile), System.err)
165+
if (javaTask) {
166+
def baseName = file.name.substring(0, file.name.lastIndexOf('.'))
167+
File outFile = new File(file.parentFile, baseName + '.out')
168+
File errFile = new File(file.parentFile, baseName + '.err')
169+
170+
javaTask.configure {
171+
ignoreExitValue = tags.validateByHand || tags.throwsException
172+
doFirst {
173+
if(outFile.exists())
174+
outFile.delete()
175+
if(tags.outputLine)
176+
outFile << tags.outputLine + "\n"
177+
178+
standardOutput = new TeeOutputStream(new FileOutputStream(outFile, true), System.out)
179+
errorOutput = new TeeOutputStream(new FileOutputStream(errFile), System.err)
180+
}
181+
doLast {
182+
if(outFile.size() == 0)
183+
outFile.delete()
184+
else if(!outFile.text.contains("/* Output:"))
185+
outFile.delete()
186+
if(errFile.size() == 0) errFile.delete()
187+
}
165188
}
166-
doLast {
167-
if(outFile.size() == 0)
168-
outFile.delete()
169-
else if(!outFile.text.contains("/* Output:"))
170-
outFile.delete()
171-
if(errFile.size() == 0) errFile.delete()
189+
190+
if (!tags.validateByHand) {
191+
// Only add tasks that we know we can run successfully to the task list
192+
createdTasks.add(javaTask)
172193
}
173194
}
174195
}
175-
// Exclude java sources that will not compile
176-
if (tags.compileTimeError) {
177-
sourceSets.main.java.excludes.add(it.name)
178-
}
179196
}
180197
}
181-
task run(dependsOn: tasks)
198+
task run(dependsOn: createdTasks)
182199
}
183200

184201
configure(subprojects - project(':onjava')) {

0 commit comments

Comments
 (0)