Hi, I'm developing a Guice based Java-framework that dependends heavily on annotations and Guice supports many type of ways to inject dependencies via them. So far, I haven't found information about how to annotate constructors or their parameters. So given following Java-example, how is it expressed in Kotlin?
@Transactional
public class FooService {
@Inject
public FooService(DatabaseConnection connection,
@Named(“debugMode”) String debuMode) {
}
}
[Transactional] class FooService
[Inject] (c: DatabaseConnection, [Named("debugMode")] dm: String) {
}
Square brackets are optional im post cases
It seems that the parameter annotation works differently in Kotlin than in Java, because parameter annotation is not present. I made this kind of test:
TestAnnotation.java
import java.lang.annotation.Target;
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
@Target({ FIELD, PARAMETER, METHOD })
@Retention(RUNTIME)
public @interface TestAnnotation {
}
JavaClass.java
public class JavaClass {
public JavaClass(String foo, @TestAnnotation String foo2) {
}
}
KotlinClass.kt
public class KotlinClass(foo: String, [TestAnnotation] foo2 : String) {
}
AnnotationTest.java
import org.junit.Test;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
public class AnnotationTest {
private void listAnnotations(Class cl) {
System.out.println(cl + “: Constructor annotations”);
for (Constructor<!--?--> constructor : cl.getConstructors()) {
int i = 0;
for (Annotation annos : constructor.getParameterAnnotations()) {
System.out.print(“Parameter " + i + “:”);
for (Annotation _ : annos) {
System.out.print(” " + _.annotationType());
}
i++;
System.out.println(“”);
}
}
}
@Test
public void test() {
listAnnotations(JavaClass.class);
listAnnotations(KotlinClass.class);
}
}
The result is following:
class JavaClass: Constructor annotations
Parameter 0:
Parameter 1: interface TestAnnotation
class KotlinClass: Constructor annotations
Parameter 0: interface jet.runtime.typeinfo.JetValueParameter
Parameter 1: interface jet.runtime.typeinfo.JetValueParameter
Is this by design or a bug?
It's a bug. Please, report it to the tracker. Thanks