package com.engebretson; import java.lang.annotation.Annotation; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Method; public class AnnotationUtilsSpeedTest extends BaseSpeedTest { @Retention(RetentionPolicy.RUNTIME) @interface TestAnnotation { String realValue() default ""; } @TestAnnotation() private static class TestObject { } public static void main(String[] args) { runWithStackDepth(() -> new AnnotationUtilsSpeedTest().runTests(), stackDepth); } private static void runWithStackDepth(Runnable runnable, int depth) { if (depth > 0) { runWithStackDepth(runnable, depth - 1); } else { runnable.run(); } } static final int stackDepth = 100; Object annotatedObject = new TestObject(); public AnnotationUtilsSpeedTest() { super(5, 500000); } private Object originalMethod(Annotation annotation, String attributeName) { if (annotation == null || attributeName == null || attributeName.length() == 0) { return null; } try { Method method = annotation.annotationType().getDeclaredMethod(attributeName); return method.invoke(annotation); } catch (NoSuchMethodException ex) { return null; } catch (Throwable ex) { return null; } } private Object revisedMethod(Annotation annotation, String attributeName) { if (annotation == null || attributeName == null || attributeName.length() == 0) { return null; } try { Method[] declaredMethods = annotation.annotationType().getDeclaredMethods(); for (Method method : declaredMethods) { if (method.getName().equals(attributeName)) { return method.invoke(annotation); } } return null; } catch (Throwable ex) { return null; } } @Override protected void runNewTest() { Annotation annotation = annotatedObject.getClass().getAnnotations()[0]; revisedMethod(annotation, "false"); } @Override protected void runOldTest() { Annotation annotation = annotatedObject.getClass().getAnnotations()[0]; originalMethod(annotation, "false"); } }