code: @VisibleForTesting
    static String packageName(String source) {
      try (StringReader r = new StringReader(source)) {
        StreamTokenizer tokenizer = new StreamTokenizer(r);
        tokenizer.slashSlashComments(true);
        tokenizer.slashStarComments(true);
        StringBuilder sb = new StringBuilder();
        boolean inName = false;
        while (tokenizer.nextToken() != StreamTokenizer.TT_EOF) {
          if (inName) {
            switch (tokenizer.ttype) {
              case ';':
                return sb.length() > 0 ? sb.toString() : null;
              case '.':
                sb.append('.');
                break;
              case StreamTokenizer.TT_WORD:
                sb.append(tokenizer.sval);
                break;
              default:
                inName = false; // Invalid package statement pattern.
                break;
            }
          } else if (tokenizer.ttype == StreamTokenizer.TT_WORD && tokenizer.sval.equals("package")) {
            inName = true;
          }
        }
        return null; // Package statement not found.
      } catch (IOException e) {
        throw new AssertionError("Exception reading string: " + e);
      }
    }
docs: Extract the name of a Java source's package, or null if not found. This method is only used
  before javac parsing to determine the main type name.