Class SourceBuilder
- java.lang.Object
-
- org.inferred.freebuilder.processor.source.SourceBuilder
-
public class SourceBuilder extends java.lang.ObjectSource 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 Summary
Fields Modifier and Type Field Description private static java.lang.StringLINE_SEPARATORprivate CompilationUnitBuildersource
-
Constructor Summary
Constructors Modifier Constructor Description privateSourceBuilder(ScopeHandler.Reflection reflect, FeatureSet features)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidadd(java.lang.Object arg)SourceBuilderadd(java.lang.String fmt, java.lang.Object... args)Appends formatted text to the source.SourceBuilderadd(Excerpt excerpt)Equivalent toadd("%s", excerpt).SourceBuilderaddLine(java.lang.String fmt, java.lang.Object... args)Appends a formatted line of code to the source.<T extends Feature<T>>
Tfeature(FeatureType<T> featureType)Returns the instance offeatureTypeappropriate for the source being written.static SourceBuilderforEnvironment(javax.annotation.processing.ProcessingEnvironment env, FeatureSet features)Returns aSourceBuilder.static SourceBuilderforTesting(Feature<?>... features)Returns aSourceBuilderusingfeatures.static SourceBuilderforTesting(FeatureSet features)Returns aSourceBuilderusingfeatures.Scopescope()Returns the current scope (e.g.java.lang.StringtoString()QualifiedNametypename()Return the qualified name of the main type declared by this unit.
-
-
-
Field Detail
-
LINE_SEPARATOR
private static final java.lang.String LINE_SEPARATOR
-
source
private final CompilationUnitBuilder source
-
-
Constructor Detail
-
SourceBuilder
private SourceBuilder(ScopeHandler.Reflection reflect, FeatureSet features)
-
-
Method Detail
-
forEnvironment
public static SourceBuilder forEnvironment(javax.annotation.processing.ProcessingEnvironment env, FeatureSet features)
Returns aSourceBuilder.envwill be inspected for potential import collisions. Iffeaturesis not null, it will be used instead of those deduced fromenv.
-
forTesting
public static SourceBuilder forTesting(Feature<?>... features)
Returns aSourceBuilderusingfeatures. The system classloader will be inspected for potential import collisions.
-
forTesting
public static SourceBuilder forTesting(FeatureSet features)
Returns aSourceBuilderusingfeatures. 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
%sand%n$s. Most args are converted according to theirObject.toString()method, except that:-
PackageandPackageElementinstances use their fully-qualified names (no "package " prefix). -
Class,TypeElement,DeclaredTypeandQualifiedNameinstances use their qualified names where necessary, or shorter versions if a suitable import line can be added. -
Excerptinstances haveExcerpt.addTo(SourceBuilder)called.
-
-
add
public SourceBuilder add(Excerpt excerpt)
Equivalent toadd("%s", excerpt).
-
addLine
public SourceBuilder addLine(java.lang.String fmt, java.lang.Object... args)
Appends a formatted line of code to the source.Formatting supports
%sand%n$s. Most args are converted according to theirObject.toString()method, except that:-
PackageandPackageElementinstances use their fully-qualified names (no "package " prefix). -
Class,TypeElement,DeclaredTypeandQualifiedNameinstances use their qualified names where necessary, or shorter versions if a suitable import line can be added. -
Excerptinstances haveExcerpt.addTo(SourceBuilder)called.
-
-
feature
public <T extends Feature<T>> T feature(FeatureType<T> featureType)
Returns the instance offeatureTypeappropriate for the source being written. For instance,code.feature(returns true if the Guava library can be used in the generated source code.GUAVA).isAvailable()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:
toStringin classjava.lang.Object
-
add
private void add(java.lang.Object arg)
-
-