org.dbtools.codegen.kotlin.KotlinFun.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dbtools-gen Show documentation
Show all versions of dbtools-gen Show documentation
DBTools ORM Class Generator.
/*
* KotlinFun.kt
*
* Created on Nov 7, 2015
*
* Copyright 2007 Jeff Campbell. All rights reserved. Unauthorized reproduction
* is a violation of applicable law. This material contains certain
* confidential or proprietary information and trade secrets of Jeff Campbell.
*/
package org.dbtools.codegen.kotlin
import java.util.ArrayList
class KotlinFun {
var funType = KotlinFunType.STANDARD
var returnType = "" // void / Unit
var name = ""
var access = KotlinAccess.PRIVATE
var abstract = false
var inline = false
var lateInit = false
var override = false
var open = false
val parameters = ArrayList()
val annotations = ArrayList()
val exceptions = ArrayList()
var content = ""
var tab = KotlinClass.tab
var startEndTab = "" // some cases there needs to be an extra tab (such as companion object)
var constructorDelegate = ""
constructor(name: String, parameters: List = ArrayList(), returnType: String = "", content: String = "", funAccess: KotlinAccess = KotlinAccess.PUBLIC, funType: KotlinFunType = KotlinFunType.STANDARD) {
for (parameter in parameters) {
parameter.variableType = KotlinVarType.METHOD_PARAMETER
}
this.parameters.addAll(parameters)
this.access = funAccess
this.funType = funType
this.returnType = returnType
this.name = name
this.content = content
}
override fun toString(): String {
return toString(false)
}
fun addAnnotation(annotation: String) {
if (annotation.isEmpty()) {
throw IllegalArgumentException("annotation cannot be null or empty")
}
if (annotation[0] != '@') {
annotations.add('@' + annotation)
} else {
annotations.add(annotation)
}
}
fun toString(interfaceOnly: Boolean): String {
var text = ""
for (annotation in annotations) {
text += tab + annotation + "\n"
}
// access
val access = KotlinClass.getAccessString(access)
if (!access.isEmpty()) {
text += tab + access
} else {
text += tab
}
// modifiers
if (open) {
text += "open "
}
if (override) {
text += "override "
}
if (abstract) {
text += "abstract "
}
if (inline) {
text += "inline "
}
if (lateInit) {
text += "isLateinit "
}
// method name
if (funType == KotlinFunType.STANDARD) {
text += startEndTab + "fun $name"
} else if (funType == KotlinFunType.CONSTRUCTOR) {
text += "constructor"
}
// parameters
text += "("
var paramCounter = 0
for (parameter in parameters) {
if (paramCounter > 0) {
text += ", "
}
text += parameter.toString()
paramCounter++
}
text += ")"
// constructor delegate
if (constructorDelegate.isNotEmpty()) {
text += " : $constructorDelegate"
}
// return
if (returnType.isNotEmpty()) {
text += " : $returnType"
}
// exceptions
if (!exceptions.isEmpty()) {
text += " throws"
var expCount = 0
for (exception in exceptions) {
if (expCount == 0) {
text += " "
} else {
text += ", "
}
text += exception
expCount++
}
}
if (interfaceOnly || abstract) {
text += "\n"
return text
} else {
// content
text += " {\n"
if (!content.isEmpty()) {
val splitContent = content.split("\n".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
for (aSplitContent in splitContent) {
text += startEndTab + tab + tab + aSplitContent + "\n"
}
}
text += startEndTab + tab + "}\n"
}
return text
}
fun addParameter(parameter: KotlinVal) {
parameter.variableType = KotlinVarType.CLASS_VARIABLE
parameters.add(parameter)
}
fun setParameters(parameters: ArrayList) {
for (parameter in parameters) {
parameter.variableType = KotlinVarType.METHOD_PARAMETER
}
this.parameters.clear()
this.parameters.addAll(parameters)
}
fun addThrowsException(exceptionClass: String) {
if (exceptionClass.isEmpty()) {
throw IllegalArgumentException("exceptionClass cannot be empty or null")
}
exceptions.add(exceptionClass)
}
}