Class Analyser
FreeBuilder datatype type, returning
a GeneratedType for a Builder superclass derived from its API.
Any deviations from the FreeBuilder spec in the user's class will result in errors being issued, but unless code generation is totally impossible, a type will still be returned. This allows the user to extend an existing type without worrying that a mistake will cause compiler errors in all dependent code—which would make it very hard to find the real error.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classThrown when a FreeBuilder type cannot have a Builder type generated, for instance if it is private.private classprivate static final classVisitor that returns true if the visited type extends a generatedsuperclassin the same package. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final Stringprivate static final SimpleTypeVisitor8<Boolean, ?> Visitor that returns true if a cast to the visited type is guaranteed to be fully checked at runtime.private final Elementsprivate final ProcessingEnvironmentprivate static final SimpleTypeVisitor8<Boolean, ?> Visitor that returns true if the visited type is an unbounded wildcard, i.e.private final Messagerprivate final Typesprivate static final String -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate voidaddPropertyData(Property.Builder propertyBuilder, TypeElement valueType, ExecutableElement method, Optional<JacksonSupport> jacksonSupport) Introspectsmethod, as found onvalueType.(package private) GeneratedTypeanalyse(TypeElement type) Returns a BuilderGeneratedTypefortype.private static StringcamelCaseToAllCaps(String camelCase) Converts camelCaseConvention to ALL_CAPS_CONVENTION.private Datatype.BuilderconstructionAndExtension(DeclaredType builder) private static PropertyCodeGeneratorfindUnderriddenMethods(Iterable<ExecutableElement> methods) Find any standard methods the user has 'underridden' in their type.private StringReturns the simple name of the builder class that should be generated for the given type.private booleanhasToBuilderMethod(DeclaredType builder, boolean isExtensible, Iterable<ExecutableElement> methods) Find a toBuilder method, if the user has provided one.private Predicate<TypeMirror> private booleanisToBuilderMethod(DeclaredType declaredType, ExecutableElement method) private static booleanisUnderride(ExecutableElement method) private static Optional<Datatype.StandardMethod> maybeStandardMethod(ExecutableElement method) Returns whether a method is one of theDatatype.StandardMethods, and if so, which.private Map<Property, PropertyCodeGenerator> pickPropertyGenerators(TypeElement type, Datatype datatype, DeclaredType builder, Iterable<ExecutableElement> methods) private Set<ExecutableElement> removeNonGetterMethods(DeclaredType builder, Iterable<ExecutableElement> methods) private booleanshouldBuilderBeSerializable(DeclaredType builder) private Optional<DeclaredType> tryFindBuilder(QualifiedName superclass, TypeElement valueType) Looks for a nested type invalueTypecalled Builder, and verifies it extends the autogeneratedsuperclass.private voidverifyType(TypeElement type, PackageElement pkg) Basic sanity-checking to ensure we can fulfil the @FreeBuilder contract for this type.private voidIssues an error iftypedoes not have a package-visible no-args constructor.
-
Field Details
-
BUILDER_SIMPLE_NAME_TEMPLATE
- See Also:
-
USER_BUILDER_NAME
- See Also:
-
env
-
elements
-
messager
-
types
-
CAST_IS_FULLY_CHECKED
Visitor that returns true if a cast to the visited type is guaranteed to be fully checked at runtime. This is true for any type that is non-generic, raw, or parameterized with unbounded wildcards, such asInteger,ListorMap<?, ?>. -
IS_UNBOUNDED_WILDCARD
Visitor that returns true if the visited type is an unbounded wildcard, i.e.<?>.
-
-
Constructor Details
-
Analyser
Analyser(ProcessingEnvironment env, Messager messager)
-
-
Method Details
-
analyse
Returns a BuilderGeneratedTypefortype.- Throws:
Analyser.CannotGenerateCodeException- if code cannot be generated, e.g. if the type is private
-
verifyType
private void verifyType(TypeElement type, PackageElement pkg) throws Analyser.CannotGenerateCodeException Basic sanity-checking to ensure we can fulfil the @FreeBuilder contract for this type. -
verifyTypeIsConstructible
private void verifyTypeIsConstructible(TypeElement type) throws Analyser.CannotGenerateCodeException Issues an error iftypedoes not have a package-visible no-args constructor. -
findUnderriddenMethods
private Map<Datatype.StandardMethod,Datatype.UnderrideLevel> findUnderriddenMethods(Iterable<ExecutableElement> methods) Find any standard methods the user has 'underridden' in their type. -
hasToBuilderMethod
private boolean hasToBuilderMethod(DeclaredType builder, boolean isExtensible, Iterable<ExecutableElement> methods) Find a toBuilder method, if the user has provided one. -
isToBuilderMethod
-
removeNonGetterMethods
private Set<ExecutableElement> removeNonGetterMethods(DeclaredType builder, Iterable<ExecutableElement> methods) -
isUnderride
-
tryFindBuilder
Looks for a nested type invalueTypecalled Builder, and verifies it extends the autogeneratedsuperclass.If the value type is generic, the builder type must match, and the returned DeclaredType will be parameterized with the type variables from the value type, not the builder. (This makes type checking massively easier.)
Issues an error if the wrong type is being subclassed—a typical copy-and-paste error when renaming an existing @FreeBuilder type, or using one as a template.
-
constructionAndExtension
-
pickPropertyGenerators
private Map<Property,PropertyCodeGenerator> pickPropertyGenerators(TypeElement type, Datatype datatype, DeclaredType builder, Iterable<ExecutableElement> methods) -
getMethodsInvokedInBuilderConstructor
-
addPropertyData
private void addPropertyData(Property.Builder propertyBuilder, TypeElement valueType, ExecutableElement method, Optional<JacksonSupport> jacksonSupport) Introspectsmethod, as found onvalueType. -
createCodeGenerator
-
generatedBuilderSimpleName
Returns the simple name of the builder class that should be generated for the given type.This is simply the
BUILDER_SIMPLE_NAME_TEMPLATEwith the original type name substituted in. (If the original type is nested, its enclosing classes will be included, separated with underscores, to ensure uniqueness.) -
shouldBuilderBeSerializable
-
maybeStandardMethod
Returns whether a method is one of theDatatype.StandardMethods, and if so, which. -
camelCaseToAllCaps
Converts camelCaseConvention to ALL_CAPS_CONVENTION. -
isEqualTo
-