2013-11-08 20:52:08 +00:00
|
|
|
import java.lang.reflect.InvocationHandler;
|
2009-09-19 00:01:54 +00:00
|
|
|
import java.lang.reflect.Method;
|
2013-11-08 20:52:08 +00:00
|
|
|
import java.lang.reflect.Proxy;
|
2009-09-19 00:01:54 +00:00
|
|
|
|
2012-05-22 20:00:31 +00:00
|
|
|
import avian.testing.annotations.Color;
|
|
|
|
import avian.testing.annotations.Test;
|
|
|
|
import avian.testing.annotations.TestEnum;
|
|
|
|
import avian.testing.annotations.TestInteger;
|
|
|
|
|
2009-09-19 00:01:54 +00:00
|
|
|
public class Annotations {
|
|
|
|
private static void expect(boolean v) {
|
|
|
|
if (! v) throw new RuntimeException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
Method m = Annotations.class.getMethod("foo");
|
|
|
|
|
|
|
|
expect(m.isAnnotationPresent(Test.class));
|
|
|
|
|
|
|
|
expect(((Test) m.getAnnotation(Test.class)).value().equals("couscous"));
|
|
|
|
|
|
|
|
expect(((TestEnum) m.getAnnotation(TestEnum.class)).value()
|
|
|
|
.equals(Color.Red));
|
|
|
|
|
|
|
|
expect(((TestInteger) m.getAnnotation(TestInteger.class)).value() == 42);
|
2012-05-22 20:00:31 +00:00
|
|
|
|
|
|
|
expect(m.getAnnotations().length == 3);
|
|
|
|
|
|
|
|
Method noAnno = Annotations.class.getMethod("noAnnotation");
|
|
|
|
expect(noAnno.getAnnotation(Test.class) == null);
|
|
|
|
expect(noAnno.getAnnotations().length == 0);
|
2013-11-08 20:52:08 +00:00
|
|
|
testProxyDefaultValue();
|
2009-09-19 00:01:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test("couscous")
|
|
|
|
@TestEnum(Color.Red)
|
|
|
|
@TestInteger(42)
|
|
|
|
public static void foo() {
|
|
|
|
|
|
|
|
}
|
2012-05-22 20:00:31 +00:00
|
|
|
|
|
|
|
public static void noAnnotation() {
|
|
|
|
|
2009-09-19 00:01:54 +00:00
|
|
|
}
|
2013-11-08 20:52:08 +00:00
|
|
|
|
|
|
|
private static void testProxyDefaultValue() {
|
|
|
|
ClassLoader loader = Annotations.class.getClassLoader();
|
|
|
|
InvocationHandler handler = new InvocationHandler() {
|
|
|
|
public Object invoke(Object proxy, Method method, Object... args) {
|
|
|
|
return method.getDefaultValue();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Test test = (Test)
|
|
|
|
Proxy.newProxyInstance(loader, new Class[] { Test.class }, handler);
|
|
|
|
expect("Hello, world!".equals(test.value()));
|
|
|
|
}
|
2009-09-19 00:01:54 +00:00
|
|
|
}
|