/* package whatever; // don't place package name! */
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface Test {
Class expected();
}
class TestAnnotationParser {
public <T
> void parse
(Class
<T
> clazz, T obj
) throws Exception { Method[] methods
= clazz.
getMethods(); int pass = 0;
int fail = 0;
for (Method method
: methods
) { if (method.isAnnotationPresent(Test.class)) {
Test test = method.getAnnotation(Test.class);
Class expected = test.expected();
try {
method.invoke(obj);
pass++;
e.printStackTrace();
fail++;
} else {
pass++;
}
}
}
}
System.
out.
println("Passed:" + pass
+ " Fail:" + fail
); }
}
class MyTest {
public void testBlah() {
}
}
/* Name of the class has to be "Main" only if the class is public. */
public class Main {
public static void main
(String[] args
) { TestAnnotationParser parser = new TestAnnotationParser();
try {
parser.parse(MyTest.class, new MyTest());
e.printStackTrace();
}
}
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEubGFuZy5hbm5vdGF0aW9uLkVsZW1lbnRUeXBlOwppbXBvcnQgamF2YS5sYW5nLmFubm90YXRpb24uUmV0ZW50aW9uOwppbXBvcnQgamF2YS5sYW5nLmFubm90YXRpb24uUmV0ZW50aW9uUG9saWN5OwppbXBvcnQgamF2YS5sYW5nLmFubm90YXRpb24uVGFyZ2V0OwppbXBvcnQgamF2YS5sYW5nLnJlZmxlY3QuTWV0aG9kOwoKQFRhcmdldChFbGVtZW50VHlwZS5NRVRIT0QpCkBSZXRlbnRpb24oUmV0ZW50aW9uUG9saWN5LlJVTlRJTUUpCkBpbnRlcmZhY2UgVGVzdCB7CiAgICBDbGFzcyBleHBlY3RlZCgpOwp9CgpjbGFzcyBUZXN0QW5ub3RhdGlvblBhcnNlciB7CiAgICBwdWJsaWMgPFQ+IHZvaWQgcGFyc2UoQ2xhc3M8VD4gY2xhenosIFQgb2JqKSB0aHJvd3MgRXhjZXB0aW9uIHsKICAgICAgICBNZXRob2RbXSBtZXRob2RzID0gY2xhenouZ2V0TWV0aG9kcygpOwogICAgICAgIGludCBwYXNzID0gMDsKICAgICAgICBpbnQgZmFpbCA9IDA7CgogICAgICAgIGZvciAoTWV0aG9kIG1ldGhvZCA6IG1ldGhvZHMpIHsKICAgICAgICAgICAgaWYgKG1ldGhvZC5pc0Fubm90YXRpb25QcmVzZW50KFRlc3QuY2xhc3MpKSB7CiAgICAgICAgICAgICAgICBUZXN0IHRlc3QgPSBtZXRob2QuZ2V0QW5ub3RhdGlvbihUZXN0LmNsYXNzKTsKICAgICAgICAgICAgICAgIENsYXNzIGV4cGVjdGVkID0gdGVzdC5leHBlY3RlZCgpOwogICAgICAgICAgICAgICAgdHJ5IHsKICAgICAgICAgICAgICAgICAgICBtZXRob2QuaW52b2tlKG9iaik7CiAgICAgICAgICAgICAgICAgICAgcGFzcysrOwogICAgICAgICAgICAgICAgfSBjYXRjaCAoRXhjZXB0aW9uIGUpIHsKICAgICAgICAgICAgICAgICAgICBpZiAoRXhjZXB0aW9uLmNsYXNzICE9IGV4cGVjdGVkKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIGUucHJpbnRTdGFja1RyYWNlKCk7CiAgICAgICAgICAgICAgICAgICAgICAgIGZhaWwrKzsKICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICBwYXNzKys7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiUGFzc2VkOiIgKyBwYXNzICsgIiAgIEZhaWw6IiArIGZhaWwpOwogICAgfQp9CgpjbGFzcyBNeVRlc3QgewoKICAgIEBUZXN0KGV4cGVjdGVkID0gUnVudGltZUV4Y2VwdGlvbi5jbGFzcykKICAgIHB1YmxpYyB2b2lkIHRlc3RCbGFoKCkgewogICAgfQp9CgovKiBOYW1lIG9mIHRoZSBjbGFzcyBoYXMgdG8gYmUgIk1haW4iIG9ubHkgaWYgdGhlIGNsYXNzIGlzIHB1YmxpYy4gKi8KcHVibGljIGNsYXNzIE1haW4gewogICAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgewogICAgICAgIFRlc3RBbm5vdGF0aW9uUGFyc2VyIHBhcnNlciA9IG5ldyBUZXN0QW5ub3RhdGlvblBhcnNlcigpOwogICAgICAgIHRyeSB7CiAgICAgICAgICAgIHBhcnNlci5wYXJzZShNeVRlc3QuY2xhc3MsIG5ldyBNeVRlc3QoKSk7CiAgICAgICAgfSBjYXRjaCAoRXhjZXB0aW9uIGUpIHsKICAgICAgICAgICAgZS5wcmludFN0YWNrVHJhY2UoKTsKICAgICAgICB9CiAgICB9Cn0K