Example usage for org.springframework.core.type MethodMetadata getMethodName

List of usage examples for org.springframework.core.type MethodMetadata getMethodName

Introduction

In this page you can find the example usage for org.springframework.core.type MethodMetadata getMethodName.

Prototype

String getMethodName();

Source Link

Document

Return the name of the method.

Usage

From source file:lodsve.core.condition.SpringBootCondition.java

private static String getClassOrMethodName(AnnotatedTypeMetadata metadata) {
    if (metadata instanceof ClassMetadata) {
        ClassMetadata classMetadata = (ClassMetadata) metadata;
        return classMetadata.getClassName();
    }//from www .jav a  2 s  .c  om
    MethodMetadata methodMetadata = (MethodMetadata) metadata;
    return methodMetadata.getDeclaringClassName() + "#" + methodMetadata.getMethodName();
}

From source file:lodsve.core.condition.SpringBootCondition.java

private String getName(AnnotatedTypeMetadata metadata) {
    if (metadata instanceof AnnotationMetadata) {
        return ((AnnotationMetadata) metadata).getClassName();
    }/*from   w w  w.ja va  2  s . co  m*/
    if (metadata instanceof MethodMetadata) {
        MethodMetadata methodMetadata = (MethodMetadata) metadata;
        return methodMetadata.getDeclaringClassName() + "." + methodMetadata.getMethodName();
    }
    return metadata.toString();
}

From source file:org.duracloud.account.db.util.security.impl.AnnotationParserImpl.java

private Map<String, Object[]> doGetMethodAnnotationsForClass(Class annotationClass, Class targetClass) {
    Map<String, Object[]> methodAnnotations = new HashMap<String, Object[]>();

    // Find class/interface in stack that has the annotation
    Class iface = getAnnotatedInterface(annotationClass, targetClass);

    // Read the annotation metadata from the found class/interface
    MetadataReader metadataReader = getMetadataReader(iface);
    AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();

    Set<MethodMetadata> annotatedMethods = getAnnotatedMethods(annotationMetadata, annotationClass);

    Iterator itr = getAnnotatedMethodsIterator(annotatedMethods);
    while (itr.hasNext()) {
        MethodMetadata methodMetadata = (MethodMetadata) itr.next();

        Map<String, Object> annotationAtts = methodMetadata.getAnnotationAttributes(annotationClass.getName());
        Object[] values = getValues(annotationAtts);
        methodAnnotations.put(methodMetadata.getMethodName(), values);
    }//from w  w w.jav  a  2s  . com
    return methodAnnotations;
}

From source file:io.neba.core.selftests.SelftestReference.java

public SelftestReference(BeanFactory factory, String beanName, MethodMetadata methodMetadata, Bundle bundle) {
    super(beanName, factory, bundle);
    Map<String, Object> annotationAttributes = methodMetadata.getAnnotationAttributes(SelfTest.class.getName());
    this.success = (String) annotationAttributes.get("success");
    this.failure = (String) annotationAttributes.get("failure");
    this.description = (String) annotationAttributes.get("value");
    this.methodName = methodMetadata.getMethodName();
    this.hashCode = new HashCodeBuilder().append(beanName).append(bundle.getBundleId()).append(this.methodName)
            .toHashCode();//from  www.  ja v  a  2  s . c  om
}

From source file:org.springframework.boot.autoconfigure.condition.AbstractOnBeanCondition.java

@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
    MultiValueMap<String, Object> attributes = metadata.getAllAnnotationAttributes(annotationClass().getName(),
            true);/*from   w  w w.j  a  v  a  2 s  .  co  m*/
    final List<String> beanClasses = collect(attributes, "value");
    final List<String> beanNames = collect(attributes, "name");

    if (beanClasses.size() == 0) {
        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())) {
                            beanClasses.add(method.getReturnType().getName());
                        }
                    }
                });
            } catch (Exception ex) {
                // swallow exception and continue
            }
        }
    }

    Assert.isTrue(beanClasses.size() > 0 || beanNames.size() > 0,
            "@" + ClassUtils.getShortName(annotationClass()) + " annotations must specify at least one bean");

    return matches(context, metadata, beanClasses, beanNames);
}

From source file:org.springframework.boot.autoconfigure.condition.ConditionLogUtils.java

private static String getMethodName(MethodMetadata metadata) {
    return metadata.getDeclaringClassName() + "#" + metadata.getMethodName();
}

From source file:org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.java

/**
 * Read the given {@link BeanMethod}, registering bean definitions
 * with the BeanDefinitionRegistry based on its contents.
 *///from   w w  w  . ja va2 s  .c  o m
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);
    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(methodName);
    } else {
        // instance @Bean method
        beanDef.setFactoryBeanName(configClass.getBeanName());
        beanDef.setUniqueFactoryMethodName(methodName);
    }
    beanDef.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
    beanDef.setAttribute(RequiredAnnotationBeanPostProcessor.SKIP_REQUIRED_CHECK_ATTRIBUTE, Boolean.TRUE);

    AnnotationConfigUtils.processCommonDefinitionAnnotations(beanDef, metadata);

    Autowire autowire = bean.getEnum("autowire");
    if (autowire.isAutowire()) {
        beanDef.setAutowireMode(autowire.value());
    }

    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);
    }

    if (logger.isDebugEnabled()) {
        logger.debug(String.format("Registering bean definition for @Bean method %s.%s()",
                configClass.getMetadata().getClassName(), beanName));
    }

    this.registry.registerBeanDefinition(beanName, beanDefToRegister);
}

From source file:org.springframework.context.annotation.ConfigurationClassParser.java

/**
 * Retrieve the metadata for all <code>@Bean</code> methods.
 *//*ww w. j a va  2 s .  c  o  m*/
private Set<MethodMetadata> retrieveBeanMethodMetadata(SourceClass sourceClass) {
    AnnotationMetadata original = sourceClass.getMetadata();
    Set<MethodMetadata> beanMethods = original.getAnnotatedMethods(Bean.class.getName());
    if (beanMethods.size() > 1 && original instanceof StandardAnnotationMetadata) {
        // Try reading the class file via ASM for deterministic declaration order...
        // Unfortunately, the JVM's standard reflection returns methods in arbitrary
        // order, even between different runs of the same application on the same JVM.
        try {
            AnnotationMetadata asm = this.metadataReaderFactory.getMetadataReader(original.getClassName())
                    .getAnnotationMetadata();
            Set<MethodMetadata> asmMethods = asm.getAnnotatedMethods(Bean.class.getName());
            if (asmMethods.size() >= beanMethods.size()) {
                Set<MethodMetadata> selectedMethods = new LinkedHashSet<>(asmMethods.size());
                for (MethodMetadata asmMethod : asmMethods) {
                    for (MethodMetadata beanMethod : beanMethods) {
                        if (beanMethod.getMethodName().equals(asmMethod.getMethodName())) {
                            selectedMethods.add(beanMethod);
                            break;
                        }
                    }
                }
                if (selectedMethods.size() == beanMethods.size()) {
                    // All reflection-detected methods found in ASM method set -> proceed
                    beanMethods = selectedMethods;
                }
            }
        } catch (IOException ex) {
            logger.debug("Failed to read class file via ASM for determining @Bean method order", ex);
            // No worries, let's continue with the reflection metadata we started with...
        }
    }
    return beanMethods;
}