Class JavaScanner


  • class JavaScanner
    extends java.lang.Object
    A simplistic Java scanner. This scanner returns a sequence of tokens that can be used to reconstruct the source code. Since the source code is coming from a string, the scanner in fact just returns token boundaries rather than the tokens themselves.

    We are not dealing with arbitrary user code so we can assume there are no exotic things like tabs or Unicode escapes that resolve into quotes. The purpose of the scanner here is to return a sequence of offsets that split the string up in a way that allows us to work with spaces without having to worry whether they are inside strings or comments. The particular properties we use are that every string and character literal and every comment is a single token; every newline plus all following indentation is a single token; and every other string of consecutive spaces outside a comment or literal is a single token. That means that we can safely compress a token that starts with a space into a single space, without falsely removing indentation or changing the contents of strings.

    In addition to real Java syntax, this scanner recognizes tokens of the form `text`, which are used in the templates to wrap fully-qualified type names, so that they can be extracted and replaced by imported names if possible.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.String s  
    • Constructor Summary

      Constructors 
      Constructor Description
      JavaScanner​(java.lang.String s)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private int blockCommentEnd​(int start)  
      private int lineCommentEnd​(int start)  
      private int quoteEnd​(int start)  
      private int spaceEnd​(int start)  
      (package private) int tokenEnd​(int start)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • s

        private final java.lang.String s
    • Constructor Detail

      • JavaScanner

        JavaScanner​(java.lang.String s)
    • Method Detail

      • tokenEnd

        int tokenEnd​(int start)
      • spaceEnd

        private int spaceEnd​(int start)
      • blockCommentEnd

        private int blockCommentEnd​(int start)
      • lineCommentEnd

        private int lineCommentEnd​(int start)
      • quoteEnd

        private int quoteEnd​(int start)