@Override @Nullable public org.springframework.jmx.export.metadata.ManagedOperation getManagedOperation(Method method) throws InvalidMetadataException { ManagedOperation ann = AnnotationUtils.findAnnotation(method, ManagedOperation.class); return copyPropertiesToBean(ann, org.springframework.jmx.export.metadata.ManagedOperation.class); }
@Override @Nullable public org.springframework.jmx.export.metadata.ManagedMetric getManagedMetric(Method method) throws InvalidMetadataException { ManagedMetric ann = AnnotationUtils.findAnnotation(method, ManagedMetric.class); return copyPropertiesToBean(ann, org.springframework.jmx.export.metadata.ManagedMetric.class); }
@Override public boolean matches(Method method) { return ((AnnotationUtils.findAnnotation(method, RequestMapping.class) == null) && (AnnotationUtils.findAnnotation(method, ModelAttribute.class) != null)); } };
@Test public void findMethodAnnotationFromInterface() throws Exception { Method method = ImplementsInterfaceWithAnnotatedMethod.class.getMethod("foo"); Order order = findAnnotation(method, Order.class); assertNotNull(order); }
@Test public void findMethodAnnotationNotAnnotated() throws Exception { Method m = Leaf.class.getMethod("notAnnotated"); assertNull(findAnnotation(m, Order.class)); }
@Test public void findMethodAnnotationFromInterfaceOnSuper() throws Exception { Method method = SubOfImplementsInterfaceWithAnnotatedMethod.class.getMethod("foo"); Order order = findAnnotation(method, Order.class); assertNotNull(order); }
@Test // SPR-16060 public void findMethodAnnotationFromGenericInterface() throws Exception { Method method = ImplementsInterfaceWithGenericAnnotatedMethod.class.getMethod("foo", String.class); Order order = findAnnotation(method, Order.class); assertNotNull(order); }
@Test public void findClassAnnotationOnSubInheritedAnnotationInterface() { Transactional tx = findAnnotation(SubInheritedAnnotationInterface.class, Transactional.class); assertNotNull("Should find @Transactional on SubInheritedAnnotationInterface", tx); }
@Test public void findClassAnnotationOnAnnotatedClassWithMissingTargetMetaAnnotation() { // TransactionalClass is NOT annotated or meta-annotated with @Component Component component = findAnnotation(TransactionalClass.class, Component.class); assertNull("Should not find @Component on TransactionalClass", component); }
@Test public void findClassAnnotationOnMetaCycleAnnotatedClassWithMissingTargetMetaAnnotation() { Component component = findAnnotation(MetaCycleAnnotatedClass.class, Component.class); assertNull("Should not find @Component on MetaCycleAnnotatedClass", component); }
@Test public void findClassAnnotationOnInheritedAnnotationInterface() { Transactional tx = findAnnotation(InheritedAnnotationInterface.class, Transactional.class); assertNotNull("Should find @Transactional on InheritedAnnotationInterface", tx); }
@Test public void findClassAnnotationOnNonInheritedAnnotationInterface() { Order order = findAnnotation(NonInheritedAnnotationInterface.class, Order.class); assertNotNull("Should find @Order on NonInheritedAnnotationInterface", order); }
@Test public void findMethodAnnotationWithAnnotationOnMethodInInterface() throws Exception { Method m = Leaf.class.getMethod("fromInterfaceImplementedByRoot"); // @Order is not @Inherited assertNull(m.getAnnotation(Order.class)); // getAnnotation() does not search on interfaces assertNull(getAnnotation(m, Order.class)); // findAnnotation() does search on interfaces assertNotNull(findAnnotation(m, Order.class)); }
@Test public void findMethodAnnotationOnRoot() throws Exception { Method m = Leaf.class.getMethod("annotatedOnRoot"); assertNotNull(m.getAnnotation(Order.class)); assertNotNull(getAnnotation(m, Order.class)); assertNotNull(findAnnotation(m, Order.class)); }
@Test public void findMethodAnnotationOnRootButOverridden() throws Exception { Method m = Leaf.class.getMethod("overrideWithoutNewAnnotation"); assertNull(m.getAnnotation(Order.class)); assertNull(getAnnotation(m, Order.class)); assertNotNull(findAnnotation(m, Order.class)); }
@Test public void getDefaultValueFromAnnotation() throws Exception { Method method = SimpleFoo.class.getMethod("something", Object.class); Order order = findAnnotation(method, Order.class); assertEquals(Ordered.LOWEST_PRECEDENCE, getDefaultValue(order, VALUE)); assertEquals(Ordered.LOWEST_PRECEDENCE, getDefaultValue(order)); }
@Test public void findClassAnnotationFavorsMoreLocallyDeclaredComposedAnnotationsOverInheritedAnnotations() { Transactional transactional = findAnnotation(SubSubClassWithInheritedAnnotation.class, Transactional.class); assertNotNull(transactional); assertTrue("readOnly flag for SubSubClassWithInheritedAnnotation", transactional.readOnly()); }
@Override protected void addPersistenceExceptionTranslation(ProxyFactory pf, PersistenceExceptionTranslator pet) { if (AnnotationUtils.findAnnotation(pf.getTargetClass(), Repository.class) != null) { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerBeanDefinition("peti", new RootBeanDefinition(PersistenceExceptionTranslationInterceptor.class)); bf.registerSingleton("pet", pet); pf.addAdvice((PersistenceExceptionTranslationInterceptor) bf.getBean("peti")); } }
@Test @SuppressWarnings("unchecked") public void findAnnotationDescriptorForTypesForClassWithMetaAnnotatedInterface() { Component rawAnnotation = AnnotationUtils.findAnnotation(ClassWithMetaAnnotatedInterface.class, Component.class); UntypedAnnotationDescriptor descriptor = findAnnotationDescriptorForTypes( ClassWithMetaAnnotatedInterface.class, Service.class, Component.class, Order.class, Transactional.class); assertNotNull(descriptor); assertEquals(ClassWithMetaAnnotatedInterface.class, descriptor.getRootDeclaringClass()); assertEquals(Meta1.class, descriptor.getDeclaringClass()); assertEquals(rawAnnotation, descriptor.getAnnotation()); assertEquals(Meta1.class, descriptor.getComposedAnnotation().annotationType()); }
@Test public void findAnnotationDescriptorForClassWithMetaAnnotatedInterface() { Component rawAnnotation = AnnotationUtils.findAnnotation(ClassWithMetaAnnotatedInterface.class, Component.class); AnnotationDescriptor<Component> descriptor = findAnnotationDescriptor(ClassWithMetaAnnotatedInterface.class, Component.class); assertNotNull(descriptor); assertEquals(ClassWithMetaAnnotatedInterface.class, descriptor.getRootDeclaringClass()); assertEquals(Meta1.class, descriptor.getDeclaringClass()); assertEquals(rawAnnotation, descriptor.getAnnotation()); assertEquals(Meta1.class, descriptor.getComposedAnnotation().annotationType()); }