com.netflix.java.refactor.find.FindFields.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-source-refactor Show documentation
Show all versions of java-source-refactor Show documentation
Pluggable and distributed refactoring tool for Java source code
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