mirror of
https://github.com/corda/corda.git
synced 2025-06-19 07:38:22 +00:00
support runtime-visible annotations and java.lang.reflect.Proxy
This commit is contained in:
49
test/Annotations.java
Normal file
49
test/Annotations.java
Normal file
@ -0,0 +1,49 @@
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@Test("couscous")
|
||||
@TestEnum(Color.Red)
|
||||
@TestInteger(42)
|
||||
public static void foo() {
|
||||
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
private @interface Test {
|
||||
public String value();
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
private @interface TestEnum {
|
||||
public Color value();
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
private @interface TestInteger {
|
||||
public int value();
|
||||
}
|
||||
|
||||
private static enum Color {
|
||||
Red, Yellow, Blue
|
||||
}
|
||||
|
||||
}
|
42
test/Proxies.java
Normal file
42
test/Proxies.java
Normal file
@ -0,0 +1,42 @@
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class Proxies {
|
||||
private static void expect(boolean v) {
|
||||
if (! v) throw new RuntimeException();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Foo foo = (Foo) Proxy.newProxyInstance
|
||||
(Proxies.class.getClassLoader(), new Class[] { Foo.class },
|
||||
new InvocationHandler() {
|
||||
public Object invoke(Object proxy, Method method, Object[] arguments)
|
||||
{
|
||||
if (method.getName().equals("bar")) {
|
||||
return "bam";
|
||||
} else if (method.getName().equals("baz")) {
|
||||
return ((Integer) arguments[0]) + 1;
|
||||
} else if (method.getName().equals("bim")) {
|
||||
return ((Long) arguments[0]) - 1L;
|
||||
} else if (method.getName().equals("boom")) {
|
||||
return ((String) arguments[0]).substring(1);
|
||||
} else {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
expect(foo.bar().equals("bam"));
|
||||
expect(foo.baz(42) == 43);
|
||||
expect(foo.bim(42L) == 41L);
|
||||
expect(foo.boom("hello").equals("ello"));
|
||||
}
|
||||
|
||||
private interface Foo {
|
||||
public String bar();
|
||||
public int baz(int v);
|
||||
public long bim(long v);
|
||||
public String boom(String s);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user