Search in sources :

Example 66 with MethodMetadata

use of org.springframework.core.type.MethodMetadata in project spring-framework-debug by Joker-5.

the class SimpleAnnotationMetadataReadingVisitor method visitEnd.

@Override
public void visitEnd() {
    String[] memberClassNames = StringUtils.toStringArray(this.memberClassNames);
    MethodMetadata[] annotatedMethods = this.annotatedMethods.toArray(new MethodMetadata[0]);
    MergedAnnotations annotations = MergedAnnotations.of(this.annotations);
    this.metadata = new SimpleAnnotationMetadata(this.className, this.access, this.enclosingClassName, this.superClassName, this.independentInnerClass, this.interfaceNames, memberClassNames, annotatedMethods, annotations);
}
Also used : MethodMetadata(org.springframework.core.type.MethodMetadata) MergedAnnotations(org.springframework.core.annotation.MergedAnnotations)

Example 67 with MethodMetadata

use of org.springframework.core.type.MethodMetadata in project stream-applications by spring-cloud.

the class ComponentCustomizerBeanPostProcessor method postProcessMergedBeanDefinition.

@Override
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {
    if (beanDefinition instanceof AnnotatedBeanDefinition) {
        AnnotatedBeanDefinition annotatedBeanDefinition = (AnnotatedBeanDefinition) beanDefinition;
        MethodMetadata factoryMethodMetadata = annotatedBeanDefinition.getFactoryMethodMetadata();
        if (factoryMethodMetadata != null && factoryMethodMetadata.isAnnotated(CustomizationAware.class.getName())) {
            this.customizationAwareBeanNames.add(beanName);
        }
    }
}
Also used : AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) MethodMetadata(org.springframework.core.type.MethodMetadata)

Example 68 with MethodMetadata

use of org.springframework.core.type.MethodMetadata in project spring-framework by spring-projects.

the class ConfigurationClass method validate.

void validate(ProblemReporter problemReporter) {
    Map<String, Object> attributes = this.metadata.getAnnotationAttributes(Configuration.class.getName());
    // A configuration class may not be final (CGLIB limitation) unless it declares proxyBeanMethods=false
    if (attributes != null && (Boolean) attributes.get("proxyBeanMethods")) {
        if (this.metadata.isFinal()) {
            problemReporter.error(new FinalConfigurationProblem());
        }
        for (BeanMethod beanMethod : this.beanMethods) {
            beanMethod.validate(problemReporter);
        }
    }
    // A configuration class may not contain overloaded bean methods unless it declares enforceUniqueMethods=false
    if (attributes != null && (Boolean) attributes.get("enforceUniqueMethods")) {
        Map<String, MethodMetadata> beanMethodsByName = new LinkedHashMap<>();
        for (BeanMethod beanMethod : this.beanMethods) {
            MethodMetadata current = beanMethod.getMetadata();
            MethodMetadata existing = beanMethodsByName.put(current.getMethodName(), current);
            if (existing != null && existing.getDeclaringClassName().equals(current.getDeclaringClassName())) {
                problemReporter.error(new BeanMethodOverloadingProblem(existing.getMethodName()));
            }
        }
    }
}
Also used : MethodMetadata(org.springframework.core.type.MethodMetadata) LinkedHashMap(java.util.LinkedHashMap)

Example 69 with MethodMetadata

use of org.springframework.core.type.MethodMetadata in project spring-framework by bluecrow1986.

the class ConfigurationClassBeanDefinitionReader method loadBeanDefinitionsForBeanMethod.

/**
 * Read the given {@link BeanMethod}, registering bean definitions
 * with the BeanDefinitionRegistry based on its contents.
 */
// for RequiredAnnotationBeanPostProcessor.SKIP_REQUIRED_CHECK_ATTRIBUTE
@SuppressWarnings("deprecation")
private void loadBeanDefinitionsForBeanMethod(BeanMethod beanMethod) {
    ConfigurationClass configClass = beanMethod.getConfigurationClass();
    MethodMetadata metadata = beanMethod.getMetadata();
    String methodName = metadata.getMethodName();
    // Do we need to mark the bean as skipped by its condition?
    if (this.conditionEvaluator.shouldSkip(metadata, ConfigurationPhase.REGISTER_BEAN)) {
        configClass.skippedBeanMethods.add(methodName);
        return;
    }
    if (configClass.skippedBeanMethods.contains(methodName)) {
        return;
    }
    AnnotationAttributes bean = AnnotationConfigUtils.attributesFor(metadata, Bean.class);
    Assert.state(bean != null, "No @Bean annotation attributes");
    // Consider name and any aliases
    List<String> names = new ArrayList<>(Arrays.asList(bean.getStringArray("name")));
    String beanName = (!names.isEmpty() ? names.remove(0) : methodName);
    // Register aliases even when overridden
    for (String alias : names) {
        this.registry.registerAlias(beanName, alias);
    }
    // Has this effectively been overridden before (e.g. via XML)?
    if (isOverriddenByExistingDefinition(beanMethod, beanName)) {
        if (beanName.equals(beanMethod.getConfigurationClass().getBeanName())) {
            throw new BeanDefinitionStoreException(beanMethod.getConfigurationClass().getResource().getDescription(), beanName, "Bean name derived from @Bean method '" + beanMethod.getMetadata().getMethodName() + "' clashes with bean name for containing configuration class; please make those names unique!");
        }
        return;
    }
    ConfigurationClassBeanDefinition beanDef = new ConfigurationClassBeanDefinition(configClass, metadata, beanName);
    beanDef.setSource(this.sourceExtractor.extractSource(metadata, configClass.getResource()));
    if (metadata.isStatic()) {
        // static @Bean method
        if (configClass.getMetadata() instanceof StandardAnnotationMetadata) {
            beanDef.setBeanClass(((StandardAnnotationMetadata) configClass.getMetadata()).getIntrospectedClass());
        } else {
            beanDef.setBeanClassName(configClass.getMetadata().getClassName());
        }
        beanDef.setUniqueFactoryMethodName(methodName);
    } else {
        // instance @Bean method
        beanDef.setFactoryBeanName(configClass.getBeanName());
        beanDef.setUniqueFactoryMethodName(methodName);
    }
    if (metadata instanceof StandardMethodMetadata) {
        beanDef.setResolvedFactoryMethod(((StandardMethodMetadata) metadata).getIntrospectedMethod());
    }
    beanDef.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_CONSTRUCTOR);
    beanDef.setAttribute(org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor.SKIP_REQUIRED_CHECK_ATTRIBUTE, Boolean.TRUE);
    AnnotationConfigUtils.processCommonDefinitionAnnotations(beanDef, metadata);
    Autowire autowire = bean.getEnum("autowire");
    if (autowire.isAutowire()) {
        beanDef.setAutowireMode(autowire.value());
    }
    boolean autowireCandidate = bean.getBoolean("autowireCandidate");
    if (!autowireCandidate) {
        beanDef.setAutowireCandidate(false);
    }
    String initMethodName = bean.getString("initMethod");
    if (StringUtils.hasText(initMethodName)) {
        beanDef.setInitMethodName(initMethodName);
    }
    String destroyMethodName = bean.getString("destroyMethod");
    beanDef.setDestroyMethodName(destroyMethodName);
    // Consider scoping
    ScopedProxyMode proxyMode = ScopedProxyMode.NO;
    AnnotationAttributes attributes = AnnotationConfigUtils.attributesFor(metadata, Scope.class);
    if (attributes != null) {
        beanDef.setScope(attributes.getString("value"));
        proxyMode = attributes.getEnum("proxyMode");
        if (proxyMode == ScopedProxyMode.DEFAULT) {
            proxyMode = ScopedProxyMode.NO;
        }
    }
    // Replace the original bean definition with the target one, if necessary
    BeanDefinition beanDefToRegister = beanDef;
    if (proxyMode != ScopedProxyMode.NO) {
        BeanDefinitionHolder proxyDef = ScopedProxyCreator.createScopedProxy(new BeanDefinitionHolder(beanDef, beanName), this.registry, proxyMode == ScopedProxyMode.TARGET_CLASS);
        beanDefToRegister = new ConfigurationClassBeanDefinition((RootBeanDefinition) proxyDef.getBeanDefinition(), configClass, metadata, beanName);
    }
    if (logger.isTraceEnabled()) {
        logger.trace(String.format("Registering bean definition for @Bean method %s.%s()", configClass.getMetadata().getClassName(), beanName));
    }
    this.registry.registerBeanDefinition(beanName, beanDefToRegister);
}
Also used : AnnotationAttributes(org.springframework.core.annotation.AnnotationAttributes) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) ArrayList(java.util.ArrayList) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) AnnotatedGenericBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition) StandardMethodMetadata(org.springframework.core.type.StandardMethodMetadata) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) StandardAnnotationMetadata(org.springframework.core.type.StandardAnnotationMetadata) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) MethodMetadata(org.springframework.core.type.MethodMetadata) StandardMethodMetadata(org.springframework.core.type.StandardMethodMetadata) Autowire(org.springframework.beans.factory.annotation.Autowire)

Example 70 with MethodMetadata

use of org.springframework.core.type.MethodMetadata in project spring-framework by bluecrow1986.

the class ConfigurationClassParser method processInterfaces.

/**
 * Register default methods on interfaces implemented by the configuration class.
 */
private void processInterfaces(ConfigurationClass configClass, SourceClass sourceClass) throws IOException {
    for (SourceClass ifc : sourceClass.getInterfaces()) {
        Set<MethodMetadata> beanMethods = retrieveBeanMethodMetadata(ifc);
        for (MethodMetadata methodMetadata : beanMethods) {
            if (!methodMetadata.isAbstract()) {
                // A default method or other concrete method on a Java 8+ interface...
                configClass.addBeanMethod(new BeanMethod(methodMetadata, configClass));
            }
        }
        processInterfaces(configClass, ifc);
    }
}
Also used : MethodMetadata(org.springframework.core.type.MethodMetadata)

Aggregations

MethodMetadata (org.springframework.core.type.MethodMetadata)73 AnnotatedBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedBeanDefinition)33 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)32 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)20 AnnotationMetadata (org.springframework.core.type.AnnotationMetadata)15 StandardMethodMetadata (org.springframework.core.type.StandardMethodMetadata)14 ArrayList (java.util.ArrayList)12 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)12 AnnotationAttributes (org.springframework.core.annotation.AnnotationAttributes)12 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)11 Map (java.util.Map)10 StandardAnnotationMetadata (org.springframework.core.type.StandardAnnotationMetadata)10 Method (java.lang.reflect.Method)9 AnnotatedGenericBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition)9 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)8 Bean (org.springframework.context.annotation.Bean)8 IOException (java.io.IOException)6 HashSet (java.util.HashSet)6 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)6 NestedIOException (org.springframework.core.NestedIOException)6