All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.prezi.spaghetti.kotlin.KotlinInterfaceGeneratorVisitor.groovy Maven / Gradle / Ivy

The newest version!
package com.prezi.spaghetti.kotlin

import com.prezi.spaghetti.ast.InterfaceNode
import com.prezi.spaghetti.ast.MethodNode

class KotlinInterfaceGeneratorVisitor extends AbstractKotlinMethodGeneratorVisitor {

	@Override
	String visitInterfaceNode(InterfaceNode node) {
		def typeName = node.name
		if (node.typeParameters) {
			typeName += "<" + node.typeParameters*.name.join(", ") + ">"
		}
		def superTypes = node.superInterfaces*.accept(this)
		def methodDefinitions = node.methods*.accept(this).join("")

		return   \
  """${defineType(typeName, superTypes)}
${methodDefinitions}
}
"""
	}

	@Override
	String visitMethodNode(MethodNode node) {
		def returnType = node.returnType.accept(this)
		returnType = wrapNullableTypeReference(returnType, node)
		def typeParams = node.typeParameters ? "<" + node.typeParameters*.name.join(", ") + "> " : ""
		def params = node.parameters*.accept(this).join(", ")

		return \
"""	native fun ${typeParams}${node.name}(${params}):${returnType}
"""
	}

	private static String defineType(String typeName, Collection superTypes) {
		def declaration = "trait ${typeName}"
		if (!superTypes.empty) {
			declaration += ': ' + superTypes.join(", ")
		}
		return declaration + " {"
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy