Skip to content

Commit

Permalink
Improve documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
theEvilReaper committed Jan 10, 2024
1 parent 0a6a4fe commit abc2577
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,23 @@ import net.theevilreaper.dartpoet.util.toImmutableSet
import kotlin.reflect.KClass

/**
* The [AnnotationSpec] contain all relevant data about a annotation.
* The [AnnotationSpec] contains all relevant data which describes a specific annotation.
* In the programming language Dart annotations are used to add additional information to your code.
* A metadata annotation begins with the character @, followed by either a reference to a compile-time constant (such as deprecated) or a call to a constant constructor.
*
* Four annotations are available to all Dart code:
* - @deprecated,
* - @override
* - @pragma
*
* Please note you can use the predefined annotations from the JDK or Kotlin but doesn't except that they work in Dart.
* @author theEvilReaper
* @version 1.0.0
* @since
**/
class AnnotationSpec(
builder: AnnotationSpecBuilder
) {

internal val typeName: TypeName = builder.typeName
internal val content: Set<CodeBlock> = builder.content.toImmutableSet()
internal val hasMultipleContentParts = content.size > 1
Expand Down Expand Up @@ -51,18 +58,48 @@ class AnnotationSpec(
return builder
}

/**
* The companion object contains some helper methods to create a new instance from the [AnnotationSpecBuilder].
*/
companion object {

/**
* Creates a new instance from the [AnnotationSpecBuilder].
* @return the created instance
* @param name the name for the annotation provided as [String]
* @return the created instance from the [AnnotationSpecBuilder]
*/
@JvmStatic
fun builder(name: String) = AnnotationSpecBuilder(ClassName(name))

/**
* Creates a new instance from the [AnnotationSpecBuilder].
* @param type the type for the annotation provided as [ClassName]
* @return the created instance from the [AnnotationSpecBuilder]
*/
@JvmStatic
fun builder(type: ClassName) = AnnotationSpecBuilder(type)

/**
* Creates a new instance from the [AnnotationSpecBuilder].
* @param type the type for the annotation provided as [TypeName]
* @return the created instance from the [AnnotationSpecBuilder]
*/
@JvmStatic
fun builder(type: TypeName) = AnnotationSpecBuilder(type)

/**
* Creates a new instance from the [AnnotationSpecBuilder].
* @param type the type for the annotation provided as [Class]
* @return the created instance from the [AnnotationSpecBuilder]
*/
@JvmStatic
fun builder(type: Class<*>) = AnnotationSpecBuilder(type.asClassName())

/**
* Creates a new instance from the [AnnotationSpecBuilder].
* @param type the type for the annotation provided as [KClass]
* @return the created instance from the [AnnotationSpecBuilder]
*/
@JvmStatic
fun builder(type: KClass<out Annotation>) = AnnotationSpecBuilder(type.asClassName())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,35 @@ import net.theevilreaper.dartpoet.type.TypeName
class AnnotationSpecBuilder(
internal val typeName: TypeName
) {

/**
* Stores the content parts from the annotation.
*/
internal val content: MutableList<CodeBlock> = mutableListOf()

/**
* Add a content part to the annotation.
* @param format the format string
* @param args the arguments for the format string
* @return the given instance of an [AnnotationSpecBuilder]
*/
fun content(format: String, vararg args: Any) = apply {
content(CodeBlock.of(format, *args))
}

/**
* Add a content part to the annotation.
* @param codeFragment the code fragment to add provided as [CodeBlock]
* @return the given instance of an [AnnotationSpecBuilder]
*/
fun content(codeFragment: CodeBlock) = apply {
this.content += codeFragment
}

/**
* Add a content part to the annotation.
* @param codeFragment the code fragment to add provided as lambda block
* @return the given instance of an [AnnotationSpecBuilder]
*/
fun content(codeFragment: () -> CodeBlock) = apply {
this.content += codeFragment()
}
Expand Down
91 changes: 87 additions & 4 deletions src/main/kotlin/net/theevilreaper/dartpoet/clazz/ClassBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ import java.lang.reflect.Type
import kotlin.reflect.KClass

//TODO: Add check to prevent illegal modifiers on some class combinations
/**
* The [ClassBuilder] is the entry point to describe all relevant object structures which are needed to generate a class.
*
* @since 1.0.0
*/
class ClassBuilder internal constructor(
internal val name: String?,
internal val classType: ClassType,
Expand All @@ -36,7 +41,6 @@ class ClassBuilder internal constructor(
internal var inheritKeyWord: InheritKeyword? = null
internal var endWithNewLine = false


/**
* Add a constant [PropertySpec] to the file.
* @param constant the property to add
Expand Down Expand Up @@ -73,18 +77,32 @@ class ClassBuilder internal constructor(

/**
* Set the class from which the generated class should inherit.
* @param className the name from the class
* @param superClass the name from the super class as [TypeName]
* @param inheritKeyword the keyword to use for the inheritance
* @return the given instance of an [ClassBuilder]
*/
fun superClass(superClass: TypeName, inheritKeyword: InheritKeyword) = apply {
this.superClass = superClass
inheritKeyWord = inheritKeyword
}

/**
* Set the class from which the generated class should inherit.
* @param superClass the name from the super class as [Type]
* @param inheritKeyword the keyword to use for the inheritance
* @return the given instance of an [ClassBuilder]
*/
fun superClass(superClass: Type, inheritKeyword: InheritKeyword) = apply {
this.superClass = superClass.asTypeName()
inheritKeyWord = inheritKeyword
}

/**
* Set the class from which the generated class should inherit.
* @param superClass the name from the super class as [KClass]
* @param inheritKeyword the keyword to use for the inheritance
* @return the given instance of an [ClassBuilder]
*/
fun superClass(superClass: KClass<*>, inheritKeyword: InheritKeyword) = apply {
this.superClass = superClass.asTypeName()
inheritKeyWord = inheritKeyword
Expand All @@ -98,54 +116,119 @@ class ClassBuilder internal constructor(
this.endWithNewLine = endWithNewLine
}

/**
* Add a [PropertySpec] to the class builder.
* @param propertySpec the property to add
* @return the given instance of an [ClassBuilder]
*/
fun property(propertySpec: PropertySpec) = apply {
this.propertyStack += propertySpec
}

/**
* Add a [PropertySpec] to the class builder over a lambda reference.
* @param propertySpec the property to add
* @return the given instance of an [ClassBuilder]
*/
fun property(propertySpec: () -> PropertySpec) = apply {
this.propertyStack += propertySpec()
}

/**
* Add an array of [PropertySpec] to the class builder.
* @param properties the properties to add
* @return the given instance of an [ClassBuilder]
*/
fun properties(vararg properties: PropertySpec) = apply {
this.propertyStack += properties
}

/**
* Add a [FunctionSpec] to the class builder.
* @param function the function to add
* @return the given instance of an [ClassBuilder]
*/
fun function(function: FunctionSpec) = apply {
this.functionStack += function
}

/**
* Add a [FunctionSpec] to the class builder over a lambda reference.
* @param function the function to add
* @return the given instance of an [ClassBuilder]
*/
fun function(function: () -> FunctionSpec) = apply {
this.functionStack += function()
}

/**
* Add a [ConstructorSpec] to the class builder.
* @param constructor the constructor to add
* @return the given instance of an [ClassBuilder]
*/
fun constructor(constructor: ConstructorSpec) = apply {
this.constructorStack += constructor
}

/**
* Add a [ConstructorSpec] to the class builder over a lambda reference.
* @param constructor the constructor to add
* @return the given instance of an [ClassBuilder]
*/
fun constructor(constructor: () -> ConstructorSpec) = apply {
this.constructorStack += constructor()
}

override fun annotation(annotation: () -> AnnotationSpec) = apply {
/**
* Add a [AnnotationSpec] to the class builder.
* @param annotation the annotation to add
* @return the given instance of an [ClassBuilder]
*/
override fun annotation(annotation: AnnotationSpec) = apply {
this.classMetaData.annotation(annotation)
}

override fun annotation(annotation: AnnotationSpec) = apply {
/**
* Add a [AnnotationSpec] to the class builder over a lambda reference.
* @param annotation the annotation to add
* @return the given instance of an [ClassBuilder]
*/
override fun annotation(annotation: () -> AnnotationSpec) = apply {
this.classMetaData.annotation(annotation)
}

/**
* Add an array of [AnnotationSpec] to the class builder.
* @param annotations the annotations to add
* @return the given instance of an [ClassBuilder]
*/
override fun annotations(vararg annotations: AnnotationSpec) = apply {
this.classMetaData.annotations(*annotations)
}

/**
* Add a [DartModifier] value to the class builder.
* @param modifier the modifier to add
* @return the given instance of an [ClassBuilder]
*/
override fun modifier(modifier: DartModifier) = apply {
this.classMetaData.modifier(modifier)
}

/**
* Add a [DartModifier] value to the class builder over a lambda reference.
* @param modifier the modifier to add
* @return the given instance of an [ClassBuilder]
*/
override fun modifier(modifier: () -> DartModifier) = apply {
this.classMetaData.modifier(modifier)
}

/**
* Add an array of [DartModifier] values to the class builder.
* @param modifiers the modifiers to add
* @return the given instance of an [ClassBuilder]
*/
override fun modifiers(vararg modifiers: DartModifier) = apply {
this.classMetaData.modifiers(*modifiers)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package net.theevilreaper.dartpoet.clazz
import net.theevilreaper.dartpoet.DartModifier

/**
* The enum contains all class variant which are currently available in dart.
* The [ClassType] enum defines all class variants which are currently available in the programming language Dart.
* @author theEvilReaper
* @version 1.0.0
* @since 1.0.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import net.theevilreaper.dartpoet.util.EMPTY_STRING
import net.theevilreaper.dartpoet.util.NEW_LINE
import net.theevilreaper.dartpoet.util.toImmutableList

/**
* The [AnnotationWriter] is responsible for writing the data from an [AnnotationSpec] into valid code for dart code.
* @since 1.0.0
* @author theEvilReaper
*/
class AnnotationWriter {

fun emit(spec: AnnotationSpec, writer: CodeWriter, inline: Boolean) {
Expand Down

0 comments on commit abc2577

Please sign in to comment.