Skip to content

Commit

Permalink
KoraSubmodule should place tags on its components (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
Squiry authored Apr 26, 2023
1 parent 2851bcb commit 4fe9e63
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -559,12 +559,21 @@ private void generateAppParts() throws IOException {
mb.addCode("return new $T(", component);
for (int i = 0; i < constructor.getParameters().size(); i++) {
var parameter = constructor.getParameters().get(i);
mb.addParameter(ParameterSpec.get(parameter));
var pb = ParameterSpec.get(parameter).toBuilder();
var tag = TagUtils.parseTagValue(parameter);
if (!tag.isEmpty()) {
pb.addAnnotation(TagUtils.makeAnnotationSpec(tag));
}
mb.addParameter(pb.build());
if (i > 0) {
mb.addCode(", ");
}
mb.addCode("$L", parameter);
}
var tag = TagUtils.parseTagValue(component);
if (!tag.isEmpty()) {
mb.addAnnotation(TagUtils.makeAnnotationSpec(tag));
}
mb.addCode(");\n");
b.addMethod(mb.build());
}
Expand All @@ -589,15 +598,26 @@ private void generateAppParts() throws IOException {
mb.addCode("return $L.$L(", moduleName, method.getSimpleName());
for (int i = 0; i < method.getParameters().size(); i++) {
var parameter = method.getParameters().get(i);
mb.addParameter(ParameterSpec.get(parameter));
var pb = ParameterSpec.get(parameter).toBuilder();
var tag = TagUtils.parseTagValue(parameter);
if (!tag.isEmpty()) {
pb.addAnnotation(TagUtils.makeAnnotationSpec(tag));
}
mb.addParameter(pb.build());
if (i > 0) {
mb.addCode(", ");
}
mb.addCode("$L", parameter);
}
var tag = TagUtils.parseTagValue(method);
if (!tag.isEmpty()) {
mb.addAnnotation(TagUtils.makeAnnotationSpec(tag));
}
if (AnnotationUtils.findAnnotation(method, CommonClassNames.defaultComponent) != null) {
mb.addAnnotation(CommonClassNames.defaultComponent);
}
mb.addCode(");\n");
b.addMethod(mb.build());

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ default Class1 class1(Class2 class2) {
}

@Tag(Class1.class)
default Class1 class1Tag(Class2 class2) {
default Class1 class1Tag(@Tag(Class1.class) Class2 class2) {
return new Class1();
}

Expand All @@ -28,5 +28,10 @@ interface Module {
default Class2 class2() {
return new Class2();
}

@Tag(Class1.class)
default Class2 class2Tagged() {
return new Class2();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ import ru.tinkoff.kora.kora.app.ksp.declaration.ComponentDeclaration
import ru.tinkoff.kora.kora.app.ksp.declaration.ModuleDeclaration
import ru.tinkoff.kora.kora.app.ksp.exception.NewRoundException
import ru.tinkoff.kora.kora.app.ksp.interceptor.ComponentInterceptors
import ru.tinkoff.kora.ksp.common.*
import ru.tinkoff.kora.ksp.common.AnnotationUtils.findAnnotation
import ru.tinkoff.kora.ksp.common.BaseSymbolProcessor
import ru.tinkoff.kora.ksp.common.CommonClassNames
import ru.tinkoff.kora.ksp.common.KspCommonUtils.generated
import ru.tinkoff.kora.ksp.common.exception.ProcessingErrorException
import ru.tinkoff.kora.ksp.common.visitClass
import java.io.IOException
import java.util.*
import java.util.concurrent.atomic.AtomicReference
Expand Down Expand Up @@ -356,12 +354,21 @@ class KoraAppProcessor(
mb.addCode("return %T(", component.toClassName())
for (i in constructor.parameters.indices) {
val parameter = constructor.parameters[i]
mb.addParameter(parameter.name!!.asString(), parameter.type.toTypeName())
val tag = parameter.parseTags()
val ps = ParameterSpec.builder(parameter.name!!.asString(), parameter.type.toTypeName())
if (tag.isNotEmpty()) {
ps.addAnnotation(tag.makeTagAnnotationSpec())
}
mb.addParameter(ps.build())
if (i > 0) {
mb.addCode(", ")
}
mb.addCode("%N", parameter)
}
val tag = component.parseTags()
if (tag.isNotEmpty()) {
mb.addAnnotation(tag.makeTagAnnotationSpec())
}
mb.addCode(")\n")
b.addFunction(mb.build())
}
Expand All @@ -377,12 +384,24 @@ class KoraAppProcessor(
mb.addCode("return %N.%N(", moduleName, component.simpleName.asString())
for (i in component.parameters.indices) {
val parameter = component.parameters[i]
mb.addParameter(parameter.name!!.asString(), parameter.type.toTypeName())
val tag = parameter.parseTags()
val ps = ParameterSpec.builder(parameter.name!!.asString(), parameter.type.toTypeName())
if (tag.isNotEmpty()) {
ps.addAnnotation(tag.makeTagAnnotationSpec())
}
mb.addParameter(ps.build())
if (i > 0) {
mb.addCode(", ")
}
mb.addCode("%N", parameter.name?.asString())
}
val tag = component.parseTags()
if (tag.isNotEmpty()) {
mb.addAnnotation(tag.makeTagAnnotationSpec())
}
if (component.findAnnotation(CommonClassNames.defaultComponent) != null) {
mb.addAnnotation(CommonClassNames.defaultComponent)
}
mb.addCode(")\n")
b.addFunction(mb.build())
}
Expand Down

0 comments on commit 4fe9e63

Please sign in to comment.