@Test public void selectImports() throws Exception { assertThat(imports.selectImports(metadata).length, is(0)); when(metadata.getAnnotationAttributes(EnableContextPropagation.class.getName(), true)).thenReturn(null); assertThat(imports.selectImports(metadata).length, is(0)); assertThat(imports.selectImports(metadata).length, is(0)); List<String> actual = Arrays.asList(imports.selectImports(new SimpleMetadataReaderFactory().getMetadataReader(Annotated.class.getName()).getAnnotationMetadata())); assertThat(actual, Matchers.containsInAnyOrder( PreservesHeadersInboundHttpRequestStrategy.class.getName(), PreservesHttpHeadersFeignStrategy.class.getName(), PreservesExecutionContextExecutorStrategy.class.getName(), PreservesHttpHeadersZuulStrategy.class.getName(), PreservesExecutionContextHystrixStrategy.class.getName(), PreservesJmsMessagePropertiesStrategy.class.getName(), PreservesStompHeadersStrategy.class.getName() )); }
@Test public void getContextCustomizerShouldAddExcludeFilters() throws Exception { ContextCustomizer customizer = this.factory .createContextCustomizer(WithExcludeFilters.class, null); customizer.customizeContext(this.context, this.mergedContextConfiguration); this.context.refresh(); TypeExcludeFilter filter = this.context.getBean(TypeExcludeFilter.class); MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(); MetadataReader metadataReader = metadataReaderFactory .getMetadataReader(NoAnnotation.class.getName()); assertThat(filter.match(metadataReader, metadataReaderFactory)).isFalse(); metadataReader = metadataReaderFactory .getMetadataReader(SimpleExclude.class.getName()); assertThat(filter.match(metadataReader, metadataReaderFactory)).isTrue(); metadataReader = metadataReaderFactory .getMetadataReader(TestClassAwareExclude.class.getName()); assertThat(filter.match(metadataReader, metadataReaderFactory)).isTrue(); }
@SuppressWarnings("unchecked") @Test public void defaultServletConfiguration() throws IOException { ScannedGenericBeanDefinition scanned = new ScannedGenericBeanDefinition( new SimpleMetadataReaderFactory() .getMetadataReader(DefaultConfigurationServlet.class.getName())); this.handler.handle(scanned, this.registry); BeanDefinition servletRegistrationBean = this.registry .getBeanDefinition(DefaultConfigurationServlet.class.getName()); MutablePropertyValues propertyValues = servletRegistrationBean .getPropertyValues(); assertThat(propertyValues.get("asyncSupported")).isEqualTo(false); assertThat(((Map<String, String>) propertyValues.get("initParameters"))) .isEmpty(); assertThat((Integer) propertyValues.get("loadOnStartup")).isEqualTo(-1); assertThat(propertyValues.get("name")) .isEqualTo(DefaultConfigurationServlet.class.getName()); assertThat((String[]) propertyValues.get("urlMappings")).isEmpty(); assertThat(propertyValues.get("servlet")).isEqualTo(scanned); }
@SuppressWarnings("unchecked") @Test public void defaultFilterConfiguration() throws IOException { ScannedGenericBeanDefinition scanned = new ScannedGenericBeanDefinition( new SimpleMetadataReaderFactory() .getMetadataReader(DefaultConfigurationFilter.class.getName())); this.handler.handle(scanned, this.registry); BeanDefinition filterRegistrationBean = this.registry .getBeanDefinition(DefaultConfigurationFilter.class.getName()); MutablePropertyValues propertyValues = filterRegistrationBean.getPropertyValues(); assertThat(propertyValues.get("asyncSupported")).isEqualTo(false); assertThat((EnumSet<DispatcherType>) propertyValues.get("dispatcherTypes")) .containsExactly(DispatcherType.REQUEST); assertThat(((Map<String, String>) propertyValues.get("initParameters"))) .isEmpty(); assertThat((String[]) propertyValues.get("servletNames")).isEmpty(); assertThat((String[]) propertyValues.get("urlPatterns")).isEmpty(); assertThat(propertyValues.get("name")) .isEqualTo(DefaultConfigurationFilter.class.getName()); assertThat(propertyValues.get("filter")).isEqualTo(scanned); }
private Set<AnnotationMetadata> findConfigurationClasses() throws IOException { Set<AnnotationMetadata> configurationClasses = new HashSet<AnnotationMetadata>(); Resource[] resources = this.resolver.getResources("classpath*:" + getClass().getPackage().getName().replace(".", "/") + "/**/*.class"); for (Resource resource : resources) { if (!isTestClass(resource)) { MetadataReader metadataReader = new SimpleMetadataReaderFactory() .getMetadataReader(resource); AnnotationMetadata annotationMetadata = metadataReader .getAnnotationMetadata(); if (annotationMetadata.getAnnotationTypes() .contains(Configuration.class.getName())) { configurationClasses.add(annotationMetadata); } } } return configurationClasses; }
@SuppressWarnings("unchecked") @Test public void defaultServletConfiguration() throws IOException { ScannedGenericBeanDefinition scanned = new ScannedGenericBeanDefinition( new SimpleMetadataReaderFactory() .getMetadataReader(DefaultConfigurationServlet.class.getName())); this.handler.handle(scanned, this.registry); BeanDefinition servletRegistrationBean = this.registry .getBeanDefinition(DefaultConfigurationServlet.class.getName()); MutablePropertyValues propertyValues = servletRegistrationBean .getPropertyValues(); assertThat(propertyValues.get("asyncSupported"), is((Object) false)); assertThat(((Map<String, String>) propertyValues.get("initParameters")).size(), is(0)); assertThat((Integer) propertyValues.get("loadOnStartup"), is(-1)); assertThat(propertyValues.get("name"), is((Object) DefaultConfigurationServlet.class.getName())); assertThat((String[]) propertyValues.get("urlMappings"), is(arrayWithSize(0))); assertThat(propertyValues.get("servlet"), is(equalTo((Object) scanned))); }
@SuppressWarnings("unchecked") @Test public void defaultFilterConfiguration() throws IOException { ScannedGenericBeanDefinition scanned = new ScannedGenericBeanDefinition( new SimpleMetadataReaderFactory() .getMetadataReader(DefaultConfigurationFilter.class.getName())); this.handler.handle(scanned, this.registry); BeanDefinition filterRegistrationBean = this.registry .getBeanDefinition(DefaultConfigurationFilter.class.getName()); MutablePropertyValues propertyValues = filterRegistrationBean.getPropertyValues(); assertThat(propertyValues.get("asyncSupported"), is((Object) false)); assertThat((EnumSet<DispatcherType>) propertyValues.get("dispatcherTypes"), is(EnumSet.of(DispatcherType.REQUEST))); assertThat(((Map<String, String>) propertyValues.get("initParameters")).size(), is(0)); assertThat((String[]) propertyValues.get("servletNames"), is(arrayWithSize(0))); assertThat((String[]) propertyValues.get("urlPatterns"), is(arrayWithSize(0))); assertThat(propertyValues.get("name"), is((Object) DefaultConfigurationFilter.class.getName())); assertThat(propertyValues.get("filter"), is(equalTo((Object) scanned))); }
public void testClassesInFileSystem() throws IOException{ PathMatchingResourcePatternResolver pr = new PathMatchingResourcePatternResolver() ; MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(pr); Resource[] rs = pr.getResources("classpath*:nu/xom/*.class") ; System.out.println(rs.length) ; for(Resource r : rs){ if(r.isReadable()){ try{ MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(r); String className = metadataReader.getClassMetadata().getClassName() ; System.out.println(className) ; }catch(Exception e){ //Not a class System.out.println("file:" + r.getURL().toString()) ; } }else{ System.out.println("no read:" + r.getFile().getAbsolutePath()) ; } } }
@Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { SimpleMetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(applicationContext); DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) beanFactory; String resolvedPath = resolvePackageToScan(); logger.debug("Scanning '{}' for JSON-RPC service interfaces.", resolvedPath); try { for (Resource resource : applicationContext.getResources(resolvedPath)) { if (resource.isReadable()) { MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource); ClassMetadata classMetadata = metadataReader.getClassMetadata(); AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata(); String jsonRpcPathAnnotation = JsonRpcService.class.getName(); if (annotationMetadata.isAnnotated(jsonRpcPathAnnotation)) { String className = classMetadata.getClassName(); String path = (String) annotationMetadata.getAnnotationAttributes(jsonRpcPathAnnotation).get("value"); logger.debug("Found JSON-RPC service to proxy [{}] on path '{}'.", className, path); registerJsonProxyBean(defaultListableBeanFactory, className, path); } } } } catch (IOException e) { throw new IllegalStateException(format("Cannot scan package '%s' for classes.", resolvedPath), e); } }
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { SimpleMetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(applicationContext); DefaultListableBeanFactory dlbf = (DefaultListableBeanFactory) beanFactory; String resolvedPath = resolvePackageToScan(); LOG.debug(format("Scanning '%s' for JSON-RPC service interfaces.", resolvedPath)); try { for (Resource resource : applicationContext.getResources(resolvedPath)) { if (resource.isReadable()) { MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource); ClassMetadata classMetadata = metadataReader.getClassMetadata(); AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata(); String jsonRpcPathAnnotation = JsonRpcService.class.getName(); if (annotationMetadata.isAnnotated(jsonRpcPathAnnotation)) { String className = classMetadata.getClassName(); String path = (String) annotationMetadata.getAnnotationAttributes(jsonRpcPathAnnotation).get("value"); boolean useNamedParams = (Boolean) annotationMetadata.getAnnotationAttributes(jsonRpcPathAnnotation).get("useNamedParams"); LOG.debug(format("Found JSON-RPC service to proxy [%s] on path '%s'.", className, path)); registerJsonProxyBean(dlbf, className, path, useNamedParams); } } } } catch (IOException e) { throw new RuntimeException(format("Cannot scan package '%s' for classes.", resolvedPath), e); } }
MemberConditions(ConditionContext context, String className) { this.context = context; this.readerFactory = new SimpleMetadataReaderFactory( context.getResourceLoader()); String[] members = getMetadata(className).getMemberClassNames(); this.memberConditions = getMemberConditions(members); }
@Test public void customRequestScopeViaAsm() throws IOException { MetadataReaderFactory readerFactory = new SimpleMetadataReaderFactory(); MetadataReader reader = readerFactory.getMetadataReader(AnnotatedWithCustomRequestScope.class.getName()); AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(reader.getAnnotationMetadata()); ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(bd); assertNotNull("resolveScopeMetadata(..) must *never* return null.", scopeMetadata); assertEquals("request", scopeMetadata.getScopeName()); assertEquals(NO, scopeMetadata.getScopedProxyMode()); }
@Test public void customRequestScopeWithAttributeViaAsm() throws IOException { MetadataReaderFactory readerFactory = new SimpleMetadataReaderFactory(); MetadataReader reader = readerFactory.getMetadataReader(AnnotatedWithCustomRequestScopeWithAttributeOverride.class.getName()); AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(reader.getAnnotationMetadata()); ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(bd); assertNotNull("resolveScopeMetadata(..) must *never* return null.", scopeMetadata); assertEquals("request", scopeMetadata.getScopeName()); assertEquals(TARGET_CLASS, scopeMetadata.getScopedProxyMode()); }
@Test public void testDirectAnnotationMatch() throws Exception { MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(); String classUnderTest = "org.springframework.core.type.AnnotationTypeFilterTests$SomeComponent"; MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest); AnnotationTypeFilter filter = new AnnotationTypeFilter(InheritedAnnotation.class); assertTrue(filter.match(metadataReader, metadataReaderFactory)); ClassloadingAssertions.assertClassNotLoaded(classUnderTest); }
@Test public void testInheritedAnnotationFromInterfaceDoesNotMatch() throws Exception { MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(); String classUnderTest = "org.springframework.core.type.AnnotationTypeFilterTests$SomeSubClassOfSomeComponentInterface"; MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest); AnnotationTypeFilter filter = new AnnotationTypeFilter(InheritedAnnotation.class); // Must fail as annotation on interfaces should not be considered a match assertFalse(filter.match(metadataReader, metadataReaderFactory)); ClassloadingAssertions.assertClassNotLoaded(classUnderTest); }
@Test public void testInheritedAnnotationFromBaseClassDoesMatch() throws Exception { MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(); String classUnderTest = "org.springframework.core.type.AnnotationTypeFilterTests$SomeSubClassOfSomeComponent"; MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest); AnnotationTypeFilter filter = new AnnotationTypeFilter(InheritedAnnotation.class); assertTrue(filter.match(metadataReader, metadataReaderFactory)); ClassloadingAssertions.assertClassNotLoaded(classUnderTest); }
@Test public void testNonInheritedAnnotationDoesNotMatch() throws Exception { MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(); String classUnderTest = "org.springframework.core.type.AnnotationTypeFilterTests$SomeSubclassOfSomeClassMarkedWithNonInheritedAnnotation"; MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest); AnnotationTypeFilter filter = new AnnotationTypeFilter(NonInheritedAnnotation.class); // Must fail as annotation isn't inherited assertFalse(filter.match(metadataReader, metadataReaderFactory)); ClassloadingAssertions.assertClassNotLoaded(classUnderTest); }
@Test public void testNonAnnotatedClassDoesntMatch() throws Exception { MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(); String classUnderTest = "org.springframework.core.type.AnnotationTypeFilterTests$SomeNonCandidateClass"; MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest); AnnotationTypeFilter filter = new AnnotationTypeFilter(Component.class); assertFalse(filter.match(metadataReader, metadataReaderFactory)); ClassloadingAssertions.assertClassNotLoaded(classUnderTest); }
@Test public void testMatchesInterfacesIfConfigured() throws Exception { MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(); String classUnderTest = "org.springframework.core.type.AnnotationTypeFilterTests$SomeComponentInterface"; MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest); AnnotationTypeFilter filter = new AnnotationTypeFilter(InheritedAnnotation.class, false, true); assertTrue(filter.match(metadataReader, metadataReaderFactory)); ClassloadingAssertions.assertClassNotLoaded(classUnderTest); }
@Test public void asmAnnotationMetadata() throws Exception { MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(); MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(AnnotatedComponent.class.getName()); AnnotationMetadata metadata = metadataReader.getAnnotationMetadata(); doTestAnnotationInfo(metadata); doTestMethodAnnotationInfo(metadata); }
@Test public void asmAnnotationMetadataForSubclass() throws Exception { MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(); MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(AnnotatedComponentSubClass.class.getName()); AnnotationMetadata metadata = metadataReader.getAnnotationMetadata(); doTestSubClassAnnotationInfo(metadata); }
@Test public void asmAnnotationMetadataForInterface() throws Exception { MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(); MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(AnnotationMetadata.class.getName()); AnnotationMetadata metadata = metadataReader.getAnnotationMetadata(); doTestMetadataForInterfaceClass(metadata); }
@Test public void asmAnnotationMetadataForAnnotation() throws Exception { MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(); MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(Component.class.getName()); AnnotationMetadata metadata = metadataReader.getAnnotationMetadata(); doTestMetadataForAnnotationClass(metadata); }
@Test public void metaAnnotationOverridesUsingAnnotationMetadataReadingVisitor() throws Exception { MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(); MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(ComposedConfigurationWithAttributeOverridesClass.class.getName()); AnnotationMetadata metadata = metadataReader.getAnnotationMetadata(); assertMetaAnnotationOverrides(metadata); }
/** * https://jira.spring.io/browse/SPR-11649 */ @Test public void multipleAnnotationsWithIdenticalAttributeNamesUsingAnnotationMetadataReadingVisitor() throws Exception { MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(); MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(NamedAnnotationsClass.class.getName()); AnnotationMetadata metadata = metadataReader.getAnnotationMetadata(); assertMultipleAnnotationsWithIdenticalAttributeNames(metadata); }
/** * https://jira.spring.io/browse/SPR-11649 */ @Test public void composedAnnotationWithMetaAnnotationsWithIdenticalAttributeNamesUsingAnnotationMetadataReadingVisitor() throws Exception { MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(); MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(NamedComposedAnnotationClass.class.getName()); AnnotationMetadata metadata = metadataReader.getAnnotationMetadata(); assertMultipleAnnotationsWithIdenticalAttributeNames(metadata); }
@Test public void directMatch() throws Exception { MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(); String classUnderTest = "org.springframework.core.type.AssignableTypeFilterTests$TestNonInheritingClass"; MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest); AssignableTypeFilter matchingFilter = new AssignableTypeFilter(TestNonInheritingClass.class); AssignableTypeFilter notMatchingFilter = new AssignableTypeFilter(TestInterface.class); assertFalse(notMatchingFilter.match(metadataReader, metadataReaderFactory)); assertTrue(matchingFilter.match(metadataReader, metadataReaderFactory)); }
@Test public void interfaceMatch() throws Exception { MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(); String classUnderTest = "org.springframework.core.type.AssignableTypeFilterTests$TestInterfaceImpl"; MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest); AssignableTypeFilter filter = new AssignableTypeFilter(TestInterface.class); assertTrue(filter.match(metadataReader, metadataReaderFactory)); ClassloadingAssertions.assertClassNotLoaded(classUnderTest); }
@Test public void superClassMatch() throws Exception { MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(); String classUnderTest = "org.springframework.core.type.AssignableTypeFilterTests$SomeDaoLikeImpl"; MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest); AssignableTypeFilter filter = new AssignableTypeFilter(SimpleJdbcDaoSupport.class); assertTrue(filter.match(metadataReader, metadataReaderFactory)); ClassloadingAssertions.assertClassNotLoaded(classUnderTest); }
@Test public void interfaceThroughSuperClassMatch() throws Exception { MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(); String classUnderTest = "org.springframework.core.type.AssignableTypeFilterTests$SomeDaoLikeImpl"; MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest); AssignableTypeFilter filter = new AssignableTypeFilter(JdbcDaoSupport.class); assertTrue(filter.match(metadataReader, metadataReaderFactory)); ClassloadingAssertions.assertClassNotLoaded(classUnderTest); }
private void assertMatch(String type, String typePattern) throws Exception { MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(); MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(type); AspectJTypeFilter filter = new AspectJTypeFilter(typePattern, getClass().getClassLoader()); assertTrue(filter.match(metadataReader, metadataReaderFactory)); ClassloadingAssertions.assertClassNotLoaded(type); }
private void assertNoMatch(String type, String typePattern) throws Exception { MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(); MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(type); AspectJTypeFilter filter = new AspectJTypeFilter(typePattern, getClass().getClassLoader()); assertFalse(filter.match(metadataReader, metadataReaderFactory)); ClassloadingAssertions.assertClassNotLoaded(type); }
private boolean match(FilterAnnotations filterAnnotations, Class<?> type) throws IOException { MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(); MetadataReader metadataReader = metadataReaderFactory .getMetadataReader(type.getName()); return filterAnnotations.anyMatches(metadataReader, metadataReaderFactory); }
@Test public void listener() throws IOException { ScannedGenericBeanDefinition scanned = new ScannedGenericBeanDefinition( new SimpleMetadataReaderFactory() .getMetadataReader(TestListener.class.getName())); this.handler.handle(scanned, this.registry); this.registry.getBeanDefinition(TestListener.class.getName()); }
@Test public void servletWithCustomName() throws IOException { ScannedGenericBeanDefinition scanned = new ScannedGenericBeanDefinition( new SimpleMetadataReaderFactory() .getMetadataReader(CustomNameServlet.class.getName())); this.handler.handle(scanned, this.registry); BeanDefinition servletRegistrationBean = this.registry .getBeanDefinition("custom"); MutablePropertyValues propertyValues = servletRegistrationBean .getPropertyValues(); assertThat(propertyValues.get("name")).isEqualTo("custom"); }