Skip to content

Commit

Permalink
Some improvements and compiler warnings (String.de/capitalize).
Browse files Browse the repository at this point in the history
  • Loading branch information
kreinhard committed Jun 26, 2021
1 parent 8376e1c commit a345bc9
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ object BeanUtils {
fun getGetterMethod(clazz: Class<*>, fieldname: String): Method? {
val methods: Array<Method> = getAllDeclaredGetters(clazz)
for (m in methods) {
if (m.name == "get${fieldname.capitalize()}") {
if (m.name == "get${fieldname.replaceFirstChar { it.uppercase() }}") {
return m
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ constructor(
* If not given, [columnHeadname] in decapitalized form will be used.
*/
var targetProperty: String? = null
get() = if (field != null) field else columnHeadname?.decapitalize()
get() = if (field != null) field else columnHeadname?.replaceFirstChar { it.lowercase() }
private set

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ class ExcelRow(val sheet: ExcelSheet, val row: Row) {
continue
}
for (alias in colDef.columnAliases) {
searchResult = getPropertyValue(obj, colDef, alias.decapitalize(), ignoreProperties)
searchResult = getPropertyValue(obj, colDef, alias.replaceFirstChar { it.lowercase() }, ignoreProperties)
if (searchResult != null) {
processPropertyValue(obj, searchResult, colDef, process)
break
Expand Down Expand Up @@ -253,7 +253,7 @@ class ExcelRow(val sheet: ExcelSheet, val row: Row) {
return PropertySearchResult(false, BeanUtils.getValue(obj, getter))
}
}
val field = BeanUtils.getDeclaredField(obj::class.java, identifier.decapitalize())
val field = BeanUtils.getDeclaredField(obj::class.java, identifier.replaceFirstChar { it.lowercase() })
if (field != null) {
sheet.cache.autoFillCache.foundPropertiesMap[colDef] = WorkingCache.Property(field)
return PropertySearchResult(false, BeanUtils.getValue(obj, field))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class ExcelSheet internal constructor(val excelWorkbook: ExcelWorkbook, val poiS
}
for (listener in columnDef.getColumnListeners()!!) {
if (listener is ExcelColumnValidator) {
if (columnDef._columnNumber < 0) {
if (columnDef._columnNumber < 0 && listener.isRequired) {
addValidationError(createValidationErrorMissingColumnByName(columnDef.columnHeadname))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ private val log = KotlinLogging.logger {}
/**
* Wraps and enhances a POI workbook.
*/
@Suppress("unused", "MemberVisibilityCanBePrivate")
class ExcelWorkbook
@JvmOverloads constructor(
@JvmOverloads
constructor(
/**
* Is used e. g. for getting number cell values as String.
*/
val locale: Locale = Locale.getDefault()
val locale: Locale = Locale.getDefault(),
workbook: Workbook? = XSSFWorkbook(),
) : AutoCloseable {

lateinit var pOIWorkbook: Workbook
Expand All @@ -37,10 +40,10 @@ class ExcelWorkbook
private var inputStream: InputStream? = null
var filename: String? = null

val filenameExtension: String?
val filenameExtension: String
get() = File(filename ?: "unkown.xlsx").extension

val filenameWithoutExtension: String?
val filenameWithoutExtension: String
get() = File(filename ?: "unkown.xlsx").nameWithoutExtension

var formulaEvaluator: FormulaEvaluator? = null
Expand All @@ -57,7 +60,7 @@ class ExcelWorkbook
workbook: Workbook,
locale: Locale = Locale.getDefault()
)
: this(locale) {
: this(locale, null) {
pOIWorkbook = workbook
}

Expand All @@ -73,7 +76,7 @@ class ExcelWorkbook
excelFile: File,
locale: Locale = Locale.getDefault()
)
: this(locale) {
: this(locale, null) {
try {
val fis = FileInputStream(excelFile)
open(fis, excelFile.name)
Expand All @@ -93,12 +96,12 @@ class ExcelWorkbook
filename: String,
locale: Locale = Locale.getDefault()
)
: this(locale) {
: this(locale, null) {
open(inputStream, filename)
}

/**
* @param inputStream The input stream to read the Excel content from.
* @param byteArray Byte array to read the Excel content from.
* @param filename Only for logging purposes if any error occurs.
*/
@JvmOverloads
Expand All @@ -107,10 +110,16 @@ class ExcelWorkbook
filename: String,
locale: Locale = Locale.getDefault()
)
: this(locale) {
: this(locale, null) {
open(byteArray.inputStream(), filename)
}

init {
if (workbook != null) {
pOIWorkbook = workbook
}
}

private fun open(inputStream: InputStream, filename: String) {
this.filename = File(filename).name
this.inputStream = inputStream
Expand Down Expand Up @@ -201,7 +210,7 @@ class ExcelWorkbook
*
* @see Workbook.cloneSheet
*/
fun cloneSheet(sheetNum: Int, name: String?): ExcelSheet? {
fun cloneSheet(sheetNum: Int, name: String?): ExcelSheet {
val index = pOIWorkbook.numberOfSheets
val poiSheet: Sheet = this.pOIWorkbook.cloneSheet(sheetNum)
this.pOIWorkbook.setSheetName(index, name)
Expand All @@ -216,7 +225,7 @@ class ExcelWorkbook
/**
* Remove the sheet at the given position.
*
* @param index
* @param idx
* @return this for chaining.
*/
fun removeSheetAt(idx: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@
package de.micromata.merlin.utils

import de.micromata.merlin.excel.importer.ImportLogger
import org.apache.commons.lang3.ArrayUtils
import org.apache.commons.lang3.StringUtils
import org.slf4j.LoggerFactory
import java.lang.reflect.Method
import java.lang.reflect.Modifier
import java.math.BigDecimal
import java.math.BigInteger
import java.time.LocalDate
import java.time.LocalDateTime
import java.util.*

/**
* Stores one imported object (e. g. MS Excel row as bean object). It also contains information about the status: New object or modified
Expand All @@ -40,23 +39,21 @@ import java.time.LocalDateTime
* @author Kai Reinhard ([email protected])
*/
object BeanHelper {
//private val log = LoggerFactory.getLogger(BeanHelper::class.java)

@JvmStatic
@JvmOverloads
fun determineGetter(clazz: Class<*>, fieldname: String, onlyPublicGetter: Boolean = true): Method? {
val cap = StringUtils.capitalize(fieldname)
val cap = fieldname.replaceFirstChar { it.uppercase() }
val methods: Array<Method> = getAllDeclaredMethods(clazz) ?: return null
for (method in methods) {
if (onlyPublicGetter && !Modifier.isPublic(method.modifiers)) {
continue
}
val matches =
if (Boolean::class.javaPrimitiveType!!.isAssignableFrom(method.returnType)) {
"is$cap" == method.name || "has$cap" == method.name || "get$cap" == method.name
} else {
"get$cap" == method.name
}
if (Boolean::class.javaPrimitiveType!!.isAssignableFrom(method.returnType)) {
"is$cap" == method.name || "has$cap" == method.name || "get$cap" == method.name
} else {
"get$cap" == method.name
}
if (matches) {
if (!method.isBridge) { // Don't return bridged methods (methods defined in interface or super class with different return type).
return method
Expand All @@ -73,7 +70,7 @@ object BeanHelper {
var methods = cls.declaredMethods
while (cls.superclass != null) {
cls = cls.superclass
methods = ArrayUtils.addAll(methods, *cls.declaredMethods) as Array<Method>
methods += cls.declaredMethods
}
return methods
}
Expand All @@ -88,7 +85,7 @@ object BeanHelper {
*/
@JvmStatic
fun determineSetter(clazz: Class<*>, fieldname: String): Method? {
val cap = fieldname.capitalize()
val cap = fieldname.replaceFirstChar { it.uppercaseChar() }
val methods: Array<Method> = getAllDeclaredMethods(clazz) ?: return null
for (method in methods) {
if ("set$cap" == method.name && method.parameterTypes.size == 1) {
Expand Down

0 comments on commit a345bc9

Please sign in to comment.