Class JavaCodeScanner

java.lang.Object
net.mcreator.java.JavaCodeScanner

public final class JavaCodeScanner extends Object
A lightweight scanner for Java-like source code that classifies each character as either actual code or part of a string literal, text block, character literal, or comment. Unlike a full parser, it is tolerant to incomplete or invalid code: unterminated literals and comments simply extend to the end of the input. This makes it suitable for processing code fragments and code being edited.
  • Constructor Details

    • JavaCodeScanner

      public JavaCodeScanner()
  • Method Details

    • scan

      public static boolean scan(String code, JavaCodeScanner.Visitor visitor)
      Scans the given code and reports every character along with its JavaCodeScanner.Region to the visitor. Text blocks (""" ... """) are reported as JavaCodeScanner.Region.STRING. The line terminator ending a line comment is not part of the comment and is reported as JavaCodeScanner.Region.CODE.
      Parameters:
      code - The code to scan
      visitor - The visitor to report each character to
      Returns:
      true if the entire code was scanned, false if the visitor aborted the scan
    • maskStringsAndComments

      public static String maskStringsAndComments(String code)
      Returns a copy of the given code in which every character of string literals, text blocks, character literals, and comments (including their delimiters) is replaced with a space. The returned string has the same length as the input, so every index maps 1:1 to the original code, which allows performing index-based operations on the original code based on findings in the masked copy.
      Parameters:
      code - The code to mask
      Returns:
      The code of the same length with strings, char literals, and comments masked out