Class JavaStubifier

java.lang.Object
org.checkerframework.framework.stubifier.JavaStubifier

public class JavaStubifier extends Object
Rewrites Java source files, in place, into minimal stub files, and also generates the compressed binary stub file (BinaryStubWriter.OUTPUT_FILENAME) for the same directory from the same parsed compilation units.

To convert a file into a stub file means to remove:

  1. everything that is private or package-private,
  2. all comments, except for an initial copyright header,
  3. all method bodies,
  4. all field initializers,
  5. all initializer blocks,
  6. attributes to the Deprecated annotation (to be Java 8 compatible).

Usage: JavaStubifier [--skipUnloadableAnnotations] <directory> [<directory> ...]. By default, an annotation whose @Target cannot be read (because its class is missing from the stubifier's own build classpath) aborts the run with a message naming the annotation and the source file being processed; see SKIP_UNLOADABLE_ANNOTATIONS_FLAG for the opt-in flag that trades that safety for being able to finish the run anyway.

Each directory is processed by its own BinaryStubWriter into its own output file (see process(java.lang.String, boolean)). An interface and its implementer being in different directories does not weaken fake-override handling: a writer records every unannotated method it sees as a presence-only signature unconditionally (BinaryStubWriter.addMethodRecord), with no dependence on the interfaces that writer happened to see, and the fake-override match runs at read time over the real class hierarchy (BinaryStubReader.applyFakeOverride, FakeOverrideResolver).

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final com.github.javaparser.ParserConfiguration.LanguageLevel
    The language level used to parse the annotated JDK sources (by this class), the built-in .astub files (by BinaryStubFileGenerator), and stub and Java files at checker runtime (JavaParserUtil.DEFAULT_LANGUAGE_LEVEL is this constant).
    static final String
    Command-line flag that makes an unloadable annotation a dropped-and-warned event rather than a fatal error.
  • Method Summary

    Modifier and Type
    Method
    Description
    static Path
    Converts a directory name to a path.
    static void
    main(String[] args)
    Processes each provided command-line argument; see class documentation for details.
    static com.github.javaparser.ParserConfiguration
    newStubParserConfiguration(boolean storeTokens)
    Returns a new configuration for parsing a stub file.

    Methods inherited from class java.lang.Object

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

    • DEFAULT_LANGUAGE_LEVEL

      public static final com.github.javaparser.ParserConfiguration.LanguageLevel DEFAULT_LANGUAGE_LEVEL
      The language level used to parse the annotated JDK sources (by this class), the built-in .astub files (by BinaryStubFileGenerator), and stub and Java files at checker runtime (JavaParserUtil.DEFAULT_LANGUAGE_LEVEL is this constant). This should be the highest version of Java that the Checker Framework can process.
    • SKIP_UNLOADABLE_ANNOTATIONS_FLAG

      public static final String SKIP_UNLOADABLE_ANNOTATIONS_FLAG
      Command-line flag that makes an unloadable annotation a dropped-and-warned event rather than a fatal error. When passed, BinaryStubWriter omits, from the binary stub output, any annotation whose @Target it cannot read -- printing one warning line to stderr naming the annotation and its source file -- and processing continues with the rest of the directory. Without the flag (the default), the same condition aborts the whole run; see BinaryStubWriter.annotationTargets(com.github.javaparser.ast.expr.AnnotationExpr).

      Trade-off: dropping such an annotation is not always safe. It could be a real type qualifier that a checker's own (wider) classpath would have resolved, in which case dropping it here silently removes it from the annotated JDK with no way for a later checker run to detect the omission. Pass this flag only when the unloadable annotation is known to be irrelevant to type-checking (e.g. a JUnit annotation on a stray test source file that ended up in the JDK source tree being stubified), not as a default way to push through classpath problems.

      See Also:
  • Method Details

    • newStubParserConfiguration

      public static com.github.javaparser.ParserConfiguration newStubParserConfiguration(boolean storeTokens)
      Returns a new configuration for parsing a stub file. Both BinaryStubFileGenerator and the text stub parser (JavaParserUtil.parseStubUnit) use it, so that a binary stub file is generated from the same parse that the text parser would produce.

      A ParserConfiguration accumulates data each time it is used to parse, so callers create a new one for each parse.

      Parameters:
      storeTokens - whether JavaParser should retain JavaToken objects on the parsed AST, which precise diagnostics for malformed stub files need
      Returns:
      a new stub-file parser configuration
    • main

      public static void main(String[] args)
      Processes each provided command-line argument; see class documentation for details.
      Parameters:
      args - command-line arguments: an optional SKIP_UNLOADABLE_ANNOTATIONS_FLAG, followed by one or more directories to process
    • dirnameToPath

      public static Path dirnameToPath(String dir)
      Converts a directory name to a path. It issues a warning and terminates the program if the argument does not exist or is not a directory.

      Unlike Paths.get, it handles "." which means the current directory in Unix.

      Parameters:
      dir - a directory name
      Returns:
      a path for the directory name