final ClassTransformer transformer = beanManager.getServices().get(ClassTransformer.class); @SuppressWarnings("unchecked") final Class<T> clazz = (Class<T>) componentClass; EnhancedAnnotatedType<T> type = transformer.getEnhancedAnnotatedType(clazz, beanManager.getId()); if (!type.getJavaClass().equals(componentClass)) { final String bdaId = beanManager.getId() + classLoader.hashCode(); type = transformer.getEnhancedAnnotatedType(clazz, bdaId);
private static ClassTransformer getClassTransformer() { TypeStore store = new TypeStore(); SharedObjectCache cache = new SharedObjectCache(); ReflectionCache reflectionCache = ReflectionCacheFactory.newInstance(store); ClassTransformer classTransformer = new ClassTransformer(store, cache, reflectionCache, "STATIC_INSTANCE"); return classTransformer; }
@SuppressWarnings({ "rawtypes", "unchecked" }) private static void doDefineNewManagedBean(BeanManagerImpl beanManager, String bdaId, Class<?> beanClass) { try { ClassTransformer classTransformer = getClassTransformer(); SlimAnnotatedType<?> annotatedType = classTransformer.getBackedAnnotatedType(beanClass, bdaId); boolean managedBeanOrDecorator = Beans.isTypeManagedBeanOrDecoratorOrInterceptor(annotatedType); if (managedBeanOrDecorator) { EnhancedAnnotatedType eat = EnhancedAnnotatedTypeImpl.of(annotatedType, classTransformer); BeanAttributes attributes = BeanAttributesFactory.forBean(eat, beanManager); ManagedBean<?> bean = ManagedBean.of(attributes, eat, beanManager); ReflectionHelper.set(beanManager, beanManager.getClass(), "beanSet", Collections.synchronizedSet(new HashSet<Bean<?>>())); beanManager.addBean(bean); beanManager.getBeanResolver().clear(); bean.initializeAfterBeanDiscovery(); LOGGER.debug("Bean defined '{}'", beanClass.getName()); } else { // TODO : define session bean LOGGER.warning("Bean NOT? defined '{}', session bean?", beanClass.getName()); } } catch (Exception e) { LOGGER.debug("Bean definition failed.", e); } }
@Override public void addStereotype(Class<? extends Annotation> stereotype, Annotation... stereotypeDef) { checkWithinObserverNotification(); TypeStore typeStore = getTypeStore(); typeStore.add(stereotype, StereotypeLiteral.INSTANCE); for (Annotation a : stereotypeDef) { typeStore.add(stereotype, a); } getBeanManager().getServices().get(ClassTransformer.class).clearAnnotationData(stereotype); getBeanManager().getServices().get(MetaAnnotationStore.class).clearAnnotationData(stereotype); BootstrapLogger.LOG.addStereoTypeCalled(getReceiver(), stereotype); }
private CustomDecoratorWrapper(Decorator<T> delegate, BeanManagerImpl beanManager) { this.delegate = delegate; this.weldClass = beanManager.getServices().get(ClassTransformer.class).getEnhancedAnnotatedType(Reflections.<Class<T>>cast(delegate.getBeanClass()), beanManager.getId()); this.decoratedMethods = new DecoratedMethods(beanManager, this); }
public void endInitialization() { tracker.start(Tracker.OP_END_INIT); final BeanIdentifierIndex index = deploymentManager.getServices().get(BeanIdentifierIndex.class); if (index != null) { deploymentManager.getServices().cleanupAfterBoot(); deploymentManager.cleanupAfterBoot(); for (BeanDeployment beanDeployment : getBeanDeployments()) { BeanManagerImpl beanManager = beanDeployment.getBeanManager(); beanManager.getInterceptorMetadataReader().cleanAfterBoot(); beanManager.getServices().cleanupAfterBoot(); beanManager.cleanupAfterBoot(); for (Decorator<?> decorator : beanManager.getDecorators()) { if (decorator instanceof DecoratorImpl<?>) { Reflections.<DecoratorImpl<?>>cast(decorator).cleanupAfterBoot(); for (Interceptor<?> interceptor : beanManager.getInterceptors()) { if (interceptor instanceof InterceptorImpl<?>) { Reflections.<InterceptorImpl<?>>cast(interceptor).cleanupAfterBoot(); deploymentManager.getServices().get(Validator.class).clearResolved(); deploymentManager.getServices().get(ClassTransformer.class).cleanupAfterBoot();
@Override public <T> AnnotatedType<T> createAnnotatedType(Class<T> type) { // first, make sure to use the right BDA ID for this class String bdaId = BeanManagerLookupService.lookupBeanManager(type, this).getId(); return getServices().get(ClassTransformer.class).getBackedAnnotatedType(type, bdaId); }
private <A extends Annotation> void addSyntheticAnnotation(AnnotatedType<A> annotation, Annotation requiredMetaAnnotation) { if (requiredMetaAnnotation != null && !annotation.isAnnotationPresent(requiredMetaAnnotation.annotationType())) { // Add required meta annotation annotation = new AnnotatedTypeWrapper<A>(annotation, requiredMetaAnnotation); } getBeanManager().getServices().get(ClassTransformer.class).addSyntheticAnnotation(annotation, getBeanManager().getId()); getBeanManager().getServices().get(MetaAnnotationStore.class).clearAnnotationData(annotation.getJavaClass()); }
private void setupInitialServices() { if (initialServices.contains(TypeStore.class)) { return; } // instantiate initial services which we need for this phase TypeStore store = new TypeStore(); SharedObjectCache cache = new SharedObjectCache(); ReflectionCache reflectionCache = ReflectionCacheFactory.newInstance(store); ClassTransformer classTransformer = new ClassTransformer(store, cache, reflectionCache, contextId); initialServices.add(TypeStore.class, store); initialServices.add(SharedObjectCache.class, cache); initialServices.add(ReflectionCache.class, reflectionCache); initialServices.add(ClassTransformer.class, classTransformer); }
@Override public <T> void disposeAnnotatedType(Class<T> type, String id) { getServices().get(ClassTransformer.class).disposeBackedAnnotatedType(type, BeanManagerLookupService.lookupBeanManager(type, this).getId(), id); }
protected EnhancedAnnotatedConstructor<T> initEnhancedSubclass(BeanManagerImpl manager, EnhancedAnnotatedType<T> type, Bean<?> bean, ConstructorInjectionPoint<T> originalConstructorInjectionPoint) { ClassTransformer transformer = manager.getServices().get(ClassTransformer.class); EnhancedAnnotatedType<T> enhancedSubclass = transformer.getEnhancedAnnotatedType(createEnhancedSubclass(type, bean, manager), type.slim().getIdentifier().getBdaId()); return findMatchingConstructor(originalConstructorInjectionPoint.getSignature(), enhancedSubclass); }
/** * Check all abstract methods are declared by the decorated types. * * @param type * @param beanManager * @param delegateType * @throws DefinitionException If any of the abstract methods is not declared by the decorated types */ public static <T> void checkAbstractMethods(Set<Type> decoratedTypes, EnhancedAnnotatedType<T> type, BeanManagerImpl beanManager) { if (decoratedTypes == null) { decoratedTypes = new HashSet<Type>(type.getInterfaceClosure()); decoratedTypes.remove(Serializable.class); } Set<MethodSignature> signatures = new HashSet<MethodSignature>(); for (Type decoratedType : decoratedTypes) { for (EnhancedAnnotatedMethod<?, ?> method : ClassTransformer.instance(beanManager) .getEnhancedAnnotatedType(Reflections.getRawType(decoratedType), beanManager.getId()).getEnhancedMethods()) { signatures.add(method.getSignature()); } } for (EnhancedAnnotatedMethod<?, ?> method : type.getEnhancedMethods()) { if (Reflections.isAbstract(((AnnotatedMethod<?>) method).getJavaMember())) { MethodSignature methodSignature = method.getSignature(); if (!signatures.contains(methodSignature)) { throw BeanLogger.LOG.abstractMethodMustMatchDecoratedType(method, Formats.formatAsStackTraceElement(method.getJavaMember())); } } } }
private InterceptorBindings getInterceptorBindings(final String ejbName, final BeanManagerImpl manager) { InterceptorBindings retVal = null; if (ejbName != null) { retVal = interceptorSupport.getInterceptorBindings(ejbName, manager); } else { // This is a managed bean SlimAnnotatedType<?> type = (SlimAnnotatedType<?>) manager.createAnnotatedType(componentClass); if (!manager.getInterceptorModelRegistry().containsKey(type)) { EnhancedAnnotatedType<?> enhancedType = manager.getServices().get(ClassTransformer.class).getEnhancedAnnotatedType(type); InterceptionModelInitializer.of(manager, enhancedType, null).init(); } InterceptionModel model = manager.getInterceptorModelRegistry().get(type); if (model != null) { retVal = new InterceptorBindingsAdapter(manager.getInterceptorModelRegistry().get(type)); } } return retVal != null ? retVal : NullInterceptorBindings.INSTANCE; }
private void checkDecoratedMethods(EnhancedAnnotatedType<T> type, List<Decorator<?>> decorators) { if (type.isFinal()) { throw BeanLogger.LOG.finalBeanClassWithDecoratorsNotAllowed(this); } checkNoArgsConstructor(type); for (Decorator<?> decorator : decorators) { EnhancedAnnotatedType<?> decoratorClass; if (decorator instanceof DecoratorImpl<?>) { DecoratorImpl<?> decoratorBean = (DecoratorImpl<?>) decorator; decoratorClass = decoratorBean.getBeanManager().getServices().get(ClassTransformer.class) .getEnhancedAnnotatedType(decoratorBean.getAnnotated()); } else if (decorator instanceof CustomDecoratorWrapper<?>) { decoratorClass = ((CustomDecoratorWrapper<?>) decorator).getEnhancedAnnotated(); } else { throw BeanLogger.LOG.nonContainerDecorator(decorator); } for (EnhancedAnnotatedMethod<?, ?> decoratorMethod : decoratorClass.getEnhancedMethods()) { EnhancedAnnotatedMethod<?, ?> method = type.getEnhancedMethod(decoratorMethod.getSignature()); if (method != null && !method.isStatic() && !method.isPrivate() && method.isFinal()) { throw BeanLogger.LOG.finalBeanClassWithInterceptorsNotAllowed(this); } } } }
@Override public void createNewSessionBeans(BeanDeployerEnvironment environment, BeanManagerImpl manager) { final SlimAnnotatedTypeStore store = manager.getServices().get(SlimAnnotatedTypeStore.class); final ClassTransformer classTransformer = manager.getServices().get(ClassTransformer.class); for (Type type : environment.getNewBeanTypes()) { Class<?> clazz = Reflections.getRawType(type); if (isEjb(clazz)) { EnhancedAnnotatedType<?> enhancedType = classTransformer.getEnhancedAnnotatedType(clazz, type, manager.getId()); InternalEjbDescriptor<?> descriptor = ejbDescriptors.getUnique(clazz); environment.addSessionBean(createNewSessionBean(enhancedType, descriptor, manager, store)); } } }
@Override public <T> BeanAttributes<T> createBeanAttributes(AnnotatedType<T> type) { EnhancedAnnotatedType<T> clazz = services.get(ClassTransformer.class).getEnhancedAnnotatedType(type, getId()); if (services.get(EjbDescriptors.class).contains(type.getJavaClass())) { return BeanAttributesFactory.forSessionBean(clazz, services.get(EjbDescriptors.class).getUnique(clazz.getJavaClass()), this); } return BeanAttributesFactory.forBean(clazz, this); }
protected void processMetaAnnotations(SetMultimap<Class<? extends Annotation>, Annotation> metaAnnotationMap, Annotation annotation, ClassTransformer classTransformer, boolean declared) { // WELD-1310 Include synthetic annotations SlimAnnotatedType<?> syntheticAnnotationAnnotatedType = classTransformer.getSyntheticAnnotationAnnotatedType(annotation.annotationType()); if (syntheticAnnotationAnnotatedType != null) { addMetaAnnotations(metaAnnotationMap, annotation, syntheticAnnotationAnnotatedType.getAnnotations(), declared); } else { addMetaAnnotations(metaAnnotationMap, annotation, classTransformer.getReflectionCache().getAnnotations(annotation.annotationType()), declared); ReflectionCache.AnnotationClass<?> annotationClass = classTransformer.getReflectionCache().getAnnotationClass(annotation.annotationType()); if (annotationClass.isRepeatableAnnotationContainer()) { processMetaAnnotations(metaAnnotationMap, annotationClass.getRepeatableAnnotations(annotation), classTransformer, declared); } } addMetaAnnotations(metaAnnotationMap, annotation, classTransformer.getTypeStore().get(annotation.annotationType()), declared); }
public <T> EnhancedAnnotatedType<T> getEnhancedAnnotatedType(AnnotatedType<T> annotatedType, String bdaId) { if (annotatedType instanceof EnhancedAnnotatedType<?>) { return cast(annotatedType); } if (annotatedType instanceof SlimAnnotatedType<?>) { return cast(getEnhancedAnnotatedType((SlimAnnotatedType<?>) annotatedType)); } return getEnhancedAnnotatedType(getUnbackedAnnotatedType(annotatedType, bdaId, AnnotatedTypes.createTypeId(annotatedType))); }
/** * Call this method after all observer methods of this event have been invoked to get the final value of this {@link AnnotatedType}. * * @return the resulting annotated type */ public SlimAnnotatedType<X> getResultingAnnotatedType() { if (isDirty()) { return ClassTransformer.instance(manager).getUnbackedAnnotatedType(originalAnnotatedType, annotatedType); } else { return originalAnnotatedType; } }
protected <T> void createNewManagedBean(Class<T> clazz, Type type) { EnhancedAnnotatedType<T> enhancedType = classTransformer.getEnhancedAnnotatedType(clazz, type, manager.getId()); slimAnnotatedTypeStore.put(enhancedType.slim()); getEnvironment().addManagedBean(NewManagedBean.of(BeanAttributesFactory.forNewManagedBean(enhancedType, manager), enhancedType, manager)); }