| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.build.code; |
| 7 |
|
| 8 |
import java.io.IOException; |
| 9 |
import java.nio.charset.StandardCharsets; |
| 10 |
import java.nio.file.FileVisitResult; |
| 11 |
import java.nio.file.Files; |
| 12 |
import java.nio.file.Path; |
| 13 |
import java.nio.file.Paths; |
| 14 |
import java.nio.file.SimpleFileVisitor; |
| 15 |
import java.nio.file.attribute.BasicFileAttributes; |
| 16 |
|
| 17 |
/** |
| 18 |
* Enable / disable AB-BA deadlock detector code. |
| 19 |
*/ |
| 20 |
public class AbbaDetect { |
| 21 |
|
| 22 |
/** |
| 23 |
* This method is called when executing this application from the command |
| 24 |
* line. |
| 25 |
* |
| 26 |
* @param args the command line parameters |
| 27 |
*/ |
| 28 |
public static void main(String... args) throws Exception { |
| 29 |
String baseDir = "src/main"; |
| 30 |
Files.walkFileTree(Paths.get(baseDir), new SimpleFileVisitor<Path>() { |
| 31 |
@Override |
| 32 |
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { |
| 33 |
process(file, true); |
| 34 |
return FileVisitResult.CONTINUE; |
| 35 |
} |
| 36 |
}); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Process a file. |
| 41 |
* |
| 42 |
* @param file the file |
| 43 |
*/ |
| 44 |
static void process(Path file, boolean enable) throws IOException { |
| 45 |
String name = file.getFileName().toString(); |
| 46 |
if (!name.endsWith(".java")) { |
| 47 |
return; |
| 48 |
} |
| 49 |
if (name.endsWith("AbbaDetector.java")) { |
| 50 |
return; |
| 51 |
} |
| 52 |
String source = new String(Files.readAllBytes(file), StandardCharsets.UTF_8); |
| 53 |
String original = source; |
| 54 |
|
| 55 |
source = disable(source); |
| 56 |
if (enable) { |
| 57 |
String s2 = enable(source); |
| 58 |
if (!source.equals(disable(s2))) { |
| 59 |
throw new IOException("Could not revert changes for file " + file); |
| 60 |
} |
| 61 |
source = s2; |
| 62 |
} |
| 63 |
|
| 64 |
if (source.equals(original)) { |
| 65 |
return; |
| 66 |
} |
| 67 |
Path newFile = Paths.get(file.toString() + ".new"); |
| 68 |
Files.write(newFile, source.getBytes(StandardCharsets.UTF_8)); |
| 69 |
|
| 70 |
Path oldFile = Paths.get(file.toString() + ".old"); |
| 71 |
Files.move(file, oldFile); |
| 72 |
Files.move(newFile, file); |
| 73 |
Files.delete(oldFile); |
| 74 |
} |
| 75 |
|
| 76 |
private static String disable(String source) { |
| 77 |
source = source.replaceAll("\\{org.h2.util.AbbaDetector.begin\\(.*\\);", "{"); |
| 78 |
source = source.replaceAll("org.h2.util.AbbaDetector.begin\\((.*\\(\\))\\)", "$1"); |
| 79 |
source = source.replaceAll("org.h2.util.AbbaDetector.begin\\((.*)\\)", "$1"); |
| 80 |
source = source.replaceAll("synchronized ", "synchronized "); |
| 81 |
return source; |
| 82 |
} |
| 83 |
|
| 84 |
private static String enable(String source) { |
| 85 |
// the word synchronized within single line comments |
| 86 |
source = source.replaceAll("(// .* synchronized )([^ ])", "$1 $2"); |
| 87 |
|
| 88 |
source = source.replaceAll("synchronized \\((.*)\\(\\)\\)", |
| 89 |
"synchronized \\(org.h2.util.AbbaDetector.begin\\($1\\(\\)\\)\\)"); |
| 90 |
source = source.replaceAll("synchronized \\((.*)\\)", |
| 91 |
"synchronized \\(org.h2.util.AbbaDetector.begin\\($1\\)\\)"); |
| 92 |
|
| 93 |
source = source.replaceAll("static synchronized ([^ (].*)\\{", |
| 94 |
"static synchronized $1{org.h2.util.AbbaDetector.begin\\(null\\);"); |
| 95 |
source = source.replaceAll("synchronized ([^ (].*)\\{", |
| 96 |
"synchronized $1{org.h2.util.AbbaDetector.begin\\(this\\);"); |
| 97 |
|
| 98 |
return source; |
| 99 |
} |
| 100 |
|
| 101 |
} |