Skip to content

Commit 0491817

Browse files
committed
Conditionally warn about Java/Kotlin attributes mismatch
In order to ease transition from Java to Kotlin the users have the possibility to enable warnings about mismatch in attributes for elements. The following new CLI parameter is available `-enableandroidattributesmismatchwarning` as well as `GLUECODIUM_ENABLE_ANDROID_ATTRIBUTES_MISMATCH_WARNING` CMake parameter. When flag is set to true and an element from LIME file uses annotations for Java, but it does not use the matching annotation for Kotlin then the warning is generated (and vice versa). Signed-off-by: Patryk Wrobel <183546751+pwrobeldev@users.noreply.github.com>
1 parent ae45726 commit 0491817

8 files changed

Lines changed: 92 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44
### Features
55
* Dart/Flutter: the generated code is now compatible with Flutter 3.29 and above. When the user invokes the callback created for lambda/interface from the thread that is the main isolate thread, but outside of isolate context then it is correclty executed. Before this release the thread would deadlock. Now the generated code identifies such case and enters the isolate context before invoking the callback.
6+
* Java/Kotlin: in order to ease transition from Java to Kotlin the possibility to conditionally warn about mismatch in attributes used for Java/Kotlin is implemented. The following new CLI parameter is available `-enableandroidattributesmismatchwarning` as well as `GLUECODIUM_ENABLE_ANDROID_ATTRIBUTES_MISMATCH_WARNING` CMake parameter.
67

78
## 13.12.0
89
Release date 2025-03-24

cmake/modules/gluecodium/gluecodium/KnownOptionalProperties.cmake

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,15 @@ _gluecodium_define_target_property(
237237
"This property is initialized by the value of the GLUECODIUM_DART_DISABLE_FINALIZABLE_MARKER_DEFAULT variable if it is set when the function gluecodium_add_generate_command is called."
238238
)
239239

240+
_gluecodium_define_target_property(
241+
GLUECODIUM_ENABLE_ANDROID_ATTRIBUTES_MISMATCH_WARNING
242+
BRIEF_DOCS "Enables generation of warnings when attributes for Java and Kotlin do not match."
243+
FULL_DOCS
244+
"Enables generation of warnings when attributes for Java and Kotlin do not match. "
245+
"Option used to ease adjustments of LIME files needed to transition from Java to Kotlin. "
246+
"This property is initialized by the value of the GLUECODIUM_ENABLE_ANDROID_ATTRIBUTES_MISMATCH_WARNING_DEFAULT variable if it is set when the function gluecodium_add_generate_command is called."
247+
)
248+
240249
_gluecodium_define_target_property(
241250
GLUECODIUM_DART_NAMERULES
242251
BRIEF_DOCS "The path to a file with name rules for Dart"

cmake/modules/gluecodium/gluecodium/details/runGenerate.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ function(_prepare_gluecodium_config_file file_path placeholder_file)
151151
_append_boolean_value(swiftexpose "${GLUECODIUM_SWIFT_EXPOSE_INTERNALS}")
152152
_append_boolean_value(strict "${GLUECODIUM_STRICT_VALIDATION}")
153153
_append_boolean_value(dartdisablefinalizablemarker "${GLUECODIUM_DART_DISABLE_FINALIZABLE_MARKER}")
154+
_append_boolean_value(enableandroidattributesmismatchwarning "${GLUECODIUM_ENABLE_ANDROID_ATTRIBUTES_MISMATCH_WARNING}")
154155

155156
_append_list_option(generators GLUECODIUM_GENERATORS)
156157
_append_list_option(werror GLUECODIUM_WERROR)

gluecodium/src/main/java/com/here/gluecodium/Gluecodium.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import com.here.gluecodium.generator.common.templates.TemplateEngine
3434
import com.here.gluecodium.model.lime.LimeModel
3535
import com.here.gluecodium.model.lime.LimeModelLoader
3636
import com.here.gluecodium.model.lime.LimeModelLoaderException
37+
import com.here.gluecodium.validator.LimeAndroidAttributesMismatchValidator
3738
import com.here.gluecodium.validator.LimeAsyncValidator
3839
import com.here.gluecodium.validator.LimeConstantRefsValidator
3940
import com.here.gluecodium.validator.LimeExternalTypesValidator
@@ -178,6 +179,9 @@ class Gluecodium(
178179
getIndependentValidators(limeLogger) +
179180
if (typeRefsValidationResult) getTypeRefDependentValidators(limeLogger) else emptyList()
180181
val validationResults = validators.map { it.invoke(filteredModel) }
182+
if (generatorOptions.enableAndroidAttributesMismatchWarning) {
183+
LimeAndroidAttributesMismatchValidator(limeLogger).validate(filteredModel)
184+
}
181185
return typeRefsValidationResult && !validationResults.contains(false)
182186
}
183187

gluecodium/src/main/java/com/here/gluecodium/cli/OptionReader.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ object OptionReader {
128128
" below 3.6.0. may experience internal compiler error when compiling the generated code, which utilizes " +
129129
"'Finalizable' marker)",
130130
)
131+
addOption(
132+
"enableandroidattributesmismatchwarning",
133+
false,
134+
"Enables generation of warnings when attributes for Java and Kotlin do not match. " +
135+
"Option used to ease adjustments of LIME files needed to transition from Java to Kotlin.",
136+
)
131137
addOption(
132138
"werror",
133139
"warning-as-error",
@@ -233,6 +239,7 @@ object OptionReader {
233239
getStringValue("dartlookuperrormessage")?.let { generatorOptions.dartLookupErrorMessage = it }
234240
getStringListValue("werror")?.let { generatorOptions.werror = it.toSet() }
235241

242+
generatorOptions.enableAndroidAttributesMismatchWarning = getFlagValue("enableandroidattributesmismatchwarning")
236243
generatorOptions.dartDisableFinalizableMarker = getFlagValue("dartdisablefinalizablemarker")
237244
generatorOptions.swiftExposeInternals = getFlagValue("swiftexpose")
238245

gluecodium/src/main/java/com/here/gluecodium/generator/common/GeneratorOptions.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ data class GeneratorOptions(
3838
var cppExportCommon: String? = null,
3939
var internalPrefix: String? = null,
4040
var libraryName: String = "library",
41+
var enableAndroidAttributesMismatchWarning: Boolean = false,
4142
var dartDisableFinalizableMarker: Boolean = false,
4243
var dartLookupErrorMessage: String =
4344
"Failed to resolve an FFI function. Perhaps `LibraryContext.init()` was not called.",
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright (C) 2016-2025 HERE Europe B.V.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
20+
package com.here.gluecodium.validator
21+
22+
import com.here.gluecodium.common.LimeLogger
23+
import com.here.gluecodium.model.lime.LimeAttributeType
24+
import com.here.gluecodium.model.lime.LimeAttributeValueType
25+
import com.here.gluecodium.model.lime.LimeModel
26+
import com.here.gluecodium.model.lime.LimeNamedElement
27+
28+
class LimeAndroidAttributesMismatchValidator(private val limeLogger: LimeLogger) {
29+
fun validate(limeModel: LimeModel) {
30+
val allElements = limeModel.referenceMap.values
31+
allElements.filterIsInstance<LimeNamedElement>().forEach { element ->
32+
val javaAttributes = element.attributes.getAllAttributeValueTypes(LimeAttributeType.JAVA)
33+
val kotlinAttributes = element.attributes.getAllAttributeValueTypes(LimeAttributeType.KOTLIN)
34+
val commonAttributes = kotlinAttributes intersect javaAttributes
35+
36+
val attributesMissingInJava = kotlinAttributes subtract commonAttributes
37+
if (attributesMissingInJava.isNotEmpty()) {
38+
logAttributesMismatch(
39+
element = element,
40+
attributes = attributesMissingInJava,
41+
present = "Kotlin",
42+
missing = "Java",
43+
)
44+
}
45+
46+
val attributesMissingInKotlin = javaAttributes subtract commonAttributes
47+
if (attributesMissingInKotlin.isNotEmpty()) {
48+
logAttributesMismatch(
49+
element = element,
50+
attributes = attributesMissingInKotlin,
51+
present = "Java",
52+
missing = "Kotlin",
53+
)
54+
}
55+
}
56+
}
57+
58+
private fun logAttributesMismatch(
59+
element: LimeNamedElement,
60+
attributes: Set<LimeAttributeValueType>,
61+
present: String,
62+
missing: String,
63+
) {
64+
val warningMessage = "Attributes missing in $missing, but present in $present: $attributes"
65+
limeLogger.warning(element, warningMessage)
66+
}
67+
}

lime-runtime/src/main/java/com/here/gluecodium/model/lime/LimeAttributes.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class LimeAttributes private constructor(
3838
else -> true
3939
}
4040

41+
fun getAllAttributeValueTypes(type: LimeAttributeType) = attributes[type]?.keys ?: emptySet()
42+
4143
fun <T> get(
4244
attributeType: LimeAttributeType,
4345
valueType: LimeAttributeValueType,

0 commit comments

Comments
 (0)