-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to get constantValue for final fields (#10693)
* Add ability to get constantValue for final fields Fixed #10692 * Update core-processor/src/main/java/io/micronaut/inject/ast/FieldElement.java --------- Co-authored-by: Sergio del Amo <[email protected]>
- Loading branch information
Showing
6 changed files
with
176 additions
and
0 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
73 changes: 73 additions & 0 deletions
73
...n/src/test/groovy/io/micronaut/kotlin/processing/ast/visitor/KotlinEnumElementSpec.groovy
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,73 @@ | ||
package io.micronaut.kotlin.processing.ast.visitor | ||
|
||
import io.micronaut.annotation.processing.test.AbstractKotlinCompilerSpec | ||
import io.micronaut.inject.ast.ClassElement | ||
import io.micronaut.inject.ast.ElementQuery | ||
import io.micronaut.inject.ast.EnumElement | ||
import spock.lang.Ignore | ||
import spock.lang.PendingFeature | ||
|
||
class KotlinEnumElementSpec extends AbstractKotlinCompilerSpec { | ||
|
||
void "test is enum"() { | ||
given: | ||
def element = buildClassElement("test.MyEnum", """ | ||
package test | ||
enum class MyEnum { | ||
A, B | ||
} | ||
""") | ||
expect: | ||
element.isEnum() | ||
element.getPrimaryConstructor().get().getDeclaringType().isEnum() | ||
} | ||
|
||
void "test inner enum is enum"() { | ||
given: | ||
def element = buildClassElement("test.Foo",""" | ||
package test | ||
class Foo { | ||
enum class MyEnum { | ||
A, B | ||
} | ||
} | ||
""") | ||
expect: | ||
element.getEnclosedElement(ElementQuery.of(ClassElement.class)).get().getPrimaryConstructor().get().getDeclaringType().isEnum() | ||
} | ||
|
||
@Ignore | ||
@PendingFeature(reason = "constantValue not available with KSP") | ||
void "get enum constantValue for final fields"() { | ||
given: | ||
def element = (EnumElement) buildClassElement("test.MyEnum", """ | ||
package test | ||
enum class MyEnum( | ||
value: Int | ||
) { | ||
ENUM_VAL1(10), | ||
ENUM_VAL2(11), | ||
UNRECOGNIZED(-1), | ||
; | ||
companion object { | ||
const val ENUM_VAL1_VALUE = 10 | ||
const val ENUM_VAL2_VALUE = 11 | ||
} | ||
} | ||
""") | ||
expect: | ||
for (def field : element.fields) { | ||
if (field.name == 'ENUM_VAL1_VALUE') { | ||
field.constantValue == 10 | ||
} else if (field.name == 'ENUM_VAL2_VALUE') { | ||
field.constantValue == 11 | ||
} | ||
} | ||
} | ||
} |