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); } } } }
this.superclass = classTransformer.getEnhancedAnnotatedType(Object.class, AnnotatedTypeIdentifier.NULL_BDA_ID); if ((superclass != null) && (superclass.getJavaClass() != Object.class)) { fieldsTemp = Sets.union(fieldsTemp, Reflections.<Set<EnhancedAnnotatedField<?, ? super T>>>cast(superclass.getFields())); this.declaredAnnotatedFields = Multimaps.unmodifiableMultimap(declaredAnnotatedFields); EnhancedAnnotatedMethod<?, ? super T> weldMethod = EnhancedAnnotatedMethodImpl.of(method, this, classTransformer); declaredMethodsTemp.add(weldMethod); for (Annotation annotation : weldMethod.getAnnotations()) { if (weldMethod.getEnhancedParameters(annotationType).size() > 0) { for (Class<?> interfaceClazz : Reflections.getInterfaceClosure(javaClass)) { EnhancedAnnotatedType<?> interfaceType = classTransformer.getEnhancedAnnotatedType(interfaceClazz, slim.getIdentifier().getBdaId()); for (EnhancedAnnotatedMethod<?, ?> interfaceMethod : interfaceType.getEnhancedMethods()) { if(Reflections.isDefault(interfaceMethod.getJavaMember())) { methodsTemp.add((EnhancedAnnotatedMethod<?, ? super T>) interfaceMethod); declaredMethodsTemp.add(enhancedMethod); for (Annotation annotation : enhancedMethod.getAnnotations()) { if (enhancedMethod.getEnhancedParameters(annotationType).size() > 0) { this.declaredAnnotatedMethods = Multimaps.unmodifiableMultimap(declaredAnnotatedMethods); SetMultimap<Class<? extends Annotation>, Annotation> declaredMetaAnnotationMap = SetMultimap.newSetMultimap();
/** * 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())); } } } }
this.superclass = classTransformer.getEnhancedAnnotatedType(Object.class, AnnotatedTypeIdentifier.NULL_BDA_ID); if ((superclass != null) && (superclass.getJavaClass() != Object.class)) { fieldsTemp = Sets.union(fieldsTemp, Reflections.<Set<EnhancedAnnotatedField<?, ? super T>>>cast(superclass.getFields())); this.declaredAnnotatedFields = Multimaps.unmodifiableMultimap(declaredAnnotatedFields); EnhancedAnnotatedMethod<?, ? super T> weldMethod = EnhancedAnnotatedMethodImpl.of(method, this, classTransformer); declaredMethodsTemp.add(weldMethod); for (Annotation annotation : weldMethod.getAnnotations()) { if (weldMethod.getEnhancedParameters(annotationType).size() > 0) { for (Class<?> interfaceClazz : Reflections.getInterfaceClosure(javaClass)) { EnhancedAnnotatedType<?> interfaceType = classTransformer.getEnhancedAnnotatedType(interfaceClazz, slim.getIdentifier().getBdaId()); for (EnhancedAnnotatedMethod<?, ?> interfaceMethod : interfaceType.getEnhancedMethods()) { if(Reflections.isDefault(interfaceMethod.getJavaMember())) { methodsTemp.add((EnhancedAnnotatedMethod<?, ? super T>) interfaceMethod); declaredMethodsTemp.add(enhancedMethod); for (Annotation annotation : enhancedMethod.getAnnotations()) { if (enhancedMethod.getEnhancedParameters(annotationType).size() > 0) { this.declaredAnnotatedMethods = Multimaps.unmodifiableMultimap(declaredAnnotatedMethods); declaredMetaAnnotationMap.put(declaredAnnotation.annotationType(), declaredAnnotation);
@Override public void processMethod(EnhancedAnnotatedMethod<?, ? super T> method) { if (method.isAnnotationPresent(Inject.class)) { if (method.getAnnotation(Produces.class) != null) { throw UtilLogger.LOG.initializerCannotBeProducer(method, Formats.formatAsStackTraceElement(method.getJavaMember())); } else if (method.getEnhancedParameters(Disposes.class).size() > 0) { throw UtilLogger.LOG.initializerCannotBeDisposalMethod(method, Formats.formatAsStackTraceElement(method.getJavaMember())); } else if (method.getEnhancedParameters(Observes.class).size() > 0) { throw EventLogger.LOG.invalidInitializer(method, Formats.formatAsStackTraceElement(method.getJavaMember())); } else if (method.isGeneric()) { throw UtilLogger.LOG.initializerMethodIsGeneric(method, Formats.formatAsStackTraceElement(method.getJavaMember())); } if (!method.isStatic()) { currentLevel.add(InjectionPointFactory.instance().createMethodInjectionPoint(MethodInjectionPointType.INITIALIZER, method, declaringBean, type.getJavaClass(), null, manager)); } } }
/** * Validates the producer method */ protected void checkProducerMethod(EnhancedAnnotatedMethod<T, ? super X> method) { if (method.getEnhancedParameters(Observes.class).size() > 0) { throw BeanLogger.LOG.inconsistentAnnotationsOnMethod(PRODUCER_ANNOTATION, "@Observes", this.method, Formats.formatAsStackTraceElement(method.getJavaMember())); } else if (method.getEnhancedParameters(ObservesAsync.class).size() > 0) { throw BeanLogger.LOG.inconsistentAnnotationsOnMethod(PRODUCER_ANNOTATION, "@ObservesAsync", this.method, Formats.formatAsStackTraceElement(method.getJavaMember())); } else if (method.getEnhancedParameters(Disposes.class).size() > 0) { throw BeanLogger.LOG.inconsistentAnnotationsOnMethod(PRODUCER_ANNOTATION, "@Disposes", this.method, Formats.formatAsStackTraceElement(method.getJavaMember())); } else if (getDeclaringBean() instanceof SessionBean<?> && !Modifier.isStatic(method.slim().getJavaMember().getModifiers())) { boolean methodDeclaredOnTypes = false; for (Type type : getDeclaringBean().getTypes()) { Class<?> clazz = Reflections.getRawType(type); try { AccessController.doPrivileged(new GetMethodAction(clazz, method.getName(), method.getParameterTypesAsArray())); methodDeclaredOnTypes = true; break; } catch (PrivilegedActionException ignored) { } } if (!methodDeclaredOnTypes) { throw BeanLogger.LOG.methodNotBusinessMethod("Producer", this, getDeclaringBean(), Formats.formatAsStackTraceElement(method.getJavaMember())); } } }
private void checkDisposalMethod(EnhancedAnnotatedMethod<T, ? super X> enhancedAnnotatedMethod, AbstractClassBean<X> declaringBean) { if (enhancedAnnotatedMethod.getEnhancedParameters(Disposes.class).size() > 1) { throw BeanLogger.LOG .multipleDisposeParams(disposalMethodInjectionPoint, Formats.formatAsStackTraceElement(enhancedAnnotatedMethod.getJavaMember())); if (enhancedAnnotatedMethod.getEnhancedParameters(Observes.class).size() > 0) { throw BeanLogger.LOG.inconsistentAnnotationsOnMethod("@Observes", DISPOSER_ANNOTATION, disposalMethodInjectionPoint, Formats.formatAsStackTraceElement(enhancedAnnotatedMethod.getJavaMember())); if (enhancedAnnotatedMethod.getAnnotation(Inject.class) != null) { throw BeanLogger.LOG.inconsistentAnnotationsOnMethod("@Inject", DISPOSER_ANNOTATION, disposalMethodInjectionPoint, Formats.formatAsStackTraceElement(enhancedAnnotatedMethod.getJavaMember())); if (enhancedAnnotatedMethod.getAnnotation(Produces.class) != null) { throw BeanLogger.LOG.inconsistentAnnotationsOnMethod("@Produces", DISPOSER_ANNOTATION, disposalMethodInjectionPoint, Formats.formatAsStackTraceElement(enhancedAnnotatedMethod.getJavaMember())); if (enhancedAnnotatedMethod.getAnnotation(Specializes.class) != null) { throw BeanLogger.LOG.inconsistentAnnotationsOnMethod("@Specialized", DISPOSER_ANNOTATION, disposalMethodInjectionPoint, Formats.formatAsStackTraceElement(enhancedAnnotatedMethod.getJavaMember())); if (!businessMethodSignatures.contains(enhancedAnnotatedMethod.getSignature())) { throw BeanLogger.LOG.methodNotBusinessMethod("Disposer", enhancedAnnotatedMethod, declaringBean, Formats.formatAsStackTraceElement(enhancedAnnotatedMethod.getJavaMember())); Class<?> rawType = Reflections.getRawType(ip.getType()); if (Bean.class.equals(rawType) || Interceptor.class.equals(rawType) || Decorator.class.equals(rawType)) { Validator.checkBeanMetadataInjectionPoint(this, ip, getDisposesParameter().getBaseType());
/** * * @param annotatedType * @param methods * @param skipOverridingBridgeMethods If set to <code>true</code> the returning set will not contain methods overriden by a bridge method * @return the set of overriden methods */ protected Set<EnhancedAnnotatedMethod<?, ? super T>> getOverriddenMethods(EnhancedAnnotatedType<T> annotatedType, Set<EnhancedAnnotatedMethod<?, ? super T>> methods, boolean skipOverridingBridgeMethods) { Set<EnhancedAnnotatedMethod<?, ? super T>> overriddenMethods = new HashSet<EnhancedAnnotatedMethod<?, ? super T>>(); Multimap<MethodSignature, Package> seenMethods = SetMultimap.newSetMultimap(); for (Class<? super T> clazz = annotatedType.getJavaClass(); clazz != null && clazz != Object.class; clazz = clazz.getSuperclass()) { for (EnhancedAnnotatedMethod<?, ? super T> method : methods) { if (method.getJavaMember().getDeclaringClass().equals(clazz)) { if (skipOverridingBridgeMethods && method.getJavaMember().isBridge()) { continue; } if (isOverridden(method, seenMethods)) { overriddenMethods.add(method); } seenMethods.put(method.getSignature(), method.getPackage()); } } } return immutableSetView(overriddenMethods); }
private static boolean isOverridden(EnhancedAnnotatedMethod<?, ?> method, Multimap<MethodSignature, Package> seenMethods) { if (method.isPrivate()) { return false; } else if (method.isPackagePrivate() && seenMethods.containsKey(method.getSignature())) { return seenMethods.get(method.getSignature()).contains(method.getPackage()); } else { return seenMethods.containsKey(method.getSignature()); } }
protected <T, X> void createObserverMethod(RIBean<X> declaringBean, BeanManagerImpl beanManager, EnhancedAnnotatedMethod<T, ? super X> method, Set<ObserverInitializationContext<?, ?>> observerMethodInitializers, boolean isAsync) { ObserverMethodImpl<T, X> observer = ObserverFactory.create(method, declaringBean, beanManager, isAsync); ObserverInitializationContext<T, X> observerMethodInitializer = ObserverInitializationContext.of(observer, method); if (Observers.isContainerLifecycleObserverMethod(observer)) { if (isAsync) { throw EventLogger.LOG.asyncContainerLifecycleEventObserver(observer, Formats.formatAsStackTraceElement(method.getJavaMember())); } if (method.isStatic()) { throw EventLogger.LOG.staticContainerLifecycleEventObserver(observer, Formats.formatAsStackTraceElement(method.getJavaMember())); } } observerMethodInitializers.add(observerMethodInitializer); }
protected Multimap<Class<? extends Annotation>, EnhancedAnnotatedMethod<?, ? super T>> buildAnnotatedParameterMethodMultimap(Set<EnhancedAnnotatedMethod<?, ? super T>> effectiveMethods) { Multimap<Class<? extends Annotation>, EnhancedAnnotatedMethod<?, ? super T>> result = SetMultimap.newSetMultimap(); for (EnhancedAnnotatedMethod<?, ? super T> method : effectiveMethods) { for (Class<? extends Annotation> annotation : MAPPED_METHOD_PARAMETER_ANNOTATIONS) { if (!method.getEnhancedParameters(annotation).isEmpty()) { result.put(annotation, method); } } } return Multimaps.unmodifiableMultimap(result); }
public static String forProducerMethod(EnhancedAnnotatedMethod<?, ?> method, AbstractClassBean<?> declaringBean) { if (declaringBean.getEnhancedAnnotated().isDiscovered()) { return forProducerMethod(declaringBean.getAnnotated().getIdentifier(), DeclaredMemberIndexer.getIndexForMethod(method.getJavaMember())); } return getPrefix(ProducerMethod.class).append(method.getDeclaringType().slim().getIdentifier()).append(AnnotatedTypes.createCallableId(method)).toString(); }
public static List<EnhancedAnnotatedMethod<?, ?>> getInterceptableMethods(EnhancedAnnotatedType<?> type) { List<EnhancedAnnotatedMethod<?, ?>> annotatedMethods = new ArrayList<EnhancedAnnotatedMethod<?, ?>>(); for (EnhancedAnnotatedMethod<?, ?> annotatedMethod : type.getEnhancedMethods()) { boolean businessMethod = !annotatedMethod.isStatic() && !annotatedMethod.isAnnotationPresent(Inject.class) && !annotatedMethod.getJavaMember().isBridge(); if (businessMethod && !isInterceptorMethod(annotatedMethod)) { annotatedMethods.add(annotatedMethod); } } return annotatedMethods; }
public static MethodSignature of(AnnotatedMethod<?> method) { if (method instanceof EnhancedAnnotatedMethod<?, ?>) { return Reflections.<EnhancedAnnotatedMethod<?, ?>>cast(method).getSignature(); } return new MethodSignatureImpl(method); }
/** * Constructor * <p/> * Initializes the superclass with the built annotation map * * @param annotationType The annotation type */ protected EnhancedAnnotationImpl(SlimAnnotatedType<T> annotatedType, Map<Class<? extends Annotation>, Annotation> annotationMap, Map<Class<? extends Annotation>, Annotation> declaredAnnotationMap, ClassTransformer classTransformer) { super(annotatedType, annotationMap, declaredAnnotationMap, classTransformer); this.clazz = annotatedType.getJavaClass(); members = new HashSet<EnhancedAnnotatedMethod<?, ?>>(); annotatedMembers = SetMultimap.newSetMultimap(); for (AnnotatedMethod<? super T> annotatedMethod : annotatedType.getMethods()) { EnhancedAnnotatedMethod<?, ? super T> enhancedAnnotatedMethod = EnhancedAnnotatedMethodImpl.of(annotatedMethod, this, classTransformer); members.add(enhancedAnnotatedMethod); for (Annotation annotation : enhancedAnnotatedMethod.getAnnotations()) { annotatedMembers.put(annotation.annotationType(), enhancedAnnotatedMethod); } } }
private void checkObserverMethods(Collection<EnhancedAnnotatedMethod<?, ? super T>> observerMethods) { if (!observerMethods.isEmpty()) { Set<MethodSignature> businessMethodSignatures = getLocalBusinessMethodSignatures(); Set<MethodSignature> remoteBusinessMethodSignatures = getRemoteBusinessMethodSignatures(); for (EnhancedAnnotatedMethod<?, ? super T> observerMethod : observerMethods) { boolean isLocalBusinessMethod = !remoteBusinessMethodSignatures.contains(observerMethod.getSignature()) && businessMethodSignatures .contains(observerMethod.getSignature()); if (!isLocalBusinessMethod && !observerMethod.isStatic()) { throw BeanLogger.LOG .observerMethodMustBeStaticOrBusiness(observerMethod, Formats.formatAsStackTraceElement(observerMethod.getJavaMember())); } } } }
public ProducerMethodProducer(EnhancedAnnotatedMethod<T, ? super X> enhancedAnnotatedMethod, DisposalMethod<?, ?> disposalMethod) { super(enhancedAnnotatedMethod, disposalMethod); // Note that for producer method injection points the declaring bean is the producer method itself this.method = InjectionPointFactory.instance().createMethodInjectionPoint(MethodInjectionPointType.PRODUCER, enhancedAnnotatedMethod, getBean(), enhancedAnnotatedMethod.getDeclaringType().getJavaClass(), null, getBeanManager()); checkProducerMethod(enhancedAnnotatedMethod); checkDelegateInjectionPoints(); }
StaticMethodInjectionPoint(MethodInjectionPointType methodInjectionPointType, EnhancedAnnotatedMethod<T, X> enhancedMethod, Bean<?> declaringBean, Class<?> declaringComponentClass, Class<? extends Annotation> specialParameterMarker, InjectionPointFactory factory, BeanManagerImpl manager) { super(methodInjectionPointType, enhancedMethod, declaringBean, declaringComponentClass, factory, manager); this.accessibleMethod = SecurityActions.getAccessibleCopyOfMethod(enhancedMethod.getJavaMember()); this.annotatedMethod = enhancedMethod.slim(); this.specialInjectionPointIndex = initSpecialInjectionPointIndex(enhancedMethod, specialParameterMarker); }
public <T, X> MethodInjectionPoint<T, X> createMethodInjectionPoint(MethodInjectionPointType methodInjectionPointType, EnhancedAnnotatedMethod<T, X> enhancedMethod, Bean<?> declaringBean, Class<?> declaringComponentClass, Set<Class<? extends Annotation>> specialParameterMarkers, BeanManagerImpl manager) { if (enhancedMethod.isStatic()) { return new StaticMethodInjectionPoint<T, X>(methodInjectionPointType, enhancedMethod, declaringBean, declaringComponentClass, specialParameterMarkers, this, manager); } else { return new VirtualMethodInjectionPoint<T, X>(methodInjectionPointType, enhancedMethod, declaringBean, declaringComponentClass, specialParameterMarkers, this, manager); } }
protected <X, T> void createProducerMethod(AbstractClassBean<X> declaringBean, EnhancedAnnotatedMethod<T, ? super X> annotatedMethod) { BeanAttributes<T> attributes = BeanAttributesFactory.forBean(annotatedMethod, getManager()); DisposalMethod<X, ?> disposalMethod = resolveDisposalMethod(attributes, declaringBean); ProducerMethod<? super X, T> bean = ProducerMethod.of(attributes, annotatedMethod, declaringBean, disposalMethod, manager, services); containerLifecycleEvents.preloadProcessBeanAttributes(bean.getType()); containerLifecycleEvents.preloadProcessBean(ProcessProducerMethod.class, annotatedMethod.getBaseType(), bean.getBeanClass()); containerLifecycleEvents.preloadProcessProducer(bean.getBeanClass(), annotatedMethod.getBaseType()); getEnvironment().addProducerMethod(bean); }