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

com.netflix.java.refactor.fix.AddField.kt Maven / Gradle / Ivy

There is a newer version: 0.6.2
Show newest version
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