Skip to content

Commit

Permalink
Add default annotations, documentation update (#85)
Browse files Browse the repository at this point in the history
* Add common annotations from Dart

* Update basic annotation generation tests

* Update documentation

* Improve code structure
  • Loading branch information
theEvilReaper authored Jan 28, 2024
1 parent ab1d2a5 commit 3c563ba
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.theevilreaper.dartpoet.annotation

import net.theevilreaper.dartpoet.code.*
import net.theevilreaper.dartpoet.code.buildCodeString
import net.theevilreaper.dartpoet.code.writer.AnnotationWriter
import net.theevilreaper.dartpoet.type.ClassName
import net.theevilreaper.dartpoet.type.TypeName
Expand All @@ -10,34 +9,37 @@ import net.theevilreaper.dartpoet.util.toImmutableSet
import kotlin.reflect.KClass

/**
* 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.
* The [AnnotationSpec] class encapsulates essential data that defines a metadata / annotation structure.
* Annotations can be used in Dart to add additional information to the code base.
* Typically, an annotation starts with the character @, followed by an identifier,
* and optionally with meta information which are encapsulated in round brackets.
* It's important to note that you can't really use the predefined annotations from the JDK or Kotlin because the languages are not interoperable to Dart,
*
* Four annotations are available to all Dart code:
* Common annotations from Dart:
* - @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.
* @param builder the builder instance to retrieve the data from
* @author theEvilReaper
* @version 1.0.0
* @since
**/
* @since 1.0.0
*/
class AnnotationSpec(
builder: AnnotationSpecBuilder
builder: AnnotationSpecBuilder,
) {
internal val typeName: TypeName = builder.typeName
internal val content: Set<CodeBlock> = builder.content.toImmutableSet()
internal val hasMultipleContentParts = content.size > 1
internal val hasContent: Boolean = content.isNotEmpty()
internal val hasMultipleContentParts: Boolean = content.size > 1

/**
* Triggers an [AnnotationWriter] to write the spec object into code.
* Triggers the write process for an [AnnotationSpec] object.
* @param codeWriter the [CodeWriter] instance to write the spec to
* @param inline if the spec should be written inline
*/
internal fun write(
codeWriter: CodeWriter,
inline: Boolean = true
inline: Boolean = true,
) {
AnnotationWriter().emit(this, codeWriter, inline = inline)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal class AnnotationWriter {
writer.emit(ANNOTATION_CHAR)
writer.emitCode("%T", spec.typeName)

if (spec.content.isEmpty()) return
if (!spec.hasContent) return

val whitespace = if (inline) EMPTY_STRING else NEW_LINE
val memberSeparator = if (inline) ", " else ",\n"
Expand Down Expand Up @@ -56,7 +56,7 @@ internal class AnnotationWriter {
spec: AnnotationSpec,
inline: Boolean,
memberSeparator: String,
memberSuffix: String
memberSuffix: String,
): CodeBlock {
return spec.content.toImmutableList()
.map { if (inline) it.replaceAll("[⇥|⇤]", EMPTY_STRING) else it }
Expand Down
14 changes: 14 additions & 0 deletions src/main/kotlin/net/theevilreaper/dartpoet/type/TypeNames.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.theevilreaper.dartpoet.type

import net.theevilreaper.dartpoet.annotation.AnnotationSpec

/**
* The file contains some common used [ClassName] instances which are used in the library.
* It contains only the primitive types which are supported by Dart.
Expand All @@ -25,3 +27,15 @@ val STRING: ClassName = ClassName("String")
// Represents the dynamic type in Dart
@JvmField
val DYNAMIC: ClassName = DynamicClassName()

// Represents the pragma metadata annotation from Dart
@JvmField
val PRAGMA: AnnotationSpec = AnnotationSpec.builder("pragma").build()

// Represents the override metadata annotation from Dart
@JvmField
val OVERRIDE: AnnotationSpec = AnnotationSpec.builder("override").build()

// Represents the deprecated metadata annotation from Dart
@JvmField
val DEPRECATED: AnnotationSpec = AnnotationSpec.builder("deprecated").build()
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
package net.theevilreaper.dartpoet.annotation

import net.theevilreaper.dartpoet.type.ClassName
import net.theevilreaper.dartpoet.type.DEPRECATED
import net.theevilreaper.dartpoet.type.OVERRIDE
import net.theevilreaper.dartpoet.type.PRAGMA
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.MethodSource
import java.util.stream.Stream
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals

class AnnotationSpecTest {

private val expectedAnnotation = "@Override"
companion object {

@Test
fun `test simple annotation without content`() {
val annotation = AnnotationSpec.builder(Override::class).build()
assertEquals(expectedAnnotation, annotation.toString())
@JvmStatic
private fun testSimpleAnnotations() = Stream.of(
Arguments.of(OVERRIDE, "@override"),
Arguments.of(DEPRECATED, "@deprecated"),
Arguments.of(PRAGMA, "@pragma"),
Arguments.of(AnnotationSpec.builder(Override::class).build(), "@Override")
)
}

@ParameterizedTest
@MethodSource("testSimpleAnnotations")
fun `test simple annotations`(annotation: AnnotationSpec, expected: String) {
assertEquals(expected, annotation.toString())
}

@Test
Expand Down

0 comments on commit 3c563ba

Please sign in to comment.