Refactor Pattern.compile calls to static final fields#207
Refactor Pattern.compile calls to static final fields#207
Conversation
Agent-Logs-Url: https://github.com/InAnYan/jabref/sessions/0740bebe-7c6a-4991-95f8-a8d901ba8a69 Co-authored-by: InAnYan <13097618+InAnYan@users.noreply.github.com>
InAnYan
left a comment
There was a problem hiding this comment.
Okay nice. Two things:
- Can you check for other patterns in the code and double check that you moved all of them?
- Is it possible to make an open rewrite rule or check style rule to keep track when a pattern compiled in a non static code but could be compiled in static? Or something to analyze the code and signal an error?
- There might be cases where a pattern is constructed dynamically using some value of a variable - which is NOT a mistake. This shouldn’t be triggered as error, idk how to write this in open rewrite. Maybe open rewrite rule will target only of the argument of pattern compile is a string
Agent-Logs-Url: https://github.com/InAnYan/jabref/sessions/baedd9c8-4d70-49bb-96da-eeabd65e4503 Co-authored-by: InAnYan <13097618+InAnYan@users.noreply.github.com>
Done in commit c19bdbb. Here's a summary of the three points:
|
Removed redundant ADDRESS_PATTERN declaration and its documentation.
|
TODO: Run the check without refactor and see what would be shown |
Pattern.compile()was called inside methods and constructors throughout the codebase, recompiling identical regex patterns on every invocation. These should be compiled once asprivate static final Patternfields.Changes
Extracted to static fields (previously compiled in methods/constructors):
IacrEprint→IACR_EPRINT_PATTERNArXivIdentifier→OLD_IDENTIFIER_PATTERNFollowUpQuestionGenerator→NUMBERED_PATTERNStringUtil→SPLIT_TEXT_PATTERNCSLFormatUtils→BIBLIOGRAPHY_NUMBER_PATTERNCSLReferenceMarkManager→CITATION_NUMBER_PATTERNCSLCitationOOAdapter→CITATION_NUMBER_PATTERNExtractReferencesAction→COMMENT_NUMBER_PATTERNPreviewTabViewModel→XML_TAG_PATTERN,XML_ATTRIBUTES_PATTERNMarkdownDefinitionProvider→CITATION_COMMAND_PATTERN,CITATION_KEY_INSIDE_PATTERNImporterTest→WHITESPACE_PATTERNPromoted instance fields to static (were
private final, nowprivate static final):CoinsParser— 9 pattern fields (DOI,TITLE,JOURNAL, etc.)MSBibEntry—ADDRESS_PATTERNExtracted constructor-time compilations to static predicates (
PagesChecker):Added Checkstyle rule (
PatternCompileToStaticField) inconfig/checkstyle/checkstyle.xmlto detect future violations. The rule flags anyPattern.compile()call where the argument is a self-contained string literal but the declaration is notstatic. Dynamic patterns (where the argument is a variable, concatenation with a variable, or a method call like.formatted()) are intentionally excluded.Dynamic patterns that are built from method parameters (e.g.,
RegexKeywordGroup,BibTeXConverter,RegExpBasedFileFinder) are intentionally left unchanged, as they cannot be made static.⚡ Quickly spin up Copilot coding agent tasks from anywhere on your macOS or Windows machine with Raycast.