Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.mostlycertain.javapoetdsl.MethodSpec.kt Maven / Gradle / Ivy
package com.mostlycertain.javapoetdsl
import com.squareup.javapoet.AnnotationSpec
import com.squareup.javapoet.CodeBlock
import com.squareup.javapoet.MethodSpec
import com.squareup.javapoet.ParameterSpec
import com.squareup.javapoet.TypeName
import java.lang.reflect.Type
import javax.lang.model.element.Modifier
import kotlin.reflect.KClass
typealias MethodCodeFunc = MethodCodeBuilder.() -> Unit
private fun methodSpecInternal(
returns: TypeName,
name: String,
parameters: List = emptyList(),
modifiers: List = emptyList(),
annotations: List = emptyList(),
throws: List = emptyList(),
varargs: Boolean = false,
body: MethodCodeFunc? = null
): MethodSpec {
val methodBuilder = MethodSpec
.methodBuilder(name)
.returns(returns)
.addParameters(parameters)
.addModifiers(modifiers)
.addAnnotations(annotations)
.addExceptions(throws)
.varargs(varargs)
val code = CodeBlock.builder()
val builder = MethodCodeBuilder(MethodMeta(name, returns, parameters, modifiers, annotations, throws, varargs), code)
if (body != null) {
builder.body()
}
builder.close()
methodBuilder.addCode(code.build())
return methodBuilder.build()
}
fun methodSpec(
name: String,
parameters: List = emptyList(),
modifiers: List = emptyList(),
annotations: List = emptyList(),
throws: List = emptyList(),
varargs: Boolean = false,
body: MethodCodeFunc? = null
) = methodSpecInternal(TypeName.VOID, name, parameters, modifiers, annotations, throws, varargs, body)
fun methodSpec(
returns: TypeName,
name: String,
parameters: List = emptyList(),
modifiers: List = emptyList(),
annotations: List = emptyList(),
throws: List = emptyList(),
varargs: Boolean = false,
body: MethodCodeFunc? = null
) = methodSpecInternal(returns, name, parameters, modifiers, annotations, throws, varargs, body)
fun methodSpec(
returns: Type,
name: String,
parameters: List = emptyList(),
modifiers: List = emptyList(),
annotations: List = emptyList(),
throws: List = emptyList(),
varargs: Boolean = false,
body: MethodCodeFunc? = null
) = methodSpecInternal(TypeName.get(returns), name, parameters, modifiers, annotations, throws, varargs, body)
fun methodSpec(
returns: KClass<*>,
name: String,
parameters: List = emptyList(),
modifiers: List = emptyList(),
annotations: List = emptyList(),
throws: List = emptyList(),
varargs: Boolean = false,
body: MethodCodeFunc? = null
) = methodSpecInternal(TypeNames.of(returns), name, parameters, modifiers, annotations, throws, varargs, body)
data class MethodMeta(
val name: String,
val returns: TypeName,
val parameters: List,
val modifiers: List,
val annotations: List,
val throws: List,
val varargs: Boolean
)
class MethodCodeBuilder(val methodMeta: MethodMeta, code: CodeBlock.Builder) : CodeBuilder(code)