Skip to content

Commit

Permalink
Kotlin: Support if-else and arrays + Core 4.7 (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
dstepanov authored Oct 11, 2024
1 parent 8dff9a0 commit 164f3ba
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 22 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[versions]
micronaut = "4.6.6"
micronaut = "4.7.0"
micronaut-docs = "2.0.0"
micronaut-test = "4.1.0"
managed-kotlinpoet = "1.18.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,27 @@ class KotlinPoetSourceGenerator : SourceGenerator {
.add(renderExpressionCode(objectDef, methodDef, expressionDef.right))
.build()
}
if (expressionDef is NewArrayOfSize) {
return CodeBlock.of(
"arrayOfNulls<%T>(%L)",
asType(expressionDef.type.componentType, objectDef),
expressionDef.size
)
}
if (expressionDef is NewArrayInitialized) {
val builder: CodeBlock.Builder = CodeBlock.builder()
builder.add("arrayOf<%T>(", asType(expressionDef.type.componentType, objectDef))
val iterator: Iterator<ExpressionDef> = expressionDef.expressions.iterator()
while (iterator.hasNext()) {
val expression = iterator.next()
builder.add(renderExpressionCode(objectDef, methodDef, expression))
if (iterator.hasNext()) {
builder.add(",")
}
}
builder.add(")")
return builder.build()
}
if (expressionDef is PrimitiveInstance) {
return renderExpressionCode(objectDef, methodDef, expressionDef.value)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@ public final class GenerateArrayVisitor implements TypeElementVisitor<io.microna

@Override
public void visitClass(ClassElement element, VisitorContext context) {
TypeDef arrayType = TypeDef.of(String[].class);
TypeDef arrayType = new TypeDef.Array(TypeDef.STRING.makeNullable(), 1, false);

ClassDef arrayClassDef1 = ClassDef.builder(element.getPackageName() + ".Array1")
.addMethod(MethodDef.builder("test").addParameter("param", String.class)
.addMethod(MethodDef.builder("test").addParameter("param", TypeDef.STRING)
.addModifiers(Modifier.PUBLIC)
.build((self, parameterDefs) -> arrayType.instantiateArray(10).returning()))
.build();

writeObject(element, context, arrayClassDef1);

ClassDef arrayClassDef2 = ClassDef.builder(element.getPackageName() + ".Array2")
.addMethod(MethodDef.builder("test").addParameter("param", String.class)
.addMethod(MethodDef.builder("test").addParameter("param", TypeDef.STRING)
.addModifiers(Modifier.PUBLIC)
.build((self, parameterDefs) ->
arrayType.instantiateArray(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@ public void visitClass(ClassElement element, VisitorContext context) {

Class<?> implementsType = Predicate.class;

TypeDef paramType = TypeDef.OBJECT.makeNullable();

ClassDef ifPredicateDef = ClassDef.builder(element.getPackageName() + ".IfPredicate")
.addSuperinterface(TypeDef.parameterized(implementsType, Object.class))
.addMethod(MethodDef.builder("test").addParameter(PARAM, Object.class)
.addSuperinterface(TypeDef.parameterized(implementsType, paramType))
.addMethod(MethodDef.builder("test").addParameter(PARAM, paramType)
.addModifiers(Modifier.PUBLIC)
.overrides()
.addStatement(new StatementDef.If(
new VariableDef.MethodParameter(PARAM, TypeDef.of(Object.class)).isNull(),
new VariableDef.MethodParameter(PARAM, paramType).isNull(),
ExpressionDef.trueValue().returning()
))
.addStatement(ExpressionDef.falseValue().returning())
Expand All @@ -71,12 +73,12 @@ public void visitClass(ClassElement element, VisitorContext context) {
writeObject(element, context, sourceGenerator, ifPredicateDef);

ClassDef ifNonPredicateDef = ClassDef.builder(element.getPackageName() + ".IfNonPredicate")
.addSuperinterface(TypeDef.parameterized(implementsType, Object.class))
.addMethod(MethodDef.builder("test").addParameter(PARAM, Object.class)
.addSuperinterface(TypeDef.parameterized(implementsType, paramType))
.addMethod(MethodDef.builder("test").addParameter(PARAM, paramType)
.addModifiers(Modifier.PUBLIC)
.overrides()
.addStatement(
new VariableDef.MethodParameter(PARAM, TypeDef.of(Object.class))
new VariableDef.MethodParameter(PARAM, paramType)
.isNonNull()
.asConditionIf(ExpressionDef.trueValue().returning())
)
Expand All @@ -88,14 +90,14 @@ public void visitClass(ClassElement element, VisitorContext context) {
writeObject(element, context, sourceGenerator, ifNonPredicateDef);

ClassDef ifElsePredicateDef = ClassDef.builder(element.getPackageName() + ".IfElsePredicate")
.addSuperinterface(TypeDef.parameterized(implementsType, Object.class))
.addMethod(MethodDef.builder("test").addParameter(PARAM, Object.class)
.addSuperinterface(TypeDef.parameterized(implementsType, paramType))
.addMethod(MethodDef.builder("test").addParameter(PARAM, paramType)
.addModifiers(Modifier.PUBLIC)
.overrides()
.addStatement(new StatementDef.IfElse(
new ExpressionDef.Condition(
" == ",
new VariableDef.MethodParameter(PARAM, TypeDef.of(Object.class)),
new VariableDef.MethodParameter(PARAM, paramType),
new ExpressionDef.Constant(TypeDef.of(Object.class), null)
),
new StatementDef.Return(
Expand All @@ -112,12 +114,12 @@ public void visitClass(ClassElement element, VisitorContext context) {
writeObject(element, context, sourceGenerator, ifElsePredicateDef);

ClassDef ifNonElsePredicateDef = ClassDef.builder(element.getPackageName() + ".IfNonElsePredicate")
.addSuperinterface(TypeDef.parameterized(implementsType, Object.class))
.addMethod(MethodDef.builder("test").addParameter(PARAM, Object.class)
.addSuperinterface(TypeDef.parameterized(implementsType, paramType))
.addMethod(MethodDef.builder("test").addParameter(PARAM, paramType)
.addModifiers(Modifier.PUBLIC)
.overrides()
.addStatement(
new VariableDef.MethodParameter(PARAM, TypeDef.of(Object.class))
new VariableDef.MethodParameter(PARAM, paramType)
.isNull()
.asConditionIfElse(
ExpressionDef.trueValue().returning(),
Expand All @@ -131,8 +133,8 @@ public void visitClass(ClassElement element, VisitorContext context) {
writeObject(element, context, sourceGenerator, ifNonElsePredicateDef);

ClassDef ifNonElseExpressionPredicateDef = ClassDef.builder(element.getPackageName() + ".IfNonElseExpressionPredicate")
.addSuperinterface(TypeDef.parameterized(implementsType, Object.class))
.addMethod(MethodDef.builder("test").addParameter(PARAM, Object.class)
.addSuperinterface(TypeDef.parameterized(implementsType, paramType))
.addMethod(MethodDef.builder("test").addParameter(PARAM, paramType)
.addModifiers(Modifier.PUBLIC)
.overrides()
.returns(boolean.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
import static org.junit.jupiter.api.Assertions.assertTrue;

class IfsPredicateTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
*/
package io.micronaut.sourcegen.example

import io.micronaut.sourcegen.custom.example.GenerateInterface
import io.micronaut.sourcegen.custom.example.GenerateArray
import io.micronaut.sourcegen.custom.example.GenerateIfsPredicate
import io.micronaut.sourcegen.custom.example.GenerateMyBean1
import io.micronaut.sourcegen.custom.example.GenerateMyBean2
import io.micronaut.sourcegen.custom.example.GenerateMyBean3
import io.micronaut.sourcegen.custom.example.GenerateMyEnum1
import io.micronaut.sourcegen.custom.example.GenerateInterface
import io.micronaut.sourcegen.custom.example.GenerateMyRecord1
import io.micronaut.sourcegen.custom.example.GenerateMyRepository1
import io.micronaut.sourcegen.custom.example.GenerateSwitch
Expand All @@ -31,5 +33,7 @@ import io.micronaut.sourcegen.custom.example.GenerateSwitch
@GenerateMyRepository1
@GenerateMyRecord1
@GenerateMyEnum1
@GenerateIfsPredicate
@GenerateSwitch
@GenerateArray
class Trigger
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2003-2024 the original author or authors.
*
* 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
*
* https://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 io.micronaut.sourcegen.example

import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test

internal class ArrayTest {
@Test
fun test1() {
val array1 = Array1()
val test = array1.test("")
Assertions.assertArrayEquals(arrayOfNulls<String>(10), test)
}

@Test
fun test2() {
val array1 = Array2()
val test = array1.test("")
Assertions.assertArrayEquals(arrayOf("A", "B", "C"), test)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2003-2024 the original author or authors.
*
* 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
*
* https://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 io.micronaut.sourcegen.example

import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test

internal class IfsPredicateTest {
@Test
fun testIf() {
val predicate: IfPredicate = IfPredicate()
assertTrue(predicate.test(null))
assertFalse(predicate.test(""))
}

@Test
fun testIfNon() {
val predicate: IfNonPredicate = IfNonPredicate()
assertFalse(predicate.test(null))
assertTrue(predicate.test(""))
}

@Test
fun testIfElse() {
val predicate: IfElsePredicate = IfElsePredicate()
assertTrue(predicate.test(null))
assertFalse(predicate.test(""))
}

@Test
fun testIfElseNon() {
val predicate: IfNonElsePredicate = IfNonElsePredicate()
assertTrue(predicate.test(null))
assertFalse(predicate.test(""))
}

@Test
fun testIfExpressionELse() {
val predicate: IfNonElseExpressionPredicate = IfNonElseExpressionPredicate()
assertTrue(predicate.test(null))
assertFalse(predicate.test(""))
}
}

0 comments on commit 164f3ba

Please sign in to comment.