Created
May 19, 2023 17:17
-
-
Save Geolykt/2b37094a5427f8aedc8e891c86b2d48d to your computer and use it in GitHub Desktop.
Migrate Java 9+ `@Deprecated` to Java 8 Deprecated annotations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.nio.file.StandardOpenOption; | |
import java.util.Iterator; | |
import java.util.List; | |
public class DeprecationAnnotationMigrator { | |
public static void main(String[] args) throws IOException { | |
migrate(Paths.get("").toAbsolutePath()); | |
} | |
private static void migrate(Path p) throws IOException { | |
Iterator<Path> children = Files.list(p).iterator(); | |
while (children.hasNext()) { | |
Path child = children.next(); | |
//System.out.println(child); | |
if (Files.isDirectory(child)) { | |
migrate(child); | |
continue; | |
} | |
if (child.getFileName().toString().endsWith(".java")) { | |
migrateFile(child); | |
} | |
} | |
} | |
private static void migrateFile(Path child) throws IOException { | |
List<String> lines = Files.readAllLines(child); | |
boolean modified = false; | |
loop0: | |
for (int i = 0; i < lines.size(); i++) { | |
String line = lines.get(i); | |
int atSign = line.indexOf('@'); | |
if (atSign == -1) { | |
continue; | |
} | |
for (int j = 0; j < atSign; j++) { | |
if (!Character.isWhitespace(line.codePointAt(j))) { | |
continue loop0; | |
} | |
} | |
if (!line.regionMatches(atSign + 1, "Deprecated(", 0, 11) || line.codePointBefore(line.length()) != ')') { | |
continue; | |
} | |
int firstEq = line.indexOf('=', atSign + 12); | |
if (firstEq == -1) { | |
continue; | |
} | |
int separator = line.indexOf(','); | |
String since = null; | |
String forRemoval = null; | |
if (separator == -1) { | |
String key = line.substring(atSign + 12, firstEq).trim(); | |
String value = line.substring(firstEq + 1, line.length() - 1).trim(); | |
if (key.equals("since")) { | |
since = value; | |
} else if (key.equals("forRemoval")) { | |
forRemoval = value; | |
} else { | |
throw new IllegalStateException("Unknown key: " + key); | |
} | |
} else { | |
int lastEq = line.lastIndexOf('='); | |
String key = line.substring(atSign + 12, firstEq).trim(); | |
String value = line.substring(firstEq + 1, separator).trim(); | |
if (key.equals("since")) { | |
since = value; | |
} else if (key.equals("forRemoval")) { | |
forRemoval = value; | |
} else { | |
throw new IllegalStateException("Unknown key: " + key); | |
} | |
key = line.substring(separator + 1, lastEq).trim(); | |
value = line.substring(lastEq + 1, line.length() - 1).trim(); | |
if (key.equals("since")) { | |
since = value; | |
} else if (key.equals("forRemoval")) { | |
forRemoval = value; | |
} else { | |
throw new IllegalStateException("Unknown key: " + key); | |
} | |
} | |
modified = true; | |
String indent = line.substring(0, atSign + 1); | |
lines.set(i, indent + "Deprecated"); | |
if (since != null) { | |
lines.add(i, indent + "DeprecatedSince(" + since + ")"); | |
} | |
if ("true".equals(forRemoval)) { | |
lines.add(i, indent + "ScheduledForRemoval(inVersion = \"3.0.0\")"); | |
} | |
} | |
if (modified) { | |
Files.write(child, lines, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment