Search in sources :

Example 61 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project iaf by ibissource.

the class MapPropertyDescriptorsTest method testPropertyDescriptorsBeingRegistered.

@Test
public void testPropertyDescriptorsBeingRegistered() throws ClassNotFoundException, IntrospectionException {
    BeanDefinitionRegistry beanDefinitionRegistry = new SimpleBeanDefinitionRegistry();
    ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(beanDefinitionRegistry);
    scanner.setIncludeAnnotationConfig(false);
    scanner.addIncludeFilter(new AssignableTypeFilter(IConfigurable.class));
    BeanNameGenerator beanNameGenerator = new AnnotationBeanNameGenerator() {

        @Override
        protected String buildDefaultBeanName(BeanDefinition definition) {
            String beanClassName = definition.getBeanClassName();
            Assert.state(beanClassName != null, "No bean class name set");
            return beanClassName;
        }
    };
    scanner.setBeanNameGenerator(beanNameGenerator);
    int numberOfBeans = scanner.scan("nl.nn.adapterframework", "nl.nn.ibistesttool");
    log.debug("Found " + numberOfBeans + " beans registered!");
    String[] names = scanner.getRegistry().getBeanDefinitionNames();
    for (String beanName : names) {
        BeanInfo beanInfo = Introspector.getBeanInfo(Class.forName(beanName));
        // get methods
        MethodDescriptor[] methodDescriptors = beanInfo.getMethodDescriptors();
        for (MethodDescriptor methodDescriptor : methodDescriptors) {
            String methodName = methodDescriptor.getName();
            if (methodName.startsWith("set")) {
                String propertyName = methodName.substring(3);
                String getterName = "get" + propertyName;
                String getterStartsWithIs = "is" + propertyName;
                propertyName = propertyName.substring(0, 1).toLowerCase() + propertyName.substring(1);
                boolean getterMatches = Arrays.stream(methodDescriptors).anyMatch(name -> name.getName().equals(getterName) || name.getName().equals(getterStartsWithIs));
                if (getterMatches) {
                    PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
                    PropertyDescriptor pd = Arrays.stream(pds).filter(name -> name.getWriteMethod() != null && methodName.equals(name.getWriteMethod().getName())).findAny().orElse(null);
                    assertNotNull("Make sure that the attribute [" + propertyName + "] has proper getter and setters in class [" + beanName + "].", pd);
                }
            }
        }
    }
}
Also used : SimpleBeanDefinitionRegistry(org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry) PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) SimpleBeanDefinitionRegistry(org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) IConfigurable(nl.nn.adapterframework.core.IConfigurable) MethodDescriptor(java.beans.MethodDescriptor) ClassPathBeanDefinitionScanner(org.springframework.context.annotation.ClassPathBeanDefinitionScanner) AnnotationBeanNameGenerator(org.springframework.context.annotation.AnnotationBeanNameGenerator) BeanNameGenerator(org.springframework.beans.factory.support.BeanNameGenerator) AnnotationBeanNameGenerator(org.springframework.context.annotation.AnnotationBeanNameGenerator) AssignableTypeFilter(org.springframework.core.type.filter.AssignableTypeFilter) Test(org.junit.Test)

Example 62 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project iaf by ibissource.

the class TestAnnotationUtils method findInterfacesWithAnnotations.

@Test
public void findInterfacesWithAnnotations() throws Exception {
    // assumeTrue(TestAssertions.isTestRunningOnCI());
    BeanDefinitionRegistry beanDefinitionRegistry = new SimpleBeanDefinitionRegistry();
    ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(beanDefinitionRegistry);
    scanner.setIncludeAnnotationConfig(false);
    scanner.addIncludeFilter(new TypeFilter() {

        // Find everything that has an interface
        @Override
        public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
            ClassMetadata metadata = metadataReader.getClassMetadata();
            return metadata.getInterfaceNames().length > 0;
        }
    });
    BeanNameGenerator beanNameGenerator = new AnnotationBeanNameGenerator() {

        @Override
        protected String buildDefaultBeanName(BeanDefinition definition) {
            String beanClassName = definition.getBeanClassName();
            Assert.state(beanClassName != null, "No bean class name set");
            return beanClassName;
        }
    };
    scanner.setBeanNameGenerator(beanNameGenerator);
    String frankFrameworkPackage = "nl.nn.adapterframework";
    int numberOfBeans = scanner.scan(frankFrameworkPackage);
    assertTrue(numberOfBeans > 0);
    Set<Class<?>> interfazes = new HashSet<>();
    String[] names = scanner.getRegistry().getBeanDefinitionNames();
    for (String beanName : names) {
        // Ignore this class
        if (beanName.contains(this.getClass().getCanonicalName()))
            continue;
        List<Class<?>> interfaces = ClassUtils.getAllInterfaces(Class.forName(beanName));
        interfazes.addAll(interfaces);
    }
    for (Class<?> interfaze : interfazes) {
        if (interfaze.getCanonicalName().startsWith(frankFrameworkPackage)) {
            for (Method method : interfaze.getDeclaredMethods()) {
                for (Annotation annotation : method.getAnnotations()) {
                    if (AnnotationFilter.PLAIN.matches(annotation) || AnnotationUtils.isInJavaLangAnnotationPackage(annotation)) {
                        fail("Found java annotation [" + annotation + "] on interface [" + interfaze.getTypeName() + "], is not seen by digester because it uses Spring AnnotationUtils");
                    }
                }
            }
        }
    }
}
Also used : SimpleBeanDefinitionRegistry(org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry) ClassMetadata(org.springframework.core.type.ClassMetadata) MetadataReaderFactory(org.springframework.core.type.classreading.MetadataReaderFactory) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) SimpleBeanDefinitionRegistry(org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry) MetadataReader(org.springframework.core.type.classreading.MetadataReader) TypeFilter(org.springframework.core.type.filter.TypeFilter) IOException(java.io.IOException) Method(java.lang.reflect.Method) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Annotation(java.lang.annotation.Annotation) ClassPathBeanDefinitionScanner(org.springframework.context.annotation.ClassPathBeanDefinitionScanner) AnnotationBeanNameGenerator(org.springframework.context.annotation.AnnotationBeanNameGenerator) BeanNameGenerator(org.springframework.beans.factory.support.BeanNameGenerator) AnnotationBeanNameGenerator(org.springframework.context.annotation.AnnotationBeanNameGenerator) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 63 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project records-management by Alfresco.

the class DictionaryBootstrapPostProcessor method postProcessBeanFactory.

/**
 * @see org.springframework.beans.factory.config.BeanFactoryPostProcessor#postProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)
 */
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
    // if the site service bootstrap bean and the RM dictionary bean are present in the bean factory
    if (beanFactory.containsBean(BEAN_SITESERVICE_BOOTSTRAP) && beanFactory.containsBean(BEAN_RM_DICTIONARY_BOOTSTRAP)) {
        // get the RM dictionary bootstrap bean definition
        BeanDefinition beanDef = beanFactory.getBeanDefinition(BEAN_RM_DICTIONARY_BOOTSTRAP);
        // set the dependency
        beanDef.setDependsOn(new String[] { BEAN_SITESERVICE_BOOTSTRAP });
    }
}
Also used : BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 64 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project webanno by webanno.

the class ProjectSettingsPanelRegistryServiceImpl method scan.

private void scan() {
    panels = new ArrayList<>();
    // Scan project settings using annotation
    ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
    scanner.addIncludeFilter(new AnnotationTypeFilter(ProjectSettingsPanel.class));
    for (BeanDefinition bd : scanner.findCandidateComponents("de.tudarmstadt.ukp")) {
        try {
            @SuppressWarnings("unchecked") Class<? extends Panel> panelClass = (Class<? extends Panel>) Class.forName(bd.getBeanClassName());
            ProjectSettingsPanel mia = panelClass.getAnnotation(ProjectSettingsPanel.class);
            ProjectSettingsPanelDecl panel = new ProjectSettingsPanelDecl();
            panel.label = mia.label();
            panel.prio = mia.prio();
            panel.panel = panelClass;
            panels.add(panel);
            log.debug("Found settings panel: {} ({})", panel.label, panel.prio);
            List<Method> methods = MethodUtils.getMethodsListWithAnnotation(panelClass, ProjectSettingsPanelCondition.class);
            if (!methods.isEmpty()) {
                panel.condition = (aProject) -> {
                    try {
                        // Need to look the method up again here because methods are not
                        // serializable
                        Method m = MethodUtils.getMethodsListWithAnnotation(panelClass, ProjectSettingsPanelCondition.class).get(0);
                        return (boolean) m.invoke(null, aProject);
                    } catch (Exception e) {
                        LoggerFactory.getLogger(ProjectSettingsPanelRegistryServiceImpl.class).error("Unable to invoke settings panel condition method", e);
                        return false;
                    }
                };
            } else {
                panel.condition = (aProject) -> aProject != null;
            }
        } catch (ClassNotFoundException e) {
            log.error("Settings panel class [{}] not found", bd.getBeanClassName(), e);
        }
    }
    panels.sort(Comparator.comparingInt(a -> a.prio));
}
Also used : Logger(org.slf4j.Logger) ProjectService(de.tudarmstadt.ukp.clarin.webanno.api.ProjectService) ClassPathScanningCandidateComponentProvider(org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider) LoggerFactory(org.slf4j.LoggerFactory) UserDao(de.tudarmstadt.ukp.clarin.webanno.security.UserDao) MethodUtils(org.apache.commons.lang3.reflect.MethodUtils) Autowired(org.springframework.beans.factory.annotation.Autowired) ArrayList(java.util.ArrayList) SmartLifecycle(org.springframework.context.SmartLifecycle) Panel(org.apache.wicket.markup.html.panel.Panel) List(java.util.List) Component(org.springframework.stereotype.Component) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Comparator(java.util.Comparator) Method(java.lang.reflect.Method) Collections(java.util.Collections) AnnotationTypeFilter(org.springframework.core.type.filter.AnnotationTypeFilter) AnnotationTypeFilter(org.springframework.core.type.filter.AnnotationTypeFilter) Method(java.lang.reflect.Method) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Panel(org.apache.wicket.markup.html.panel.Panel) ClassPathScanningCandidateComponentProvider(org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider)

Example 65 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project wildfly-camel by wildfly-extras.

the class SpringCamelContextBootstrap method getJndiNames.

/**
 * Gets JNDI names for all configured instances of {@link JndiObjectFactoryBean}
 *
 * Note: If this method is invoked before ApplicationContext.refresh() then any bean property
 * values containing property placeholders will not be resolved.
 *
 * @return the unmodifiable list of JNDI binding names
 */
public List<String> getJndiNames() {
    List<String> bindings = new ArrayList<>();
    for (String beanName : applicationContext.getBeanDefinitionNames()) {
        BeanDefinition definition = applicationContext.getBeanDefinition(beanName);
        String beanClassName = definition.getBeanClassName();
        if (beanClassName != null && beanClassName.equals(JndiObjectFactoryBean.class.getName())) {
            MutablePropertyValues propertyValues = definition.getPropertyValues();
            Object jndiPropertyValue = propertyValues.get("jndiName");
            if (jndiPropertyValue == null) {
                LOGGER.debug("Skipping JNDI binding dependency for bean: {}", beanName);
                continue;
            }
            String jndiName = null;
            if (jndiPropertyValue instanceof String) {
                jndiName = (String) jndiPropertyValue;
            } else if (jndiPropertyValue instanceof TypedStringValue) {
                jndiName = ((TypedStringValue) jndiPropertyValue).getValue();
            } else {
                LOGGER.debug("Ignoring unknown JndiObjectFactoryBean property value type {}", jndiPropertyValue.getClass().getSimpleName());
            }
            if (jndiName != null) {
                bindings.add(jndiName);
            }
        }
    }
    return Collections.unmodifiableList(bindings);
}
Also used : MutablePropertyValues(org.springframework.beans.MutablePropertyValues) ArrayList(java.util.ArrayList) TypedStringValue(org.springframework.beans.factory.config.TypedStringValue) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Aggregations

BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)601 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)188 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)116 Test (org.junit.jupiter.api.Test)112 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)89 Element (org.w3c.dom.Element)72 Test (org.junit.Test)66 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)60 BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)53 ClassPathScanningCandidateComponentProvider (org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider)41 ManagedList (org.springframework.beans.factory.support.ManagedList)39 AnnotatedBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedBeanDefinition)36 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)35 BeanMetadataElement (org.springframework.beans.BeanMetadataElement)34 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)34 AnnotationTypeFilter (org.springframework.core.type.filter.AnnotationTypeFilter)31 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)30 Method (java.lang.reflect.Method)28 ArrayList (java.util.ArrayList)28 Map (java.util.Map)27