Class SourceBuilder


  • public class SourceBuilder
    extends java.lang.Object
    Source code builder, using format strings for readability, with sensible formatting for type objects.
     // Imports StringBuilder and appends "  StringBuilder foo;\n" to the source code.
     builder.addLine("  %s foo;", StringBuilder.class);
    • Field Detail

      • LINE_SEPARATOR

        private static final java.lang.String LINE_SEPARATOR
    • Method Detail

      • forEnvironment

        public static SourceBuilder forEnvironment​(javax.annotation.processing.ProcessingEnvironment env,
                                                   FeatureSet features)
        Returns a SourceBuilder. env will be inspected for potential import collisions. If features is not null, it will be used instead of those deduced from env.
      • forTesting

        public static SourceBuilder forTesting​(Feature<?>... features)
        Returns a SourceBuilder using features. The system classloader will be inspected for potential import collisions.
      • forTesting

        public static SourceBuilder forTesting​(FeatureSet features)
        Returns a SourceBuilder using features. The system classloader will be inspected for potential import collisions.
      • add

        public SourceBuilder add​(java.lang.String fmt,
                                 java.lang.Object... args)
        Appends formatted text to the source.

        Formatting supports %s and %n$s. Most args are converted according to their Object.toString() method, except that:

        • Package and PackageElement instances use their fully-qualified names (no "package " prefix).
        • Class, TypeElement, DeclaredType and QualifiedName instances use their qualified names where necessary, or shorter versions if a suitable import line can be added.
        • Excerpt instances have Excerpt.addTo(SourceBuilder) called.
      • addLine

        public SourceBuilder addLine​(java.lang.String fmt,
                                     java.lang.Object... args)
        Appends a formatted line of code to the source.

        Formatting supports %s and %n$s. Most args are converted according to their Object.toString() method, except that:

        • Package and PackageElement instances use their fully-qualified names (no "package " prefix).
        • Class, TypeElement, DeclaredType and QualifiedName instances use their qualified names where necessary, or shorter versions if a suitable import line can be added.
        • Excerpt instances have Excerpt.addTo(SourceBuilder) called.
      • feature

        public <T extends Feature<T>> T feature​(FeatureType<T> featureType)
        Returns the instance of featureType appropriate for the source being written. For instance, code.feature(GUAVA).isAvailable() returns true if the Guava library can be used in the generated source code.

        Fluent extension point for features dynamically determined based on the current ProcessingEnvironment.

        See Also:
        Feature
      • scope

        public Scope scope()
        Returns the current scope (e.g. visible method parameters).
      • typename

        public QualifiedName typename()
        Return the qualified name of the main type declared by this unit.
        Throws:
        java.lang.IllegalStateException - if no package or type has been declared
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • add

        private void add​(java.lang.Object arg)