-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
53 changed files
with
837 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Main changes in this version: Bugfixes. | ||
Full changelog: https://github.com/element-hq/element-android/releases |
1 change: 1 addition & 0 deletions
1
...external/realmfieldnameshelper/bin/main/META-INF/gradle/incremental.annotation.processors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
dk.ilios.realmfieldnames.RealmFieldNamesProcessor,aggregating |
1 change: 1 addition & 0 deletions
1
...al/realmfieldnameshelper/bin/main/META-INF/services/javax.annotation.processing.Processor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
dk.ilios.realmfieldnames.RealmFieldNamesProcessor |
24 changes: 24 additions & 0 deletions
24
library/external/realmfieldnameshelper/bin/main/dk/ilios/realmfieldnames/ClassData.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package dk.ilios.realmfieldnames | ||
|
||
import java.util.TreeMap | ||
|
||
/** | ||
* Class responsible for keeping track of the metadata for each Realm model class. | ||
*/ | ||
class ClassData(val packageName: String?, val simpleClassName: String, val libraryClass: Boolean = false) { | ||
|
||
val fields = TreeMap<String, String?>() // <fieldName, linkedType or null> | ||
|
||
fun addField(field: String, linkedType: String?) { | ||
fields.put(field, linkedType) | ||
} | ||
|
||
val qualifiedClassName: String | ||
get() { | ||
if (packageName != null && !packageName.isEmpty()) { | ||
return packageName + "." + simpleClassName | ||
} else { | ||
return simpleClassName | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
...ry/external/realmfieldnameshelper/bin/main/dk/ilios/realmfieldnames/FieldNameFormatter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package dk.ilios.realmfieldnames | ||
|
||
import java.util.Locale | ||
|
||
/** | ||
* Class for encapsulating the rules for converting between the field name in the Realm model class | ||
* and the matching name in the "<class>Fields" class. | ||
*/ | ||
class FieldNameFormatter { | ||
|
||
@JvmOverloads | ||
fun format(fieldName: String?, locale: Locale = Locale.US): String { | ||
if (fieldName == null || fieldName == "") { | ||
return "" | ||
} | ||
|
||
// Normalize word separator chars | ||
val normalizedFieldName: String = fieldName.replace('-', '_') | ||
|
||
// Iterate field name using the following rules | ||
// lowerCase m followed by upperCase anything is considered hungarian notation | ||
// lowercase char followed by uppercase char is considered camel case | ||
// Two uppercase chars following each other is considered non-standard camelcase | ||
// _ and - are treated as word separators | ||
val result = StringBuilder(normalizedFieldName.length) | ||
|
||
if (normalizedFieldName.codePointCount(0, normalizedFieldName.length) == 1) { | ||
result.append(normalizedFieldName) | ||
} else { | ||
var previousCodepoint: Int? | ||
var currentCodepoint: Int? = null | ||
val length = normalizedFieldName.length | ||
var offset = 0 | ||
while (offset < length) { | ||
previousCodepoint = currentCodepoint | ||
currentCodepoint = normalizedFieldName.codePointAt(offset) | ||
|
||
if (previousCodepoint != null) { | ||
if (Character.isUpperCase(currentCodepoint) && | ||
!Character.isUpperCase(previousCodepoint) && | ||
previousCodepoint === 'm'.code as Int? && | ||
result.length == 1 | ||
) { | ||
// 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) { | ||
val nextCodePoint = normalizedFieldName.codePointAt(offset + Character.charCount(currentCodepoint)) | ||
if (Character.isLowerCase(nextCodePoint)) { | ||
result.append("_") | ||
} | ||
} | ||
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("_") | ||
result.appendCodePoint(currentCodepoint) | ||
} else { | ||
// Unknown type | ||
result.appendCodePoint(currentCodepoint) | ||
} | ||
} else { | ||
// Only triggered for first code point | ||
result.appendCodePoint(currentCodepoint) | ||
} | ||
offset += Character.charCount(currentCodepoint) | ||
} | ||
} | ||
|
||
return result.toString().uppercase(locale) | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
library/external/realmfieldnameshelper/bin/main/dk/ilios/realmfieldnames/FileGenerator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
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 | ||
|
||
/** | ||
* Class responsible for creating the final output files. | ||
*/ | ||
class FileGenerator(private val filer: Filer) { | ||
private val formatter: FieldNameFormatter | ||
|
||
init { | ||
this.formatter = FieldNameFormatter() | ||
} | ||
|
||
/** | ||
* Generates all the "<class>Fields" fields with field name references. | ||
* @param fileData Files to create. | ||
* * | ||
* @return `true` if the files where generated, `false` if not. | ||
*/ | ||
fun generate(fileData: Set<ClassData>): Boolean { | ||
return fileData | ||
.filter { !it.libraryClass } | ||
.all { generateFile(it, fileData) } | ||
} | ||
|
||
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) { | ||
// Add linked field names (only up to depth 1) | ||
for (data in classPool) { | ||
if (data.qualifiedClassName == value) { | ||
val linkedTypeSpec = TypeSpec.classBuilder(formatter.format(fieldName)) | ||
.addModifiers(Modifier.PUBLIC, Modifier.FINAL, Modifier.STATIC) | ||
val linkedClassFields = data.fields | ||
addField(linkedTypeSpec, "$", fieldName) | ||
for (linkedFieldName in linkedClassFields.keys) { | ||
addField(linkedTypeSpec, linkedFieldName, fieldName + "." + linkedFieldName) | ||
} | ||
fileBuilder.addType(linkedTypeSpec.build()) | ||
} | ||
} | ||
} else { | ||
// Add normal field name | ||
addField(fileBuilder, fieldName, fieldName) | ||
} | ||
} | ||
|
||
val javaFile = JavaFile.builder(classData.packageName, fileBuilder.build()).build() | ||
try { | ||
javaFile.writeTo(filer) | ||
return true | ||
} catch (e: IOException) { | ||
// e.printStackTrace() | ||
return false | ||
} | ||
} | ||
|
||
private fun addField(fileBuilder: TypeSpec.Builder, fieldName: String, fieldNameValue: String) { | ||
val field = FieldSpec.builder(String::class.java, formatter.format(fieldName)) | ||
.addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL) | ||
.initializer("\$S", fieldNameValue) | ||
.build() | ||
fileBuilder.addField(field) | ||
} | ||
} |
Oops, something went wrong.