@Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { Set<String> basePackages = getBasePackages(importingClassMetadata); ClassPathScanningCandidateComponentProvider scanner = getScanner(); scanner.addIncludeFilter(new AnnotationTypeFilter(MuonRepository.class)); for (String basePackage : basePackages) { Set<BeanDefinition> candidateComponents = scanner .findCandidateComponents(basePackage); for (BeanDefinition candidateComponent : candidateComponents) { if (candidateComponent instanceof AnnotatedBeanDefinition) { AnnotatedBeanDefinition beanDefinition = (AnnotatedBeanDefinition) candidateComponent; AnnotationMetadata annotationMetadata = beanDefinition.getMetadata(); Assert.isTrue(annotationMetadata.isInterface(), "@FeignClient can only be specified on an interface"); BeanDefinitionHolder holder = createBeanDefinition(annotationMetadata); BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry); } } } }
@Override public void afterPropertiesSet() throws Exception { // on recherche toutes les classes concrètes du package à la recherche de celles qui sont annotées 'TipiTopProcess' final ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false) { @Override protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) { return beanDefinition.getMetadata().isConcrete(); } }; scanner.addIncludeFilter(new AnnotationTypeFilter(TipiTopProcess.class)); if (excludeFilters != null) { for (TypeFilter filter : excludeFilters) { scanner.addExcludeFilter(filter); } } Set<BeanDefinition> beans = scanner.findCandidateComponents(aPackage); LOGGER.info("Registering " + beans.size() + " Tipi activities"); for (BeanDefinition bean : beans) { Class<?> clazz = Class.forName(bean.getBeanClassName()); registerClass(clazz); } }
/** * {@inheritDoc} */ @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { LepServiceProvider scanner = getScanner(); Set<String> basePackages = getBasePackages(importingClassMetadata); for (String basePackage : basePackages) { Set<BeanDefinition> candidateComponents = scanner.findCandidateComponents(basePackage); for (BeanDefinition candidateComponent : candidateComponents) { if (candidateComponent instanceof AnnotatedBeanDefinition) { AnnotatedBeanDefinition beanDefinition = (AnnotatedBeanDefinition) candidateComponent; AnnotationMetadata annotationMetadata = beanDefinition.getMetadata(); Map<String, Object> attributes = annotationMetadata .getAnnotationAttributes(LepService.class.getCanonicalName()); registerLepService(registry, annotationMetadata, attributes); } } } }
@Override public ScopeMetadata resolveScopeMetadata(final BeanDefinition definition) { if (definition instanceof AnnotatedBeanDefinition) { final AnnotatedBeanDefinition beanDefinition = (AnnotatedBeanDefinition) definition; final ScopeMetadata metadata = new ScopeMetadata(); final Set<String> annotationTypes = beanDefinition.getMetadata().getAnnotationTypes(); if (annotationTypes.contains(RequestScoped.class .getName())) { metadata.setScopeName("request"); metadata.setScopedProxyMode(ScopedProxyMode.TARGET_CLASS); } else if (annotationTypes .contains(ApplicationScoped.class.getName())) { metadata.setScopeName("singleton"); } else { return super.resolveScopeMetadata(definition); } return metadata; } else { return super.resolveScopeMetadata(definition); } }
static void processCommonDefinitionAnnotations(AnnotatedBeanDefinition abd, AnnotatedTypeMetadata metadata) { if (metadata.isAnnotated(Lazy.class.getName())) { abd.setLazyInit(attributesFor(metadata, Lazy.class).getBoolean("value")); } else if (abd.getMetadata().isAnnotated(Lazy.class.getName())) { abd.setLazyInit(attributesFor(abd.getMetadata(), Lazy.class).getBoolean("value")); } if (metadata.isAnnotated(Primary.class.getName())) { abd.setPrimary(true); } if (metadata.isAnnotated(DependsOn.class.getName())) { abd.setDependsOn(attributesFor(metadata, DependsOn.class).getStringArray("value")); } if (abd instanceof AbstractBeanDefinition) { AbstractBeanDefinition absBd = (AbstractBeanDefinition) abd; if (metadata.isAnnotated(Role.class.getName())) { absBd.setRole(attributesFor(metadata, Role.class).getNumber("value").intValue()); } if (metadata.isAnnotated(Description.class.getName())) { absBd.setDescription(attributesFor(metadata, Description.class).getString("value")); } } }
@Override public ScopeMetadata resolveScopeMetadata(BeanDefinition definition) { ScopeMetadata metadata = new ScopeMetadata(); if (definition instanceof AnnotatedBeanDefinition) { AnnotatedBeanDefinition annDef = (AnnotatedBeanDefinition) definition; AnnotationAttributes attributes = AnnotationConfigUtils.attributesFor(annDef.getMetadata(), this.scopeAnnotationType); if (attributes != null) { metadata.setScopeName(attributes.getString("value")); ScopedProxyMode proxyMode = attributes.getEnum("proxyMode"); if (proxyMode == null || proxyMode == ScopedProxyMode.DEFAULT) { proxyMode = this.defaultProxyMode; } metadata.setScopedProxyMode(proxyMode); } } return metadata; }
/** * Derive a bean name from one of the annotations on the class. * @param annotatedDef the annotation-aware bean definition * @return the bean name, or {@code null} if none is found */ protected String determineBeanNameFromAnnotation(AnnotatedBeanDefinition annotatedDef) { AnnotationMetadata amd = annotatedDef.getMetadata(); Set<String> types = amd.getAnnotationTypes(); String beanName = null; for (String type : types) { AnnotationAttributes attributes = AnnotationConfigUtils.attributesFor(amd, type); if (isStereotypeWithNameValue(type, amd.getMetaAnnotationTypes(type), attributes)) { Object value = attributes.get("value"); if (value instanceof String) { String strVal = (String) value; if (StringUtils.hasLength(strVal)) { if (beanName != null && !strVal.equals(beanName)) { throw new IllegalStateException("Stereotype annotations suggest inconsistent " + "component names: '" + beanName + "' versus '" + strVal + "'"); } beanName = strVal; } } } } return beanName; }
/** * Perform a scan within the specified base packages, * returning the registered bean definitions. * <p>This method does <i>not</i> register an annotation config processor * but rather leaves this up to the caller. * @param basePackages the packages to check for annotated classes * @return set of beans registered if any for tooling registration purposes (never {@code null}) */ protected Set<BeanDefinitionHolder> doScan(String... basePackages) { Assert.notEmpty(basePackages, "At least one base package must be specified"); Set<BeanDefinitionHolder> beanDefinitions = new LinkedHashSet<BeanDefinitionHolder>(); for (String basePackage : basePackages) { Set<BeanDefinition> candidates = findCandidateComponents(basePackage); for (BeanDefinition candidate : candidates) { ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(candidate); candidate.setScope(scopeMetadata.getScopeName()); String beanName = this.beanNameGenerator.generateBeanName(candidate, this.registry); if (candidate instanceof AbstractBeanDefinition) { postProcessBeanDefinition((AbstractBeanDefinition) candidate, beanName); } if (candidate instanceof AnnotatedBeanDefinition) { AnnotationConfigUtils.processCommonDefinitionAnnotations((AnnotatedBeanDefinition) candidate); } if (checkCandidate(beanName, candidate)) { BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(candidate, beanName); definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry); beanDefinitions.add(definitionHolder); registerBeanDefinition(definitionHolder, this.registry); } } } return beanDefinitions; }
/** * Get the Factory class name which corresponds to given bean definition. * @param definition Bean definition * @param beanFactory Bean factory * @return Factory class name, or <code>null</code> if not found */ private static String getBeanFactoryClassName(BeanDefinition definition, ConfigurableListableBeanFactory beanFactory) { if (definition instanceof AnnotatedBeanDefinition) { return ((AnnotatedBeanDefinition) definition).getMetadata().getClassName(); } else { if (definition.getFactoryBeanName() != null) { BeanDefinition fd = beanFactory.getBeanDefinition(definition.getFactoryBeanName()); if (fd != null) { return fd.getBeanClassName(); } } else { return definition.getBeanClassName(); } } return null; }
private Method getFactoryMethod(ConfigurableListableBeanFactory beanFactory, BeanDefinition definition) throws Exception { if (definition instanceof AnnotatedBeanDefinition) { MethodMetadata factoryMethodMetadata = ((AnnotatedBeanDefinition) definition) .getFactoryMethodMetadata(); if (factoryMethodMetadata instanceof StandardMethodMetadata) { return ((StandardMethodMetadata) factoryMethodMetadata) .getIntrospectedMethod(); } } BeanDefinition factoryDefinition = beanFactory .getBeanDefinition(definition.getFactoryBeanName()); Class<?> factoryClass = ClassUtils.forName(factoryDefinition.getBeanClassName(), beanFactory.getBeanClassLoader()); return getFactoryMethod(definition, factoryClass); }
static void processCommonDefinitionAnnotations(AnnotatedBeanDefinition abd, AnnotatedTypeMetadata metadata) { if (metadata.isAnnotated(Lazy.class.getName())) { abd.setLazyInit(attributesFor(metadata, Lazy.class).getBoolean("value")); } else if (abd.getMetadata() != metadata && abd.getMetadata().isAnnotated(Lazy.class.getName())) { abd.setLazyInit(attributesFor(abd.getMetadata(), Lazy.class).getBoolean("value")); } if (metadata.isAnnotated(Primary.class.getName())) { abd.setPrimary(true); } if (metadata.isAnnotated(DependsOn.class.getName())) { abd.setDependsOn(attributesFor(metadata, DependsOn.class).getStringArray("value")); } if (abd instanceof AbstractBeanDefinition) { AbstractBeanDefinition absBd = (AbstractBeanDefinition) abd; if (metadata.isAnnotated(Role.class.getName())) { absBd.setRole(attributesFor(metadata, Role.class).getNumber("value").intValue()); } if (metadata.isAnnotated(Description.class.getName())) { absBd.setDescription(attributesFor(metadata, Description.class).getString("value")); } } }
@Override public ScopeMetadata resolveScopeMetadata(BeanDefinition definition) { ScopeMetadata metadata = new ScopeMetadata(); if (definition instanceof AnnotatedBeanDefinition) { AnnotatedBeanDefinition annDef = (AnnotatedBeanDefinition) definition; AnnotationAttributes attributes = AnnotationConfigUtils.attributesFor(annDef.getMetadata(), this.scopeAnnotationType); if (attributes != null) { metadata.setScopeName(attributes.getAliasedString("value", this.scopeAnnotationType, definition.getSource())); ScopedProxyMode proxyMode = attributes.getEnum("proxyMode"); if (proxyMode == null || proxyMode == ScopedProxyMode.DEFAULT) { proxyMode = this.defaultProxyMode; } metadata.setScopedProxyMode(proxyMode); } } return metadata; }
@Override protected String buildDefaultBeanName(BeanDefinition definition) { String beanName = null; if (definition instanceof AnnotatedBeanDefinition) { boolean hasProtectedAnnotation = ((AnnotatedBeanDefinition) definition).getMetadata().hasAnnotation("io.leopard.beans.Protected"); if (hasProtectedAnnotation) { beanName = definition.getBeanClassName(); // System.err.println("beanName:" + beanName); } } if (beanName == null) { if (qualifiedBeanName) { beanName = definition.getBeanClassName(); } else { beanName = super.buildDefaultBeanName(definition); } } beanName = this.replaceBeanName(beanName); this.initPrimaryBean(definition); return beanName; }
/** * Checks how is bean defined and deduces scope name from JSF CDI annotations. * * @param definition beanDefinition */ private void registerJsfCdiToSpring(BeanDefinition definition) { if (definition instanceof AnnotatedBeanDefinition) { AnnotatedBeanDefinition annDef = (AnnotatedBeanDefinition) definition; String scopeName = null; // firstly check whether bean is defined via configuration if (annDef.getFactoryMethodMetadata() != null) { scopeName = deduceScopeName(annDef.getFactoryMethodMetadata()); } else { // fallback to type scopeName = deduceScopeName(annDef.getMetadata()); } if (scopeName != null) { definition.setScope(scopeName); log.debug("{} - Scope({})", definition.getBeanClassName(), scopeName.toUpperCase()); } } }
private static <T> boolean hasCustomBeanDefinition( ConfigurableListableBeanFactory beanFactory, Class<T> type, Class<?> configClass) { String[] names = beanFactory.getBeanNamesForType(type, true, false); if (names == null || names.length != 1) { return false; } BeanDefinition definition = beanFactory.getBeanDefinition(names[0]); if (definition instanceof AnnotatedBeanDefinition) { MethodMetadata factoryMethodMetadata = ((AnnotatedBeanDefinition) definition) .getFactoryMethodMetadata(); if (factoryMethodMetadata != null) { String className = factoryMethodMetadata.getDeclaringClassName(); return !configClass.getName().equals(className); } } return true; }
private Method getFactoryMethod(ConfigurableListableBeanFactory beanFactory, BeanDefinition definition) throws Exception { if (definition instanceof AnnotatedBeanDefinition) { MethodMetadata factoryMethodMetadata = ((AnnotatedBeanDefinition) definition) .getFactoryMethodMetadata(); if (factoryMethodMetadata instanceof StandardMethodMetadata) { return ((StandardMethodMetadata) factoryMethodMetadata) .getIntrospectedMethod(); } } BeanDefinition factoryDefinition = beanFactory .getBeanDefinition(definition.getFactoryBeanName()); Class<?> factoryClass = ClassUtils.forName(factoryDefinition.getBeanClassName(), beanFactory.getBeanClassLoader()); return ReflectionUtils.findMethod(factoryClass, definition.getFactoryMethodName()); }
@Override protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) { if (beanDefinition.getMetadata().isIndependent()) { // TODO until SPR-11711 will be resolved if (beanDefinition.getMetadata().isInterface() && beanDefinition.getMetadata() .getInterfaceNames().length == 1 && Annotation.class.getName().equals(beanDefinition .getMetadata().getInterfaceNames()[0])) { try { Class<?> target = ClassUtils.forName( beanDefinition.getMetadata().getClassName(), classLoader); return !target.isAnnotation(); } catch (Exception ex) { this.logger.error("Could not load target class: " + beanDefinition.getMetadata().getClassName(), ex); } } return true; } return false; }
private List<InterceptorInfo> collectInterceptors(ConfigurableListableBeanFactory configurableListableBeanFactory) { List<InterceptorInfo> interceptors = new ArrayList<InterceptorInfo>(); registeredInterceptorsCache = new ArrayList<InterceptorInfo>(); String[] bdNames = configurableListableBeanFactory.getBeanDefinitionNames(); for (String bdName : bdNames) { BeanDefinition bd = configurableListableBeanFactory.getBeanDefinition(bdName); if (bd instanceof AnnotatedBeanDefinition) { AnnotatedBeanDefinition abd = (AnnotatedBeanDefinition) bd; if (InterceptorInfo.isInterceptor(abd)) { if (bdName.startsWith(SCOPED_TARGET)) { bd = configurableListableBeanFactory.getBeanDefinition(bdName.replace(SCOPED_TARGET, "")); } InterceptorInfo interceptorInfo = new MethodInterceptorInfo(new BeanDefinitionHolder(bd, bdName.replace(SCOPED_TARGET, ""))); resolveInterceptorTargets(configurableListableBeanFactory, interceptorInfo); interceptors.add(interceptorInfo); } } } return interceptors; }
/** * Gets entity sub contracts. * * @param entityContract * the entity contract * @return the entity sub contracts */ @SuppressWarnings("unchecked") public static Collection<Class<IEntity>> getEntitySubContracts(Class<IEntity> entityContract) { Collection<Class<IEntity>> entitySubContracts = new HashSet<>(); ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false) { @Override protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) { // Allow to return superclasses return beanDefinition.getMetadata().isIndependent(); } }; provider.addIncludeFilter(new AssignableTypeFilter(entityContract)); Set<BeanDefinition> components = provider.findCandidateComponents(entityContract.getPackage().getName().replace('.', '/')); for (BeanDefinition component : components) { try { Class<IEntity> entitySubContract = (Class<IEntity>) Class.forName(component.getBeanClassName()); if (entitySubContract != entityContract) { entitySubContracts.add(entitySubContract); } } catch (ClassNotFoundException e) { // Ignore } } return entitySubContracts; }
public static void processCommonDefinitionAnnotations(AnnotatedBeanDefinition abd) { AnnotationMetadata metadata = abd.getMetadata(); if (metadata.isAnnotated(Primary.class.getName())) { abd.setPrimary(true); } if (metadata.isAnnotated(Lazy.class.getName())) { abd.setLazyInit(attributesFor(metadata, Lazy.class).getBoolean("value")); } if (metadata.isAnnotated(DependsOn.class.getName())) { abd.setDependsOn(attributesFor(metadata, DependsOn.class).getStringArray("value")); } if (abd instanceof AbstractBeanDefinition) { if (metadata.isAnnotated(Role.class.getName())) { Integer role = attributesFor(metadata, Role.class).getNumber("value"); ((AbstractBeanDefinition)abd).setRole(role); } } }
public ScopeMetadata resolveScopeMetadata(BeanDefinition definition) { ScopeMetadata metadata = new ScopeMetadata(); if (definition instanceof AnnotatedBeanDefinition) { AnnotatedBeanDefinition annDef = (AnnotatedBeanDefinition) definition; AnnotationAttributes attributes = attributesFor(annDef.getMetadata(), this.scopeAnnotationType); if (attributes != null) { metadata.setScopeName(attributes.getString("value")); ScopedProxyMode proxyMode = attributes.getEnum("proxyMode"); if (proxyMode == null || proxyMode == ScopedProxyMode.DEFAULT) { proxyMode = this.defaultProxyMode; } metadata.setScopedProxyMode(proxyMode); } } return metadata; }
public ScopeMetadata resolveScopeMetadata(BeanDefinition definition) { ScopeMetadata metadata = new ScopeMetadata(); metadata.setScopeName(BeanDefinition.SCOPE_PROTOTYPE); if (definition instanceof AnnotatedBeanDefinition) { AnnotatedBeanDefinition annDef = (AnnotatedBeanDefinition) definition; Set<String> annTypes = annDef.getMetadata().getAnnotationTypes(); String found = null; for (String annType : annTypes) { Set<String> metaAnns = annDef.getMetadata().getMetaAnnotationTypes(annType); if (metaAnns.contains("javax.inject.Scope")) { if (found != null) { throw new IllegalStateException("Found ambiguous scope annotations on bean class [" + definition.getBeanClassName() + "]: " + found + ", " + annType); } found = annType; String scopeName = resolveScopeName(annType); if (scopeName == null) { throw new IllegalStateException( "Unsupported scope annotation - not mapped onto Spring scope name: " + annType); } metadata.setScopeName(scopeName); } } } return metadata; }
/** * Derive a bean name from one of the annotations on the class. * @param annotatedDef the annotation-aware bean definition * @return the bean name, or {@code null} if none is found */ protected String determineBeanNameFromAnnotation(AnnotatedBeanDefinition annotatedDef) { AnnotationMetadata amd = annotatedDef.getMetadata(); Set<String> types = amd.getAnnotationTypes(); String beanName = null; for (String type : types) { AnnotationAttributes attributes = MetadataUtils.attributesFor(amd, type); if (isStereotypeWithNameValue(type, amd.getMetaAnnotationTypes(type), attributes)) { Object value = attributes.get("value"); if (value instanceof String) { String strVal = (String) value; if (StringUtils.hasLength(strVal)) { if (beanName != null && !strVal.equals(beanName)) { throw new IllegalStateException("Stereotype annotations suggest inconsistent " + "component names: '" + beanName + "' versus '" + strVal + "'"); } beanName = strVal; } } } } return beanName; }
@Override public void postProcessBeanFactory( ConfigurableListableBeanFactory beanFactory ) throws BeansException { String[] beanNames = beanFactory.getBeanDefinitionNames(); for( String beanName : beanNames ) { if( beanFactory.containsBeanDefinition( beanName ) ) { BeanDefinition beanDefinition = beanFactory.getBeanDefinition( beanName ); if( beanDefinition instanceof AnnotatedBeanDefinition ) { AnnotatedBeanDefinition annotatedBeanDefinition = (AnnotatedBeanDefinition) beanDefinition; if( annotatedBeanDefinition.getMetadata().hasAnnotation( JGivenStage.class.getName() ) ) { String className = beanDefinition.getBeanClassName(); Class<?> stageClass = createStageClass( beanName, className ); beanDefinition.setBeanClassName( stageClass.getName() ); } } } } }