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

com.netflix.java.refactor.find.FindFields.kt Maven / Gradle / Ivy

There is a newer version: 0.6.2
Show newest version
package com.netflix.java.refactor.find

import com.netflix.java.refactor.ast.AstScannerBuilder
import com.netflix.java.refactor.ast.SingleCompilationUnitAstScanner
import com.sun.source.tree.CompilationUnitTree
import com.sun.tools.javac.code.Flags
import com.sun.tools.javac.code.Symbol
import com.sun.tools.javac.code.Type
import com.sun.tools.javac.tree.JCTree
import com.sun.tools.javac.util.Context

data class Field(val name: String, val type: String)

class FindFields(val clazz: String, val includeInherited: Boolean) : AstScannerBuilder> {
    override fun scanner() = FindFieldScanner(this)
}

class FindFieldScanner(val op: FindFields) : SingleCompilationUnitAstScanner>() {
    override fun visitCompilationUnit(node: CompilationUnitTree?, p: Context?): List {
        super.visitCompilationUnit(node, p)
        return cu.defs.filterIsInstance().flatMap { superFields(it.type as Type.ClassType) }
    }

    private fun superFields(type: Type.ClassType, inHierarchy: Boolean = false): List {
        if (type.supertype_field == Type.noType)
            return emptyList()

        val fields = (type.tsym as Symbol.ClassSymbol).members_field.elements
                .filter { it is Symbol.VarSymbol }
                .filter { it.type.toString() == op.clazz }
                .filter { !inHierarchy || it.flags() and Flags.PRIVATE.toLong() == 0L }
                .map { Field(it.name.toString(), it.type.toString()) }

        return fields + (
                if (op.includeInherited && type.supertype_field is Type.ClassType)
                    superFields(type.supertype_field as Type.ClassType, true)
                else emptyList())
    }

    override fun reduce(r1: List?, r2: List?) = (r1 ?: emptyList()) + (r2 ?: emptyList())
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy