-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: add JSpecify migration and nullmarked automation script #13889
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import os | ||
| import re | ||
|
|
||
| def migrate_file(file_path): | ||
| with open(file_path, "r", encoding="utf-8") as f: | ||
| content = f.read() | ||
|
|
||
| original_content = content | ||
|
|
||
| # 1. Add JSpecify NullMarked import if not present | ||
| package_match = re.search(r"^(package\s+[\w\.]+;)", content, re.MULTILINE) | ||
| if package_match and "org.jspecify.annotations.NullMarked" not in content and "@NullMarked" not in content: | ||
| package_line = package_match.group(1) | ||
| content = content.replace(package_line, package_line + "\nimport org.jspecify.annotations.NullMarked;") | ||
|
|
||
| # 2. Add @NullMarked before class/interface/enum declaration | ||
| class_match = re.search(r"\n(public\s+)?(abstract\s+)?(final\s+)?(class|interface|enum)\s+\w+", content) | ||
| if class_match: | ||
| class_start = class_match.start() | ||
| prefix = content[:class_start] | ||
| lines = prefix.split("\n") | ||
|
|
||
| # Find the correct insertion point (before other class-level annotations) | ||
| insert_idx = len(lines) | ||
| for i in range(len(lines) - 1, -1, -1): | ||
| line = lines[i].strip() | ||
| if line.startswith("@") and not line.startswith("@Override") and not line.startswith("@Test"): | ||
| insert_idx = i | ||
| elif line == "" or line.startswith("//") or line.startswith("/*") or line.startswith("*"): | ||
| break | ||
| else: | ||
| break | ||
| lines.insert(insert_idx, "@NullMarked") | ||
| content = "\n".join(lines) + content[class_start:] | ||
|
Comment on lines
+17
to
+34
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current class-matching regex has a few limitations:
We can make this regex much more robust by using class_match = re.search(r"(\n|^)\s*(?:(?:public|protected|private|abstract|final|static|sealed|non-sealed)\s+)*(?:class|interface|enum|record)\s+\w+", content)
if class_match:
class_start = class_match.start()
if class_start == 0:
content = "@NullMarked\n" + content
else:
prefix = content[:class_start]
lines = prefix.split("\n")
# Find the correct insertion point (before other class-level annotations)
insert_idx = len(lines)
for i in range(len(lines) - 1, -1, -1):
line = lines[i].strip()
if line.startswith("@") and not line.startswith("@Override") and not line.startswith("@Test"):
insert_idx = i
elif line == "" or line.startswith("//") or line.startswith("/*") or line.startswith("*"):
break
else:
break
lines.insert(insert_idx, "@NullMarked")
content = "\n".join(lines) + content[class_start:] |
||
|
|
||
|
|
||
| # 3. Replace legacy javax Nullable import | ||
| content = content.replace("import javax.annotation.Nullable;", "import org.jspecify.annotations.Nullable;") | ||
|
|
||
| # 4. Reposition @Nullable from declaration to type-use position | ||
| # (immediately before the type name, after class modifiers) | ||
| patterns = [ | ||
| (r"@Nullable\s+public\s+abstract\s+final\s+(\w+)", r"public abstract final @Nullable \1"), | ||
| (r"@Nullable\s+public\s+abstract\s+(\w+)", r"public abstract @Nullable \1"), | ||
| (r"@Nullable\s+public\s+static\s+final\s+(\w+)", r"public static final @Nullable \1"), | ||
| (r"@Nullable\s+public\s+static\s+(\w+)", r"public static @Nullable \1"), | ||
| (r"@Nullable\s+public\s+final\s+(\w+)", r"public final @Nullable \1"), | ||
| (r"@Nullable\s+public\s+(\w+)", r"public @Nullable \1"), | ||
| (r"@Nullable\s+private\s+static\s+final\s+(\w+)", r"private static final @Nullable \1"), | ||
| (r"@Nullable\s+private\s+static\s+(\w+)", r"private static @Nullable \1"), | ||
| (r"@Nullable\s+private\s+final\s+(\w+)", r"private final @Nullable \1"), | ||
| (r"@Nullable\s+private\s+(\w+)", r"private @Nullable \1"), | ||
| (r"@Nullable\s+protected\s+static\s+final\s+(\w+)", r"protected static final @Nullable \1"), | ||
| (r"@Nullable\s+protected\s+static\s+(\w+)", r"protected static @Nullable \1"), | ||
| (r"@Nullable\s+protected\s+final\s+(\w+)", r"protected final @Nullable \1"), | ||
| (r"@Nullable\s+protected\s+(\w+)", r"protected @Nullable \1"), | ||
| ] | ||
|
|
||
| for pattern, replacement in patterns: | ||
| content = re.sub(pattern, replacement, content) | ||
|
Comment on lines
+42
to
+60
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of maintaining 14 separate hardcoded regex patterns, we can simplify this to a single, highly robust regex. This simplification has several major benefits:
# Reposition @Nullable to be after all modifiers (type-use position)
# This single regex handles any combination of modifiers (including package-private, final parameters, etc.)
modifier_pattern = r"@Nullable\s+((?:(?:public|private|protected|static|final|abstract|synchronized|volatile|transient|native|strictfp|sealed|non-sealed)\s+)+)"
content = re.sub(modifier_pattern, r"\1@Nullable ", content) |
||
|
|
||
| # Save changes if modified | ||
| if content != original_content: | ||
| with open(file_path, "w", encoding="utf-8") as f: | ||
| f.write(content) | ||
|
|
||
| def walk_and_migrate(directory): | ||
| for root, _, files in os.walk(directory): | ||
| for file in files: | ||
| if file.endswith(".java") and file != "package-info.java": | ||
| migrate_file(os.path.join(root, file)) | ||
|
|
||
| if __name__ == "__main__": | ||
| import sys | ||
| if len(sys.argv) > 1: | ||
| walk_and_migrate(sys.argv[1]) | ||
| else: | ||
| print("Usage: python jspecify_migration.py <directory_path>") | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
content.replace(package_line, ...)can lead to accidental replacements if the package declaration string appears elsewhere in the file (e.g., in comments or string literals). Additionally, the package regex can be made more robust to handle spaces or comments before the semicolon.Using the match's end position (
package_match.end()) to perform a slice-based insertion is much safer.