Fix ktlint issue

This commit is contained in:
Benoit Marty 2023-06-23 22:58:28 +02:00
parent dc9e649703
commit ea424f29fb
6 changed files with 30 additions and 28 deletions

View File

@ -1,8 +1,8 @@
apply plugin: 'kotlin'
apply plugin: 'java'
sourceCompatibility = '1.7'
targetCompatibility = '1.7'
sourceCompatibility = versions.sourceCompat
targetCompatibility = versions.sourceCompat
dependencies {
implementation 'com.squareup:javapoet:1.13.0'

View File

@ -14,7 +14,7 @@ class FieldNameFormatter {
}
// Normalize word separator chars
val normalizedFieldName : String = fieldName.replace('-', '_')
val normalizedFieldName: String = fieldName.replace('-', '_')
// Iterate field name using the following rules
// lowerCase m followed by upperCase anything is considered hungarian notation
@ -39,7 +39,6 @@ class FieldNameFormatter {
// Hungarian notation starting with: mX
result.delete(0, 1)
result.appendCodePoint(currentCodepoint)
} else if (Character.isUpperCase(currentCodepoint) && Character.isUpperCase(previousCodepoint)) {
// InvalidCamelCase: XXYx (should have been xxYx)
if (offset + Character.charCount(currentCodepoint) < normalizedFieldName.length) {
@ -49,11 +48,9 @@ class FieldNameFormatter {
}
}
result.appendCodePoint(currentCodepoint)
} else if (currentCodepoint === '-'.code as Int? || currentCodepoint === '_'.code as Int?) {
// Word-separator: x-x or x_x
result.append("_")
} else if (Character.isUpperCase(currentCodepoint) && !Character.isUpperCase(previousCodepoint) && Character.isLetterOrDigit(previousCodepoint)) {
// camelCase: xX
result.append("_")

View File

@ -3,9 +3,7 @@ package dk.ilios.realmfieldnames
import com.squareup.javapoet.FieldSpec
import com.squareup.javapoet.JavaFile
import com.squareup.javapoet.TypeSpec
import java.io.IOException
import javax.annotation.processing.Filer
import javax.lang.model.element.Modifier
@ -32,13 +30,11 @@ class FileGenerator(private val filer: Filer) {
}
private fun generateFile(classData: ClassData, classPool: Set<ClassData>): Boolean {
val fileBuilder = TypeSpec.classBuilder(classData.simpleClassName + "Fields")
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.addJavadoc("This class enumerate all queryable fields in {@link \$L.\$L}\n",
classData.packageName, classData.simpleClassName)
// Add a static field reference to each queryable field in the Realm model class
classData.fields.forEach { fieldName, value ->
if (value != null) {
@ -69,7 +65,6 @@ class FileGenerator(private val filer: Filer) {
e.printStackTrace()
return false
}
}
private fun addField(fileBuilder: TypeSpec.Builder, fieldName: String, fieldNameValue: String) {

View File

@ -1,14 +1,17 @@
package dk.ilios.realmfieldnames
import java.util.*
import javax.annotation.processing.AbstractProcessor
import javax.annotation.processing.Messager
import javax.annotation.processing.ProcessingEnvironment
import javax.annotation.processing.RoundEnvironment
import javax.annotation.processing.SupportedAnnotationTypes
import javax.lang.model.SourceVersion
import javax.lang.model.element.*
import javax.lang.model.element.Element
import javax.lang.model.element.ElementKind
import javax.lang.model.element.Modifier
import javax.lang.model.element.PackageElement
import javax.lang.model.element.TypeElement
import javax.lang.model.element.VariableElement
import javax.lang.model.type.DeclaredType
import javax.lang.model.type.TypeMirror
import javax.lang.model.util.Elements
@ -24,9 +27,9 @@ import javax.tools.Diagnostic
class RealmFieldNamesProcessor : AbstractProcessor() {
private val classes = HashSet<ClassData>()
lateinit private var typeUtils: Types
lateinit private var messager: Messager
lateinit private var elementUtils: Elements
private lateinit var typeUtils: Types
private lateinit var messager: Messager
private lateinit var elementUtils: Elements
private var ignoreAnnotation: TypeMirror? = null
private var realmClassAnnotation: TypeElement? = null
private var realmModelInterface: TypeMirror? = null
@ -35,7 +38,8 @@ class RealmFieldNamesProcessor : AbstractProcessor() {
private var fileGenerator: FileGenerator? = null
private var done = false
@Synchronized override fun init(processingEnv: ProcessingEnvironment) {
@Synchronized
override fun init(processingEnv: ProcessingEnvironment) {
super.init(processingEnv)
typeUtils = processingEnv.typeUtils!!
messager = processingEnv.messager!!
@ -51,10 +55,14 @@ class RealmFieldNamesProcessor : AbstractProcessor() {
ignoreAnnotation = elementUtils.getTypeElement("io.realm.annotations.Ignore")?.asType()
realmClassAnnotation = elementUtils.getTypeElement("io.realm.annotations.RealmClass")
realmModelInterface = elementUtils.getTypeElement("io.realm.RealmModel")?.asType()
realmListClass = typeUtils.getDeclaredType(elementUtils.getTypeElement("io.realm.RealmList"),
typeUtils.getWildcardType(null, null))
realmResultsClass = typeUtils.getDeclaredType(elementUtils.getTypeElement("io.realm.RealmResults"),
typeUtils.getWildcardType(null, null))
realmListClass = typeUtils.getDeclaredType(
elementUtils.getTypeElement("io.realm.RealmList"),
typeUtils.getWildcardType(null, null)
)
realmResultsClass = typeUtils.getDeclaredType(
elementUtils.getTypeElement("io.realm.RealmResults"),
typeUtils.getWildcardType(null, null)
)
fileGenerator = FileGenerator(processingEnv.filer)
}
}
@ -83,8 +91,8 @@ class RealmFieldNamesProcessor : AbstractProcessor() {
classes.forEach {
it.fields.forEach { _, value ->
// Analyze the library class file the first time it is encountered.
if (value != null ) {
if (classes.all{ it.qualifiedClassName != value } && !libraryClasses.containsKey(value)) {
if (value != null) {
if (classes.all { it.qualifiedClassName != value } && !libraryClasses.containsKey(value)) {
libraryClasses.put(value, processLibraryClass(value))
}
}
@ -172,8 +180,10 @@ class RealmFieldNamesProcessor : AbstractProcessor() {
val enclosingElement = classElement.enclosingElement
if (enclosingElement.kind != ElementKind.PACKAGE) {
messager.printMessage(Diagnostic.Kind.ERROR,
"Could not determine the package name. Enclosing element was: " + enclosingElement.kind)
messager.printMessage(
Diagnostic.Kind.ERROR,
"Could not determine the package name. Enclosing element was: " + enclosingElement.kind
)
return null
}