com.netflix.java.refactor.fix.AddField.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.fix
import com.netflix.java.refactor.RefactorFix
import com.netflix.java.refactor.ast.*
import com.sun.source.tree.ClassTree
import com.sun.tools.javac.tree.JCTree
import com.sun.tools.javac.util.Context
class AddField(val clazz: String, val name: String, val init: String?): RefactoringAstScannerBuilder {
override fun scanner() = CompositeScanner(AddFieldScanner(this), AddImport(clazz).scanner())
}
class AddFieldScanner(val op: AddField): FixingScanner() {
override fun visitClass(node: ClassTree?, p: Context?): List {
val decl = node as JCTree.JCClassDecl
val assignment = if(op.init is String) " = ${op.init}" else ""
return listOf(insertAt(sourceText.indexOf('{', decl.startPosition) + 1, "\n ${className(op.clazz)} ${op.name}$assignment;"))
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy