Skip to content

Commit

Permalink
fix: Getter now calls method from parent even if getter starts with is (
Browse files Browse the repository at this point in the history
#241)

* fix: Getter from parent methods are not anymore renamed, preperty getter which rely on super implementation will not be visible from java

* Renamed variables to make it clearer and apply same to setter
  • Loading branch information
piiertho authored Jun 15, 2021
1 parent 42e5259 commit 33f74e8
Show file tree
Hide file tree
Showing 16 changed files with 122 additions and 69 deletions.
35 changes: 28 additions & 7 deletions kt/api-generator/src/main/kotlin/godot/codegen/Property.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class Property @JsonCreator constructor(
lateinit var engineSetterIndexName: String
lateinit var engineGetterIndexName: String

var parentMethodToCall: String? = null
var shouldUseSuperGetter = false
var shouldUseSuperSetter = false

init {
type = type.convertTypeToKotlin()
Expand Down Expand Up @@ -83,6 +84,26 @@ class Property @JsonCreator constructor(
modifiers
)

fun generateSuperAccessor(isSetter: Boolean = false): FunSpec {
val methodName = if (isSetter) setter else getter

return FunSpec.getterBuilder()
.addStatement(
"return super.$methodName()"
)
.addAnnotation(
AnnotationSpec.builder(JvmName::class)
.addMember("\"${methodName}_prop\"")
.build()
)
.addAnnotation(
AnnotationSpec.builder(Suppress::class)
.addMember("\"INAPPLICABLE_JVM_NAME\"")
.build()
)
.build()
}

if (hasValidSetter) {
propertySpecBuilder.mutable()
propertySpecBuilder.setter(
Expand All @@ -97,6 +118,10 @@ class Property @JsonCreator constructor(
)
.build()
)
} else if (shouldUseSuperSetter) {
propertySpecBuilder.setter(
generateSuperAccessor(true)
)
}

if (hasValidGetter) {
Expand All @@ -112,13 +137,9 @@ class Property @JsonCreator constructor(
.build()
)
} else {
if (parentMethodToCall != null) {
if (shouldUseSuperGetter) {
propertySpecBuilder.getter(
FunSpec.getterBuilder()
.addStatement(
"return super.$parentMethodToCall()"
)
.build()
generateSuperAccessor()
)
} else {
propertySpecBuilder.getter(
Expand Down
46 changes: 28 additions & 18 deletions kt/api-generator/src/main/kotlin/godot/codegen/generationEntry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package godot.codegen
import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import com.squareup.kotlinpoet.*
import godot.codegen.utils.convertToSnakeCase
import godot.codegen.utils.getPackage
import godot.codegen.utils.jvmVariantTypeValue
import godot.docgen.ClassDoc
Expand Down Expand Up @@ -31,30 +32,39 @@ fun File.generateApiFrom(jsonSource: File, docsDir: File? = null) {
it.properties.forEach { property -> property.initEngineIndexNames(it.engineClassDBIndexName) }
}

val methodsToRename = mutableMapOf<Method, String>()
classes.forEach { clazz ->
clazz.properties.forEach { property ->
val method = Method(
"get_${property.oldName}",
property.type,
isVirtual = false,
hasVarargs = false,
arguments = listOf()
)
val parentClassAndMethod = tree.getMethodFromAncestor(clazz, method)
if (parentClassAndMethod != null && !property.hasValidGetter) {
val parentMethodName = "get${parentClassAndMethod.first.newName}${property.newName.capitalize()}"
property.parentMethodToCall = parentMethodName
val find = parentClassAndMethod.first.methods.find { it.newName == "get${property.newName.capitalize()}" }
if (find != null) {
methodsToRename[find] = parentMethodName
fun inferMethodAccessorFromParent(isSetter: Boolean = false) {
val methodName = if (isSetter) property.setter else property.getter
val returnType = if (isSetter) "void" else property.type
val arguments = if (isSetter) listOf(Argument(property.oldName, property.type)) else listOf()

val method = Method(
methodName.convertToSnakeCase(),
returnType,
isVirtual = false,
hasVarargs = false,
arguments = arguments
)

val parentClassAndMethod = tree.getMethodFromAncestor(clazz, method)
val hasValidAccessor = if (isSetter) property.hasValidSetter else property.hasValidGetter
if (parentClassAndMethod != null && !hasValidAccessor) {
if (isSetter) {
property.shouldUseSuperSetter = true
} else {
property.shouldUseSuperGetter = true
}
}
}

inferMethodAccessorFromParent()

// It does not seems to have any case where setter should call parent in api. But in case this happen in
// future, this is here.
inferMethodAccessorFromParent(true)
}
}
methodsToRename.forEach {
it.key.newName = it.value
}

generateEngineIndexesFile(classes).writeTo(this)

Expand Down
17 changes: 10 additions & 7 deletions kt/godot-library/src/main/kotlin/godot/gen/godot/BitmapFont.kt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions kt/godot-library/src/main/kotlin/godot/gen/godot/Font.kt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 33f74e8

Please sign in to comment.