Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal to update generated function names #265

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 5 additions & 59 deletions compiler/src/main/kotlin/motif/compiler/Names.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package motif.compiler

import androidx.room.compiler.processing.XAnnotation
import androidx.room.compiler.processing.XType
import com.google.common.annotations.VisibleForTesting
import java.util.Locale
import motif.ast.compiler.CompilerAnnotation
import motif.ast.compiler.CompilerType
import motif.models.Type
Expand Down Expand Up @@ -49,14 +51,11 @@ private class UniqueNameSet(blacklist: Iterable<String>) {
object Names {

@JvmStatic
@VisibleForTesting
fun safeName(typeMirror: XType, annotation: XAnnotation?): String {
var name = XNameVisitor.visit(typeMirror)
val name = XNameVisitor.visit(typeMirror)
val annotationString = annotationString(annotation)
name = "$annotationString$name".decapitalize()
if (name in KEYWORDS) {
name += "_"
}
return name
return "$annotationString${name}_".replaceFirstChar { it.lowercase(Locale.ENGLISH) }
}

private fun annotationString(annotation: XAnnotation?): String {
Expand All @@ -67,56 +66,3 @@ object Names {
}
}
}

private val KEYWORDS =
setOf(
"abstract",
"continue",
"for",
"new",
"switch",
"assert",
"default",
"goto",
"package",
"synchronized",
"boolean",
"do",
"if",
"private",
"this",
"break",
"double",
"implements",
"protected",
"throw",
"byte",
"else",
"import",
"public",
"throws",
"case",
"enum",
"instanceof",
"return",
"transient",
"catch",
"extends",
"int",
"short",
"try",
"char",
"final",
"interface",
"static",
"void",
"class",
"finally",
"long",
"strictfp",
"volatile",
"const",
"float",
"native",
"super",
"while")
32 changes: 17 additions & 15 deletions compiler/src/test/java/motif/compiler/NamesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import org.junit.runners.Parameterized

@RunWith(Parameterized::class)
@OptIn(ExperimentalProcessingApi::class)
class XNamesTest(private val processorType: ProcessorType, private val srcLang: SourceLanguage) {
class NamesTest(private val processorType: ProcessorType, private val srcLang: SourceLanguage) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Matching the file name


companion object {
@JvmStatic
Expand All @@ -49,64 +49,66 @@ class XNamesTest(private val processorType: ProcessorType, private val srcLang:

@Test
fun primitive() {
assertSafeName("int", "kotlin.Int", "integer")
assertSafeName("int", "kotlin.Int", "integer_")
}

@Test
fun boxed() {
assertSafeName("java.lang.Integer", "kotlin.Int?", "integer")
assertSafeName("java.lang.Integer", "kotlin.Int?", "integer_")
}

@Test
fun raw() {
assertSafeName("java.util.HashMap", "java.util.HashMap<*, *>", "hashMap")
assertSafeName("java.util.HashMap", "java.util.HashMap<*, *>", "hashMap_")
}

@Test
fun typeArgument() {
assertSafeName(
"java.util.HashMap<String, Integer>",
"java.util.HashMap<String, Integer>",
"stringIntegerHashMap")
"stringIntegerHashMap_")
}

@Test
fun wildcard() {
assertSafeName(
"java.util.HashMap<? extends String, ? super Integer>",
"java.util.HashMap<out String, out Integer>",
"stringIntegerHashMap")
"stringIntegerHashMap_")
}

@Test
fun wildcardUnbounded() {
assertSafeName("java.util.HashMap<?, ?>", "java.util.HashMap<*, *>", "hashMap")
assertSafeName("java.util.HashMap<?, ?>", "java.util.HashMap<*, *>", "hashMap_")
}

@Test
fun typeVariable() {
assertSafeName("java.util.HashMap<String, A>", "java.util.HashMap<String, A>", "stringAHashMap")
assertSafeName(
"java.util.HashMap<String, A>", "java.util.HashMap<String, A>", "stringAHashMap_")
}

@Test
fun typeVariableUnbounded() {
assertSafeName("java.util.HashMap<String, B>", "java.util.HashMap<String, B>", "stringBHashMap")
assertSafeName(
"java.util.HashMap<String, B>", "java.util.HashMap<String, B>", "stringBHashMap_")
}

@Test
fun nested() {
assertSafeName(
"java.util.HashMap<java.util.HashMap<String, Integer>, Integer>",
"java.util.HashMap<java.util.HashMap<String, Integer>, Integer>",
"stringIntegerHashMapIntegerHashMap")
"stringIntegerHashMapIntegerHashMap_")
}

@Test
fun innerClass() {
assertSafeName(
"java.util.Map.Entry<String, Integer>",
"java.util.Map.Entry<String, Integer>",
"stringIntegerMapEntry")
"stringIntegerMapEntry_")
}

@Test
Expand All @@ -116,22 +118,22 @@ class XNamesTest(private val processorType: ProcessorType, private val srcLang:

@Test
fun named() {
assertSafeName("String", "String", "fooString", "@javax.inject.Named(\"Foo\")")
assertSafeName("String", "String", "fooString_", "@javax.inject.Named(\"Foo\")")
}

@Test
fun customQualifier() {
assertSafeName("String", "String", "fooString", qualifierString = "@Foo")
assertSafeName("String", "String", "fooString_", qualifierString = "@Foo")
}

@Test
fun array() {
assertSafeName("String[]", "Array<String>", "stringArray")
assertSafeName("String[]", "Array<String>", "stringArray_")
}

@Test
fun enum() {
assertSafeName("LogLevel", "LogLevel", "logLevel")
assertSafeName("LogLevel", "LogLevel", "logLevel_")
}

@Test
Expand Down
1 change: 1 addition & 0 deletions intellij/src/test/kotlin/motif/intellij/TestHarness.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class TestHarness : LightJavaCodeInsightFixtureTestCase() {
addLibrary(Inject::class)
addLibrary(Scope::class)
addLibrary(Nullable::class)
addLibrary(JvmSuppressWildcards::class)
}

private fun addLibrary(clazz: KClass<*>) {
Expand Down
26 changes: 26 additions & 0 deletions tests/src/main/java/testcases/KT008_funky_names/ChildScope.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2023 Uber Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package testcases.KT008_funky_names

@motif.Scope
interface ChildScope {

@motif.Objects
abstract class Objects {

abstract fun myNull(): Null
}
}
102 changes: 102 additions & 0 deletions tests/src/main/java/testcases/KT008_funky_names/GRAPH.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
########################################################################
# #
# This file is auto-generated by running the Motif compiler tests and #
# serves a as validation of graph correctness. IntelliJ plugin tests #
# also rely on this file to ensure that the plugin graph understanding #
# is equivalent to the compiler's. #
# #
# - Do not edit manually. #
# - Commit changes to source control. #
# - Since this file is autogenerated, code review changes carefully to #
# ensure correctness. #
# #
########################################################################

-------
| Scope |
-------

==== Required ====

==== Provides ====

---- boolean | Objects.myBoolean ----
[ Required ]
[ Consumed By ]
* Scope | Scope.myBoolean()

---- byte | Objects.myByte ----
[ Required ]
[ Consumed By ]
* Scope | Scope.myByte()

---- char | Objects.myChar ----
[ Required ]
[ Consumed By ]
* Scope | Scope.myChar()

---- double | Objects.myDouble ----
[ Required ]
[ Consumed By ]
* Scope | Scope.myDouble()

---- float | Objects.myFloat ----
[ Required ]
[ Consumed By ]
* Scope | Scope.myFloat()

---- int | Objects.myInt ----
[ Required ]
[ Consumed By ]
* Scope | Scope.myInt()

---- Object | Objects.myAny ----
[ Required ]
[ Consumed By ]
* Scope | Scope.myAny()

---- Function2<Long,Double,Short> | Objects.function2 ----
[ Required ]
[ Consumed By ]
* ChildScope | Objects.myNull(function2)

---- long | Objects.myLong ----
[ Required ]
[ Consumed By ]
* Scope | Scope.myLong()

---- short | Objects.myShort ----
[ Required ]
[ Consumed By ]
* Scope | Scope.myShort()

---- Scope | implicit ----
[ Required ]
[ Consumed By ]

------------
| ChildScope |
------------

==== Required ====

---- Function2<Long,Double,Short> ----
[ Provided By ]
* Scope | Objects.function2
[ Consumed By ]
* ChildScope | Objects.myNull(function2)

==== Provides ====

---- ChildScope | implicit ----
[ Required ]
[ Consumed By ]

---- Null | Objects.myNull ----
[ Required ]
Function2<Long,Double,Short>
[ Provided By ]
* Scope | Objects.function2
[ Consumed By ]


18 changes: 18 additions & 0 deletions tests/src/main/java/testcases/KT008_funky_names/Null.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (c) 2023 Uber Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package testcases.KT008_funky_names

class Null(function2: @JvmSuppressWildcards ((Long, Double) -> Short))
Loading
Loading