almost full support for annotations
This commit is contained in:
@@ -18,9 +18,7 @@ package org.jetbrains.jet.codegen;
|
||||
|
||||
import jet.JetObject;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.*;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
@@ -120,4 +118,42 @@ public class AnnotationGenTest extends CodegenTestCase {
|
||||
|
||||
assertEquals("239", methods[0].invoke(bClassAnnotation));
|
||||
}
|
||||
|
||||
public void testAnnotationClassWithAnnotationProperty()
|
||||
throws
|
||||
NoSuchFieldException,
|
||||
NoSuchMethodException,
|
||||
ClassNotFoundException,
|
||||
IllegalAccessException,
|
||||
InstantiationException,
|
||||
InvocationTargetException {
|
||||
loadText("import java.lang.annotation.*\n" +
|
||||
"" +
|
||||
"annotation class C(val c: String)\n" +
|
||||
"Retention(RetentionPolicy.RUNTIME) annotation class A(val a: C)\n" +
|
||||
"" +
|
||||
"A(C(\"239\")) class B()");
|
||||
Class aClass = generateClass("A");
|
||||
|
||||
Retention annotation = (Retention)aClass.getAnnotation(Retention.class);
|
||||
RetentionPolicy value = annotation.value();
|
||||
assertEquals(RetentionPolicy.RUNTIME, value);
|
||||
|
||||
Method[] methods = aClass.getDeclaredMethods();
|
||||
assertEquals(1, methods.length);
|
||||
assertEquals("a", methods[0].getName());
|
||||
assertEquals("C", methods[0].getReturnType().getName());
|
||||
assertEquals(0, methods[0].getParameterTypes().length);
|
||||
assertTrue(aClass.isAnnotation());
|
||||
|
||||
Class<?> bClass = aClass.getClassLoader().loadClass("B");
|
||||
Annotation bClassAnnotation = bClass.getAnnotation(aClass);
|
||||
assertNotNull(bClassAnnotation);
|
||||
|
||||
Object invoke = methods[0].invoke(bClassAnnotation);
|
||||
// there is some Proxy here
|
||||
Class<?> cClass = invoke.getClass().getInterfaces()[0];
|
||||
assertEquals("C", cClass.getName());
|
||||
assertEquals("239", cClass.getDeclaredMethod("c").invoke(invoke));
|
||||
}
|
||||
}
|
||||
@@ -20,9 +20,7 @@ import org.jetbrains.jet.compiler.CompileEnvironment;
|
||||
import org.jetbrains.jet.lang.psi.JetPsiUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.*;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.MalformedURLException;
|
||||
@@ -269,4 +267,55 @@ public class StdlibTest extends CodegenTestCase {
|
||||
assertEquals(239, ((int[])invoke)[0]);
|
||||
assertEquals(932, ((int[])invoke)[1]);
|
||||
}
|
||||
|
||||
public void testAnnotationClassWithEnumArrayProperty()
|
||||
throws
|
||||
NoSuchFieldException,
|
||||
NoSuchMethodException,
|
||||
ClassNotFoundException,
|
||||
IllegalAccessException,
|
||||
InstantiationException,
|
||||
InvocationTargetException {
|
||||
loadText("import java.lang.annotation.*\n" +
|
||||
"" +
|
||||
"Target(ElementType.TYPE, ElementType.METHOD) annotation class A");
|
||||
Class aClass = generateClass("A");
|
||||
|
||||
Target annotation = (Target)aClass.getAnnotation(Target.class);
|
||||
ElementType[] value = annotation.value();
|
||||
assertEquals(2, value.length);
|
||||
|
||||
assertEquals(ElementType.TYPE, value[0]);
|
||||
assertEquals(ElementType.METHOD, value[1]);
|
||||
}
|
||||
|
||||
public void testAnnotationClassWithAnnotationArrayProperty()
|
||||
throws
|
||||
NoSuchFieldException,
|
||||
NoSuchMethodException,
|
||||
ClassNotFoundException,
|
||||
IllegalAccessException,
|
||||
InstantiationException,
|
||||
InvocationTargetException {
|
||||
loadText("import java.lang.annotation.*\n" +
|
||||
"" +
|
||||
"Retention(RetentionPolicy.RUNTIME) annotation class A(val a: Array<Retention>)\n" +
|
||||
"" +
|
||||
"A(array(Retention(RetentionPolicy.RUNTIME),Retention(RetentionPolicy.SOURCE))) class B()");
|
||||
Class aClass = generateClass("A");
|
||||
|
||||
Method[] methods = aClass.getDeclaredMethods();
|
||||
assertEquals(1, methods.length);
|
||||
assertEquals("a", methods[0].getName());
|
||||
|
||||
Class<?> bClass = aClass.getClassLoader().loadClass("B");
|
||||
Annotation bClassAnnotation = bClass.getAnnotation(aClass);
|
||||
assertNotNull(bClassAnnotation);
|
||||
|
||||
Object invoke = methods[0].invoke(bClassAnnotation);
|
||||
Retention[] invoke1 = (Retention[])invoke;
|
||||
assertEquals(2, invoke1.length);
|
||||
assertEquals(invoke1[0].value(), RetentionPolicy.RUNTIME);
|
||||
assertEquals(invoke1[1].value(), RetentionPolicy.SOURCE);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user