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

gsonpath.util.DefaultValueDetector.kt Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
package gsonpath.util

import com.squareup.javapoet.ClassName
import com.squareup.javapoet.TypeName
import com.sun.source.tree.VariableTree
import com.sun.source.util.TreePathScanner
import javax.lang.model.element.Element

interface DefaultValueDetector {
    fun hasDefaultValue(element: Element): Boolean
}

class DefaultValueDetectorImpl(private val sunTreesProvider: SunTreesProvider) : DefaultValueDetector {
    override fun hasDefaultValue(element: Element): Boolean {
        return DefaultValueScanner(element)
                .scan(sunTreesProvider.getTrees().getPath(element), null) != null
    }

    /**
     * Scans a field and detects whether a default value has been set.
     *
     * If a value has been set, the result will be an empty list, otherwise it will be null.
     */
    private class DefaultValueScanner(val fieldElement: Element) : TreePathScanner?, Void>() {
        override fun visitVariable(node: VariableTree?, p: Void?): List? {
            // Ignore default values for Kotlin classes (the stubs always set a default, but the real bytecode does not)
            if (isKotlinClass(fieldElement.enclosingElement)) {
                return null
            }
            return node?.initializer?.let { emptyList() }
        }

        private fun isKotlinClass(element: Element): Boolean {
            return element.annotationMirrors.any {
                TypeName.get(it.annotationType.asElement().asType()) == ClassName.get("kotlin", "Metadata")
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy