diff --git a/src/main/kotlin/net/theevilreaper/dartpoet/DartFile.kt b/src/main/kotlin/net/theevilreaper/dartpoet/DartFile.kt index 9138a787..74e69afb 100644 --- a/src/main/kotlin/net/theevilreaper/dartpoet/DartFile.kt +++ b/src/main/kotlin/net/theevilreaper/dartpoet/DartFile.kt @@ -3,15 +3,14 @@ package net.theevilreaper.dartpoet import net.theevilreaper.dartpoet.annotation.AnnotationSpec import net.theevilreaper.dartpoet.clazz.ClassSpec import net.theevilreaper.dartpoet.code.CodeWriter +import net.theevilreaper.dartpoet.code.WriterHelper import net.theevilreaper.dartpoet.code.buildCodeString -import net.theevilreaper.dartpoet.code.writer.DartFileWriter import net.theevilreaper.dartpoet.directive.DartDirective import net.theevilreaper.dartpoet.directive.ExportDirective import net.theevilreaper.dartpoet.directive.LibraryDirective import net.theevilreaper.dartpoet.directive.PartDirective import net.theevilreaper.dartpoet.directive.RelativeDirective import net.theevilreaper.dartpoet.extension.ExtensionSpec -import net.theevilreaper.dartpoet.function.FunctionSpec import net.theevilreaper.dartpoet.util.* import net.theevilreaper.dartpoet.property.consts.ConstantPropertySpec import net.theevilreaper.dartpoet.util.DART_FILE_ENDING @@ -58,23 +57,11 @@ class DartFile internal constructor( } internal fun write(codeWriter: CodeWriter) { - DartFileWriter().write(this, codeWriter) + WriterHelper.fileWriter.write(this, codeWriter) } override fun toString() = buildCodeString { write(this) } - internal val callEmit: (Any, CodeWriter) -> Unit = { o: Any, c: CodeWriter -> - emitInternal(o, c) - } - - private fun emitInternal(o: Any, c: CodeWriter) { - when (o::class) { - FunctionSpec::class -> { - callEmit(o, c) - } - } - } - /** * Writes the content from a [DartFile] to the given [Appendable]. * @param out the [Appendable] where the content should be written diff --git a/src/main/kotlin/net/theevilreaper/dartpoet/annotation/AnnotationSpec.kt b/src/main/kotlin/net/theevilreaper/dartpoet/annotation/AnnotationSpec.kt index f975dd87..422b847d 100644 --- a/src/main/kotlin/net/theevilreaper/dartpoet/annotation/AnnotationSpec.kt +++ b/src/main/kotlin/net/theevilreaper/dartpoet/annotation/AnnotationSpec.kt @@ -1,7 +1,6 @@ package net.theevilreaper.dartpoet.annotation import net.theevilreaper.dartpoet.code.* -import net.theevilreaper.dartpoet.code.writer.AnnotationWriter import net.theevilreaper.dartpoet.type.ClassName import net.theevilreaper.dartpoet.type.TypeName import net.theevilreaper.dartpoet.type.asClassName @@ -41,7 +40,7 @@ class AnnotationSpec internal constructor( codeWriter: CodeWriter, inline: Boolean = true, ) { - AnnotationWriter().emit(this, codeWriter, inline = inline) + WriterHelper.annotationWriter.emit(this, codeWriter, inline = inline) } /** diff --git a/src/main/kotlin/net/theevilreaper/dartpoet/clazz/ClassSpec.kt b/src/main/kotlin/net/theevilreaper/dartpoet/clazz/ClassSpec.kt index 22cc30ec..cbe19a5e 100644 --- a/src/main/kotlin/net/theevilreaper/dartpoet/clazz/ClassSpec.kt +++ b/src/main/kotlin/net/theevilreaper/dartpoet/clazz/ClassSpec.kt @@ -6,6 +6,7 @@ import net.theevilreaper.dartpoet.DartModifier.WITH import net.theevilreaper.dartpoet.InheritKeyword import net.theevilreaper.dartpoet.annotation.AnnotationSpec import net.theevilreaper.dartpoet.code.CodeWriter +import net.theevilreaper.dartpoet.code.WriterHelper import net.theevilreaper.dartpoet.code.buildCodeString import net.theevilreaper.dartpoet.code.writer.ClassWriter import net.theevilreaper.dartpoet.enum.EnumPropertySpec @@ -75,7 +76,7 @@ class ClassSpec internal constructor( * @param codeWriter the [CodeWriter] instance to apply the data */ internal fun write(codeWriter: CodeWriter) { - ClassWriter().write(this, codeWriter) + WriterHelper.classWriter.write(this, codeWriter) } /** diff --git a/src/main/kotlin/net/theevilreaper/dartpoet/code/WriterHelper.kt b/src/main/kotlin/net/theevilreaper/dartpoet/code/WriterHelper.kt new file mode 100644 index 00000000..3943065f --- /dev/null +++ b/src/main/kotlin/net/theevilreaper/dartpoet/code/WriterHelper.kt @@ -0,0 +1,35 @@ +package net.theevilreaper.dartpoet.code + +import net.theevilreaper.dartpoet.code.writer.* +import org.jetbrains.annotations.ApiStatus.Internal +import org.jetbrains.annotations.ApiStatus.NonExtendable + +/** + * A helper class that holds references to writer implementations. + * Maintaining a single instance of each writer improves runtime performance + * by avoiding the overhead of creating new instances for each write operation. + * + * Since writer classes do not store any state, creating new instances is unnecessary. + * + * **Note**: Update this class only when adding or removing writer implementations. + * + * @since 1.0.0 + * author theEvilReaper + */ +@Internal +@NonExtendable +internal object WriterHelper { + + internal val annotationWriter by lazy { AnnotationWriter() } + internal val classWriter by lazy { ClassWriter() } + internal val constantPropertyWriter by lazy { ConstantPropertyWriter() } + internal val constructorWriter by lazy { ConstructorWriter() } + internal val fileWriter by lazy { DartFileWriter() } + internal val enumPropertyWriter by lazy { EnumPropertyWriter() } + internal val extensionWriter by lazy { ExtensionWriter() } + internal val factoryWriter by lazy { FactoryWriter() } + internal val functionWriter by lazy { FunctionWriter() } + internal val parameterWriter by lazy { ParameterWriter() } + internal val propertyWriter by lazy { PropertyWriter() } + internal val typeDefWriter by lazy { TypeDefWriter() } +} \ No newline at end of file diff --git a/src/main/kotlin/net/theevilreaper/dartpoet/constructor/ConstructorSpec.kt b/src/main/kotlin/net/theevilreaper/dartpoet/constructor/ConstructorSpec.kt index 2206fcf2..80ecdac7 100644 --- a/src/main/kotlin/net/theevilreaper/dartpoet/constructor/ConstructorSpec.kt +++ b/src/main/kotlin/net/theevilreaper/dartpoet/constructor/ConstructorSpec.kt @@ -1,6 +1,7 @@ package net.theevilreaper.dartpoet.constructor import net.theevilreaper.dartpoet.code.CodeWriter +import net.theevilreaper.dartpoet.code.WriterHelper import net.theevilreaper.dartpoet.code.buildCodeString import net.theevilreaper.dartpoet.code.writer.ConstructorWriter import net.theevilreaper.dartpoet.util.toImmutableList @@ -37,7 +38,7 @@ class ConstructorSpec internal constructor( * @param codeWriter the instance to a [CodeWriter] to append the data */ internal fun write(codeWriter: CodeWriter) { - ConstructorWriter().write(this, codeWriter) + WriterHelper.constructorWriter.write(this, codeWriter) } /** diff --git a/src/main/kotlin/net/theevilreaper/dartpoet/constructor/factory/FactorySpec.kt b/src/main/kotlin/net/theevilreaper/dartpoet/constructor/factory/FactorySpec.kt index 42c457d7..9a31a983 100644 --- a/src/main/kotlin/net/theevilreaper/dartpoet/constructor/factory/FactorySpec.kt +++ b/src/main/kotlin/net/theevilreaper/dartpoet/constructor/factory/FactorySpec.kt @@ -3,6 +3,7 @@ package net.theevilreaper.dartpoet.constructor.factory import net.theevilreaper.dartpoet.annotation.AnnotationSpec import net.theevilreaper.dartpoet.code.CodeBlock import net.theevilreaper.dartpoet.code.CodeWriter +import net.theevilreaper.dartpoet.code.WriterHelper import net.theevilreaper.dartpoet.code.buildCodeString import net.theevilreaper.dartpoet.code.writer.FactoryWriter import net.theevilreaper.dartpoet.constructor.ConstructorBase @@ -54,7 +55,7 @@ class FactorySpec( * @see FactoryWriter */ internal fun write(codeWriter: CodeWriter) { - FactoryWriter().write(this, codeWriter) + WriterHelper.factoryWriter.write(this, codeWriter) } /** diff --git a/src/main/kotlin/net/theevilreaper/dartpoet/enum/EnumPropertySpec.kt b/src/main/kotlin/net/theevilreaper/dartpoet/enum/EnumPropertySpec.kt index 6740c22d..cf9fbab4 100644 --- a/src/main/kotlin/net/theevilreaper/dartpoet/enum/EnumPropertySpec.kt +++ b/src/main/kotlin/net/theevilreaper/dartpoet/enum/EnumPropertySpec.kt @@ -1,6 +1,7 @@ package net.theevilreaper.dartpoet.enum import net.theevilreaper.dartpoet.code.CodeWriter +import net.theevilreaper.dartpoet.code.WriterHelper import net.theevilreaper.dartpoet.code.buildCodeString import net.theevilreaper.dartpoet.code.writer.EnumPropertyWriter import net.theevilreaper.dartpoet.util.toImmutableList @@ -31,7 +32,7 @@ class EnumPropertySpec internal constructor( internal fun write(codeWriter: CodeWriter) { - EnumPropertyWriter().write(this, codeWriter) + WriterHelper.enumPropertyWriter.write(this, codeWriter) } /** diff --git a/src/main/kotlin/net/theevilreaper/dartpoet/extension/ExtensionSpec.kt b/src/main/kotlin/net/theevilreaper/dartpoet/extension/ExtensionSpec.kt index cae61f63..617dc797 100644 --- a/src/main/kotlin/net/theevilreaper/dartpoet/extension/ExtensionSpec.kt +++ b/src/main/kotlin/net/theevilreaper/dartpoet/extension/ExtensionSpec.kt @@ -2,6 +2,7 @@ package net.theevilreaper.dartpoet.extension import net.theevilreaper.dartpoet.code.CodeBlock import net.theevilreaper.dartpoet.code.CodeWriter +import net.theevilreaper.dartpoet.code.WriterHelper import net.theevilreaper.dartpoet.code.buildCodeString import net.theevilreaper.dartpoet.code.writer.ExtensionWriter import net.theevilreaper.dartpoet.function.FunctionSpec @@ -64,7 +65,7 @@ class ExtensionSpec internal constructor( * @param codeWriter the writer instance to apply the data as code */ internal fun write(codeWriter: CodeWriter) { - ExtensionWriter().write(this, codeWriter) + WriterHelper.extensionWriter.write(this, codeWriter) } /** diff --git a/src/main/kotlin/net/theevilreaper/dartpoet/function/FunctionSpec.kt b/src/main/kotlin/net/theevilreaper/dartpoet/function/FunctionSpec.kt index 35985fa9..52d98145 100644 --- a/src/main/kotlin/net/theevilreaper/dartpoet/function/FunctionSpec.kt +++ b/src/main/kotlin/net/theevilreaper/dartpoet/function/FunctionSpec.kt @@ -4,6 +4,7 @@ import net.theevilreaper.dartpoet.DartModifier import net.theevilreaper.dartpoet.annotation.AnnotationSpec import net.theevilreaper.dartpoet.code.CodeBlock import net.theevilreaper.dartpoet.code.CodeWriter +import net.theevilreaper.dartpoet.code.WriterHelper import net.theevilreaper.dartpoet.code.writer.FunctionWriter import net.theevilreaper.dartpoet.code.buildCodeString import net.theevilreaper.dartpoet.parameter.ParameterSpec @@ -73,7 +74,7 @@ class FunctionSpec internal constructor( * @param codeWriter the writer instance */ internal fun write(codeWriter: CodeWriter) { - FunctionWriter().write(this, codeWriter) + WriterHelper.functionWriter.write(this, codeWriter) } /** diff --git a/src/main/kotlin/net/theevilreaper/dartpoet/function/typedef/TypeDefSpec.kt b/src/main/kotlin/net/theevilreaper/dartpoet/function/typedef/TypeDefSpec.kt index 710fdc2e..5cbfce29 100644 --- a/src/main/kotlin/net/theevilreaper/dartpoet/function/typedef/TypeDefSpec.kt +++ b/src/main/kotlin/net/theevilreaper/dartpoet/function/typedef/TypeDefSpec.kt @@ -1,9 +1,9 @@ package net.theevilreaper.dartpoet.function.typedef import net.theevilreaper.dartpoet.code.CodeWriter +import net.theevilreaper.dartpoet.code.WriterHelper import net.theevilreaper.dartpoet.code.buildCodeString import net.theevilreaper.dartpoet.code.writer.FunctionWriter -import net.theevilreaper.dartpoet.code.writer.TypeDefWriter import net.theevilreaper.dartpoet.type.ClassName import net.theevilreaper.dartpoet.type.TypeName import net.theevilreaper.dartpoet.type.asTypeName @@ -50,7 +50,7 @@ class TypeDefSpec( * @param codeWriter the writer instance */ internal fun write(codeWriter: CodeWriter) { - TypeDefWriter().write(this, codeWriter) + WriterHelper.typeDefWriter.write(this, codeWriter) } /** diff --git a/src/main/kotlin/net/theevilreaper/dartpoet/parameter/ParameterSpec.kt b/src/main/kotlin/net/theevilreaper/dartpoet/parameter/ParameterSpec.kt index 0a9d31be..dd0a66b2 100644 --- a/src/main/kotlin/net/theevilreaper/dartpoet/parameter/ParameterSpec.kt +++ b/src/main/kotlin/net/theevilreaper/dartpoet/parameter/ParameterSpec.kt @@ -2,6 +2,7 @@ package net.theevilreaper.dartpoet.parameter import net.theevilreaper.dartpoet.DartModifier import net.theevilreaper.dartpoet.code.CodeWriter +import net.theevilreaper.dartpoet.code.WriterHelper import net.theevilreaper.dartpoet.code.buildCodeString import net.theevilreaper.dartpoet.code.writer.ParameterWriter import net.theevilreaper.dartpoet.type.ClassName @@ -51,7 +52,7 @@ class ParameterSpec internal constructor( * @param codeWriter the [CodeWriter] to which the parameter should be written */ internal fun write(codeWriter: CodeWriter) { - ParameterWriter().write(this, codeWriter) + WriterHelper.parameterWriter.write(this, codeWriter) } /** diff --git a/src/main/kotlin/net/theevilreaper/dartpoet/property/PropertySpec.kt b/src/main/kotlin/net/theevilreaper/dartpoet/property/PropertySpec.kt index b51e9118..9ec0774f 100644 --- a/src/main/kotlin/net/theevilreaper/dartpoet/property/PropertySpec.kt +++ b/src/main/kotlin/net/theevilreaper/dartpoet/property/PropertySpec.kt @@ -3,6 +3,7 @@ package net.theevilreaper.dartpoet.property import net.theevilreaper.dartpoet.DartModifier import net.theevilreaper.dartpoet.annotation.AnnotationSpec import net.theevilreaper.dartpoet.code.CodeWriter +import net.theevilreaper.dartpoet.code.WriterHelper import net.theevilreaper.dartpoet.code.writer.PropertyWriter import net.theevilreaper.dartpoet.code.buildCodeString import net.theevilreaper.dartpoet.type.ClassName @@ -60,7 +61,7 @@ class PropertySpec internal constructor( * @param codeWriter the [CodeWriter] to apply the content from the spec */ internal fun write(codeWriter: CodeWriter) { - PropertyWriter().write(this, codeWriter) + WriterHelper.propertyWriter.write(this, codeWriter) } /** diff --git a/src/main/kotlin/net/theevilreaper/dartpoet/property/consts/ConstantPropertySpec.kt b/src/main/kotlin/net/theevilreaper/dartpoet/property/consts/ConstantPropertySpec.kt index 65d8ed7c..0b2a9da8 100644 --- a/src/main/kotlin/net/theevilreaper/dartpoet/property/consts/ConstantPropertySpec.kt +++ b/src/main/kotlin/net/theevilreaper/dartpoet/property/consts/ConstantPropertySpec.kt @@ -2,6 +2,7 @@ package net.theevilreaper.dartpoet.property.consts import net.theevilreaper.dartpoet.DartModifier import net.theevilreaper.dartpoet.code.CodeWriter +import net.theevilreaper.dartpoet.code.WriterHelper import net.theevilreaper.dartpoet.code.buildCodeString import net.theevilreaper.dartpoet.code.writer.ConstantPropertyWriter import net.theevilreaper.dartpoet.property.PropertySpec @@ -44,7 +45,7 @@ class ConstantPropertySpec( * @param codeWriter the [CodeWriter] to apply the content from the spec */ internal fun write(codeWriter: CodeWriter) { - ConstantPropertyWriter().write(this, codeWriter) + WriterHelper.constantPropertyWriter.write(this, codeWriter) } /**