/** * Obtain the annotation of the specified {@code annotationType} that is on this type descriptor. * <p>As of Spring Framework 4.2, this method supports arbitrary levels of meta-annotations. * @param annotationType the annotation type * @return the annotation, or {@code null} if no such annotation exists on this type descriptor */ @Nullable public <T extends Annotation> T getAnnotation(Class<T> annotationType) { if (this.annotatedElement.isEmpty()) { // Shortcut: AnnotatedElementUtils would have to expect AnnotatedElement.getAnnotations() // to return a copy of the array, whereas we can do it more efficiently here. return null; } return AnnotatedElementUtils.getMergedAnnotation(this.annotatedElement, annotationType); }
/** * Obtain the annotation of the specified {@code annotationType} that is on this type descriptor. * <p>As of Spring Framework 4.2, this method supports arbitrary levels of meta-annotations. * @param annotationType the annotation type * @return the annotation, or {@code null} if no such annotation exists on this type descriptor */ @Nullable public <T extends Annotation> T getAnnotation(Class<T> annotationType) { if (this.annotatedElement.isEmpty()) { // Shortcut: AnnotatedElementUtils would have to expect AnnotatedElement.getAnnotations() // to return a copy of the array, whereas we can do it more efficiently here. return null; } return AnnotatedElementUtils.getMergedAnnotation(this.annotatedElement, annotationType); }
/** * Provides the {@link DefaultCacheConfig} instance for the specified {@link Class}. * * @param target the class-level to handle * @return the default config (never {@code null}) */ DefaultCacheConfig getDefaultCacheConfig(Class<?> target) { CacheConfig annotation = AnnotatedElementUtils.getMergedAnnotation(target, CacheConfig.class); if (annotation != null) { return new DefaultCacheConfig(annotation.cacheNames(), annotation.keyGenerator(), annotation.cacheManager(), annotation.cacheResolver()); } return new DefaultCacheConfig(); }
private void assertGetMergedAnnotation(Class<?> element, String... expected) { String name = ContextConfig.class.getName(); ContextConfig contextConfig = getMergedAnnotation(element, ContextConfig.class); assertNotNull("Should find @ContextConfig on " + element.getSimpleName(), contextConfig); assertArrayEquals("locations", expected, contextConfig.locations()); assertArrayEquals("value", expected, contextConfig.value()); assertArrayEquals("classes", new Class<?>[0], contextConfig.classes()); // Verify contracts between utility methods: assertTrue(isAnnotated(element, name)); }
@Test public void getMergedAnnotationWithImplicitAliasesInMetaAnnotationOnComposedAnnotation() { Class<?> element = ComposedImplicitAliasesContextConfigClass.class; String name = ImplicitAliasesContextConfig.class.getName(); ImplicitAliasesContextConfig config = getMergedAnnotation(element, ImplicitAliasesContextConfig.class); String[] expected = asArray("A.xml", "B.xml"); assertNotNull("Should find @ImplicitAliasesContextConfig on " + element.getSimpleName(), config); assertArrayEquals("groovyScripts", expected, config.groovyScripts()); assertArrayEquals("xmlFiles", expected, config.xmlFiles()); assertArrayEquals("locations", expected, config.locations()); assertArrayEquals("value", expected, config.value()); // Verify contracts between utility methods: assertTrue(isAnnotated(element, name)); }
public static <T extends Annotation> T getAnnotation(Method method, Class<T> annotation) { try { Class.forName("org.springframework.core.annotation.AnnotatedElementUtils"); return AnnotatedElementUtils.getMergedAnnotation(method, annotation); } catch (ClassNotFoundException e) { return method.getAnnotation(annotation); } }
@Override protected String[] getProperties(Class<?> testClass) { JdbcTest annotation = AnnotatedElementUtils.getMergedAnnotation(testClass, JdbcTest.class); return (annotation != null) ? annotation.properties() : null; }
@Override protected String[] getProperties(Class<?> testClass) { DataJdbcTest annotation = AnnotatedElementUtils.getMergedAnnotation(testClass, DataJdbcTest.class); return (annotation != null) ? annotation.properties() : null; }
@Override protected String[] getProperties(Class<?> testClass) { JsonTest annotation = AnnotatedElementUtils.getMergedAnnotation(testClass, JsonTest.class); return (annotation != null) ? annotation.properties() : null; }
@Override protected String[] getProperties(Class<?> testClass) { WebFluxTest annotation = AnnotatedElementUtils.getMergedAnnotation(testClass, WebFluxTest.class); return (annotation != null) ? annotation.properties() : null; }
@Override protected String[] getProperties(Class<?> testClass) { DataMongoTest annotation = AnnotatedElementUtils.getMergedAnnotation(testClass, DataMongoTest.class); return (annotation != null) ? annotation.properties() : null; }
@Override protected String[] getProperties(Class<?> testClass) { DataRedisTest annotation = AnnotatedElementUtils.getMergedAnnotation(testClass, DataRedisTest.class); return (annotation != null) ? annotation.properties() : null; }
@Override protected String[] getProperties(Class<?> testClass) { DataNeo4jTest annotation = AnnotatedElementUtils.getMergedAnnotation(testClass, DataNeo4jTest.class); return (annotation != null) ? annotation.properties() : null; }
@Override protected String[] getProperties(Class<?> testClass) { RestClientTest annotation = AnnotatedElementUtils.getMergedAnnotation(testClass, RestClientTest.class); return (annotation != null) ? annotation.properties() : null; }
private Inject findReferenceAnnotation(AccessibleObject accessibleObject) { if (accessibleObject.getAnnotations().length > 0) { Inject inject = getMergedAnnotation(accessibleObject, Inject.class); return inject; } return null; }
@Override protected String[] getProperties(Class<?> testClass) { WebMvcTest annotation = AnnotatedElementUtils.getMergedAnnotation(testClass, WebMvcTest.class); return (annotation != null) ? annotation.properties() : null; }
@Override protected String[] getProperties(Class<?> testClass) { JooqTest annotation = AnnotatedElementUtils.getMergedAnnotation(testClass, JooqTest.class); return (annotation != null) ? annotation.properties() : null; }
@Override protected String[] getProperties(Class<?> testClass) { DataJpaTest annotation = AnnotatedElementUtils.getMergedAnnotation(testClass, DataJpaTest.class); return (annotation != null) ? annotation.properties() : null; }
@Override public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) { SpringBootTest annotation = AnnotatedElementUtils .getMergedAnnotation(mergedConfig.getTestClass(), SpringBootTest.class); if (annotation.webEnvironment().isEmbedded()) { registerWebTestClient(context); } }
@Override public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedContextConfiguration) { SpringBootTest annotation = AnnotatedElementUtils.getMergedAnnotation( mergedContextConfiguration.getTestClass(), SpringBootTest.class); if (annotation.webEnvironment().isEmbedded()) { registerTestRestTemplate(context); } }