Skip to content
This repository has been archived by the owner on Sep 17, 2023. It is now read-only.

Commit

Permalink
feat: generate source files with javapoet and kotlinpoet
Browse files Browse the repository at this point in the history
Signed-off-by: PranavPurwar <purwarpranav80@gmail.com>
  • Loading branch information
PranavPurwar authored and aikrq committed Mar 11, 2023
1 parent 9f8e666 commit 3b53a49
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 65 deletions.
1 change: 1 addition & 0 deletions java-completion/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
5 changes: 5 additions & 0 deletions project/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ plugins {
java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

dependencies {
implementation("com.squareup:javapoet:1.13.0")
implementation("com.squareup:kotlinpoet:1.12.0")
}
172 changes: 107 additions & 65 deletions project/src/main/java/org/cosmicide/project/templates/Templates.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package org.cosmicide.project.templates

import com.squareup.javapoet.JavaFile
import com.squareup.javapoet.MethodSpec
import com.squareup.javapoet.TypeSpec
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.KModifier
import javax.lang.model.element.Modifier
import com.squareup.kotlinpoet.TypeSpec as KotlinTypeSpec

/**
* Returns the template for a Java class.
*
Expand All @@ -9,15 +18,25 @@ package org.cosmicide.project.templates
* @return The template for a Java class.
*/
fun javaClass(className: String, packageName: String, body: String = ""): String {
return StringBuilder()
.appendLine("package $packageName;")
.appendLine()
.appendLine("public class $className {")
.appendLine(" public static void main(String[] args) {")
.append(body)
.appendLine(" }")
.appendLine("}")
.toString()

val main = MethodSpec.methodBuilder("main").apply {
addModifiers(Modifier.PUBLIC, Modifier.STATIC)
returns(Void.TYPE)
addParameter(Array<String>::class.java, "args")
if (body.isNotEmpty()) {
addCode(body)
}
}.build()

val clazz: TypeSpec = TypeSpec.classBuilder(className)
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.addMethod(main)
.build()

val javaFile =
JavaFile.builder(packageName, clazz).indent("\t").skipJavaLangImports(true).build()

return javaFile.toString()
}

/**
Expand All @@ -28,12 +47,12 @@ fun javaClass(className: String, packageName: String, body: String = ""): String
* @return The template for a Java interface.
*/
fun javaInterface(interfaceName: String, packageName: String): String {
return StringBuilder()
.appendLine("package $packageName;")
.appendLine()
.appendLine("public interface $interfaceName {")
.appendLine("}")
.toString()
val clazz: TypeSpec = TypeSpec.interfaceBuilder(interfaceName)
.addModifiers(Modifier.PUBLIC)
.build()

val javaFile = JavaFile.builder(packageName, clazz).indent("\t").build()
return javaFile.toString()
}

/**
Expand All @@ -44,12 +63,13 @@ fun javaInterface(interfaceName: String, packageName: String): String {
* @return The template for a Java enum.
*/
fun javaEnum(enumName: String, packageName: String): String {
return StringBuilder()
.appendLine("package $packageName;")
.appendLine()
.appendLine("public enum $enumName {")
.appendLine("}")
.toString()
val clazz: TypeSpec = TypeSpec.enumBuilder(enumName)
.addModifiers(Modifier.PUBLIC)
.addEnumConstant("EXAMPLE")
.build()

val javaFile = JavaFile.builder(packageName, clazz).indent("\t").build()
return javaFile.toString()
}

/**
Expand All @@ -61,15 +81,24 @@ fun javaEnum(enumName: String, packageName: String): String {
* @return The template for a Kotlin class.
*/
fun kotlinClass(className: String, packageName: String, body: String = ""): String {
return StringBuilder()
.appendLine("package $packageName")
.appendLine()
.appendLine("class $className {")
.appendLine(" fun main(args: Array<String>) {")
.append(body)
.appendLine(" }")
.appendLine("}")
.toString()
val clazz: KotlinTypeSpec = KotlinTypeSpec.classBuilder(className)
.addModifiers(KModifier.PUBLIC, KModifier.FINAL)
.addFunction(FunSpec.builder("main").apply {
addModifiers(KModifier.PUBLIC)
returns(Void.TYPE)
addParameter("args", Array<String>::class.java)
if (body.isNotEmpty()) {
addCode(body)
}
}.build())
.build()

val file = FileSpec.builder(packageName, className)
.addType(clazz)
.indent("\t")
.addKotlinDefaultImports(true)
.build()
return file.toString()
}

/**
Expand All @@ -80,12 +109,14 @@ fun kotlinClass(className: String, packageName: String, body: String = ""): Stri
* @return The template for a Kotlin interface.
*/
fun kotlinInterface(interfaceName: String, packageName: String): String {
return StringBuilder()
.appendLine("package $packageName")
.appendLine()
.appendLine("interface $interfaceName {")
.appendLine("}")
.toString()
val clazz: KotlinTypeSpec = KotlinTypeSpec.interfaceBuilder(interfaceName)
.addModifiers(KModifier.PUBLIC)
.build()
val file = FileSpec.builder(packageName, interfaceName)
.addType(clazz)
.indent("\t")
.build()
return file.toString()
}

/**
Expand All @@ -96,12 +127,14 @@ fun kotlinInterface(interfaceName: String, packageName: String): String {
* @return The template for a Kotlin object.
*/
fun kotlinObject(objectName: String, packageName: String): String {
return StringBuilder()
.appendLine("package $packageName")
.appendLine()
.appendLine("object $objectName {")
.appendLine("}")
.toString()
val clazz: KotlinTypeSpec = KotlinTypeSpec.objectBuilder(objectName)
.addModifiers(KModifier.PUBLIC)
.build()
val file = FileSpec.builder(packageName, objectName)
.addType(clazz)
.indent("\t")
.build()
return file.toString()
}

/**
Expand All @@ -112,12 +145,14 @@ fun kotlinObject(objectName: String, packageName: String): String {
* @return The template for a Kotlin enum.
*/
fun kotlinEnum(enumName: String, packageName: String): String {
return StringBuilder()
.appendLine("package $packageName")
.appendLine()
.appendLine("enum class $enumName {")
.appendLine("}")
.toString()
val clazz: KotlinTypeSpec = KotlinTypeSpec.enumBuilder(enumName)
.addModifiers(KModifier.PUBLIC)
.build()
val file = FileSpec.builder(packageName, enumName)
.addType(clazz)
.indent("\t")
.build()
return file.toString()
}

/**
Expand All @@ -128,11 +163,14 @@ fun kotlinEnum(enumName: String, packageName: String): String {
* @return The template for a Kotlin annotation.
*/
fun kotlinAnnotation(annotationName: String, packageName: String): String {
return StringBuilder()
.appendLine("package $packageName")
.appendLine()
.appendLine("annotation class $annotationName")
.toString()
val clazz: KotlinTypeSpec = KotlinTypeSpec.annotationBuilder(annotationName)
.addModifiers(KModifier.PUBLIC)
.build()
val file = FileSpec.builder(packageName, annotationName)
.addType(clazz)
.indent("\t")
.build()
return file.toString()
}

/**
Expand All @@ -143,12 +181,14 @@ fun kotlinAnnotation(annotationName: String, packageName: String): String {
* @return The template for a Kotlin data class.
*/
fun kotlinDataClass(dataClassName: String, packageName: String): String {
return StringBuilder()
.appendLine("package $packageName")
.appendLine()
.appendLine("data class $dataClassName(")
.appendLine(")")
.toString()
val clazz: KotlinTypeSpec = KotlinTypeSpec.classBuilder(dataClassName)
.addModifiers(KModifier.PUBLIC, KModifier.DATA)
.build()
val file = FileSpec.builder(packageName, dataClassName)
.addType(clazz)
.indent("\t")
.build()
return file.toString()
}

/**
Expand All @@ -159,10 +199,12 @@ fun kotlinDataClass(dataClassName: String, packageName: String): String {
* @return The template for a Kotlin sealed class.
*/
fun kotlinSealedClass(sealedClassName: String, packageName: String): String {
return StringBuilder()
.appendLine("package $packageName")
.appendLine()
.appendLine("sealed class $sealedClassName {")
.appendLine("}")
.toString()
val clazz: KotlinTypeSpec = KotlinTypeSpec.classBuilder(sealedClassName)
.addModifiers(KModifier.PUBLIC, KModifier.SEALED)
.build()
val file = FileSpec.builder(packageName, sealedClassName)
.addType(clazz)
.indent("\t")
.build()
return file.toString()
}

0 comments on commit 3b53a49

Please sign in to comment.