Package org.springframework.core.type

Examples of org.springframework.core.type.MethodMetadata


   * Read the given {@link BeanMethod}, registering bean definitions
   * with the BeanDefinitionRegistry based on its contents.
   */
  private void loadBeanDefinitionsForBeanMethod(BeanMethod beanMethod) {
    ConfigurationClass configClass = beanMethod.getConfigurationClass();
    MethodMetadata metadata = beanMethod.getMetadata();

    RootBeanDefinition beanDef = new ConfigurationClassBeanDefinition(configClass);
    beanDef.setResource(configClass.getResource());
    beanDef.setSource(this.sourceExtractor.extractSource(metadata, configClass.getResource()));
    if (metadata.isStatic()) {
      // static @Bean method
      beanDef.setBeanClassName(configClass.getMetadata().getClassName());
      beanDef.setFactoryMethodName(metadata.getMethodName());
    }
    else {
      // instance @Bean method
      beanDef.setFactoryBeanName(configClass.getBeanName());
      beanDef.setUniqueFactoryMethodName(metadata.getMethodName());
    }
    beanDef.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
    beanDef.setAttribute(RequiredAnnotationBeanPostProcessor.SKIP_REQUIRED_CHECK_ATTRIBUTE, Boolean.TRUE);

    // consider role
    Map<String, Object> roleAttributes = metadata.getAnnotationAttributes(Role.class.getName());
    if (roleAttributes != null) {
      int role = (Integer) roleAttributes.get("value");
      beanDef.setRole(role);
    }

    // consider name and any aliases
    Map<String, Object> beanAttributes = metadata.getAnnotationAttributes(Bean.class.getName());
    List<String> names = new ArrayList<String>(Arrays.asList((String[]) beanAttributes.get("name")));
    String beanName = (names.size() > 0 ? names.remove(0) : beanMethod.getMetadata().getMethodName());
    for (String alias : names) {
      this.registry.registerAlias(beanName, alias);
    }

    // has this already been overridden (e.g. via XML)?
    if (this.registry.containsBeanDefinition(beanName)) {
      BeanDefinition existingBeanDef = registry.getBeanDefinition(beanName);
      // is the existing bean definition one that was created from a configuration class?
      if (!(existingBeanDef instanceof ConfigurationClassBeanDefinition)) {
        // no -> then it's an external override, probably XML
        // overriding is legal, return immediately
        if (logger.isDebugEnabled()) {
          logger.debug(String.format("Skipping loading bean definition for %s: a definition for bean " +
              "'%s' already exists. This is likely due to an override in XML.", beanMethod, beanName));
        }
        return;
      }
    }

    if (metadata.isAnnotated(Primary.class.getName())) {
      beanDef.setPrimary(true);
    }

    // is this bean to be instantiated lazily?
    if (metadata.isAnnotated(Lazy.class.getName())) {
      beanDef.setLazyInit((Boolean) metadata.getAnnotationAttributes(Lazy.class.getName()).get("value"));
    }
    else if (configClass.getMetadata().isAnnotated(Lazy.class.getName())){
      beanDef.setLazyInit((Boolean) configClass.getMetadata().getAnnotationAttributes(Lazy.class.getName()).get("value"));
    }

    if (metadata.isAnnotated(DependsOn.class.getName())) {
      String[] dependsOn = (String[]) metadata.getAnnotationAttributes(DependsOn.class.getName()).get("value");
      if (dependsOn.length > 0) {
        beanDef.setDependsOn(dependsOn);
      }
    }

    Autowire autowire = (Autowire) beanAttributes.get("autowire");
    if (autowire.isAutowire()) {
      beanDef.setAutowireMode(autowire.value());
    }

    String initMethodName = (String) beanAttributes.get("initMethod");
    if (StringUtils.hasText(initMethodName)) {
      beanDef.setInitMethodName(initMethodName);
    }

    String destroyMethodName = (String) beanAttributes.get("destroyMethod");
    if (StringUtils.hasText(destroyMethodName)) {
      beanDef.setDestroyMethodName(destroyMethodName);
    }

    // consider scoping
    ScopedProxyMode proxyMode = ScopedProxyMode.NO;
    Map<String, Object> scopeAttributes = metadata.getAnnotationAttributes(Scope.class.getName());
    if (scopeAttributes != null) {
      beanDef.setScope((String) scopeAttributes.get("value"));
      proxyMode = (ScopedProxyMode) scopeAttributes.get("proxyMode");
      if (proxyMode == ScopedProxyMode.DEFAULT) {
        proxyMode = ScopedProxyMode.NO;
View Full Code Here


   * Read the given {@link BeanMethod}, registering bean definitions
   * with the BeanDefinitionRegistry based on its contents.
   */
  private void loadBeanDefinitionsForBeanMethod(BeanMethod beanMethod) {
    ConfigurationClass configClass = beanMethod.getConfigurationClass();
    MethodMetadata metadata = beanMethod.getMetadata();

    RootBeanDefinition beanDef = new ConfigurationClassBeanDefinition(configClass);
    beanDef.setResource(configClass.getResource());
    beanDef.setSource(this.sourceExtractor.extractSource(metadata, configClass.getResource()));
    if (metadata.isStatic()) {
      // static @Bean method
      beanDef.setBeanClassName(configClass.getMetadata().getClassName());
      beanDef.setFactoryMethodName(metadata.getMethodName());
    }
    else {
      // instance @Bean method
      beanDef.setFactoryBeanName(configClass.getBeanName());
      beanDef.setUniqueFactoryMethodName(metadata.getMethodName());
    }
    beanDef.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
    beanDef.setAttribute(RequiredAnnotationBeanPostProcessor.SKIP_REQUIRED_CHECK_ATTRIBUTE, Boolean.TRUE);

    // consider role
    AnnotationAttributes role = attributesFor(metadata, Role.class);
    if (role != null) {
      beanDef.setRole(role.<Integer>getNumber("value"));
    }

    // consider name and any aliases
    AnnotationAttributes bean = attributesFor(metadata, Bean.class);
    List<String> names = new ArrayList<String>(Arrays.asList(bean.getStringArray("name")));
    String beanName = (names.size() > 0 ? names.remove(0) : beanMethod.getMetadata().getMethodName());
    for (String alias : names) {
      this.registry.registerAlias(beanName, alias);
    }

    // has this already been overridden (e.g. via XML)?
    if (this.registry.containsBeanDefinition(beanName)) {
      BeanDefinition existingBeanDef = registry.getBeanDefinition(beanName);
      // is the existing bean definition one that was created from a configuration class?
      if (!(existingBeanDef instanceof ConfigurationClassBeanDefinition)) {
        // no -> then it's an external override, probably XML
        // overriding is legal, return immediately
        if (logger.isDebugEnabled()) {
          logger.debug(String.format("Skipping loading bean definition for %s: a definition for bean " +
              "'%s' already exists. This is likely due to an override in XML.", beanMethod, beanName));
        }
        return;
      }
    }

    if (metadata.isAnnotated(Primary.class.getName())) {
      beanDef.setPrimary(true);
    }

    // is this bean to be instantiated lazily?
    if (metadata.isAnnotated(Lazy.class.getName())) {
      AnnotationAttributes lazy = attributesFor(metadata, Lazy.class);
      beanDef.setLazyInit(lazy.getBoolean("value"));
    }
    else if (configClass.getMetadata().isAnnotated(Lazy.class.getName())){
      AnnotationAttributes lazy = attributesFor(configClass.getMetadata(), Lazy.class);
      beanDef.setLazyInit(lazy.getBoolean("value"));
    }

    if (metadata.isAnnotated(DependsOn.class.getName())) {
      AnnotationAttributes dependsOn = attributesFor(metadata, DependsOn.class);
      String[] otherBeans = dependsOn.getStringArray("value");
      if (otherBeans.length > 0) {
        beanDef.setDependsOn(otherBeans);
      }
View Full Code Here

    if (this.conditionEvaluator.shouldSkip(beanMethod.getMetadata(), ConfigurationPhase.REGISTER_BEAN)) {
      return;
    }

    ConfigurationClass configClass = beanMethod.getConfigurationClass();
    MethodMetadata metadata = beanMethod.getMetadata();

    ConfigurationClassBeanDefinition beanDef = new ConfigurationClassBeanDefinition(configClass, metadata);
    beanDef.setResource(configClass.getResource());
    beanDef.setSource(this.sourceExtractor.extractSource(metadata, configClass.getResource()));
    if (metadata.isStatic()) {
      // static @Bean method
      beanDef.setBeanClassName(configClass.getMetadata().getClassName());
      beanDef.setFactoryMethodName(metadata.getMethodName());
    }
    else {
      // instance @Bean method
      beanDef.setFactoryBeanName(configClass.getBeanName());
      beanDef.setUniqueFactoryMethodName(metadata.getMethodName());
    }
    beanDef.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
    beanDef.setAttribute(RequiredAnnotationBeanPostProcessor.SKIP_REQUIRED_CHECK_ATTRIBUTE, Boolean.TRUE);

    // Consider name and any aliases
View Full Code Here

   * Reads a particular {@link ConfigurationClassMethod}, registering bean definitions with
   * the BeanDefinitionRegistry based on its contents.
   */
  private void loadBeanDefinitionsForModelMethod(ConfigurationClassMethod method) {
    ConfigurationClass configClass = method.getConfigurationClass();
    MethodMetadata metadata = method.getMetadata();

    RootBeanDefinition beanDef = new ConfigurationClassBeanDefinition(configClass);
    beanDef.setResource(configClass.getResource());
    beanDef.setSource(this.sourceExtractor.extractSource(metadata, configClass.getResource()));
    beanDef.setFactoryBeanName(configClass.getBeanName());
    beanDef.setUniqueFactoryMethodName(metadata.getMethodName());
    beanDef.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
    beanDef.setAttribute(RequiredAnnotationBeanPostProcessor.SKIP_REQUIRED_CHECK_ATTRIBUTE, Boolean.TRUE);

    // consider name and any aliases
    Map<String, Object> beanAttributes = metadata.getAnnotationAttributes(Bean.class.getName());
    List<String> names = new ArrayList<String>(Arrays.asList((String[]) beanAttributes.get("name")));
    String beanName = (names.size() > 0 ? names.remove(0) : method.getMetadata().getMethodName());
    for (String alias : names) {
      this.registry.registerAlias(beanName, alias);
    }

    // has this already been overridden (e.g. via XML)?
    if (this.registry.containsBeanDefinition(beanName)) {
      BeanDefinition existingBeanDef = registry.getBeanDefinition(beanName);
      // is the existing bean definition one that was created from a configuration class?
      if (!(existingBeanDef instanceof ConfigurationClassBeanDefinition)) {
        // no -> then it's an external override, probably XML
        // overriding is legal, return immediately
        if (logger.isDebugEnabled()) {
          logger.debug(String.format("Skipping loading bean definition for %s: a definition for bean " +
              "'%s' already exists. This is likely due to an override in XML.", method, beanName));
        }
        return;
      }
    }

    if (metadata.isAnnotated(Primary.class.getName())) {
      beanDef.setPrimary(true);
    }

    // is this bean to be instantiated lazily?
    if (metadata.isAnnotated(Lazy.class.getName())) {
      beanDef.setLazyInit((Boolean) metadata.getAnnotationAttributes(Lazy.class.getName()).get("value"));
    }
    else if (configClass.getMetadata().isAnnotated(Lazy.class.getName())){
      beanDef.setLazyInit((Boolean) configClass.getMetadata().getAnnotationAttributes(Lazy.class.getName()).get("value"));
    }

    if (metadata.isAnnotated(DependsOn.class.getName())) {
      String[] dependsOn = (String[]) metadata.getAnnotationAttributes(DependsOn.class.getName()).get("value");
      if (dependsOn.length > 0) {
        beanDef.setDependsOn(dependsOn);
      }
    }

    Autowire autowire = (Autowire) beanAttributes.get("autowire");
    if (autowire.isAutowire()) {
      beanDef.setAutowireMode(autowire.value());
    }

    String initMethodName = (String) beanAttributes.get("initMethod");
    if (StringUtils.hasText(initMethodName)) {
      beanDef.setInitMethodName(initMethodName);
    }

    String destroyMethodName = (String) beanAttributes.get("destroyMethod");
    if (StringUtils.hasText(destroyMethodName)) {
      beanDef.setDestroyMethodName(destroyMethodName);
    }

    // consider scoping
    ScopedProxyMode proxyMode = ScopedProxyMode.NO;
    Map<String, Object> scopeAttributes = metadata.getAnnotationAttributes(Scope.class.getName());
    if (scopeAttributes != null) {
      beanDef.setScope((String) scopeAttributes.get("value"));
      proxyMode = (ScopedProxyMode) scopeAttributes.get("proxyMode");
      if (proxyMode == ScopedProxyMode.DEFAULT) {
        proxyMode = ScopedProxyMode.NO;
View Full Code Here

  private String getName(AnnotatedTypeMetadata metadata) {
    if (metadata instanceof AnnotationMetadata) {
      return ((AnnotationMetadata) metadata).getClassName();
    }
    if (metadata instanceof MethodMetadata) {
      MethodMetadata methodMetadata = (MethodMetadata) metadata;
      return methodMetadata.getDeclaringClassName() + "."
          + methodMetadata.getMethodName();
    }
    return metadata.toString();
  }
View Full Code Here

  private static String getClassOrMethodName(AnnotatedTypeMetadata metadata) {
    if (metadata instanceof ClassMetadata) {
      ClassMetadata classMetadata = (ClassMetadata) metadata;
      return classMetadata.getClassName();
    }
    MethodMetadata methodMetadata = (MethodMetadata) metadata;
    return methodMetadata.getDeclaringClassName() + "#"
        + methodMetadata.getMethodName();
  }
View Full Code Here

    private void addDeducedBeanType(ConditionContext context,
        AnnotatedTypeMetadata metadata, final List<String> beanTypes) {
      if (metadata instanceof MethodMetadata
          && metadata.isAnnotated(Bean.class.getName())) {
        try {
          final MethodMetadata methodMetadata = (MethodMetadata) metadata;
          // We should be safe to load at this point since we are in the
          // REGISTER_BEAN phase
          Class<?> configClass = ClassUtils.forName(
              methodMetadata.getDeclaringClassName(),
              context.getClassLoader());
          ReflectionUtils.doWithMethods(configClass, new MethodCallback() {
            @Override
            public void doWith(Method method)
                throws IllegalArgumentException, IllegalAccessException {
              if (methodMetadata.getMethodName().equals(method.getName())) {
                beanTypes.add(method.getReturnType().getName());
              }
            }
          });
        }
View Full Code Here

        }
      }
    }
    else if ( definition != null ) {
      if ( definition.getSource() instanceof MethodMetadata ) {
        MethodMetadata metadata = (MethodMetadata) definition.getSource();

        try {
          Method method = ReflectionUtils.findMethod(
              ClassUtils.getUserClass( Class.forName( metadata.getDeclaringClassName() ) ),
              metadata.getMethodName() );

          for ( T allowed : allowedItems ) {
            if ( matches( method.getReturnType(), allowed ) ) {
              return true;
            }
View Full Code Here

    }

    // Even though a bean might not match, perhaps the definition that created it does
    if ( definition != null ) {
      if ( definition.getSource() instanceof MethodMetadata ) {
        MethodMetadata metadata = (MethodMetadata) definition.getSource();

        // If method itself has the annotation it applies
        for ( Class<? extends Annotation> annotation : annotations ) {
          if ( metadata.isAnnotated( annotation.getName() ) ) {
            return true;
          }
        }

        try {
          Class factoryClass = ClassUtils.getUserClass( Class.forName( metadata.getDeclaringClassName() ) );

          Object factory = beanFactory.getSingleton( definition.getFactoryBeanName() );

          if ( factory != null ) {
            factoryClass = ClassUtils.getUserClass( AopProxyUtils.ultimateTargetClass( factory ) );
          }

          if ( isMatchIfBeanFactoryApplies() ) {
            // If the bean factory has the annotation, then it should apply as well
            for ( Class<? extends Annotation> annotation : annotations ) {
              if ( AnnotationUtils.getAnnotation( factoryClass, annotation ) != null ) {
                return true;
              }
            }
          }

          if ( bean == null ) {
            // If the target of the method has the annotation, it applies - in case of a bean
            // this has already been tested
            Method method = ReflectionUtils.findMethod( factoryClass, metadata.getMethodName() );

            for ( Class<? extends Annotation> annotation : annotations ) {
              if ( AnnotationUtils.getAnnotation( method.getReturnType(), annotation ) != null ) {
                return true;
              }
View Full Code Here

    assertTrue(annotationMetadata.hasAnnotatedMethods(Bean.class.getName()));

    Set<MethodMetadata> methods = annotationMetadata.getAnnotatedMethods(Bean.class.getName());
    assertEquals(1, methods.size());
    MethodMetadata methodMetadata = methods.iterator().next();
    assertTrue(methodMetadata.isAnnotated(Bean.class.getName()));
    assertEquals("getInstanceOfBean", methodMetadata.getMethodName());
    assertEquals("org.test.spring.SimpleBeanClass", methodMetadata.getDeclaringClassName());
   
    Map<String, Object> annotationAttributes = methodMetadata.getAnnotationAttributes(Bean.class.getName());
    assertEquals(4, annotationAttributes.size());
   
    assertEquals("", annotationAttributes.get("initMethod"));
    assertEquals("(inferred)", annotationAttributes.get("destroyMethod"));
   
    Object nameArray = annotationAttributes.get("name");
    assertNotNull(nameArray);
    assertTrue(nameArray instanceof Object[]);
    assertEquals(0, ((Object[]) nameArray).length);
   
    assertEquals(Autowire.class, annotationAttributes.get("autowire").getClass());
    assertEquals(Autowire.NO, annotationAttributes.get("autowire"));
   
    assertTrue(methodMetadata instanceof JdtConnectedMetadata);
    IType type = JdtUtils.getJavaType(project, "org.test.spring.SimpleBeanClass");
    IMethod method = type.getMethods()[0];
    assertEquals(method, ((JdtConnectedMetadata) methodMetadata).getJavaElement());
   
    assertNull(methodMetadata.getAnnotationAttributes(Role.class.getName()));
   
    assertEquals(0, annotationMetadata.getMemberClassNames().length);
  }
View Full Code Here

TOP

Related Classes of org.springframework.core.type.MethodMetadata

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.